[PR67828] don't unswitch on default defs of non-parms
[official-gcc.git] / gcc / tree.c
blobf78a2c26cd7e00a0b1c6c680ae2d9f598a6477ba
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 "backend.h"
34 #include "tree.h"
35 #include "gimple.h"
36 #include "rtl.h"
37 #include "ssa.h"
38 #include "flags.h"
39 #include "alias.h"
40 #include "fold-const.h"
41 #include "stor-layout.h"
42 #include "calls.h"
43 #include "attribs.h"
44 #include "varasm.h"
45 #include "tm_p.h"
46 #include "toplev.h" /* get_random_seed */
47 #include "filenames.h"
48 #include "output.h"
49 #include "target.h"
50 #include "common/common-target.h"
51 #include "langhooks.h"
52 #include "tree-inline.h"
53 #include "tree-iterator.h"
54 #include "internal-fn.h"
55 #include "gimple-iterator.h"
56 #include "gimplify.h"
57 #include "cgraph.h"
58 #include "insn-config.h"
59 #include "expmed.h"
60 #include "dojump.h"
61 #include "explow.h"
62 #include "emit-rtl.h"
63 #include "stmt.h"
64 #include "expr.h"
65 #include "tree-dfa.h"
66 #include "params.h"
67 #include "tree-pass.h"
68 #include "langhooks-def.h"
69 #include "diagnostic.h"
70 #include "tree-diagnostic.h"
71 #include "tree-pretty-print.h"
72 #include "except.h"
73 #include "debug.h"
74 #include "intl.h"
75 #include "builtins.h"
76 #include "print-tree.h"
77 #include "ipa-utils.h"
79 /* Tree code classes. */
81 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
82 #define END_OF_BASE_TREE_CODES tcc_exceptional,
84 const enum tree_code_class tree_code_type[] = {
85 #include "all-tree.def"
88 #undef DEFTREECODE
89 #undef END_OF_BASE_TREE_CODES
91 /* Table indexed by tree code giving number of expression
92 operands beyond the fixed part of the node structure.
93 Not used for types or decls. */
95 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
96 #define END_OF_BASE_TREE_CODES 0,
98 const unsigned char tree_code_length[] = {
99 #include "all-tree.def"
102 #undef DEFTREECODE
103 #undef END_OF_BASE_TREE_CODES
105 /* Names of tree components.
106 Used for printing out the tree and error messages. */
107 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
108 #define END_OF_BASE_TREE_CODES "@dummy",
110 static const char *const tree_code_name[] = {
111 #include "all-tree.def"
114 #undef DEFTREECODE
115 #undef END_OF_BASE_TREE_CODES
117 /* Each tree code class has an associated string representation.
118 These must correspond to the tree_code_class entries. */
120 const char *const tree_code_class_strings[] =
122 "exceptional",
123 "constant",
124 "type",
125 "declaration",
126 "reference",
127 "comparison",
128 "unary",
129 "binary",
130 "statement",
131 "vl_exp",
132 "expression"
135 /* obstack.[ch] explicitly declined to prototype this. */
136 extern int _obstack_allocated_p (struct obstack *h, void *obj);
138 /* Statistics-gathering stuff. */
140 static int tree_code_counts[MAX_TREE_CODES];
141 int tree_node_counts[(int) all_kinds];
142 int tree_node_sizes[(int) all_kinds];
144 /* Keep in sync with tree.h:enum tree_node_kind. */
145 static const char * const tree_node_kind_names[] = {
146 "decls",
147 "types",
148 "blocks",
149 "stmts",
150 "refs",
151 "exprs",
152 "constants",
153 "identifiers",
154 "vecs",
155 "binfos",
156 "ssa names",
157 "constructors",
158 "random kinds",
159 "lang_decl kinds",
160 "lang_type kinds",
161 "omp clauses",
164 /* Unique id for next decl created. */
165 static GTY(()) int next_decl_uid;
166 /* Unique id for next type created. */
167 static GTY(()) int next_type_uid = 1;
168 /* Unique id for next debug decl created. Use negative numbers,
169 to catch erroneous uses. */
170 static GTY(()) int next_debug_decl_uid;
172 /* Since we cannot rehash a type after it is in the table, we have to
173 keep the hash code. */
175 struct GTY((for_user)) type_hash {
176 unsigned long hash;
177 tree type;
180 /* Initial size of the hash table (rounded to next prime). */
181 #define TYPE_HASH_INITIAL_SIZE 1000
183 struct type_cache_hasher : ggc_cache_ptr_hash<type_hash>
185 static hashval_t hash (type_hash *t) { return t->hash; }
186 static bool equal (type_hash *a, type_hash *b);
188 static int
189 keep_cache_entry (type_hash *&t)
191 return ggc_marked_p (t->type);
195 /* Now here is the hash table. When recording a type, it is added to
196 the slot whose index is the hash code. Note that the hash table is
197 used for several kinds of types (function types, array types and
198 array index range types, for now). While all these live in the
199 same table, they are completely independent, and the hash code is
200 computed differently for each of these. */
202 static GTY ((cache)) hash_table<type_cache_hasher> *type_hash_table;
204 /* Hash table and temporary node for larger integer const values. */
205 static GTY (()) tree int_cst_node;
207 struct int_cst_hasher : ggc_cache_ptr_hash<tree_node>
209 static hashval_t hash (tree t);
210 static bool equal (tree x, tree y);
213 static GTY ((cache)) hash_table<int_cst_hasher> *int_cst_hash_table;
215 /* Hash table for optimization flags and target option flags. Use the same
216 hash table for both sets of options. Nodes for building the current
217 optimization and target option nodes. The assumption is most of the time
218 the options created will already be in the hash table, so we avoid
219 allocating and freeing up a node repeatably. */
220 static GTY (()) tree cl_optimization_node;
221 static GTY (()) tree cl_target_option_node;
223 struct cl_option_hasher : ggc_cache_ptr_hash<tree_node>
225 static hashval_t hash (tree t);
226 static bool equal (tree x, tree y);
229 static GTY ((cache)) hash_table<cl_option_hasher> *cl_option_hash_table;
231 /* General tree->tree mapping structure for use in hash tables. */
234 static GTY ((cache))
235 hash_table<tree_decl_map_cache_hasher> *debug_expr_for_decl;
237 static GTY ((cache))
238 hash_table<tree_decl_map_cache_hasher> *value_expr_for_decl;
240 struct tree_vec_map_cache_hasher : ggc_cache_ptr_hash<tree_vec_map>
242 static hashval_t hash (tree_vec_map *m) { return DECL_UID (m->base.from); }
244 static bool
245 equal (tree_vec_map *a, tree_vec_map *b)
247 return a->base.from == b->base.from;
250 static int
251 keep_cache_entry (tree_vec_map *&m)
253 return ggc_marked_p (m->base.from);
257 static GTY ((cache))
258 hash_table<tree_vec_map_cache_hasher> *debug_args_for_decl;
260 static void set_type_quals (tree, int);
261 static void print_type_hash_statistics (void);
262 static void print_debug_expr_statistics (void);
263 static void print_value_expr_statistics (void);
264 static void type_hash_list (const_tree, inchash::hash &);
265 static void attribute_hash_list (const_tree, inchash::hash &);
267 tree global_trees[TI_MAX];
268 tree integer_types[itk_none];
270 bool int_n_enabled_p[NUM_INT_N_ENTS];
271 struct int_n_trees_t int_n_trees [NUM_INT_N_ENTS];
273 unsigned char tree_contains_struct[MAX_TREE_CODES][64];
275 /* Number of operands for each OpenMP clause. */
276 unsigned const char omp_clause_num_ops[] =
278 0, /* OMP_CLAUSE_ERROR */
279 1, /* OMP_CLAUSE_PRIVATE */
280 1, /* OMP_CLAUSE_SHARED */
281 1, /* OMP_CLAUSE_FIRSTPRIVATE */
282 2, /* OMP_CLAUSE_LASTPRIVATE */
283 4, /* OMP_CLAUSE_REDUCTION */
284 1, /* OMP_CLAUSE_COPYIN */
285 1, /* OMP_CLAUSE_COPYPRIVATE */
286 3, /* OMP_CLAUSE_LINEAR */
287 2, /* OMP_CLAUSE_ALIGNED */
288 1, /* OMP_CLAUSE_DEPEND */
289 1, /* OMP_CLAUSE_UNIFORM */
290 2, /* OMP_CLAUSE_FROM */
291 2, /* OMP_CLAUSE_TO */
292 2, /* OMP_CLAUSE_MAP */
293 2, /* OMP_CLAUSE__CACHE_ */
294 1, /* OMP_CLAUSE_DEVICE_RESIDENT */
295 1, /* OMP_CLAUSE_USE_DEVICE */
296 2, /* OMP_CLAUSE_GANG */
297 1, /* OMP_CLAUSE_ASYNC */
298 1, /* OMP_CLAUSE_WAIT */
299 0, /* OMP_CLAUSE_AUTO */
300 0, /* OMP_CLAUSE_SEQ */
301 1, /* OMP_CLAUSE__LOOPTEMP_ */
302 1, /* OMP_CLAUSE_IF */
303 1, /* OMP_CLAUSE_NUM_THREADS */
304 1, /* OMP_CLAUSE_SCHEDULE */
305 0, /* OMP_CLAUSE_NOWAIT */
306 0, /* OMP_CLAUSE_ORDERED */
307 0, /* OMP_CLAUSE_DEFAULT */
308 3, /* OMP_CLAUSE_COLLAPSE */
309 0, /* OMP_CLAUSE_UNTIED */
310 1, /* OMP_CLAUSE_FINAL */
311 0, /* OMP_CLAUSE_MERGEABLE */
312 1, /* OMP_CLAUSE_DEVICE */
313 1, /* OMP_CLAUSE_DIST_SCHEDULE */
314 0, /* OMP_CLAUSE_INBRANCH */
315 0, /* OMP_CLAUSE_NOTINBRANCH */
316 1, /* OMP_CLAUSE_NUM_TEAMS */
317 1, /* OMP_CLAUSE_THREAD_LIMIT */
318 0, /* OMP_CLAUSE_PROC_BIND */
319 1, /* OMP_CLAUSE_SAFELEN */
320 1, /* OMP_CLAUSE_SIMDLEN */
321 0, /* OMP_CLAUSE_FOR */
322 0, /* OMP_CLAUSE_PARALLEL */
323 0, /* OMP_CLAUSE_SECTIONS */
324 0, /* OMP_CLAUSE_TASKGROUP */
325 1, /* OMP_CLAUSE__SIMDUID_ */
326 1, /* OMP_CLAUSE__CILK_FOR_COUNT_ */
327 0, /* OMP_CLAUSE_INDEPENDENT */
328 1, /* OMP_CLAUSE_WORKER */
329 1, /* OMP_CLAUSE_VECTOR */
330 1, /* OMP_CLAUSE_NUM_GANGS */
331 1, /* OMP_CLAUSE_NUM_WORKERS */
332 1, /* OMP_CLAUSE_VECTOR_LENGTH */
335 const char * const omp_clause_code_name[] =
337 "error_clause",
338 "private",
339 "shared",
340 "firstprivate",
341 "lastprivate",
342 "reduction",
343 "copyin",
344 "copyprivate",
345 "linear",
346 "aligned",
347 "depend",
348 "uniform",
349 "from",
350 "to",
351 "map",
352 "_cache_",
353 "device_resident",
354 "use_device",
355 "gang",
356 "async",
357 "wait",
358 "auto",
359 "seq",
360 "_looptemp_",
361 "if",
362 "num_threads",
363 "schedule",
364 "nowait",
365 "ordered",
366 "default",
367 "collapse",
368 "untied",
369 "final",
370 "mergeable",
371 "device",
372 "dist_schedule",
373 "inbranch",
374 "notinbranch",
375 "num_teams",
376 "thread_limit",
377 "proc_bind",
378 "safelen",
379 "simdlen",
380 "for",
381 "parallel",
382 "sections",
383 "taskgroup",
384 "_simduid_",
385 "_Cilk_for_count_",
386 "independent",
387 "worker",
388 "vector",
389 "num_gangs",
390 "num_workers",
391 "vector_length"
395 /* Return the tree node structure used by tree code CODE. */
397 static inline enum tree_node_structure_enum
398 tree_node_structure_for_code (enum tree_code code)
400 switch (TREE_CODE_CLASS (code))
402 case tcc_declaration:
404 switch (code)
406 case FIELD_DECL:
407 return TS_FIELD_DECL;
408 case PARM_DECL:
409 return TS_PARM_DECL;
410 case VAR_DECL:
411 return TS_VAR_DECL;
412 case LABEL_DECL:
413 return TS_LABEL_DECL;
414 case RESULT_DECL:
415 return TS_RESULT_DECL;
416 case DEBUG_EXPR_DECL:
417 return TS_DECL_WRTL;
418 case CONST_DECL:
419 return TS_CONST_DECL;
420 case TYPE_DECL:
421 return TS_TYPE_DECL;
422 case FUNCTION_DECL:
423 return TS_FUNCTION_DECL;
424 case TRANSLATION_UNIT_DECL:
425 return TS_TRANSLATION_UNIT_DECL;
426 default:
427 return TS_DECL_NON_COMMON;
430 case tcc_type:
431 return TS_TYPE_NON_COMMON;
432 case tcc_reference:
433 case tcc_comparison:
434 case tcc_unary:
435 case tcc_binary:
436 case tcc_expression:
437 case tcc_statement:
438 case tcc_vl_exp:
439 return TS_EXP;
440 default: /* tcc_constant and tcc_exceptional */
441 break;
443 switch (code)
445 /* tcc_constant cases. */
446 case VOID_CST: return TS_TYPED;
447 case INTEGER_CST: return TS_INT_CST;
448 case REAL_CST: return TS_REAL_CST;
449 case FIXED_CST: return TS_FIXED_CST;
450 case COMPLEX_CST: return TS_COMPLEX;
451 case VECTOR_CST: return TS_VECTOR;
452 case STRING_CST: return TS_STRING;
453 /* tcc_exceptional cases. */
454 case ERROR_MARK: return TS_COMMON;
455 case IDENTIFIER_NODE: return TS_IDENTIFIER;
456 case TREE_LIST: return TS_LIST;
457 case TREE_VEC: return TS_VEC;
458 case SSA_NAME: return TS_SSA_NAME;
459 case PLACEHOLDER_EXPR: return TS_COMMON;
460 case STATEMENT_LIST: return TS_STATEMENT_LIST;
461 case BLOCK: return TS_BLOCK;
462 case CONSTRUCTOR: return TS_CONSTRUCTOR;
463 case TREE_BINFO: return TS_BINFO;
464 case OMP_CLAUSE: return TS_OMP_CLAUSE;
465 case OPTIMIZATION_NODE: return TS_OPTIMIZATION;
466 case TARGET_OPTION_NODE: return TS_TARGET_OPTION;
468 default:
469 gcc_unreachable ();
474 /* Initialize tree_contains_struct to describe the hierarchy of tree
475 nodes. */
477 static void
478 initialize_tree_contains_struct (void)
480 unsigned i;
482 for (i = ERROR_MARK; i < LAST_AND_UNUSED_TREE_CODE; i++)
484 enum tree_code code;
485 enum tree_node_structure_enum ts_code;
487 code = (enum tree_code) i;
488 ts_code = tree_node_structure_for_code (code);
490 /* Mark the TS structure itself. */
491 tree_contains_struct[code][ts_code] = 1;
493 /* Mark all the structures that TS is derived from. */
494 switch (ts_code)
496 case TS_TYPED:
497 case TS_BLOCK:
498 MARK_TS_BASE (code);
499 break;
501 case TS_COMMON:
502 case TS_INT_CST:
503 case TS_REAL_CST:
504 case TS_FIXED_CST:
505 case TS_VECTOR:
506 case TS_STRING:
507 case TS_COMPLEX:
508 case TS_SSA_NAME:
509 case TS_CONSTRUCTOR:
510 case TS_EXP:
511 case TS_STATEMENT_LIST:
512 MARK_TS_TYPED (code);
513 break;
515 case TS_IDENTIFIER:
516 case TS_DECL_MINIMAL:
517 case TS_TYPE_COMMON:
518 case TS_LIST:
519 case TS_VEC:
520 case TS_BINFO:
521 case TS_OMP_CLAUSE:
522 case TS_OPTIMIZATION:
523 case TS_TARGET_OPTION:
524 MARK_TS_COMMON (code);
525 break;
527 case TS_TYPE_WITH_LANG_SPECIFIC:
528 MARK_TS_TYPE_COMMON (code);
529 break;
531 case TS_TYPE_NON_COMMON:
532 MARK_TS_TYPE_WITH_LANG_SPECIFIC (code);
533 break;
535 case TS_DECL_COMMON:
536 MARK_TS_DECL_MINIMAL (code);
537 break;
539 case TS_DECL_WRTL:
540 case TS_CONST_DECL:
541 MARK_TS_DECL_COMMON (code);
542 break;
544 case TS_DECL_NON_COMMON:
545 MARK_TS_DECL_WITH_VIS (code);
546 break;
548 case TS_DECL_WITH_VIS:
549 case TS_PARM_DECL:
550 case TS_LABEL_DECL:
551 case TS_RESULT_DECL:
552 MARK_TS_DECL_WRTL (code);
553 break;
555 case TS_FIELD_DECL:
556 MARK_TS_DECL_COMMON (code);
557 break;
559 case TS_VAR_DECL:
560 MARK_TS_DECL_WITH_VIS (code);
561 break;
563 case TS_TYPE_DECL:
564 case TS_FUNCTION_DECL:
565 MARK_TS_DECL_NON_COMMON (code);
566 break;
568 case TS_TRANSLATION_UNIT_DECL:
569 MARK_TS_DECL_COMMON (code);
570 break;
572 default:
573 gcc_unreachable ();
577 /* Basic consistency checks for attributes used in fold. */
578 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_NON_COMMON]);
579 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_NON_COMMON]);
580 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_COMMON]);
581 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_COMMON]);
582 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_COMMON]);
583 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_COMMON]);
584 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_COMMON]);
585 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_COMMON]);
586 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_COMMON]);
587 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_COMMON]);
588 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_COMMON]);
589 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WRTL]);
590 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_WRTL]);
591 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_WRTL]);
592 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WRTL]);
593 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_WRTL]);
594 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_MINIMAL]);
595 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_MINIMAL]);
596 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_MINIMAL]);
597 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_MINIMAL]);
598 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_MINIMAL]);
599 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_MINIMAL]);
600 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_MINIMAL]);
601 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_MINIMAL]);
602 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_MINIMAL]);
603 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WITH_VIS]);
604 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WITH_VIS]);
605 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_WITH_VIS]);
606 gcc_assert (tree_contains_struct[VAR_DECL][TS_VAR_DECL]);
607 gcc_assert (tree_contains_struct[FIELD_DECL][TS_FIELD_DECL]);
608 gcc_assert (tree_contains_struct[PARM_DECL][TS_PARM_DECL]);
609 gcc_assert (tree_contains_struct[LABEL_DECL][TS_LABEL_DECL]);
610 gcc_assert (tree_contains_struct[RESULT_DECL][TS_RESULT_DECL]);
611 gcc_assert (tree_contains_struct[CONST_DECL][TS_CONST_DECL]);
612 gcc_assert (tree_contains_struct[TYPE_DECL][TS_TYPE_DECL]);
613 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_FUNCTION_DECL]);
614 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_MINIMAL]);
615 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_COMMON]);
616 gcc_assert (tree_contains_struct[NAMELIST_DECL][TS_DECL_MINIMAL]);
617 gcc_assert (tree_contains_struct[NAMELIST_DECL][TS_DECL_COMMON]);
621 /* Init tree.c. */
623 void
624 init_ttree (void)
626 /* Initialize the hash table of types. */
627 type_hash_table
628 = hash_table<type_cache_hasher>::create_ggc (TYPE_HASH_INITIAL_SIZE);
630 debug_expr_for_decl
631 = hash_table<tree_decl_map_cache_hasher>::create_ggc (512);
633 value_expr_for_decl
634 = hash_table<tree_decl_map_cache_hasher>::create_ggc (512);
636 int_cst_hash_table = hash_table<int_cst_hasher>::create_ggc (1024);
638 int_cst_node = make_int_cst (1, 1);
640 cl_option_hash_table = hash_table<cl_option_hasher>::create_ggc (64);
642 cl_optimization_node = make_node (OPTIMIZATION_NODE);
643 cl_target_option_node = make_node (TARGET_OPTION_NODE);
645 /* Initialize the tree_contains_struct array. */
646 initialize_tree_contains_struct ();
647 lang_hooks.init_ts ();
651 /* The name of the object as the assembler will see it (but before any
652 translations made by ASM_OUTPUT_LABELREF). Often this is the same
653 as DECL_NAME. It is an IDENTIFIER_NODE. */
654 tree
655 decl_assembler_name (tree decl)
657 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
658 lang_hooks.set_decl_assembler_name (decl);
659 return DECL_WITH_VIS_CHECK (decl)->decl_with_vis.assembler_name;
662 /* When the target supports COMDAT groups, this indicates which group the
663 DECL is associated with. This can be either an IDENTIFIER_NODE or a
664 decl, in which case its DECL_ASSEMBLER_NAME identifies the group. */
665 tree
666 decl_comdat_group (const_tree node)
668 struct symtab_node *snode = symtab_node::get (node);
669 if (!snode)
670 return NULL;
671 return snode->get_comdat_group ();
674 /* Likewise, but make sure it's been reduced to an IDENTIFIER_NODE. */
675 tree
676 decl_comdat_group_id (const_tree node)
678 struct symtab_node *snode = symtab_node::get (node);
679 if (!snode)
680 return NULL;
681 return snode->get_comdat_group_id ();
684 /* When the target supports named section, return its name as IDENTIFIER_NODE
685 or NULL if it is in no section. */
686 const char *
687 decl_section_name (const_tree node)
689 struct symtab_node *snode = symtab_node::get (node);
690 if (!snode)
691 return NULL;
692 return snode->get_section ();
695 /* Set section name of NODE to VALUE (that is expected to be
696 identifier node) */
697 void
698 set_decl_section_name (tree node, const char *value)
700 struct symtab_node *snode;
702 if (value == NULL)
704 snode = symtab_node::get (node);
705 if (!snode)
706 return;
708 else if (TREE_CODE (node) == VAR_DECL)
709 snode = varpool_node::get_create (node);
710 else
711 snode = cgraph_node::get_create (node);
712 snode->set_section (value);
715 /* Return TLS model of a variable NODE. */
716 enum tls_model
717 decl_tls_model (const_tree node)
719 struct varpool_node *snode = varpool_node::get (node);
720 if (!snode)
721 return TLS_MODEL_NONE;
722 return snode->tls_model;
725 /* Set TLS model of variable NODE to MODEL. */
726 void
727 set_decl_tls_model (tree node, enum tls_model model)
729 struct varpool_node *vnode;
731 if (model == TLS_MODEL_NONE)
733 vnode = varpool_node::get (node);
734 if (!vnode)
735 return;
737 else
738 vnode = varpool_node::get_create (node);
739 vnode->tls_model = model;
742 /* Compute the number of bytes occupied by a tree with code CODE.
743 This function cannot be used for nodes that have variable sizes,
744 including TREE_VEC, INTEGER_CST, STRING_CST, and CALL_EXPR. */
745 size_t
746 tree_code_size (enum tree_code code)
748 switch (TREE_CODE_CLASS (code))
750 case tcc_declaration: /* A decl node */
752 switch (code)
754 case FIELD_DECL:
755 return sizeof (struct tree_field_decl);
756 case PARM_DECL:
757 return sizeof (struct tree_parm_decl);
758 case VAR_DECL:
759 return sizeof (struct tree_var_decl);
760 case LABEL_DECL:
761 return sizeof (struct tree_label_decl);
762 case RESULT_DECL:
763 return sizeof (struct tree_result_decl);
764 case CONST_DECL:
765 return sizeof (struct tree_const_decl);
766 case TYPE_DECL:
767 return sizeof (struct tree_type_decl);
768 case FUNCTION_DECL:
769 return sizeof (struct tree_function_decl);
770 case DEBUG_EXPR_DECL:
771 return sizeof (struct tree_decl_with_rtl);
772 case TRANSLATION_UNIT_DECL:
773 return sizeof (struct tree_translation_unit_decl);
774 case NAMESPACE_DECL:
775 case IMPORTED_DECL:
776 case NAMELIST_DECL:
777 return sizeof (struct tree_decl_non_common);
778 default:
779 return lang_hooks.tree_size (code);
783 case tcc_type: /* a type node */
784 return sizeof (struct tree_type_non_common);
786 case tcc_reference: /* a reference */
787 case tcc_expression: /* an expression */
788 case tcc_statement: /* an expression with side effects */
789 case tcc_comparison: /* a comparison expression */
790 case tcc_unary: /* a unary arithmetic expression */
791 case tcc_binary: /* a binary arithmetic expression */
792 return (sizeof (struct tree_exp)
793 + (TREE_CODE_LENGTH (code) - 1) * sizeof (tree));
795 case tcc_constant: /* a constant */
796 switch (code)
798 case VOID_CST: return sizeof (struct tree_typed);
799 case INTEGER_CST: gcc_unreachable ();
800 case REAL_CST: return sizeof (struct tree_real_cst);
801 case FIXED_CST: return sizeof (struct tree_fixed_cst);
802 case COMPLEX_CST: return sizeof (struct tree_complex);
803 case VECTOR_CST: return sizeof (struct tree_vector);
804 case STRING_CST: gcc_unreachable ();
805 default:
806 return lang_hooks.tree_size (code);
809 case tcc_exceptional: /* something random, like an identifier. */
810 switch (code)
812 case IDENTIFIER_NODE: return lang_hooks.identifier_size;
813 case TREE_LIST: return sizeof (struct tree_list);
815 case ERROR_MARK:
816 case PLACEHOLDER_EXPR: return sizeof (struct tree_common);
818 case TREE_VEC:
819 case OMP_CLAUSE: gcc_unreachable ();
821 case SSA_NAME: return sizeof (struct tree_ssa_name);
823 case STATEMENT_LIST: return sizeof (struct tree_statement_list);
824 case BLOCK: return sizeof (struct tree_block);
825 case CONSTRUCTOR: return sizeof (struct tree_constructor);
826 case OPTIMIZATION_NODE: return sizeof (struct tree_optimization_option);
827 case TARGET_OPTION_NODE: return sizeof (struct tree_target_option);
829 default:
830 return lang_hooks.tree_size (code);
833 default:
834 gcc_unreachable ();
838 /* Compute the number of bytes occupied by NODE. This routine only
839 looks at TREE_CODE, except for those nodes that have variable sizes. */
840 size_t
841 tree_size (const_tree node)
843 const enum tree_code code = TREE_CODE (node);
844 switch (code)
846 case INTEGER_CST:
847 return (sizeof (struct tree_int_cst)
848 + (TREE_INT_CST_EXT_NUNITS (node) - 1) * sizeof (HOST_WIDE_INT));
850 case TREE_BINFO:
851 return (offsetof (struct tree_binfo, base_binfos)
852 + vec<tree, va_gc>
853 ::embedded_size (BINFO_N_BASE_BINFOS (node)));
855 case TREE_VEC:
856 return (sizeof (struct tree_vec)
857 + (TREE_VEC_LENGTH (node) - 1) * sizeof (tree));
859 case VECTOR_CST:
860 return (sizeof (struct tree_vector)
861 + (TYPE_VECTOR_SUBPARTS (TREE_TYPE (node)) - 1) * sizeof (tree));
863 case STRING_CST:
864 return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
866 case OMP_CLAUSE:
867 return (sizeof (struct tree_omp_clause)
868 + (omp_clause_num_ops[OMP_CLAUSE_CODE (node)] - 1)
869 * sizeof (tree));
871 default:
872 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
873 return (sizeof (struct tree_exp)
874 + (VL_EXP_OPERAND_LENGTH (node) - 1) * sizeof (tree));
875 else
876 return tree_code_size (code);
880 /* Record interesting allocation statistics for a tree node with CODE
881 and LENGTH. */
883 static void
884 record_node_allocation_statistics (enum tree_code code ATTRIBUTE_UNUSED,
885 size_t length ATTRIBUTE_UNUSED)
887 enum tree_code_class type = TREE_CODE_CLASS (code);
888 tree_node_kind kind;
890 if (!GATHER_STATISTICS)
891 return;
893 switch (type)
895 case tcc_declaration: /* A decl node */
896 kind = d_kind;
897 break;
899 case tcc_type: /* a type node */
900 kind = t_kind;
901 break;
903 case tcc_statement: /* an expression with side effects */
904 kind = s_kind;
905 break;
907 case tcc_reference: /* a reference */
908 kind = r_kind;
909 break;
911 case tcc_expression: /* an expression */
912 case tcc_comparison: /* a comparison expression */
913 case tcc_unary: /* a unary arithmetic expression */
914 case tcc_binary: /* a binary arithmetic expression */
915 kind = e_kind;
916 break;
918 case tcc_constant: /* a constant */
919 kind = c_kind;
920 break;
922 case tcc_exceptional: /* something random, like an identifier. */
923 switch (code)
925 case IDENTIFIER_NODE:
926 kind = id_kind;
927 break;
929 case TREE_VEC:
930 kind = vec_kind;
931 break;
933 case TREE_BINFO:
934 kind = binfo_kind;
935 break;
937 case SSA_NAME:
938 kind = ssa_name_kind;
939 break;
941 case BLOCK:
942 kind = b_kind;
943 break;
945 case CONSTRUCTOR:
946 kind = constr_kind;
947 break;
949 case OMP_CLAUSE:
950 kind = omp_clause_kind;
951 break;
953 default:
954 kind = x_kind;
955 break;
957 break;
959 case tcc_vl_exp:
960 kind = e_kind;
961 break;
963 default:
964 gcc_unreachable ();
967 tree_code_counts[(int) code]++;
968 tree_node_counts[(int) kind]++;
969 tree_node_sizes[(int) kind] += length;
972 /* Allocate and return a new UID from the DECL_UID namespace. */
975 allocate_decl_uid (void)
977 return next_decl_uid++;
980 /* Return a newly allocated node of code CODE. For decl and type
981 nodes, some other fields are initialized. The rest of the node is
982 initialized to zero. This function cannot be used for TREE_VEC,
983 INTEGER_CST or OMP_CLAUSE nodes, which is enforced by asserts in
984 tree_code_size.
986 Achoo! I got a code in the node. */
988 tree
989 make_node_stat (enum tree_code code MEM_STAT_DECL)
991 tree t;
992 enum tree_code_class type = TREE_CODE_CLASS (code);
993 size_t length = tree_code_size (code);
995 record_node_allocation_statistics (code, length);
997 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
998 TREE_SET_CODE (t, code);
1000 switch (type)
1002 case tcc_statement:
1003 TREE_SIDE_EFFECTS (t) = 1;
1004 break;
1006 case tcc_declaration:
1007 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1009 if (code == FUNCTION_DECL)
1011 DECL_ALIGN (t) = FUNCTION_BOUNDARY;
1012 DECL_MODE (t) = FUNCTION_MODE;
1014 else
1015 DECL_ALIGN (t) = 1;
1017 DECL_SOURCE_LOCATION (t) = input_location;
1018 if (TREE_CODE (t) == DEBUG_EXPR_DECL)
1019 DECL_UID (t) = --next_debug_decl_uid;
1020 else
1022 DECL_UID (t) = allocate_decl_uid ();
1023 SET_DECL_PT_UID (t, -1);
1025 if (TREE_CODE (t) == LABEL_DECL)
1026 LABEL_DECL_UID (t) = -1;
1028 break;
1030 case tcc_type:
1031 TYPE_UID (t) = next_type_uid++;
1032 TYPE_ALIGN (t) = BITS_PER_UNIT;
1033 TYPE_USER_ALIGN (t) = 0;
1034 TYPE_MAIN_VARIANT (t) = t;
1035 TYPE_CANONICAL (t) = t;
1037 /* Default to no attributes for type, but let target change that. */
1038 TYPE_ATTRIBUTES (t) = NULL_TREE;
1039 targetm.set_default_type_attributes (t);
1041 /* We have not yet computed the alias set for this type. */
1042 TYPE_ALIAS_SET (t) = -1;
1043 break;
1045 case tcc_constant:
1046 TREE_CONSTANT (t) = 1;
1047 break;
1049 case tcc_expression:
1050 switch (code)
1052 case INIT_EXPR:
1053 case MODIFY_EXPR:
1054 case VA_ARG_EXPR:
1055 case PREDECREMENT_EXPR:
1056 case PREINCREMENT_EXPR:
1057 case POSTDECREMENT_EXPR:
1058 case POSTINCREMENT_EXPR:
1059 /* All of these have side-effects, no matter what their
1060 operands are. */
1061 TREE_SIDE_EFFECTS (t) = 1;
1062 break;
1064 default:
1065 break;
1067 break;
1069 case tcc_exceptional:
1070 switch (code)
1072 case TARGET_OPTION_NODE:
1073 TREE_TARGET_OPTION(t)
1074 = ggc_cleared_alloc<struct cl_target_option> ();
1075 break;
1077 case OPTIMIZATION_NODE:
1078 TREE_OPTIMIZATION (t)
1079 = ggc_cleared_alloc<struct cl_optimization> ();
1080 break;
1082 default:
1083 break;
1085 break;
1087 default:
1088 /* Other classes need no special treatment. */
1089 break;
1092 return t;
1095 /* Return a new node with the same contents as NODE except that its
1096 TREE_CHAIN, if it has one, is zero and it has a fresh uid. */
1098 tree
1099 copy_node_stat (tree node MEM_STAT_DECL)
1101 tree t;
1102 enum tree_code code = TREE_CODE (node);
1103 size_t length;
1105 gcc_assert (code != STATEMENT_LIST);
1107 length = tree_size (node);
1108 record_node_allocation_statistics (code, length);
1109 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
1110 memcpy (t, node, length);
1112 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
1113 TREE_CHAIN (t) = 0;
1114 TREE_ASM_WRITTEN (t) = 0;
1115 TREE_VISITED (t) = 0;
1117 if (TREE_CODE_CLASS (code) == tcc_declaration)
1119 if (code == DEBUG_EXPR_DECL)
1120 DECL_UID (t) = --next_debug_decl_uid;
1121 else
1123 DECL_UID (t) = allocate_decl_uid ();
1124 if (DECL_PT_UID_SET_P (node))
1125 SET_DECL_PT_UID (t, DECL_PT_UID (node));
1127 if ((TREE_CODE (node) == PARM_DECL || TREE_CODE (node) == VAR_DECL)
1128 && DECL_HAS_VALUE_EXPR_P (node))
1130 SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (node));
1131 DECL_HAS_VALUE_EXPR_P (t) = 1;
1133 /* DECL_DEBUG_EXPR is copied explicitely by callers. */
1134 if (TREE_CODE (node) == VAR_DECL)
1136 DECL_HAS_DEBUG_EXPR_P (t) = 0;
1137 t->decl_with_vis.symtab_node = NULL;
1139 if (TREE_CODE (node) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (node))
1141 SET_DECL_INIT_PRIORITY (t, DECL_INIT_PRIORITY (node));
1142 DECL_HAS_INIT_PRIORITY_P (t) = 1;
1144 if (TREE_CODE (node) == FUNCTION_DECL)
1146 DECL_STRUCT_FUNCTION (t) = NULL;
1147 t->decl_with_vis.symtab_node = NULL;
1150 else if (TREE_CODE_CLASS (code) == tcc_type)
1152 TYPE_UID (t) = next_type_uid++;
1153 /* The following is so that the debug code for
1154 the copy is different from the original type.
1155 The two statements usually duplicate each other
1156 (because they clear fields of the same union),
1157 but the optimizer should catch that. */
1158 TYPE_SYMTAB_POINTER (t) = 0;
1159 TYPE_SYMTAB_ADDRESS (t) = 0;
1161 /* Do not copy the values cache. */
1162 if (TYPE_CACHED_VALUES_P (t))
1164 TYPE_CACHED_VALUES_P (t) = 0;
1165 TYPE_CACHED_VALUES (t) = NULL_TREE;
1168 else if (code == TARGET_OPTION_NODE)
1170 TREE_TARGET_OPTION (t) = ggc_alloc<struct cl_target_option>();
1171 memcpy (TREE_TARGET_OPTION (t), TREE_TARGET_OPTION (node),
1172 sizeof (struct cl_target_option));
1174 else if (code == OPTIMIZATION_NODE)
1176 TREE_OPTIMIZATION (t) = ggc_alloc<struct cl_optimization>();
1177 memcpy (TREE_OPTIMIZATION (t), TREE_OPTIMIZATION (node),
1178 sizeof (struct cl_optimization));
1181 return t;
1184 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
1185 For example, this can copy a list made of TREE_LIST nodes. */
1187 tree
1188 copy_list (tree list)
1190 tree head;
1191 tree prev, next;
1193 if (list == 0)
1194 return 0;
1196 head = prev = copy_node (list);
1197 next = TREE_CHAIN (list);
1198 while (next)
1200 TREE_CHAIN (prev) = copy_node (next);
1201 prev = TREE_CHAIN (prev);
1202 next = TREE_CHAIN (next);
1204 return head;
1208 /* Return the value that TREE_INT_CST_EXT_NUNITS should have for an
1209 INTEGER_CST with value CST and type TYPE. */
1211 static unsigned int
1212 get_int_cst_ext_nunits (tree type, const wide_int &cst)
1214 gcc_checking_assert (cst.get_precision () == TYPE_PRECISION (type));
1215 /* We need an extra zero HWI if CST is an unsigned integer with its
1216 upper bit set, and if CST occupies a whole number of HWIs. */
1217 if (TYPE_UNSIGNED (type)
1218 && wi::neg_p (cst)
1219 && (cst.get_precision () % HOST_BITS_PER_WIDE_INT) == 0)
1220 return cst.get_precision () / HOST_BITS_PER_WIDE_INT + 1;
1221 return cst.get_len ();
1224 /* Return a new INTEGER_CST with value CST and type TYPE. */
1226 static tree
1227 build_new_int_cst (tree type, const wide_int &cst)
1229 unsigned int len = cst.get_len ();
1230 unsigned int ext_len = get_int_cst_ext_nunits (type, cst);
1231 tree nt = make_int_cst (len, ext_len);
1233 if (len < ext_len)
1235 --ext_len;
1236 TREE_INT_CST_ELT (nt, ext_len) = 0;
1237 for (unsigned int i = len; i < ext_len; ++i)
1238 TREE_INT_CST_ELT (nt, i) = -1;
1240 else if (TYPE_UNSIGNED (type)
1241 && cst.get_precision () < len * HOST_BITS_PER_WIDE_INT)
1243 len--;
1244 TREE_INT_CST_ELT (nt, len)
1245 = zext_hwi (cst.elt (len),
1246 cst.get_precision () % HOST_BITS_PER_WIDE_INT);
1249 for (unsigned int i = 0; i < len; i++)
1250 TREE_INT_CST_ELT (nt, i) = cst.elt (i);
1251 TREE_TYPE (nt) = type;
1252 return nt;
1255 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1257 tree
1258 build_int_cst (tree type, HOST_WIDE_INT low)
1260 /* Support legacy code. */
1261 if (!type)
1262 type = integer_type_node;
1264 return wide_int_to_tree (type, wi::shwi (low, TYPE_PRECISION (type)));
1267 tree
1268 build_int_cstu (tree type, unsigned HOST_WIDE_INT cst)
1270 return wide_int_to_tree (type, wi::uhwi (cst, TYPE_PRECISION (type)));
1273 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1275 tree
1276 build_int_cst_type (tree type, HOST_WIDE_INT low)
1278 gcc_assert (type);
1279 return wide_int_to_tree (type, wi::shwi (low, TYPE_PRECISION (type)));
1282 /* Constructs tree in type TYPE from with value given by CST. Signedness
1283 of CST is assumed to be the same as the signedness of TYPE. */
1285 tree
1286 double_int_to_tree (tree type, double_int cst)
1288 return wide_int_to_tree (type, widest_int::from (cst, TYPE_SIGN (type)));
1291 /* We force the wide_int CST to the range of the type TYPE by sign or
1292 zero extending it. OVERFLOWABLE indicates if we are interested in
1293 overflow of the value, when >0 we are only interested in signed
1294 overflow, for <0 we are interested in any overflow. OVERFLOWED
1295 indicates whether overflow has already occurred. CONST_OVERFLOWED
1296 indicates whether constant overflow has already occurred. We force
1297 T's value to be within range of T's type (by setting to 0 or 1 all
1298 the bits outside the type's range). We set TREE_OVERFLOWED if,
1299 OVERFLOWED is nonzero,
1300 or OVERFLOWABLE is >0 and signed overflow occurs
1301 or OVERFLOWABLE is <0 and any overflow occurs
1302 We return a new tree node for the extended wide_int. The node
1303 is shared if no overflow flags are set. */
1306 tree
1307 force_fit_type (tree type, const wide_int_ref &cst,
1308 int overflowable, bool overflowed)
1310 signop sign = TYPE_SIGN (type);
1312 /* If we need to set overflow flags, return a new unshared node. */
1313 if (overflowed || !wi::fits_to_tree_p (cst, type))
1315 if (overflowed
1316 || overflowable < 0
1317 || (overflowable > 0 && sign == SIGNED))
1319 wide_int tmp = wide_int::from (cst, TYPE_PRECISION (type), sign);
1320 tree t = build_new_int_cst (type, tmp);
1321 TREE_OVERFLOW (t) = 1;
1322 return t;
1326 /* Else build a shared node. */
1327 return wide_int_to_tree (type, cst);
1330 /* These are the hash table functions for the hash table of INTEGER_CST
1331 nodes of a sizetype. */
1333 /* Return the hash code X, an INTEGER_CST. */
1335 hashval_t
1336 int_cst_hasher::hash (tree x)
1338 const_tree const t = x;
1339 hashval_t code = TYPE_UID (TREE_TYPE (t));
1340 int i;
1342 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
1343 code ^= TREE_INT_CST_ELT (t, i);
1345 return code;
1348 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
1349 is the same as that given by *Y, which is the same. */
1351 bool
1352 int_cst_hasher::equal (tree x, tree y)
1354 const_tree const xt = x;
1355 const_tree const yt = y;
1357 if (TREE_TYPE (xt) != TREE_TYPE (yt)
1358 || TREE_INT_CST_NUNITS (xt) != TREE_INT_CST_NUNITS (yt)
1359 || TREE_INT_CST_EXT_NUNITS (xt) != TREE_INT_CST_EXT_NUNITS (yt))
1360 return false;
1362 for (int i = 0; i < TREE_INT_CST_NUNITS (xt); i++)
1363 if (TREE_INT_CST_ELT (xt, i) != TREE_INT_CST_ELT (yt, i))
1364 return false;
1366 return true;
1369 /* Create an INT_CST node of TYPE and value CST.
1370 The returned node is always shared. For small integers we use a
1371 per-type vector cache, for larger ones we use a single hash table.
1372 The value is extended from its precision according to the sign of
1373 the type to be a multiple of HOST_BITS_PER_WIDE_INT. This defines
1374 the upper bits and ensures that hashing and value equality based
1375 upon the underlying HOST_WIDE_INTs works without masking. */
1377 tree
1378 wide_int_to_tree (tree type, const wide_int_ref &pcst)
1380 tree t;
1381 int ix = -1;
1382 int limit = 0;
1384 gcc_assert (type);
1385 unsigned int prec = TYPE_PRECISION (type);
1386 signop sgn = TYPE_SIGN (type);
1388 /* Verify that everything is canonical. */
1389 int l = pcst.get_len ();
1390 if (l > 1)
1392 if (pcst.elt (l - 1) == 0)
1393 gcc_checking_assert (pcst.elt (l - 2) < 0);
1394 if (pcst.elt (l - 1) == (HOST_WIDE_INT) -1)
1395 gcc_checking_assert (pcst.elt (l - 2) >= 0);
1398 wide_int cst = wide_int::from (pcst, prec, sgn);
1399 unsigned int ext_len = get_int_cst_ext_nunits (type, cst);
1401 if (ext_len == 1)
1403 /* We just need to store a single HOST_WIDE_INT. */
1404 HOST_WIDE_INT hwi;
1405 if (TYPE_UNSIGNED (type))
1406 hwi = cst.to_uhwi ();
1407 else
1408 hwi = cst.to_shwi ();
1410 switch (TREE_CODE (type))
1412 case NULLPTR_TYPE:
1413 gcc_assert (hwi == 0);
1414 /* Fallthru. */
1416 case POINTER_TYPE:
1417 case REFERENCE_TYPE:
1418 case POINTER_BOUNDS_TYPE:
1419 /* Cache NULL pointer and zero bounds. */
1420 if (hwi == 0)
1422 limit = 1;
1423 ix = 0;
1425 break;
1427 case BOOLEAN_TYPE:
1428 /* Cache false or true. */
1429 limit = 2;
1430 if (hwi < 2)
1431 ix = hwi;
1432 break;
1434 case INTEGER_TYPE:
1435 case OFFSET_TYPE:
1436 if (TYPE_SIGN (type) == UNSIGNED)
1438 /* Cache [0, N). */
1439 limit = INTEGER_SHARE_LIMIT;
1440 if (IN_RANGE (hwi, 0, INTEGER_SHARE_LIMIT - 1))
1441 ix = hwi;
1443 else
1445 /* Cache [-1, N). */
1446 limit = INTEGER_SHARE_LIMIT + 1;
1447 if (IN_RANGE (hwi, -1, INTEGER_SHARE_LIMIT - 1))
1448 ix = hwi + 1;
1450 break;
1452 case ENUMERAL_TYPE:
1453 break;
1455 default:
1456 gcc_unreachable ();
1459 if (ix >= 0)
1461 /* Look for it in the type's vector of small shared ints. */
1462 if (!TYPE_CACHED_VALUES_P (type))
1464 TYPE_CACHED_VALUES_P (type) = 1;
1465 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1468 t = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix);
1469 if (t)
1470 /* Make sure no one is clobbering the shared constant. */
1471 gcc_checking_assert (TREE_TYPE (t) == type
1472 && TREE_INT_CST_NUNITS (t) == 1
1473 && TREE_INT_CST_OFFSET_NUNITS (t) == 1
1474 && TREE_INT_CST_EXT_NUNITS (t) == 1
1475 && TREE_INT_CST_ELT (t, 0) == hwi);
1476 else
1478 /* Create a new shared int. */
1479 t = build_new_int_cst (type, cst);
1480 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1483 else
1485 /* Use the cache of larger shared ints, using int_cst_node as
1486 a temporary. */
1488 TREE_INT_CST_ELT (int_cst_node, 0) = hwi;
1489 TREE_TYPE (int_cst_node) = type;
1491 tree *slot = int_cst_hash_table->find_slot (int_cst_node, INSERT);
1492 t = *slot;
1493 if (!t)
1495 /* Insert this one into the hash table. */
1496 t = int_cst_node;
1497 *slot = t;
1498 /* Make a new node for next time round. */
1499 int_cst_node = make_int_cst (1, 1);
1503 else
1505 /* The value either hashes properly or we drop it on the floor
1506 for the gc to take care of. There will not be enough of them
1507 to worry about. */
1509 tree nt = build_new_int_cst (type, cst);
1510 tree *slot = int_cst_hash_table->find_slot (nt, INSERT);
1511 t = *slot;
1512 if (!t)
1514 /* Insert this one into the hash table. */
1515 t = nt;
1516 *slot = t;
1520 return t;
1523 void
1524 cache_integer_cst (tree t)
1526 tree type = TREE_TYPE (t);
1527 int ix = -1;
1528 int limit = 0;
1529 int prec = TYPE_PRECISION (type);
1531 gcc_assert (!TREE_OVERFLOW (t));
1533 switch (TREE_CODE (type))
1535 case NULLPTR_TYPE:
1536 gcc_assert (integer_zerop (t));
1537 /* Fallthru. */
1539 case POINTER_TYPE:
1540 case REFERENCE_TYPE:
1541 /* Cache NULL pointer. */
1542 if (integer_zerop (t))
1544 limit = 1;
1545 ix = 0;
1547 break;
1549 case BOOLEAN_TYPE:
1550 /* Cache false or true. */
1551 limit = 2;
1552 if (wi::ltu_p (t, 2))
1553 ix = TREE_INT_CST_ELT (t, 0);
1554 break;
1556 case INTEGER_TYPE:
1557 case OFFSET_TYPE:
1558 if (TYPE_UNSIGNED (type))
1560 /* Cache 0..N */
1561 limit = INTEGER_SHARE_LIMIT;
1563 /* This is a little hokie, but if the prec is smaller than
1564 what is necessary to hold INTEGER_SHARE_LIMIT, then the
1565 obvious test will not get the correct answer. */
1566 if (prec < HOST_BITS_PER_WIDE_INT)
1568 if (tree_to_uhwi (t) < (unsigned HOST_WIDE_INT) INTEGER_SHARE_LIMIT)
1569 ix = tree_to_uhwi (t);
1571 else if (wi::ltu_p (t, INTEGER_SHARE_LIMIT))
1572 ix = tree_to_uhwi (t);
1574 else
1576 /* Cache -1..N */
1577 limit = INTEGER_SHARE_LIMIT + 1;
1579 if (integer_minus_onep (t))
1580 ix = 0;
1581 else if (!wi::neg_p (t))
1583 if (prec < HOST_BITS_PER_WIDE_INT)
1585 if (tree_to_shwi (t) < INTEGER_SHARE_LIMIT)
1586 ix = tree_to_shwi (t) + 1;
1588 else if (wi::ltu_p (t, INTEGER_SHARE_LIMIT))
1589 ix = tree_to_shwi (t) + 1;
1592 break;
1594 case ENUMERAL_TYPE:
1595 break;
1597 default:
1598 gcc_unreachable ();
1601 if (ix >= 0)
1603 /* Look for it in the type's vector of small shared ints. */
1604 if (!TYPE_CACHED_VALUES_P (type))
1606 TYPE_CACHED_VALUES_P (type) = 1;
1607 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1610 gcc_assert (TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) == NULL_TREE);
1611 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1613 else
1615 /* Use the cache of larger shared ints. */
1616 tree *slot = int_cst_hash_table->find_slot (t, INSERT);
1617 /* If there is already an entry for the number verify it's the
1618 same. */
1619 if (*slot)
1620 gcc_assert (wi::eq_p (tree (*slot), t));
1621 else
1622 /* Otherwise insert this one into the hash table. */
1623 *slot = t;
1628 /* Builds an integer constant in TYPE such that lowest BITS bits are ones
1629 and the rest are zeros. */
1631 tree
1632 build_low_bits_mask (tree type, unsigned bits)
1634 gcc_assert (bits <= TYPE_PRECISION (type));
1636 return wide_int_to_tree (type, wi::mask (bits, false,
1637 TYPE_PRECISION (type)));
1640 /* Checks that X is integer constant that can be expressed in (unsigned)
1641 HOST_WIDE_INT without loss of precision. */
1643 bool
1644 cst_and_fits_in_hwi (const_tree x)
1646 if (TREE_CODE (x) != INTEGER_CST)
1647 return false;
1649 if (TYPE_PRECISION (TREE_TYPE (x)) > HOST_BITS_PER_WIDE_INT)
1650 return false;
1652 return TREE_INT_CST_NUNITS (x) == 1;
1655 /* Build a newly constructed VECTOR_CST node of length LEN. */
1657 tree
1658 make_vector_stat (unsigned len MEM_STAT_DECL)
1660 tree t;
1661 unsigned length = (len - 1) * sizeof (tree) + sizeof (struct tree_vector);
1663 record_node_allocation_statistics (VECTOR_CST, length);
1665 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1667 TREE_SET_CODE (t, VECTOR_CST);
1668 TREE_CONSTANT (t) = 1;
1670 return t;
1673 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1674 are in a list pointed to by VALS. */
1676 tree
1677 build_vector_stat (tree type, tree *vals MEM_STAT_DECL)
1679 int over = 0;
1680 unsigned cnt = 0;
1681 tree v = make_vector (TYPE_VECTOR_SUBPARTS (type));
1682 TREE_TYPE (v) = type;
1684 /* Iterate through elements and check for overflow. */
1685 for (cnt = 0; cnt < TYPE_VECTOR_SUBPARTS (type); ++cnt)
1687 tree value = vals[cnt];
1689 VECTOR_CST_ELT (v, cnt) = value;
1691 /* Don't crash if we get an address constant. */
1692 if (!CONSTANT_CLASS_P (value))
1693 continue;
1695 over |= TREE_OVERFLOW (value);
1698 TREE_OVERFLOW (v) = over;
1699 return v;
1702 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1703 are extracted from V, a vector of CONSTRUCTOR_ELT. */
1705 tree
1706 build_vector_from_ctor (tree type, vec<constructor_elt, va_gc> *v)
1708 tree *vec = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
1709 unsigned HOST_WIDE_INT idx;
1710 tree value;
1712 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1713 vec[idx] = value;
1714 for (; idx < TYPE_VECTOR_SUBPARTS (type); ++idx)
1715 vec[idx] = build_zero_cst (TREE_TYPE (type));
1717 return build_vector (type, vec);
1720 /* Build a vector of type VECTYPE where all the elements are SCs. */
1721 tree
1722 build_vector_from_val (tree vectype, tree sc)
1724 int i, nunits = TYPE_VECTOR_SUBPARTS (vectype);
1726 if (sc == error_mark_node)
1727 return sc;
1729 /* Verify that the vector type is suitable for SC. Note that there
1730 is some inconsistency in the type-system with respect to restrict
1731 qualifications of pointers. Vector types always have a main-variant
1732 element type and the qualification is applied to the vector-type.
1733 So TREE_TYPE (vector-type) does not return a properly qualified
1734 vector element-type. */
1735 gcc_checking_assert (types_compatible_p (TYPE_MAIN_VARIANT (TREE_TYPE (sc)),
1736 TREE_TYPE (vectype)));
1738 if (CONSTANT_CLASS_P (sc))
1740 tree *v = XALLOCAVEC (tree, nunits);
1741 for (i = 0; i < nunits; ++i)
1742 v[i] = sc;
1743 return build_vector (vectype, v);
1745 else
1747 vec<constructor_elt, va_gc> *v;
1748 vec_alloc (v, nunits);
1749 for (i = 0; i < nunits; ++i)
1750 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, sc);
1751 return build_constructor (vectype, v);
1755 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1756 are in the vec pointed to by VALS. */
1757 tree
1758 build_constructor (tree type, vec<constructor_elt, va_gc> *vals)
1760 tree c = make_node (CONSTRUCTOR);
1761 unsigned int i;
1762 constructor_elt *elt;
1763 bool constant_p = true;
1764 bool side_effects_p = false;
1766 TREE_TYPE (c) = type;
1767 CONSTRUCTOR_ELTS (c) = vals;
1769 FOR_EACH_VEC_SAFE_ELT (vals, i, elt)
1771 /* Mostly ctors will have elts that don't have side-effects, so
1772 the usual case is to scan all the elements. Hence a single
1773 loop for both const and side effects, rather than one loop
1774 each (with early outs). */
1775 if (!TREE_CONSTANT (elt->value))
1776 constant_p = false;
1777 if (TREE_SIDE_EFFECTS (elt->value))
1778 side_effects_p = true;
1781 TREE_SIDE_EFFECTS (c) = side_effects_p;
1782 TREE_CONSTANT (c) = constant_p;
1784 return c;
1787 /* Build a CONSTRUCTOR node made of a single initializer, with the specified
1788 INDEX and VALUE. */
1789 tree
1790 build_constructor_single (tree type, tree index, tree value)
1792 vec<constructor_elt, va_gc> *v;
1793 constructor_elt elt = {index, value};
1795 vec_alloc (v, 1);
1796 v->quick_push (elt);
1798 return build_constructor (type, v);
1802 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1803 are in a list pointed to by VALS. */
1804 tree
1805 build_constructor_from_list (tree type, tree vals)
1807 tree t;
1808 vec<constructor_elt, va_gc> *v = NULL;
1810 if (vals)
1812 vec_alloc (v, list_length (vals));
1813 for (t = vals; t; t = TREE_CHAIN (t))
1814 CONSTRUCTOR_APPEND_ELT (v, TREE_PURPOSE (t), TREE_VALUE (t));
1817 return build_constructor (type, v);
1820 /* Return a new CONSTRUCTOR node whose type is TYPE. NELTS is the number
1821 of elements, provided as index/value pairs. */
1823 tree
1824 build_constructor_va (tree type, int nelts, ...)
1826 vec<constructor_elt, va_gc> *v = NULL;
1827 va_list p;
1829 va_start (p, nelts);
1830 vec_alloc (v, nelts);
1831 while (nelts--)
1833 tree index = va_arg (p, tree);
1834 tree value = va_arg (p, tree);
1835 CONSTRUCTOR_APPEND_ELT (v, index, value);
1837 va_end (p);
1838 return build_constructor (type, v);
1841 /* Return a new FIXED_CST node whose type is TYPE and value is F. */
1843 tree
1844 build_fixed (tree type, FIXED_VALUE_TYPE f)
1846 tree v;
1847 FIXED_VALUE_TYPE *fp;
1849 v = make_node (FIXED_CST);
1850 fp = ggc_alloc<fixed_value> ();
1851 memcpy (fp, &f, sizeof (FIXED_VALUE_TYPE));
1853 TREE_TYPE (v) = type;
1854 TREE_FIXED_CST_PTR (v) = fp;
1855 return v;
1858 /* Return a new REAL_CST node whose type is TYPE and value is D. */
1860 tree
1861 build_real (tree type, REAL_VALUE_TYPE d)
1863 tree v;
1864 REAL_VALUE_TYPE *dp;
1865 int overflow = 0;
1867 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
1868 Consider doing it via real_convert now. */
1870 v = make_node (REAL_CST);
1871 dp = ggc_alloc<real_value> ();
1872 memcpy (dp, &d, sizeof (REAL_VALUE_TYPE));
1874 TREE_TYPE (v) = type;
1875 TREE_REAL_CST_PTR (v) = dp;
1876 TREE_OVERFLOW (v) = overflow;
1877 return v;
1880 /* Like build_real, but first truncate D to the type. */
1882 tree
1883 build_real_truncate (tree type, REAL_VALUE_TYPE d)
1885 return build_real (type, real_value_truncate (TYPE_MODE (type), d));
1888 /* Return a new REAL_CST node whose type is TYPE
1889 and whose value is the integer value of the INTEGER_CST node I. */
1891 REAL_VALUE_TYPE
1892 real_value_from_int_cst (const_tree type, const_tree i)
1894 REAL_VALUE_TYPE d;
1896 /* Clear all bits of the real value type so that we can later do
1897 bitwise comparisons to see if two values are the same. */
1898 memset (&d, 0, sizeof d);
1900 real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode, i,
1901 TYPE_SIGN (TREE_TYPE (i)));
1902 return d;
1905 /* Given a tree representing an integer constant I, return a tree
1906 representing the same value as a floating-point constant of type TYPE. */
1908 tree
1909 build_real_from_int_cst (tree type, const_tree i)
1911 tree v;
1912 int overflow = TREE_OVERFLOW (i);
1914 v = build_real (type, real_value_from_int_cst (type, i));
1916 TREE_OVERFLOW (v) |= overflow;
1917 return v;
1920 /* Return a newly constructed STRING_CST node whose value is
1921 the LEN characters at STR.
1922 Note that for a C string literal, LEN should include the trailing NUL.
1923 The TREE_TYPE is not initialized. */
1925 tree
1926 build_string (int len, const char *str)
1928 tree s;
1929 size_t length;
1931 /* Do not waste bytes provided by padding of struct tree_string. */
1932 length = len + offsetof (struct tree_string, str) + 1;
1934 record_node_allocation_statistics (STRING_CST, length);
1936 s = (tree) ggc_internal_alloc (length);
1938 memset (s, 0, sizeof (struct tree_typed));
1939 TREE_SET_CODE (s, STRING_CST);
1940 TREE_CONSTANT (s) = 1;
1941 TREE_STRING_LENGTH (s) = len;
1942 memcpy (s->string.str, str, len);
1943 s->string.str[len] = '\0';
1945 return s;
1948 /* Return a newly constructed COMPLEX_CST node whose value is
1949 specified by the real and imaginary parts REAL and IMAG.
1950 Both REAL and IMAG should be constant nodes. TYPE, if specified,
1951 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
1953 tree
1954 build_complex (tree type, tree real, tree imag)
1956 tree t = make_node (COMPLEX_CST);
1958 TREE_REALPART (t) = real;
1959 TREE_IMAGPART (t) = imag;
1960 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
1961 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
1962 return t;
1965 /* Return the constant 1 in type TYPE. If TYPE has several elements, each
1966 element is set to 1. In particular, this is 1 + i for complex types. */
1968 tree
1969 build_each_one_cst (tree type)
1971 if (TREE_CODE (type) == COMPLEX_TYPE)
1973 tree scalar = build_one_cst (TREE_TYPE (type));
1974 return build_complex (type, scalar, scalar);
1976 else
1977 return build_one_cst (type);
1980 /* Return a constant of arithmetic type TYPE which is the
1981 multiplicative identity of the set TYPE. */
1983 tree
1984 build_one_cst (tree type)
1986 switch (TREE_CODE (type))
1988 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1989 case POINTER_TYPE: case REFERENCE_TYPE:
1990 case OFFSET_TYPE:
1991 return build_int_cst (type, 1);
1993 case REAL_TYPE:
1994 return build_real (type, dconst1);
1996 case FIXED_POINT_TYPE:
1997 /* We can only generate 1 for accum types. */
1998 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
1999 return build_fixed (type, FCONST1 (TYPE_MODE (type)));
2001 case VECTOR_TYPE:
2003 tree scalar = build_one_cst (TREE_TYPE (type));
2005 return build_vector_from_val (type, scalar);
2008 case COMPLEX_TYPE:
2009 return build_complex (type,
2010 build_one_cst (TREE_TYPE (type)),
2011 build_zero_cst (TREE_TYPE (type)));
2013 default:
2014 gcc_unreachable ();
2018 /* Return an integer of type TYPE containing all 1's in as much precision as
2019 it contains, or a complex or vector whose subparts are such integers. */
2021 tree
2022 build_all_ones_cst (tree type)
2024 if (TREE_CODE (type) == COMPLEX_TYPE)
2026 tree scalar = build_all_ones_cst (TREE_TYPE (type));
2027 return build_complex (type, scalar, scalar);
2029 else
2030 return build_minus_one_cst (type);
2033 /* Return a constant of arithmetic type TYPE which is the
2034 opposite of the multiplicative identity of the set TYPE. */
2036 tree
2037 build_minus_one_cst (tree type)
2039 switch (TREE_CODE (type))
2041 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2042 case POINTER_TYPE: case REFERENCE_TYPE:
2043 case OFFSET_TYPE:
2044 return build_int_cst (type, -1);
2046 case REAL_TYPE:
2047 return build_real (type, dconstm1);
2049 case FIXED_POINT_TYPE:
2050 /* We can only generate 1 for accum types. */
2051 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
2052 return build_fixed (type, fixed_from_double_int (double_int_minus_one,
2053 TYPE_MODE (type)));
2055 case VECTOR_TYPE:
2057 tree scalar = build_minus_one_cst (TREE_TYPE (type));
2059 return build_vector_from_val (type, scalar);
2062 case COMPLEX_TYPE:
2063 return build_complex (type,
2064 build_minus_one_cst (TREE_TYPE (type)),
2065 build_zero_cst (TREE_TYPE (type)));
2067 default:
2068 gcc_unreachable ();
2072 /* Build 0 constant of type TYPE. This is used by constructor folding
2073 and thus the constant should be represented in memory by
2074 zero(es). */
2076 tree
2077 build_zero_cst (tree type)
2079 switch (TREE_CODE (type))
2081 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2082 case POINTER_TYPE: case REFERENCE_TYPE:
2083 case OFFSET_TYPE: case NULLPTR_TYPE:
2084 return build_int_cst (type, 0);
2086 case REAL_TYPE:
2087 return build_real (type, dconst0);
2089 case FIXED_POINT_TYPE:
2090 return build_fixed (type, FCONST0 (TYPE_MODE (type)));
2092 case VECTOR_TYPE:
2094 tree scalar = build_zero_cst (TREE_TYPE (type));
2096 return build_vector_from_val (type, scalar);
2099 case COMPLEX_TYPE:
2101 tree zero = build_zero_cst (TREE_TYPE (type));
2103 return build_complex (type, zero, zero);
2106 default:
2107 if (!AGGREGATE_TYPE_P (type))
2108 return fold_convert (type, integer_zero_node);
2109 return build_constructor (type, NULL);
2114 /* Build a BINFO with LEN language slots. */
2116 tree
2117 make_tree_binfo_stat (unsigned base_binfos MEM_STAT_DECL)
2119 tree t;
2120 size_t length = (offsetof (struct tree_binfo, base_binfos)
2121 + vec<tree, va_gc>::embedded_size (base_binfos));
2123 record_node_allocation_statistics (TREE_BINFO, length);
2125 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
2127 memset (t, 0, offsetof (struct tree_binfo, base_binfos));
2129 TREE_SET_CODE (t, TREE_BINFO);
2131 BINFO_BASE_BINFOS (t)->embedded_init (base_binfos);
2133 return t;
2136 /* Create a CASE_LABEL_EXPR tree node and return it. */
2138 tree
2139 build_case_label (tree low_value, tree high_value, tree label_decl)
2141 tree t = make_node (CASE_LABEL_EXPR);
2143 TREE_TYPE (t) = void_type_node;
2144 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (label_decl));
2146 CASE_LOW (t) = low_value;
2147 CASE_HIGH (t) = high_value;
2148 CASE_LABEL (t) = label_decl;
2149 CASE_CHAIN (t) = NULL_TREE;
2151 return t;
2154 /* Build a newly constructed INTEGER_CST node. LEN and EXT_LEN are the
2155 values of TREE_INT_CST_NUNITS and TREE_INT_CST_EXT_NUNITS respectively.
2156 The latter determines the length of the HOST_WIDE_INT vector. */
2158 tree
2159 make_int_cst_stat (int len, int ext_len MEM_STAT_DECL)
2161 tree t;
2162 int length = ((ext_len - 1) * sizeof (HOST_WIDE_INT)
2163 + sizeof (struct tree_int_cst));
2165 gcc_assert (len);
2166 record_node_allocation_statistics (INTEGER_CST, length);
2168 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2170 TREE_SET_CODE (t, INTEGER_CST);
2171 TREE_INT_CST_NUNITS (t) = len;
2172 TREE_INT_CST_EXT_NUNITS (t) = ext_len;
2173 /* to_offset can only be applied to trees that are offset_int-sized
2174 or smaller. EXT_LEN is correct if it fits, otherwise the constant
2175 must be exactly the precision of offset_int and so LEN is correct. */
2176 if (ext_len <= OFFSET_INT_ELTS)
2177 TREE_INT_CST_OFFSET_NUNITS (t) = ext_len;
2178 else
2179 TREE_INT_CST_OFFSET_NUNITS (t) = len;
2181 TREE_CONSTANT (t) = 1;
2183 return t;
2186 /* Build a newly constructed TREE_VEC node of length LEN. */
2188 tree
2189 make_tree_vec_stat (int len MEM_STAT_DECL)
2191 tree t;
2192 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
2194 record_node_allocation_statistics (TREE_VEC, length);
2196 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2198 TREE_SET_CODE (t, TREE_VEC);
2199 TREE_VEC_LENGTH (t) = len;
2201 return t;
2204 /* Grow a TREE_VEC node to new length LEN. */
2206 tree
2207 grow_tree_vec_stat (tree v, int len MEM_STAT_DECL)
2209 gcc_assert (TREE_CODE (v) == TREE_VEC);
2211 int oldlen = TREE_VEC_LENGTH (v);
2212 gcc_assert (len > oldlen);
2214 int oldlength = (oldlen - 1) * sizeof (tree) + sizeof (struct tree_vec);
2215 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
2217 record_node_allocation_statistics (TREE_VEC, length - oldlength);
2219 v = (tree) ggc_realloc (v, length PASS_MEM_STAT);
2221 TREE_VEC_LENGTH (v) = len;
2223 return v;
2226 /* Return 1 if EXPR is the constant zero, whether it is integral, float or
2227 fixed, and scalar, complex or vector. */
2230 zerop (const_tree expr)
2232 return (integer_zerop (expr)
2233 || real_zerop (expr)
2234 || fixed_zerop (expr));
2237 /* Return 1 if EXPR is the integer constant zero or a complex constant
2238 of zero. */
2241 integer_zerop (const_tree expr)
2243 STRIP_NOPS (expr);
2245 switch (TREE_CODE (expr))
2247 case INTEGER_CST:
2248 return wi::eq_p (expr, 0);
2249 case COMPLEX_CST:
2250 return (integer_zerop (TREE_REALPART (expr))
2251 && integer_zerop (TREE_IMAGPART (expr)));
2252 case VECTOR_CST:
2254 unsigned i;
2255 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2256 if (!integer_zerop (VECTOR_CST_ELT (expr, i)))
2257 return false;
2258 return true;
2260 default:
2261 return false;
2265 /* Return 1 if EXPR is the integer constant one or the corresponding
2266 complex constant. */
2269 integer_onep (const_tree expr)
2271 STRIP_NOPS (expr);
2273 switch (TREE_CODE (expr))
2275 case INTEGER_CST:
2276 return wi::eq_p (wi::to_widest (expr), 1);
2277 case COMPLEX_CST:
2278 return (integer_onep (TREE_REALPART (expr))
2279 && integer_zerop (TREE_IMAGPART (expr)));
2280 case VECTOR_CST:
2282 unsigned i;
2283 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2284 if (!integer_onep (VECTOR_CST_ELT (expr, i)))
2285 return false;
2286 return true;
2288 default:
2289 return false;
2293 /* Return 1 if EXPR is the integer constant one. For complex and vector,
2294 return 1 if every piece is the integer constant one. */
2297 integer_each_onep (const_tree expr)
2299 STRIP_NOPS (expr);
2301 if (TREE_CODE (expr) == COMPLEX_CST)
2302 return (integer_onep (TREE_REALPART (expr))
2303 && integer_onep (TREE_IMAGPART (expr)));
2304 else
2305 return integer_onep (expr);
2308 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
2309 it contains, or a complex or vector whose subparts are such integers. */
2312 integer_all_onesp (const_tree expr)
2314 STRIP_NOPS (expr);
2316 if (TREE_CODE (expr) == COMPLEX_CST
2317 && integer_all_onesp (TREE_REALPART (expr))
2318 && integer_all_onesp (TREE_IMAGPART (expr)))
2319 return 1;
2321 else if (TREE_CODE (expr) == VECTOR_CST)
2323 unsigned i;
2324 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2325 if (!integer_all_onesp (VECTOR_CST_ELT (expr, i)))
2326 return 0;
2327 return 1;
2330 else if (TREE_CODE (expr) != INTEGER_CST)
2331 return 0;
2333 return wi::max_value (TYPE_PRECISION (TREE_TYPE (expr)), UNSIGNED) == expr;
2336 /* Return 1 if EXPR is the integer constant minus one. */
2339 integer_minus_onep (const_tree expr)
2341 STRIP_NOPS (expr);
2343 if (TREE_CODE (expr) == COMPLEX_CST)
2344 return (integer_all_onesp (TREE_REALPART (expr))
2345 && integer_zerop (TREE_IMAGPART (expr)));
2346 else
2347 return integer_all_onesp (expr);
2350 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
2351 one bit on). */
2354 integer_pow2p (const_tree expr)
2356 STRIP_NOPS (expr);
2358 if (TREE_CODE (expr) == COMPLEX_CST
2359 && integer_pow2p (TREE_REALPART (expr))
2360 && integer_zerop (TREE_IMAGPART (expr)))
2361 return 1;
2363 if (TREE_CODE (expr) != INTEGER_CST)
2364 return 0;
2366 return wi::popcount (expr) == 1;
2369 /* Return 1 if EXPR is an integer constant other than zero or a
2370 complex constant other than zero. */
2373 integer_nonzerop (const_tree expr)
2375 STRIP_NOPS (expr);
2377 return ((TREE_CODE (expr) == INTEGER_CST
2378 && !wi::eq_p (expr, 0))
2379 || (TREE_CODE (expr) == COMPLEX_CST
2380 && (integer_nonzerop (TREE_REALPART (expr))
2381 || integer_nonzerop (TREE_IMAGPART (expr)))));
2384 /* Return 1 if EXPR is the integer constant one. For vector,
2385 return 1 if every piece is the integer constant minus one
2386 (representing the value TRUE). */
2389 integer_truep (const_tree expr)
2391 STRIP_NOPS (expr);
2393 if (TREE_CODE (expr) == VECTOR_CST)
2394 return integer_all_onesp (expr);
2395 return integer_onep (expr);
2398 /* Return 1 if EXPR is the fixed-point constant zero. */
2401 fixed_zerop (const_tree expr)
2403 return (TREE_CODE (expr) == FIXED_CST
2404 && TREE_FIXED_CST (expr).data.is_zero ());
2407 /* Return the power of two represented by a tree node known to be a
2408 power of two. */
2411 tree_log2 (const_tree expr)
2413 STRIP_NOPS (expr);
2415 if (TREE_CODE (expr) == COMPLEX_CST)
2416 return tree_log2 (TREE_REALPART (expr));
2418 return wi::exact_log2 (expr);
2421 /* Similar, but return the largest integer Y such that 2 ** Y is less
2422 than or equal to EXPR. */
2425 tree_floor_log2 (const_tree expr)
2427 STRIP_NOPS (expr);
2429 if (TREE_CODE (expr) == COMPLEX_CST)
2430 return tree_log2 (TREE_REALPART (expr));
2432 return wi::floor_log2 (expr);
2435 /* Return number of known trailing zero bits in EXPR, or, if the value of
2436 EXPR is known to be zero, the precision of it's type. */
2438 unsigned int
2439 tree_ctz (const_tree expr)
2441 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
2442 && !POINTER_TYPE_P (TREE_TYPE (expr)))
2443 return 0;
2445 unsigned int ret1, ret2, prec = TYPE_PRECISION (TREE_TYPE (expr));
2446 switch (TREE_CODE (expr))
2448 case INTEGER_CST:
2449 ret1 = wi::ctz (expr);
2450 return MIN (ret1, prec);
2451 case SSA_NAME:
2452 ret1 = wi::ctz (get_nonzero_bits (expr));
2453 return MIN (ret1, prec);
2454 case PLUS_EXPR:
2455 case MINUS_EXPR:
2456 case BIT_IOR_EXPR:
2457 case BIT_XOR_EXPR:
2458 case MIN_EXPR:
2459 case MAX_EXPR:
2460 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2461 if (ret1 == 0)
2462 return ret1;
2463 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2464 return MIN (ret1, ret2);
2465 case POINTER_PLUS_EXPR:
2466 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2467 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2468 /* Second operand is sizetype, which could be in theory
2469 wider than pointer's precision. Make sure we never
2470 return more than prec. */
2471 ret2 = MIN (ret2, prec);
2472 return MIN (ret1, ret2);
2473 case BIT_AND_EXPR:
2474 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2475 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2476 return MAX (ret1, ret2);
2477 case MULT_EXPR:
2478 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2479 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2480 return MIN (ret1 + ret2, prec);
2481 case LSHIFT_EXPR:
2482 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2483 if (tree_fits_uhwi_p (TREE_OPERAND (expr, 1))
2484 && (tree_to_uhwi (TREE_OPERAND (expr, 1)) < prec))
2486 ret2 = tree_to_uhwi (TREE_OPERAND (expr, 1));
2487 return MIN (ret1 + ret2, prec);
2489 return ret1;
2490 case RSHIFT_EXPR:
2491 if (tree_fits_uhwi_p (TREE_OPERAND (expr, 1))
2492 && (tree_to_uhwi (TREE_OPERAND (expr, 1)) < prec))
2494 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2495 ret2 = tree_to_uhwi (TREE_OPERAND (expr, 1));
2496 if (ret1 > ret2)
2497 return ret1 - ret2;
2499 return 0;
2500 case TRUNC_DIV_EXPR:
2501 case CEIL_DIV_EXPR:
2502 case FLOOR_DIV_EXPR:
2503 case ROUND_DIV_EXPR:
2504 case EXACT_DIV_EXPR:
2505 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
2506 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) == 1)
2508 int l = tree_log2 (TREE_OPERAND (expr, 1));
2509 if (l >= 0)
2511 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2512 ret2 = l;
2513 if (ret1 > ret2)
2514 return ret1 - ret2;
2517 return 0;
2518 CASE_CONVERT:
2519 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2520 if (ret1 && ret1 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2521 ret1 = prec;
2522 return MIN (ret1, prec);
2523 case SAVE_EXPR:
2524 return tree_ctz (TREE_OPERAND (expr, 0));
2525 case COND_EXPR:
2526 ret1 = tree_ctz (TREE_OPERAND (expr, 1));
2527 if (ret1 == 0)
2528 return 0;
2529 ret2 = tree_ctz (TREE_OPERAND (expr, 2));
2530 return MIN (ret1, ret2);
2531 case COMPOUND_EXPR:
2532 return tree_ctz (TREE_OPERAND (expr, 1));
2533 case ADDR_EXPR:
2534 ret1 = get_pointer_alignment (CONST_CAST_TREE (expr));
2535 if (ret1 > BITS_PER_UNIT)
2537 ret1 = ctz_hwi (ret1 / BITS_PER_UNIT);
2538 return MIN (ret1, prec);
2540 return 0;
2541 default:
2542 return 0;
2546 /* Return 1 if EXPR is the real constant zero. Trailing zeroes matter for
2547 decimal float constants, so don't return 1 for them. */
2550 real_zerop (const_tree expr)
2552 STRIP_NOPS (expr);
2554 switch (TREE_CODE (expr))
2556 case REAL_CST:
2557 return real_equal (&TREE_REAL_CST (expr), &dconst0)
2558 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2559 case COMPLEX_CST:
2560 return real_zerop (TREE_REALPART (expr))
2561 && real_zerop (TREE_IMAGPART (expr));
2562 case VECTOR_CST:
2564 unsigned i;
2565 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2566 if (!real_zerop (VECTOR_CST_ELT (expr, i)))
2567 return false;
2568 return true;
2570 default:
2571 return false;
2575 /* Return 1 if EXPR is the real constant one in real or complex form.
2576 Trailing zeroes matter for decimal float constants, so don't return
2577 1 for them. */
2580 real_onep (const_tree expr)
2582 STRIP_NOPS (expr);
2584 switch (TREE_CODE (expr))
2586 case REAL_CST:
2587 return real_equal (&TREE_REAL_CST (expr), &dconst1)
2588 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2589 case COMPLEX_CST:
2590 return real_onep (TREE_REALPART (expr))
2591 && real_zerop (TREE_IMAGPART (expr));
2592 case VECTOR_CST:
2594 unsigned i;
2595 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2596 if (!real_onep (VECTOR_CST_ELT (expr, i)))
2597 return false;
2598 return true;
2600 default:
2601 return false;
2605 /* Return 1 if EXPR is the real constant minus one. Trailing zeroes
2606 matter for decimal float constants, so don't return 1 for them. */
2609 real_minus_onep (const_tree expr)
2611 STRIP_NOPS (expr);
2613 switch (TREE_CODE (expr))
2615 case REAL_CST:
2616 return real_equal (&TREE_REAL_CST (expr), &dconstm1)
2617 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2618 case COMPLEX_CST:
2619 return real_minus_onep (TREE_REALPART (expr))
2620 && real_zerop (TREE_IMAGPART (expr));
2621 case VECTOR_CST:
2623 unsigned i;
2624 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2625 if (!real_minus_onep (VECTOR_CST_ELT (expr, i)))
2626 return false;
2627 return true;
2629 default:
2630 return false;
2634 /* Nonzero if EXP is a constant or a cast of a constant. */
2637 really_constant_p (const_tree exp)
2639 /* This is not quite the same as STRIP_NOPS. It does more. */
2640 while (CONVERT_EXPR_P (exp)
2641 || TREE_CODE (exp) == NON_LVALUE_EXPR)
2642 exp = TREE_OPERAND (exp, 0);
2643 return TREE_CONSTANT (exp);
2646 /* Return first list element whose TREE_VALUE is ELEM.
2647 Return 0 if ELEM is not in LIST. */
2649 tree
2650 value_member (tree elem, tree list)
2652 while (list)
2654 if (elem == TREE_VALUE (list))
2655 return list;
2656 list = TREE_CHAIN (list);
2658 return NULL_TREE;
2661 /* Return first list element whose TREE_PURPOSE is ELEM.
2662 Return 0 if ELEM is not in LIST. */
2664 tree
2665 purpose_member (const_tree elem, tree list)
2667 while (list)
2669 if (elem == TREE_PURPOSE (list))
2670 return list;
2671 list = TREE_CHAIN (list);
2673 return NULL_TREE;
2676 /* Return true if ELEM is in V. */
2678 bool
2679 vec_member (const_tree elem, vec<tree, va_gc> *v)
2681 unsigned ix;
2682 tree t;
2683 FOR_EACH_VEC_SAFE_ELT (v, ix, t)
2684 if (elem == t)
2685 return true;
2686 return false;
2689 /* Returns element number IDX (zero-origin) of chain CHAIN, or
2690 NULL_TREE. */
2692 tree
2693 chain_index (int idx, tree chain)
2695 for (; chain && idx > 0; --idx)
2696 chain = TREE_CHAIN (chain);
2697 return chain;
2700 /* Return nonzero if ELEM is part of the chain CHAIN. */
2703 chain_member (const_tree elem, const_tree chain)
2705 while (chain)
2707 if (elem == chain)
2708 return 1;
2709 chain = DECL_CHAIN (chain);
2712 return 0;
2715 /* Return the length of a chain of nodes chained through TREE_CHAIN.
2716 We expect a null pointer to mark the end of the chain.
2717 This is the Lisp primitive `length'. */
2720 list_length (const_tree t)
2722 const_tree p = t;
2723 #ifdef ENABLE_TREE_CHECKING
2724 const_tree q = t;
2725 #endif
2726 int len = 0;
2728 while (p)
2730 p = TREE_CHAIN (p);
2731 #ifdef ENABLE_TREE_CHECKING
2732 if (len % 2)
2733 q = TREE_CHAIN (q);
2734 gcc_assert (p != q);
2735 #endif
2736 len++;
2739 return len;
2742 /* Returns the first FIELD_DECL in the TYPE_FIELDS of the RECORD_TYPE or
2743 UNION_TYPE TYPE, or NULL_TREE if none. */
2745 tree
2746 first_field (const_tree type)
2748 tree t = TYPE_FIELDS (type);
2749 while (t && TREE_CODE (t) != FIELD_DECL)
2750 t = TREE_CHAIN (t);
2751 return t;
2754 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
2755 by modifying the last node in chain 1 to point to chain 2.
2756 This is the Lisp primitive `nconc'. */
2758 tree
2759 chainon (tree op1, tree op2)
2761 tree t1;
2763 if (!op1)
2764 return op2;
2765 if (!op2)
2766 return op1;
2768 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
2769 continue;
2770 TREE_CHAIN (t1) = op2;
2772 #ifdef ENABLE_TREE_CHECKING
2774 tree t2;
2775 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
2776 gcc_assert (t2 != t1);
2778 #endif
2780 return op1;
2783 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
2785 tree
2786 tree_last (tree chain)
2788 tree next;
2789 if (chain)
2790 while ((next = TREE_CHAIN (chain)))
2791 chain = next;
2792 return chain;
2795 /* Reverse the order of elements in the chain T,
2796 and return the new head of the chain (old last element). */
2798 tree
2799 nreverse (tree t)
2801 tree prev = 0, decl, next;
2802 for (decl = t; decl; decl = next)
2804 /* We shouldn't be using this function to reverse BLOCK chains; we
2805 have blocks_nreverse for that. */
2806 gcc_checking_assert (TREE_CODE (decl) != BLOCK);
2807 next = TREE_CHAIN (decl);
2808 TREE_CHAIN (decl) = prev;
2809 prev = decl;
2811 return prev;
2814 /* Return a newly created TREE_LIST node whose
2815 purpose and value fields are PARM and VALUE. */
2817 tree
2818 build_tree_list_stat (tree parm, tree value MEM_STAT_DECL)
2820 tree t = make_node_stat (TREE_LIST PASS_MEM_STAT);
2821 TREE_PURPOSE (t) = parm;
2822 TREE_VALUE (t) = value;
2823 return t;
2826 /* Build a chain of TREE_LIST nodes from a vector. */
2828 tree
2829 build_tree_list_vec_stat (const vec<tree, va_gc> *vec MEM_STAT_DECL)
2831 tree ret = NULL_TREE;
2832 tree *pp = &ret;
2833 unsigned int i;
2834 tree t;
2835 FOR_EACH_VEC_SAFE_ELT (vec, i, t)
2837 *pp = build_tree_list_stat (NULL, t PASS_MEM_STAT);
2838 pp = &TREE_CHAIN (*pp);
2840 return ret;
2843 /* Return a newly created TREE_LIST node whose
2844 purpose and value fields are PURPOSE and VALUE
2845 and whose TREE_CHAIN is CHAIN. */
2847 tree
2848 tree_cons_stat (tree purpose, tree value, tree chain MEM_STAT_DECL)
2850 tree node;
2852 node = ggc_alloc_tree_node_stat (sizeof (struct tree_list) PASS_MEM_STAT);
2853 memset (node, 0, sizeof (struct tree_common));
2855 record_node_allocation_statistics (TREE_LIST, sizeof (struct tree_list));
2857 TREE_SET_CODE (node, TREE_LIST);
2858 TREE_CHAIN (node) = chain;
2859 TREE_PURPOSE (node) = purpose;
2860 TREE_VALUE (node) = value;
2861 return node;
2864 /* Return the values of the elements of a CONSTRUCTOR as a vector of
2865 trees. */
2867 vec<tree, va_gc> *
2868 ctor_to_vec (tree ctor)
2870 vec<tree, va_gc> *vec;
2871 vec_alloc (vec, CONSTRUCTOR_NELTS (ctor));
2872 unsigned int ix;
2873 tree val;
2875 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), ix, val)
2876 vec->quick_push (val);
2878 return vec;
2881 /* Return the size nominally occupied by an object of type TYPE
2882 when it resides in memory. The value is measured in units of bytes,
2883 and its data type is that normally used for type sizes
2884 (which is the first type created by make_signed_type or
2885 make_unsigned_type). */
2887 tree
2888 size_in_bytes (const_tree type)
2890 tree t;
2892 if (type == error_mark_node)
2893 return integer_zero_node;
2895 type = TYPE_MAIN_VARIANT (type);
2896 t = TYPE_SIZE_UNIT (type);
2898 if (t == 0)
2900 lang_hooks.types.incomplete_type_error (NULL_TREE, type);
2901 return size_zero_node;
2904 return t;
2907 /* Return the 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 int_size_in_bytes (const_tree type)
2913 tree t;
2915 if (type == error_mark_node)
2916 return 0;
2918 type = TYPE_MAIN_VARIANT (type);
2919 t = TYPE_SIZE_UNIT (type);
2921 if (t && tree_fits_uhwi_p (t))
2922 return TREE_INT_CST_LOW (t);
2923 else
2924 return -1;
2927 /* Return the maximum size of TYPE (in bytes) as a wide integer
2928 or return -1 if the size can vary or is larger than an integer. */
2930 HOST_WIDE_INT
2931 max_int_size_in_bytes (const_tree type)
2933 HOST_WIDE_INT size = -1;
2934 tree size_tree;
2936 /* If this is an array type, check for a possible MAX_SIZE attached. */
2938 if (TREE_CODE (type) == ARRAY_TYPE)
2940 size_tree = TYPE_ARRAY_MAX_SIZE (type);
2942 if (size_tree && tree_fits_uhwi_p (size_tree))
2943 size = tree_to_uhwi (size_tree);
2946 /* If we still haven't been able to get a size, see if the language
2947 can compute a maximum size. */
2949 if (size == -1)
2951 size_tree = lang_hooks.types.max_size (type);
2953 if (size_tree && tree_fits_uhwi_p (size_tree))
2954 size = tree_to_uhwi (size_tree);
2957 return size;
2960 /* Return the bit position of FIELD, in bits from the start of the record.
2961 This is a tree of type bitsizetype. */
2963 tree
2964 bit_position (const_tree field)
2966 return bit_from_pos (DECL_FIELD_OFFSET (field),
2967 DECL_FIELD_BIT_OFFSET (field));
2970 /* Return the byte position of FIELD, in bytes from the start of the record.
2971 This is a tree of type sizetype. */
2973 tree
2974 byte_position (const_tree field)
2976 return byte_from_pos (DECL_FIELD_OFFSET (field),
2977 DECL_FIELD_BIT_OFFSET (field));
2980 /* Likewise, but return as an integer. It must be representable in
2981 that way (since it could be a signed value, we don't have the
2982 option of returning -1 like int_size_in_byte can. */
2984 HOST_WIDE_INT
2985 int_byte_position (const_tree field)
2987 return tree_to_shwi (byte_position (field));
2990 /* Return the strictest alignment, in bits, that T is known to have. */
2992 unsigned int
2993 expr_align (const_tree t)
2995 unsigned int align0, align1;
2997 switch (TREE_CODE (t))
2999 CASE_CONVERT: case NON_LVALUE_EXPR:
3000 /* If we have conversions, we know that the alignment of the
3001 object must meet each of the alignments of the types. */
3002 align0 = expr_align (TREE_OPERAND (t, 0));
3003 align1 = TYPE_ALIGN (TREE_TYPE (t));
3004 return MAX (align0, align1);
3006 case SAVE_EXPR: case COMPOUND_EXPR: case MODIFY_EXPR:
3007 case INIT_EXPR: case TARGET_EXPR: case WITH_CLEANUP_EXPR:
3008 case CLEANUP_POINT_EXPR:
3009 /* These don't change the alignment of an object. */
3010 return expr_align (TREE_OPERAND (t, 0));
3012 case COND_EXPR:
3013 /* The best we can do is say that the alignment is the least aligned
3014 of the two arms. */
3015 align0 = expr_align (TREE_OPERAND (t, 1));
3016 align1 = expr_align (TREE_OPERAND (t, 2));
3017 return MIN (align0, align1);
3019 /* FIXME: LABEL_DECL and CONST_DECL never have DECL_ALIGN set
3020 meaningfully, it's always 1. */
3021 case LABEL_DECL: case CONST_DECL:
3022 case VAR_DECL: case PARM_DECL: case RESULT_DECL:
3023 case FUNCTION_DECL:
3024 gcc_assert (DECL_ALIGN (t) != 0);
3025 return DECL_ALIGN (t);
3027 default:
3028 break;
3031 /* Otherwise take the alignment from that of the type. */
3032 return TYPE_ALIGN (TREE_TYPE (t));
3035 /* Return, as a tree node, the number of elements for TYPE (which is an
3036 ARRAY_TYPE) minus one. This counts only elements of the top array. */
3038 tree
3039 array_type_nelts (const_tree type)
3041 tree index_type, min, max;
3043 /* If they did it with unspecified bounds, then we should have already
3044 given an error about it before we got here. */
3045 if (! TYPE_DOMAIN (type))
3046 return error_mark_node;
3048 index_type = TYPE_DOMAIN (type);
3049 min = TYPE_MIN_VALUE (index_type);
3050 max = TYPE_MAX_VALUE (index_type);
3052 /* TYPE_MAX_VALUE may not be set if the array has unknown length. */
3053 if (!max)
3054 return error_mark_node;
3056 return (integer_zerop (min)
3057 ? max
3058 : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
3061 /* If arg is static -- a reference to an object in static storage -- then
3062 return the object. This is not the same as the C meaning of `static'.
3063 If arg isn't static, return NULL. */
3065 tree
3066 staticp (tree arg)
3068 switch (TREE_CODE (arg))
3070 case FUNCTION_DECL:
3071 /* Nested functions are static, even though taking their address will
3072 involve a trampoline as we unnest the nested function and create
3073 the trampoline on the tree level. */
3074 return arg;
3076 case VAR_DECL:
3077 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
3078 && ! DECL_THREAD_LOCAL_P (arg)
3079 && ! DECL_DLLIMPORT_P (arg)
3080 ? arg : NULL);
3082 case CONST_DECL:
3083 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
3084 ? arg : NULL);
3086 case CONSTRUCTOR:
3087 return TREE_STATIC (arg) ? arg : NULL;
3089 case LABEL_DECL:
3090 case STRING_CST:
3091 return arg;
3093 case COMPONENT_REF:
3094 /* If the thing being referenced is not a field, then it is
3095 something language specific. */
3096 gcc_assert (TREE_CODE (TREE_OPERAND (arg, 1)) == FIELD_DECL);
3098 /* If we are referencing a bitfield, we can't evaluate an
3099 ADDR_EXPR at compile time and so it isn't a constant. */
3100 if (DECL_BIT_FIELD (TREE_OPERAND (arg, 1)))
3101 return NULL;
3103 return staticp (TREE_OPERAND (arg, 0));
3105 case BIT_FIELD_REF:
3106 return NULL;
3108 case INDIRECT_REF:
3109 return TREE_CONSTANT (TREE_OPERAND (arg, 0)) ? arg : NULL;
3111 case ARRAY_REF:
3112 case ARRAY_RANGE_REF:
3113 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
3114 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
3115 return staticp (TREE_OPERAND (arg, 0));
3116 else
3117 return NULL;
3119 case COMPOUND_LITERAL_EXPR:
3120 return TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (arg)) ? arg : NULL;
3122 default:
3123 return NULL;
3130 /* Return whether OP is a DECL whose address is function-invariant. */
3132 bool
3133 decl_address_invariant_p (const_tree op)
3135 /* The conditions below are slightly less strict than the one in
3136 staticp. */
3138 switch (TREE_CODE (op))
3140 case PARM_DECL:
3141 case RESULT_DECL:
3142 case LABEL_DECL:
3143 case FUNCTION_DECL:
3144 return true;
3146 case VAR_DECL:
3147 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
3148 || DECL_THREAD_LOCAL_P (op)
3149 || DECL_CONTEXT (op) == current_function_decl
3150 || decl_function_context (op) == current_function_decl)
3151 return true;
3152 break;
3154 case CONST_DECL:
3155 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
3156 || decl_function_context (op) == current_function_decl)
3157 return true;
3158 break;
3160 default:
3161 break;
3164 return false;
3167 /* Return whether OP is a DECL whose address is interprocedural-invariant. */
3169 bool
3170 decl_address_ip_invariant_p (const_tree op)
3172 /* The conditions below are slightly less strict than the one in
3173 staticp. */
3175 switch (TREE_CODE (op))
3177 case LABEL_DECL:
3178 case FUNCTION_DECL:
3179 case STRING_CST:
3180 return true;
3182 case VAR_DECL:
3183 if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
3184 && !DECL_DLLIMPORT_P (op))
3185 || DECL_THREAD_LOCAL_P (op))
3186 return true;
3187 break;
3189 case CONST_DECL:
3190 if ((TREE_STATIC (op) || DECL_EXTERNAL (op)))
3191 return true;
3192 break;
3194 default:
3195 break;
3198 return false;
3202 /* Return true if T is function-invariant (internal function, does
3203 not handle arithmetic; that's handled in skip_simple_arithmetic and
3204 tree_invariant_p). */
3206 static bool tree_invariant_p (tree t);
3208 static bool
3209 tree_invariant_p_1 (tree t)
3211 tree op;
3213 if (TREE_CONSTANT (t)
3214 || (TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t)))
3215 return true;
3217 switch (TREE_CODE (t))
3219 case SAVE_EXPR:
3220 return true;
3222 case ADDR_EXPR:
3223 op = TREE_OPERAND (t, 0);
3224 while (handled_component_p (op))
3226 switch (TREE_CODE (op))
3228 case ARRAY_REF:
3229 case ARRAY_RANGE_REF:
3230 if (!tree_invariant_p (TREE_OPERAND (op, 1))
3231 || TREE_OPERAND (op, 2) != NULL_TREE
3232 || TREE_OPERAND (op, 3) != NULL_TREE)
3233 return false;
3234 break;
3236 case COMPONENT_REF:
3237 if (TREE_OPERAND (op, 2) != NULL_TREE)
3238 return false;
3239 break;
3241 default:;
3243 op = TREE_OPERAND (op, 0);
3246 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
3248 default:
3249 break;
3252 return false;
3255 /* Return true if T is function-invariant. */
3257 static bool
3258 tree_invariant_p (tree t)
3260 tree inner = skip_simple_arithmetic (t);
3261 return tree_invariant_p_1 (inner);
3264 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
3265 Do this to any expression which may be used in more than one place,
3266 but must be evaluated only once.
3268 Normally, expand_expr would reevaluate the expression each time.
3269 Calling save_expr produces something that is evaluated and recorded
3270 the first time expand_expr is called on it. Subsequent calls to
3271 expand_expr just reuse the recorded value.
3273 The call to expand_expr that generates code that actually computes
3274 the value is the first call *at compile time*. Subsequent calls
3275 *at compile time* generate code to use the saved value.
3276 This produces correct result provided that *at run time* control
3277 always flows through the insns made by the first expand_expr
3278 before reaching the other places where the save_expr was evaluated.
3279 You, the caller of save_expr, must make sure this is so.
3281 Constants, and certain read-only nodes, are returned with no
3282 SAVE_EXPR because that is safe. Expressions containing placeholders
3283 are not touched; see tree.def for an explanation of what these
3284 are used for. */
3286 tree
3287 save_expr (tree expr)
3289 tree t = fold (expr);
3290 tree inner;
3292 /* If the tree evaluates to a constant, then we don't want to hide that
3293 fact (i.e. this allows further folding, and direct checks for constants).
3294 However, a read-only object that has side effects cannot be bypassed.
3295 Since it is no problem to reevaluate literals, we just return the
3296 literal node. */
3297 inner = skip_simple_arithmetic (t);
3298 if (TREE_CODE (inner) == ERROR_MARK)
3299 return inner;
3301 if (tree_invariant_p_1 (inner))
3302 return t;
3304 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
3305 it means that the size or offset of some field of an object depends on
3306 the value within another field.
3308 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
3309 and some variable since it would then need to be both evaluated once and
3310 evaluated more than once. Front-ends must assure this case cannot
3311 happen by surrounding any such subexpressions in their own SAVE_EXPR
3312 and forcing evaluation at the proper time. */
3313 if (contains_placeholder_p (inner))
3314 return t;
3316 t = build1 (SAVE_EXPR, TREE_TYPE (expr), t);
3317 SET_EXPR_LOCATION (t, EXPR_LOCATION (expr));
3319 /* This expression might be placed ahead of a jump to ensure that the
3320 value was computed on both sides of the jump. So make sure it isn't
3321 eliminated as dead. */
3322 TREE_SIDE_EFFECTS (t) = 1;
3323 return t;
3326 /* Look inside EXPR into any simple arithmetic operations. Return the
3327 outermost non-arithmetic or non-invariant node. */
3329 tree
3330 skip_simple_arithmetic (tree expr)
3332 /* We don't care about whether this can be used as an lvalue in this
3333 context. */
3334 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3335 expr = TREE_OPERAND (expr, 0);
3337 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
3338 a constant, it will be more efficient to not make another SAVE_EXPR since
3339 it will allow better simplification and GCSE will be able to merge the
3340 computations if they actually occur. */
3341 while (true)
3343 if (UNARY_CLASS_P (expr))
3344 expr = TREE_OPERAND (expr, 0);
3345 else if (BINARY_CLASS_P (expr))
3347 if (tree_invariant_p (TREE_OPERAND (expr, 1)))
3348 expr = TREE_OPERAND (expr, 0);
3349 else if (tree_invariant_p (TREE_OPERAND (expr, 0)))
3350 expr = TREE_OPERAND (expr, 1);
3351 else
3352 break;
3354 else
3355 break;
3358 return expr;
3361 /* Look inside EXPR into simple arithmetic operations involving constants.
3362 Return the outermost non-arithmetic or non-constant node. */
3364 tree
3365 skip_simple_constant_arithmetic (tree expr)
3367 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3368 expr = TREE_OPERAND (expr, 0);
3370 while (true)
3372 if (UNARY_CLASS_P (expr))
3373 expr = TREE_OPERAND (expr, 0);
3374 else if (BINARY_CLASS_P (expr))
3376 if (TREE_CONSTANT (TREE_OPERAND (expr, 1)))
3377 expr = TREE_OPERAND (expr, 0);
3378 else if (TREE_CONSTANT (TREE_OPERAND (expr, 0)))
3379 expr = TREE_OPERAND (expr, 1);
3380 else
3381 break;
3383 else
3384 break;
3387 return expr;
3390 /* Return which tree structure is used by T. */
3392 enum tree_node_structure_enum
3393 tree_node_structure (const_tree t)
3395 const enum tree_code code = TREE_CODE (t);
3396 return tree_node_structure_for_code (code);
3399 /* Set various status flags when building a CALL_EXPR object T. */
3401 static void
3402 process_call_operands (tree t)
3404 bool side_effects = TREE_SIDE_EFFECTS (t);
3405 bool read_only = false;
3406 int i = call_expr_flags (t);
3408 /* Calls have side-effects, except those to const or pure functions. */
3409 if ((i & ECF_LOOPING_CONST_OR_PURE) || !(i & (ECF_CONST | ECF_PURE)))
3410 side_effects = true;
3411 /* Propagate TREE_READONLY of arguments for const functions. */
3412 if (i & ECF_CONST)
3413 read_only = true;
3415 if (!side_effects || read_only)
3416 for (i = 1; i < TREE_OPERAND_LENGTH (t); i++)
3418 tree op = TREE_OPERAND (t, i);
3419 if (op && TREE_SIDE_EFFECTS (op))
3420 side_effects = true;
3421 if (op && !TREE_READONLY (op) && !CONSTANT_CLASS_P (op))
3422 read_only = false;
3425 TREE_SIDE_EFFECTS (t) = side_effects;
3426 TREE_READONLY (t) = read_only;
3429 /* Return true if EXP contains a PLACEHOLDER_EXPR, i.e. if it represents a
3430 size or offset that depends on a field within a record. */
3432 bool
3433 contains_placeholder_p (const_tree exp)
3435 enum tree_code code;
3437 if (!exp)
3438 return 0;
3440 code = TREE_CODE (exp);
3441 if (code == PLACEHOLDER_EXPR)
3442 return 1;
3444 switch (TREE_CODE_CLASS (code))
3446 case tcc_reference:
3447 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
3448 position computations since they will be converted into a
3449 WITH_RECORD_EXPR involving the reference, which will assume
3450 here will be valid. */
3451 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3453 case tcc_exceptional:
3454 if (code == TREE_LIST)
3455 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
3456 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
3457 break;
3459 case tcc_unary:
3460 case tcc_binary:
3461 case tcc_comparison:
3462 case tcc_expression:
3463 switch (code)
3465 case COMPOUND_EXPR:
3466 /* Ignoring the first operand isn't quite right, but works best. */
3467 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
3469 case COND_EXPR:
3470 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3471 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
3472 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
3474 case SAVE_EXPR:
3475 /* The save_expr function never wraps anything containing
3476 a PLACEHOLDER_EXPR. */
3477 return 0;
3479 default:
3480 break;
3483 switch (TREE_CODE_LENGTH (code))
3485 case 1:
3486 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3487 case 2:
3488 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3489 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
3490 default:
3491 return 0;
3494 case tcc_vl_exp:
3495 switch (code)
3497 case CALL_EXPR:
3499 const_tree arg;
3500 const_call_expr_arg_iterator iter;
3501 FOR_EACH_CONST_CALL_EXPR_ARG (arg, iter, exp)
3502 if (CONTAINS_PLACEHOLDER_P (arg))
3503 return 1;
3504 return 0;
3506 default:
3507 return 0;
3510 default:
3511 return 0;
3513 return 0;
3516 /* Return true if any part of the structure of TYPE involves a PLACEHOLDER_EXPR
3517 directly. This includes size, bounds, qualifiers (for QUAL_UNION_TYPE) and
3518 field positions. */
3520 static bool
3521 type_contains_placeholder_1 (const_tree type)
3523 /* If the size contains a placeholder or the parent type (component type in
3524 the case of arrays) type involves a placeholder, this type does. */
3525 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
3526 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
3527 || (!POINTER_TYPE_P (type)
3528 && TREE_TYPE (type)
3529 && type_contains_placeholder_p (TREE_TYPE (type))))
3530 return true;
3532 /* Now do type-specific checks. Note that the last part of the check above
3533 greatly limits what we have to do below. */
3534 switch (TREE_CODE (type))
3536 case VOID_TYPE:
3537 case POINTER_BOUNDS_TYPE:
3538 case COMPLEX_TYPE:
3539 case ENUMERAL_TYPE:
3540 case BOOLEAN_TYPE:
3541 case POINTER_TYPE:
3542 case OFFSET_TYPE:
3543 case REFERENCE_TYPE:
3544 case METHOD_TYPE:
3545 case FUNCTION_TYPE:
3546 case VECTOR_TYPE:
3547 case NULLPTR_TYPE:
3548 return false;
3550 case INTEGER_TYPE:
3551 case REAL_TYPE:
3552 case FIXED_POINT_TYPE:
3553 /* Here we just check the bounds. */
3554 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
3555 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
3557 case ARRAY_TYPE:
3558 /* We have already checked the component type above, so just check the
3559 domain type. */
3560 return type_contains_placeholder_p (TYPE_DOMAIN (type));
3562 case RECORD_TYPE:
3563 case UNION_TYPE:
3564 case QUAL_UNION_TYPE:
3566 tree field;
3568 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3569 if (TREE_CODE (field) == FIELD_DECL
3570 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
3571 || (TREE_CODE (type) == QUAL_UNION_TYPE
3572 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
3573 || type_contains_placeholder_p (TREE_TYPE (field))))
3574 return true;
3576 return false;
3579 default:
3580 gcc_unreachable ();
3584 /* Wrapper around above function used to cache its result. */
3586 bool
3587 type_contains_placeholder_p (tree type)
3589 bool result;
3591 /* If the contains_placeholder_bits field has been initialized,
3592 then we know the answer. */
3593 if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
3594 return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
3596 /* Indicate that we've seen this type node, and the answer is false.
3597 This is what we want to return if we run into recursion via fields. */
3598 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
3600 /* Compute the real value. */
3601 result = type_contains_placeholder_1 (type);
3603 /* Store the real value. */
3604 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
3606 return result;
3609 /* Push tree EXP onto vector QUEUE if it is not already present. */
3611 static void
3612 push_without_duplicates (tree exp, vec<tree> *queue)
3614 unsigned int i;
3615 tree iter;
3617 FOR_EACH_VEC_ELT (*queue, i, iter)
3618 if (simple_cst_equal (iter, exp) == 1)
3619 break;
3621 if (!iter)
3622 queue->safe_push (exp);
3625 /* Given a tree EXP, find all occurrences of references to fields
3626 in a PLACEHOLDER_EXPR and place them in vector REFS without
3627 duplicates. Also record VAR_DECLs and CONST_DECLs. Note that
3628 we assume here that EXP contains only arithmetic expressions
3629 or CALL_EXPRs with PLACEHOLDER_EXPRs occurring only in their
3630 argument list. */
3632 void
3633 find_placeholder_in_expr (tree exp, vec<tree> *refs)
3635 enum tree_code code = TREE_CODE (exp);
3636 tree inner;
3637 int i;
3639 /* We handle TREE_LIST and COMPONENT_REF separately. */
3640 if (code == TREE_LIST)
3642 FIND_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), refs);
3643 FIND_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), refs);
3645 else if (code == COMPONENT_REF)
3647 for (inner = TREE_OPERAND (exp, 0);
3648 REFERENCE_CLASS_P (inner);
3649 inner = TREE_OPERAND (inner, 0))
3652 if (TREE_CODE (inner) == PLACEHOLDER_EXPR)
3653 push_without_duplicates (exp, refs);
3654 else
3655 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), refs);
3657 else
3658 switch (TREE_CODE_CLASS (code))
3660 case tcc_constant:
3661 break;
3663 case tcc_declaration:
3664 /* Variables allocated to static storage can stay. */
3665 if (!TREE_STATIC (exp))
3666 push_without_duplicates (exp, refs);
3667 break;
3669 case tcc_expression:
3670 /* This is the pattern built in ada/make_aligning_type. */
3671 if (code == ADDR_EXPR
3672 && TREE_CODE (TREE_OPERAND (exp, 0)) == PLACEHOLDER_EXPR)
3674 push_without_duplicates (exp, refs);
3675 break;
3678 /* Fall through... */
3680 case tcc_exceptional:
3681 case tcc_unary:
3682 case tcc_binary:
3683 case tcc_comparison:
3684 case tcc_reference:
3685 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
3686 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3687 break;
3689 case tcc_vl_exp:
3690 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3691 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3692 break;
3694 default:
3695 gcc_unreachable ();
3699 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
3700 return a tree with all occurrences of references to F in a
3701 PLACEHOLDER_EXPR replaced by R. Also handle VAR_DECLs and
3702 CONST_DECLs. Note that we assume here that EXP contains only
3703 arithmetic expressions or CALL_EXPRs with PLACEHOLDER_EXPRs
3704 occurring only in their argument list. */
3706 tree
3707 substitute_in_expr (tree exp, tree f, tree r)
3709 enum tree_code code = TREE_CODE (exp);
3710 tree op0, op1, op2, op3;
3711 tree new_tree;
3713 /* We handle TREE_LIST and COMPONENT_REF separately. */
3714 if (code == TREE_LIST)
3716 op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
3717 op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
3718 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3719 return exp;
3721 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3723 else if (code == COMPONENT_REF)
3725 tree inner;
3727 /* If this expression is getting a value from a PLACEHOLDER_EXPR
3728 and it is the right field, replace it with R. */
3729 for (inner = TREE_OPERAND (exp, 0);
3730 REFERENCE_CLASS_P (inner);
3731 inner = TREE_OPERAND (inner, 0))
3734 /* The field. */
3735 op1 = TREE_OPERAND (exp, 1);
3737 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f)
3738 return r;
3740 /* If this expression hasn't been completed let, leave it alone. */
3741 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && !TREE_TYPE (inner))
3742 return exp;
3744 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3745 if (op0 == TREE_OPERAND (exp, 0))
3746 return exp;
3748 new_tree
3749 = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE);
3751 else
3752 switch (TREE_CODE_CLASS (code))
3754 case tcc_constant:
3755 return exp;
3757 case tcc_declaration:
3758 if (exp == f)
3759 return r;
3760 else
3761 return exp;
3763 case tcc_expression:
3764 if (exp == f)
3765 return r;
3767 /* Fall through... */
3769 case tcc_exceptional:
3770 case tcc_unary:
3771 case tcc_binary:
3772 case tcc_comparison:
3773 case tcc_reference:
3774 switch (TREE_CODE_LENGTH (code))
3776 case 0:
3777 return exp;
3779 case 1:
3780 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3781 if (op0 == TREE_OPERAND (exp, 0))
3782 return exp;
3784 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3785 break;
3787 case 2:
3788 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3789 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3791 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3792 return exp;
3794 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3795 break;
3797 case 3:
3798 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3799 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3800 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3802 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3803 && op2 == TREE_OPERAND (exp, 2))
3804 return exp;
3806 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3807 break;
3809 case 4:
3810 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3811 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3812 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3813 op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
3815 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3816 && op2 == TREE_OPERAND (exp, 2)
3817 && op3 == TREE_OPERAND (exp, 3))
3818 return exp;
3820 new_tree
3821 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3822 break;
3824 default:
3825 gcc_unreachable ();
3827 break;
3829 case tcc_vl_exp:
3831 int i;
3833 new_tree = NULL_TREE;
3835 /* If we are trying to replace F with a constant, inline back
3836 functions which do nothing else than computing a value from
3837 the arguments they are passed. This makes it possible to
3838 fold partially or entirely the replacement expression. */
3839 if (CONSTANT_CLASS_P (r) && code == CALL_EXPR)
3841 tree t = maybe_inline_call_in_expr (exp);
3842 if (t)
3843 return SUBSTITUTE_IN_EXPR (t, f, r);
3846 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3848 tree op = TREE_OPERAND (exp, i);
3849 tree new_op = SUBSTITUTE_IN_EXPR (op, f, r);
3850 if (new_op != op)
3852 if (!new_tree)
3853 new_tree = copy_node (exp);
3854 TREE_OPERAND (new_tree, i) = new_op;
3858 if (new_tree)
3860 new_tree = fold (new_tree);
3861 if (TREE_CODE (new_tree) == CALL_EXPR)
3862 process_call_operands (new_tree);
3864 else
3865 return exp;
3867 break;
3869 default:
3870 gcc_unreachable ();
3873 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3875 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3876 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3878 return new_tree;
3881 /* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
3882 for it within OBJ, a tree that is an object or a chain of references. */
3884 tree
3885 substitute_placeholder_in_expr (tree exp, tree obj)
3887 enum tree_code code = TREE_CODE (exp);
3888 tree op0, op1, op2, op3;
3889 tree new_tree;
3891 /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
3892 in the chain of OBJ. */
3893 if (code == PLACEHOLDER_EXPR)
3895 tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
3896 tree elt;
3898 for (elt = obj; elt != 0;
3899 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3900 || TREE_CODE (elt) == COND_EXPR)
3901 ? TREE_OPERAND (elt, 1)
3902 : (REFERENCE_CLASS_P (elt)
3903 || UNARY_CLASS_P (elt)
3904 || BINARY_CLASS_P (elt)
3905 || VL_EXP_CLASS_P (elt)
3906 || EXPRESSION_CLASS_P (elt))
3907 ? TREE_OPERAND (elt, 0) : 0))
3908 if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
3909 return elt;
3911 for (elt = obj; elt != 0;
3912 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3913 || TREE_CODE (elt) == COND_EXPR)
3914 ? TREE_OPERAND (elt, 1)
3915 : (REFERENCE_CLASS_P (elt)
3916 || UNARY_CLASS_P (elt)
3917 || BINARY_CLASS_P (elt)
3918 || VL_EXP_CLASS_P (elt)
3919 || EXPRESSION_CLASS_P (elt))
3920 ? TREE_OPERAND (elt, 0) : 0))
3921 if (POINTER_TYPE_P (TREE_TYPE (elt))
3922 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
3923 == need_type))
3924 return fold_build1 (INDIRECT_REF, need_type, elt);
3926 /* If we didn't find it, return the original PLACEHOLDER_EXPR. If it
3927 survives until RTL generation, there will be an error. */
3928 return exp;
3931 /* TREE_LIST is special because we need to look at TREE_VALUE
3932 and TREE_CHAIN, not TREE_OPERANDS. */
3933 else if (code == TREE_LIST)
3935 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
3936 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
3937 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3938 return exp;
3940 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3942 else
3943 switch (TREE_CODE_CLASS (code))
3945 case tcc_constant:
3946 case tcc_declaration:
3947 return exp;
3949 case tcc_exceptional:
3950 case tcc_unary:
3951 case tcc_binary:
3952 case tcc_comparison:
3953 case tcc_expression:
3954 case tcc_reference:
3955 case tcc_statement:
3956 switch (TREE_CODE_LENGTH (code))
3958 case 0:
3959 return exp;
3961 case 1:
3962 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3963 if (op0 == TREE_OPERAND (exp, 0))
3964 return exp;
3966 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3967 break;
3969 case 2:
3970 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3971 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3973 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3974 return exp;
3976 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3977 break;
3979 case 3:
3980 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3981 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3982 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3984 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3985 && op2 == TREE_OPERAND (exp, 2))
3986 return exp;
3988 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3989 break;
3991 case 4:
3992 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3993 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3994 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3995 op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
3997 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3998 && op2 == TREE_OPERAND (exp, 2)
3999 && op3 == TREE_OPERAND (exp, 3))
4000 return exp;
4002 new_tree
4003 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
4004 break;
4006 default:
4007 gcc_unreachable ();
4009 break;
4011 case tcc_vl_exp:
4013 int i;
4015 new_tree = NULL_TREE;
4017 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
4019 tree op = TREE_OPERAND (exp, i);
4020 tree new_op = SUBSTITUTE_PLACEHOLDER_IN_EXPR (op, obj);
4021 if (new_op != op)
4023 if (!new_tree)
4024 new_tree = copy_node (exp);
4025 TREE_OPERAND (new_tree, i) = new_op;
4029 if (new_tree)
4031 new_tree = fold (new_tree);
4032 if (TREE_CODE (new_tree) == CALL_EXPR)
4033 process_call_operands (new_tree);
4035 else
4036 return exp;
4038 break;
4040 default:
4041 gcc_unreachable ();
4044 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
4046 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
4047 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
4049 return new_tree;
4053 /* Subroutine of stabilize_reference; this is called for subtrees of
4054 references. Any expression with side-effects must be put in a SAVE_EXPR
4055 to ensure that it is only evaluated once.
4057 We don't put SAVE_EXPR nodes around everything, because assigning very
4058 simple expressions to temporaries causes us to miss good opportunities
4059 for optimizations. Among other things, the opportunity to fold in the
4060 addition of a constant into an addressing mode often gets lost, e.g.
4061 "y[i+1] += x;". In general, we take the approach that we should not make
4062 an assignment unless we are forced into it - i.e., that any non-side effect
4063 operator should be allowed, and that cse should take care of coalescing
4064 multiple utterances of the same expression should that prove fruitful. */
4066 static tree
4067 stabilize_reference_1 (tree e)
4069 tree result;
4070 enum tree_code code = TREE_CODE (e);
4072 /* We cannot ignore const expressions because it might be a reference
4073 to a const array but whose index contains side-effects. But we can
4074 ignore things that are actual constant or that already have been
4075 handled by this function. */
4077 if (tree_invariant_p (e))
4078 return e;
4080 switch (TREE_CODE_CLASS (code))
4082 case tcc_exceptional:
4083 case tcc_type:
4084 case tcc_declaration:
4085 case tcc_comparison:
4086 case tcc_statement:
4087 case tcc_expression:
4088 case tcc_reference:
4089 case tcc_vl_exp:
4090 /* If the expression has side-effects, then encase it in a SAVE_EXPR
4091 so that it will only be evaluated once. */
4092 /* The reference (r) and comparison (<) classes could be handled as
4093 below, but it is generally faster to only evaluate them once. */
4094 if (TREE_SIDE_EFFECTS (e))
4095 return save_expr (e);
4096 return e;
4098 case tcc_constant:
4099 /* Constants need no processing. In fact, we should never reach
4100 here. */
4101 return e;
4103 case tcc_binary:
4104 /* Division is slow and tends to be compiled with jumps,
4105 especially the division by powers of 2 that is often
4106 found inside of an array reference. So do it just once. */
4107 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
4108 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
4109 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
4110 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
4111 return save_expr (e);
4112 /* Recursively stabilize each operand. */
4113 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
4114 stabilize_reference_1 (TREE_OPERAND (e, 1)));
4115 break;
4117 case tcc_unary:
4118 /* Recursively stabilize each operand. */
4119 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
4120 break;
4122 default:
4123 gcc_unreachable ();
4126 TREE_TYPE (result) = TREE_TYPE (e);
4127 TREE_READONLY (result) = TREE_READONLY (e);
4128 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
4129 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
4131 return result;
4134 /* Stabilize a reference so that we can use it any number of times
4135 without causing its operands to be evaluated more than once.
4136 Returns the stabilized reference. This works by means of save_expr,
4137 so see the caveats in the comments about save_expr.
4139 Also allows conversion expressions whose operands are references.
4140 Any other kind of expression is returned unchanged. */
4142 tree
4143 stabilize_reference (tree ref)
4145 tree result;
4146 enum tree_code code = TREE_CODE (ref);
4148 switch (code)
4150 case VAR_DECL:
4151 case PARM_DECL:
4152 case RESULT_DECL:
4153 /* No action is needed in this case. */
4154 return ref;
4156 CASE_CONVERT:
4157 case FLOAT_EXPR:
4158 case FIX_TRUNC_EXPR:
4159 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
4160 break;
4162 case INDIRECT_REF:
4163 result = build_nt (INDIRECT_REF,
4164 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
4165 break;
4167 case COMPONENT_REF:
4168 result = build_nt (COMPONENT_REF,
4169 stabilize_reference (TREE_OPERAND (ref, 0)),
4170 TREE_OPERAND (ref, 1), NULL_TREE);
4171 break;
4173 case BIT_FIELD_REF:
4174 result = build_nt (BIT_FIELD_REF,
4175 stabilize_reference (TREE_OPERAND (ref, 0)),
4176 TREE_OPERAND (ref, 1), TREE_OPERAND (ref, 2));
4177 break;
4179 case ARRAY_REF:
4180 result = build_nt (ARRAY_REF,
4181 stabilize_reference (TREE_OPERAND (ref, 0)),
4182 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
4183 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
4184 break;
4186 case ARRAY_RANGE_REF:
4187 result = build_nt (ARRAY_RANGE_REF,
4188 stabilize_reference (TREE_OPERAND (ref, 0)),
4189 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
4190 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
4191 break;
4193 case COMPOUND_EXPR:
4194 /* We cannot wrap the first expression in a SAVE_EXPR, as then
4195 it wouldn't be ignored. This matters when dealing with
4196 volatiles. */
4197 return stabilize_reference_1 (ref);
4199 /* If arg isn't a kind of lvalue we recognize, make no change.
4200 Caller should recognize the error for an invalid lvalue. */
4201 default:
4202 return ref;
4204 case ERROR_MARK:
4205 return error_mark_node;
4208 TREE_TYPE (result) = TREE_TYPE (ref);
4209 TREE_READONLY (result) = TREE_READONLY (ref);
4210 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
4211 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
4213 return result;
4216 /* Low-level constructors for expressions. */
4218 /* A helper function for build1 and constant folders. Set TREE_CONSTANT,
4219 and TREE_SIDE_EFFECTS for an ADDR_EXPR. */
4221 void
4222 recompute_tree_invariant_for_addr_expr (tree t)
4224 tree node;
4225 bool tc = true, se = false;
4227 /* We started out assuming this address is both invariant and constant, but
4228 does not have side effects. Now go down any handled components and see if
4229 any of them involve offsets that are either non-constant or non-invariant.
4230 Also check for side-effects.
4232 ??? Note that this code makes no attempt to deal with the case where
4233 taking the address of something causes a copy due to misalignment. */
4235 #define UPDATE_FLAGS(NODE) \
4236 do { tree _node = (NODE); \
4237 if (_node && !TREE_CONSTANT (_node)) tc = false; \
4238 if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
4240 for (node = TREE_OPERAND (t, 0); handled_component_p (node);
4241 node = TREE_OPERAND (node, 0))
4243 /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
4244 array reference (probably made temporarily by the G++ front end),
4245 so ignore all the operands. */
4246 if ((TREE_CODE (node) == ARRAY_REF
4247 || TREE_CODE (node) == ARRAY_RANGE_REF)
4248 && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
4250 UPDATE_FLAGS (TREE_OPERAND (node, 1));
4251 if (TREE_OPERAND (node, 2))
4252 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4253 if (TREE_OPERAND (node, 3))
4254 UPDATE_FLAGS (TREE_OPERAND (node, 3));
4256 /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
4257 FIELD_DECL, apparently. The G++ front end can put something else
4258 there, at least temporarily. */
4259 else if (TREE_CODE (node) == COMPONENT_REF
4260 && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
4262 if (TREE_OPERAND (node, 2))
4263 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4267 node = lang_hooks.expr_to_decl (node, &tc, &se);
4269 /* Now see what's inside. If it's an INDIRECT_REF, copy our properties from
4270 the address, since &(*a)->b is a form of addition. If it's a constant, the
4271 address is constant too. If it's a decl, its address is constant if the
4272 decl is static. Everything else is not constant and, furthermore,
4273 taking the address of a volatile variable is not volatile. */
4274 if (TREE_CODE (node) == INDIRECT_REF
4275 || TREE_CODE (node) == MEM_REF)
4276 UPDATE_FLAGS (TREE_OPERAND (node, 0));
4277 else if (CONSTANT_CLASS_P (node))
4279 else if (DECL_P (node))
4280 tc &= (staticp (node) != NULL_TREE);
4281 else
4283 tc = false;
4284 se |= TREE_SIDE_EFFECTS (node);
4288 TREE_CONSTANT (t) = tc;
4289 TREE_SIDE_EFFECTS (t) = se;
4290 #undef UPDATE_FLAGS
4293 /* Build an expression of code CODE, data type TYPE, and operands as
4294 specified. Expressions and reference nodes can be created this way.
4295 Constants, decls, types and misc nodes cannot be.
4297 We define 5 non-variadic functions, from 0 to 4 arguments. This is
4298 enough for all extant tree codes. */
4300 tree
4301 build0_stat (enum tree_code code, tree tt MEM_STAT_DECL)
4303 tree t;
4305 gcc_assert (TREE_CODE_LENGTH (code) == 0);
4307 t = make_node_stat (code PASS_MEM_STAT);
4308 TREE_TYPE (t) = tt;
4310 return t;
4313 tree
4314 build1_stat (enum tree_code code, tree type, tree node MEM_STAT_DECL)
4316 int length = sizeof (struct tree_exp);
4317 tree t;
4319 record_node_allocation_statistics (code, length);
4321 gcc_assert (TREE_CODE_LENGTH (code) == 1);
4323 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
4325 memset (t, 0, sizeof (struct tree_common));
4327 TREE_SET_CODE (t, code);
4329 TREE_TYPE (t) = type;
4330 SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
4331 TREE_OPERAND (t, 0) = node;
4332 if (node && !TYPE_P (node))
4334 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
4335 TREE_READONLY (t) = TREE_READONLY (node);
4338 if (TREE_CODE_CLASS (code) == tcc_statement)
4339 TREE_SIDE_EFFECTS (t) = 1;
4340 else switch (code)
4342 case VA_ARG_EXPR:
4343 /* All of these have side-effects, no matter what their
4344 operands are. */
4345 TREE_SIDE_EFFECTS (t) = 1;
4346 TREE_READONLY (t) = 0;
4347 break;
4349 case INDIRECT_REF:
4350 /* Whether a dereference is readonly has nothing to do with whether
4351 its operand is readonly. */
4352 TREE_READONLY (t) = 0;
4353 break;
4355 case ADDR_EXPR:
4356 if (node)
4357 recompute_tree_invariant_for_addr_expr (t);
4358 break;
4360 default:
4361 if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
4362 && node && !TYPE_P (node)
4363 && TREE_CONSTANT (node))
4364 TREE_CONSTANT (t) = 1;
4365 if (TREE_CODE_CLASS (code) == tcc_reference
4366 && node && TREE_THIS_VOLATILE (node))
4367 TREE_THIS_VOLATILE (t) = 1;
4368 break;
4371 return t;
4374 #define PROCESS_ARG(N) \
4375 do { \
4376 TREE_OPERAND (t, N) = arg##N; \
4377 if (arg##N &&!TYPE_P (arg##N)) \
4379 if (TREE_SIDE_EFFECTS (arg##N)) \
4380 side_effects = 1; \
4381 if (!TREE_READONLY (arg##N) \
4382 && !CONSTANT_CLASS_P (arg##N)) \
4383 (void) (read_only = 0); \
4384 if (!TREE_CONSTANT (arg##N)) \
4385 (void) (constant = 0); \
4387 } while (0)
4389 tree
4390 build2_stat (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
4392 bool constant, read_only, side_effects;
4393 tree t;
4395 gcc_assert (TREE_CODE_LENGTH (code) == 2);
4397 if ((code == MINUS_EXPR || code == PLUS_EXPR || code == MULT_EXPR)
4398 && arg0 && arg1 && tt && POINTER_TYPE_P (tt)
4399 /* When sizetype precision doesn't match that of pointers
4400 we need to be able to build explicit extensions or truncations
4401 of the offset argument. */
4402 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (tt))
4403 gcc_assert (TREE_CODE (arg0) == INTEGER_CST
4404 && TREE_CODE (arg1) == INTEGER_CST);
4406 if (code == POINTER_PLUS_EXPR && arg0 && arg1 && tt)
4407 gcc_assert (POINTER_TYPE_P (tt) && POINTER_TYPE_P (TREE_TYPE (arg0))
4408 && ptrofftype_p (TREE_TYPE (arg1)));
4410 t = make_node_stat (code PASS_MEM_STAT);
4411 TREE_TYPE (t) = tt;
4413 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
4414 result based on those same flags for the arguments. But if the
4415 arguments aren't really even `tree' expressions, we shouldn't be trying
4416 to do this. */
4418 /* Expressions without side effects may be constant if their
4419 arguments are as well. */
4420 constant = (TREE_CODE_CLASS (code) == tcc_comparison
4421 || TREE_CODE_CLASS (code) == tcc_binary);
4422 read_only = 1;
4423 side_effects = TREE_SIDE_EFFECTS (t);
4425 PROCESS_ARG (0);
4426 PROCESS_ARG (1);
4428 TREE_SIDE_EFFECTS (t) = side_effects;
4429 if (code == MEM_REF)
4431 if (arg0 && TREE_CODE (arg0) == ADDR_EXPR)
4433 tree o = TREE_OPERAND (arg0, 0);
4434 TREE_READONLY (t) = TREE_READONLY (o);
4435 TREE_THIS_VOLATILE (t) = TREE_THIS_VOLATILE (o);
4438 else
4440 TREE_READONLY (t) = read_only;
4441 TREE_CONSTANT (t) = constant;
4442 TREE_THIS_VOLATILE (t)
4443 = (TREE_CODE_CLASS (code) == tcc_reference
4444 && arg0 && TREE_THIS_VOLATILE (arg0));
4447 return t;
4451 tree
4452 build3_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4453 tree arg2 MEM_STAT_DECL)
4455 bool constant, read_only, side_effects;
4456 tree t;
4458 gcc_assert (TREE_CODE_LENGTH (code) == 3);
4459 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4461 t = make_node_stat (code PASS_MEM_STAT);
4462 TREE_TYPE (t) = tt;
4464 read_only = 1;
4466 /* As a special exception, if COND_EXPR has NULL branches, we
4467 assume that it is a gimple statement and always consider
4468 it to have side effects. */
4469 if (code == COND_EXPR
4470 && tt == void_type_node
4471 && arg1 == NULL_TREE
4472 && arg2 == NULL_TREE)
4473 side_effects = true;
4474 else
4475 side_effects = TREE_SIDE_EFFECTS (t);
4477 PROCESS_ARG (0);
4478 PROCESS_ARG (1);
4479 PROCESS_ARG (2);
4481 if (code == COND_EXPR)
4482 TREE_READONLY (t) = read_only;
4484 TREE_SIDE_EFFECTS (t) = side_effects;
4485 TREE_THIS_VOLATILE (t)
4486 = (TREE_CODE_CLASS (code) == tcc_reference
4487 && arg0 && TREE_THIS_VOLATILE (arg0));
4489 return t;
4492 tree
4493 build4_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4494 tree arg2, tree arg3 MEM_STAT_DECL)
4496 bool constant, read_only, side_effects;
4497 tree t;
4499 gcc_assert (TREE_CODE_LENGTH (code) == 4);
4501 t = make_node_stat (code PASS_MEM_STAT);
4502 TREE_TYPE (t) = tt;
4504 side_effects = TREE_SIDE_EFFECTS (t);
4506 PROCESS_ARG (0);
4507 PROCESS_ARG (1);
4508 PROCESS_ARG (2);
4509 PROCESS_ARG (3);
4511 TREE_SIDE_EFFECTS (t) = side_effects;
4512 TREE_THIS_VOLATILE (t)
4513 = (TREE_CODE_CLASS (code) == tcc_reference
4514 && arg0 && TREE_THIS_VOLATILE (arg0));
4516 return t;
4519 tree
4520 build5_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4521 tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
4523 bool constant, read_only, side_effects;
4524 tree t;
4526 gcc_assert (TREE_CODE_LENGTH (code) == 5);
4528 t = make_node_stat (code PASS_MEM_STAT);
4529 TREE_TYPE (t) = tt;
4531 side_effects = TREE_SIDE_EFFECTS (t);
4533 PROCESS_ARG (0);
4534 PROCESS_ARG (1);
4535 PROCESS_ARG (2);
4536 PROCESS_ARG (3);
4537 PROCESS_ARG (4);
4539 TREE_SIDE_EFFECTS (t) = side_effects;
4540 if (code == TARGET_MEM_REF)
4542 if (arg0 && TREE_CODE (arg0) == ADDR_EXPR)
4544 tree o = TREE_OPERAND (arg0, 0);
4545 TREE_READONLY (t) = TREE_READONLY (o);
4546 TREE_THIS_VOLATILE (t) = TREE_THIS_VOLATILE (o);
4549 else
4550 TREE_THIS_VOLATILE (t)
4551 = (TREE_CODE_CLASS (code) == tcc_reference
4552 && arg0 && TREE_THIS_VOLATILE (arg0));
4554 return t;
4557 /* Build a simple MEM_REF tree with the sematics of a plain INDIRECT_REF
4558 on the pointer PTR. */
4560 tree
4561 build_simple_mem_ref_loc (location_t loc, tree ptr)
4563 HOST_WIDE_INT offset = 0;
4564 tree ptype = TREE_TYPE (ptr);
4565 tree tem;
4566 /* For convenience allow addresses that collapse to a simple base
4567 and offset. */
4568 if (TREE_CODE (ptr) == ADDR_EXPR
4569 && (handled_component_p (TREE_OPERAND (ptr, 0))
4570 || TREE_CODE (TREE_OPERAND (ptr, 0)) == MEM_REF))
4572 ptr = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &offset);
4573 gcc_assert (ptr);
4574 ptr = build_fold_addr_expr (ptr);
4575 gcc_assert (is_gimple_reg (ptr) || is_gimple_min_invariant (ptr));
4577 tem = build2 (MEM_REF, TREE_TYPE (ptype),
4578 ptr, build_int_cst (ptype, offset));
4579 SET_EXPR_LOCATION (tem, loc);
4580 return tem;
4583 /* Return the constant offset of a MEM_REF or TARGET_MEM_REF tree T. */
4585 offset_int
4586 mem_ref_offset (const_tree t)
4588 return offset_int::from (TREE_OPERAND (t, 1), SIGNED);
4591 /* Return an invariant ADDR_EXPR of type TYPE taking the address of BASE
4592 offsetted by OFFSET units. */
4594 tree
4595 build_invariant_address (tree type, tree base, HOST_WIDE_INT offset)
4597 tree ref = fold_build2 (MEM_REF, TREE_TYPE (type),
4598 build_fold_addr_expr (base),
4599 build_int_cst (ptr_type_node, offset));
4600 tree addr = build1 (ADDR_EXPR, type, ref);
4601 recompute_tree_invariant_for_addr_expr (addr);
4602 return addr;
4605 /* Similar except don't specify the TREE_TYPE
4606 and leave the TREE_SIDE_EFFECTS as 0.
4607 It is permissible for arguments to be null,
4608 or even garbage if their values do not matter. */
4610 tree
4611 build_nt (enum tree_code code, ...)
4613 tree t;
4614 int length;
4615 int i;
4616 va_list p;
4618 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4620 va_start (p, code);
4622 t = make_node (code);
4623 length = TREE_CODE_LENGTH (code);
4625 for (i = 0; i < length; i++)
4626 TREE_OPERAND (t, i) = va_arg (p, tree);
4628 va_end (p);
4629 return t;
4632 /* Similar to build_nt, but for creating a CALL_EXPR object with a
4633 tree vec. */
4635 tree
4636 build_nt_call_vec (tree fn, vec<tree, va_gc> *args)
4638 tree ret, t;
4639 unsigned int ix;
4641 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
4642 CALL_EXPR_FN (ret) = fn;
4643 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
4644 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
4645 CALL_EXPR_ARG (ret, ix) = t;
4646 return ret;
4649 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
4650 We do NOT enter this node in any sort of symbol table.
4652 LOC is the location of the decl.
4654 layout_decl is used to set up the decl's storage layout.
4655 Other slots are initialized to 0 or null pointers. */
4657 tree
4658 build_decl_stat (location_t loc, enum tree_code code, tree name,
4659 tree type MEM_STAT_DECL)
4661 tree t;
4663 t = make_node_stat (code PASS_MEM_STAT);
4664 DECL_SOURCE_LOCATION (t) = loc;
4666 /* if (type == error_mark_node)
4667 type = integer_type_node; */
4668 /* That is not done, deliberately, so that having error_mark_node
4669 as the type can suppress useless errors in the use of this variable. */
4671 DECL_NAME (t) = name;
4672 TREE_TYPE (t) = type;
4674 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
4675 layout_decl (t, 0);
4677 return t;
4680 /* Builds and returns function declaration with NAME and TYPE. */
4682 tree
4683 build_fn_decl (const char *name, tree type)
4685 tree id = get_identifier (name);
4686 tree decl = build_decl (input_location, FUNCTION_DECL, id, type);
4688 DECL_EXTERNAL (decl) = 1;
4689 TREE_PUBLIC (decl) = 1;
4690 DECL_ARTIFICIAL (decl) = 1;
4691 TREE_NOTHROW (decl) = 1;
4693 return decl;
4696 vec<tree, va_gc> *all_translation_units;
4698 /* Builds a new translation-unit decl with name NAME, queues it in the
4699 global list of translation-unit decls and returns it. */
4701 tree
4702 build_translation_unit_decl (tree name)
4704 tree tu = build_decl (UNKNOWN_LOCATION, TRANSLATION_UNIT_DECL,
4705 name, NULL_TREE);
4706 TRANSLATION_UNIT_LANGUAGE (tu) = lang_hooks.name;
4707 vec_safe_push (all_translation_units, tu);
4708 return tu;
4712 /* BLOCK nodes are used to represent the structure of binding contours
4713 and declarations, once those contours have been exited and their contents
4714 compiled. This information is used for outputting debugging info. */
4716 tree
4717 build_block (tree vars, tree subblocks, tree supercontext, tree chain)
4719 tree block = make_node (BLOCK);
4721 BLOCK_VARS (block) = vars;
4722 BLOCK_SUBBLOCKS (block) = subblocks;
4723 BLOCK_SUPERCONTEXT (block) = supercontext;
4724 BLOCK_CHAIN (block) = chain;
4725 return block;
4729 /* Like SET_EXPR_LOCATION, but make sure the tree can have a location.
4731 LOC is the location to use in tree T. */
4733 void
4734 protected_set_expr_location (tree t, location_t loc)
4736 if (CAN_HAVE_LOCATION_P (t))
4737 SET_EXPR_LOCATION (t, loc);
4740 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
4741 is ATTRIBUTE. */
4743 tree
4744 build_decl_attribute_variant (tree ddecl, tree attribute)
4746 DECL_ATTRIBUTES (ddecl) = attribute;
4747 return ddecl;
4750 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4751 is ATTRIBUTE and its qualifiers are QUALS.
4753 Record such modified types already made so we don't make duplicates. */
4755 tree
4756 build_type_attribute_qual_variant (tree ttype, tree attribute, int quals)
4758 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
4760 inchash::hash hstate;
4761 tree ntype;
4762 int i;
4763 tree t;
4764 enum tree_code code = TREE_CODE (ttype);
4766 /* Building a distinct copy of a tagged type is inappropriate; it
4767 causes breakage in code that expects there to be a one-to-one
4768 relationship between a struct and its fields.
4769 build_duplicate_type is another solution (as used in
4770 handle_transparent_union_attribute), but that doesn't play well
4771 with the stronger C++ type identity model. */
4772 if (TREE_CODE (ttype) == RECORD_TYPE
4773 || TREE_CODE (ttype) == UNION_TYPE
4774 || TREE_CODE (ttype) == QUAL_UNION_TYPE
4775 || TREE_CODE (ttype) == ENUMERAL_TYPE)
4777 warning (OPT_Wattributes,
4778 "ignoring attributes applied to %qT after definition",
4779 TYPE_MAIN_VARIANT (ttype));
4780 return build_qualified_type (ttype, quals);
4783 ttype = build_qualified_type (ttype, TYPE_UNQUALIFIED);
4784 ntype = build_distinct_type_copy (ttype);
4786 TYPE_ATTRIBUTES (ntype) = attribute;
4788 hstate.add_int (code);
4789 if (TREE_TYPE (ntype))
4790 hstate.add_object (TYPE_HASH (TREE_TYPE (ntype)));
4791 attribute_hash_list (attribute, hstate);
4793 switch (TREE_CODE (ntype))
4795 case FUNCTION_TYPE:
4796 type_hash_list (TYPE_ARG_TYPES (ntype), hstate);
4797 break;
4798 case ARRAY_TYPE:
4799 if (TYPE_DOMAIN (ntype))
4800 hstate.add_object (TYPE_HASH (TYPE_DOMAIN (ntype)));
4801 break;
4802 case INTEGER_TYPE:
4803 t = TYPE_MAX_VALUE (ntype);
4804 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
4805 hstate.add_object (TREE_INT_CST_ELT (t, i));
4806 break;
4807 case REAL_TYPE:
4808 case FIXED_POINT_TYPE:
4810 unsigned int precision = TYPE_PRECISION (ntype);
4811 hstate.add_object (precision);
4813 break;
4814 default:
4815 break;
4818 ntype = type_hash_canon (hstate.end(), ntype);
4820 /* If the target-dependent attributes make NTYPE different from
4821 its canonical type, we will need to use structural equality
4822 checks for this type. */
4823 if (TYPE_STRUCTURAL_EQUALITY_P (ttype)
4824 || !comp_type_attributes (ntype, ttype))
4825 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
4826 else if (TYPE_CANONICAL (ntype) == ntype)
4827 TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype);
4829 ttype = build_qualified_type (ntype, quals);
4831 else if (TYPE_QUALS (ttype) != quals)
4832 ttype = build_qualified_type (ttype, quals);
4834 return ttype;
4837 /* Check if "omp declare simd" attribute arguments, CLAUSES1 and CLAUSES2, are
4838 the same. */
4840 static bool
4841 omp_declare_simd_clauses_equal (tree clauses1, tree clauses2)
4843 tree cl1, cl2;
4844 for (cl1 = clauses1, cl2 = clauses2;
4845 cl1 && cl2;
4846 cl1 = OMP_CLAUSE_CHAIN (cl1), cl2 = OMP_CLAUSE_CHAIN (cl2))
4848 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_CODE (cl2))
4849 return false;
4850 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_SIMDLEN)
4852 if (simple_cst_equal (OMP_CLAUSE_DECL (cl1),
4853 OMP_CLAUSE_DECL (cl2)) != 1)
4854 return false;
4856 switch (OMP_CLAUSE_CODE (cl1))
4858 case OMP_CLAUSE_ALIGNED:
4859 if (simple_cst_equal (OMP_CLAUSE_ALIGNED_ALIGNMENT (cl1),
4860 OMP_CLAUSE_ALIGNED_ALIGNMENT (cl2)) != 1)
4861 return false;
4862 break;
4863 case OMP_CLAUSE_LINEAR:
4864 if (simple_cst_equal (OMP_CLAUSE_LINEAR_STEP (cl1),
4865 OMP_CLAUSE_LINEAR_STEP (cl2)) != 1)
4866 return false;
4867 break;
4868 case OMP_CLAUSE_SIMDLEN:
4869 if (simple_cst_equal (OMP_CLAUSE_SIMDLEN_EXPR (cl1),
4870 OMP_CLAUSE_SIMDLEN_EXPR (cl2)) != 1)
4871 return false;
4872 default:
4873 break;
4876 return true;
4879 /* Compare two constructor-element-type constants. Return 1 if the lists
4880 are known to be equal; otherwise return 0. */
4882 static bool
4883 simple_cst_list_equal (const_tree l1, const_tree l2)
4885 while (l1 != NULL_TREE && l2 != NULL_TREE)
4887 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
4888 return false;
4890 l1 = TREE_CHAIN (l1);
4891 l2 = TREE_CHAIN (l2);
4894 return l1 == l2;
4897 /* Compare two identifier nodes representing attributes. Either one may
4898 be in wrapped __ATTR__ form. Return true if they are the same, false
4899 otherwise. */
4901 static bool
4902 cmp_attrib_identifiers (const_tree attr1, const_tree attr2)
4904 /* Make sure we're dealing with IDENTIFIER_NODEs. */
4905 gcc_checking_assert (TREE_CODE (attr1) == IDENTIFIER_NODE
4906 && TREE_CODE (attr2) == IDENTIFIER_NODE);
4908 /* Identifiers can be compared directly for equality. */
4909 if (attr1 == attr2)
4910 return true;
4912 /* If they are not equal, they may still be one in the form
4913 'text' while the other one is in the form '__text__'. TODO:
4914 If we were storing attributes in normalized 'text' form, then
4915 this could all go away and we could take full advantage of
4916 the fact that we're comparing identifiers. :-) */
4917 const size_t attr1_len = IDENTIFIER_LENGTH (attr1);
4918 const size_t attr2_len = IDENTIFIER_LENGTH (attr2);
4920 if (attr2_len == attr1_len + 4)
4922 const char *p = IDENTIFIER_POINTER (attr2);
4923 const char *q = IDENTIFIER_POINTER (attr1);
4924 if (p[0] == '_' && p[1] == '_'
4925 && p[attr2_len - 2] == '_' && p[attr2_len - 1] == '_'
4926 && strncmp (q, p + 2, attr1_len) == 0)
4927 return true;;
4929 else if (attr2_len + 4 == attr1_len)
4931 const char *p = IDENTIFIER_POINTER (attr2);
4932 const char *q = IDENTIFIER_POINTER (attr1);
4933 if (q[0] == '_' && q[1] == '_'
4934 && q[attr1_len - 2] == '_' && q[attr1_len - 1] == '_'
4935 && strncmp (q + 2, p, attr2_len) == 0)
4936 return true;
4939 return false;
4942 /* Compare two attributes for their value identity. Return true if the
4943 attribute values are known to be equal; otherwise return false. */
4945 bool
4946 attribute_value_equal (const_tree attr1, const_tree attr2)
4948 if (TREE_VALUE (attr1) == TREE_VALUE (attr2))
4949 return true;
4951 if (TREE_VALUE (attr1) != NULL_TREE
4952 && TREE_CODE (TREE_VALUE (attr1)) == TREE_LIST
4953 && TREE_VALUE (attr2) != NULL_TREE
4954 && TREE_CODE (TREE_VALUE (attr2)) == TREE_LIST)
4956 /* Handle attribute format. */
4957 if (is_attribute_p ("format", TREE_PURPOSE (attr1)))
4959 attr1 = TREE_VALUE (attr1);
4960 attr2 = TREE_VALUE (attr2);
4961 /* Compare the archetypes (printf/scanf/strftime/...). */
4962 if (!cmp_attrib_identifiers (TREE_VALUE (attr1),
4963 TREE_VALUE (attr2)))
4964 return false;
4965 /* Archetypes are the same. Compare the rest. */
4966 return (simple_cst_list_equal (TREE_CHAIN (attr1),
4967 TREE_CHAIN (attr2)) == 1);
4969 return (simple_cst_list_equal (TREE_VALUE (attr1),
4970 TREE_VALUE (attr2)) == 1);
4973 if ((flag_openmp || flag_openmp_simd)
4974 && TREE_VALUE (attr1) && TREE_VALUE (attr2)
4975 && TREE_CODE (TREE_VALUE (attr1)) == OMP_CLAUSE
4976 && TREE_CODE (TREE_VALUE (attr2)) == OMP_CLAUSE)
4977 return omp_declare_simd_clauses_equal (TREE_VALUE (attr1),
4978 TREE_VALUE (attr2));
4980 return (simple_cst_equal (TREE_VALUE (attr1), TREE_VALUE (attr2)) == 1);
4983 /* Return 0 if the attributes for two types are incompatible, 1 if they
4984 are compatible, and 2 if they are nearly compatible (which causes a
4985 warning to be generated). */
4987 comp_type_attributes (const_tree type1, const_tree type2)
4989 const_tree a1 = TYPE_ATTRIBUTES (type1);
4990 const_tree a2 = TYPE_ATTRIBUTES (type2);
4991 const_tree a;
4993 if (a1 == a2)
4994 return 1;
4995 for (a = a1; a != NULL_TREE; a = TREE_CHAIN (a))
4997 const struct attribute_spec *as;
4998 const_tree attr;
5000 as = lookup_attribute_spec (get_attribute_name (a));
5001 if (!as || as->affects_type_identity == false)
5002 continue;
5004 attr = lookup_attribute (as->name, CONST_CAST_TREE (a2));
5005 if (!attr || !attribute_value_equal (a, attr))
5006 break;
5008 if (!a)
5010 for (a = a2; a != NULL_TREE; a = TREE_CHAIN (a))
5012 const struct attribute_spec *as;
5014 as = lookup_attribute_spec (get_attribute_name (a));
5015 if (!as || as->affects_type_identity == false)
5016 continue;
5018 if (!lookup_attribute (as->name, CONST_CAST_TREE (a1)))
5019 break;
5020 /* We don't need to compare trees again, as we did this
5021 already in first loop. */
5023 /* All types - affecting identity - are equal, so
5024 there is no need to call target hook for comparison. */
5025 if (!a)
5026 return 1;
5028 if (lookup_attribute ("transaction_safe", CONST_CAST_TREE (a)))
5029 return 0;
5030 /* As some type combinations - like default calling-convention - might
5031 be compatible, we have to call the target hook to get the final result. */
5032 return targetm.comp_type_attributes (type1, type2);
5035 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
5036 is ATTRIBUTE.
5038 Record such modified types already made so we don't make duplicates. */
5040 tree
5041 build_type_attribute_variant (tree ttype, tree attribute)
5043 return build_type_attribute_qual_variant (ttype, attribute,
5044 TYPE_QUALS (ttype));
5048 /* Reset the expression *EXPR_P, a size or position.
5050 ??? We could reset all non-constant sizes or positions. But it's cheap
5051 enough to not do so and refrain from adding workarounds to dwarf2out.c.
5053 We need to reset self-referential sizes or positions because they cannot
5054 be gimplified and thus can contain a CALL_EXPR after the gimplification
5055 is finished, which will run afoul of LTO streaming. And they need to be
5056 reset to something essentially dummy but not constant, so as to preserve
5057 the properties of the object they are attached to. */
5059 static inline void
5060 free_lang_data_in_one_sizepos (tree *expr_p)
5062 tree expr = *expr_p;
5063 if (CONTAINS_PLACEHOLDER_P (expr))
5064 *expr_p = build0 (PLACEHOLDER_EXPR, TREE_TYPE (expr));
5068 /* Reset all the fields in a binfo node BINFO. We only keep
5069 BINFO_VTABLE, which is used by gimple_fold_obj_type_ref. */
5071 static void
5072 free_lang_data_in_binfo (tree binfo)
5074 unsigned i;
5075 tree t;
5077 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
5079 BINFO_VIRTUALS (binfo) = NULL_TREE;
5080 BINFO_BASE_ACCESSES (binfo) = NULL;
5081 BINFO_INHERITANCE_CHAIN (binfo) = NULL_TREE;
5082 BINFO_SUBVTT_INDEX (binfo) = NULL_TREE;
5084 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (binfo), i, t)
5085 free_lang_data_in_binfo (t);
5089 /* Reset all language specific information still present in TYPE. */
5091 static void
5092 free_lang_data_in_type (tree type)
5094 gcc_assert (TYPE_P (type));
5096 /* Give the FE a chance to remove its own data first. */
5097 lang_hooks.free_lang_data (type);
5099 TREE_LANG_FLAG_0 (type) = 0;
5100 TREE_LANG_FLAG_1 (type) = 0;
5101 TREE_LANG_FLAG_2 (type) = 0;
5102 TREE_LANG_FLAG_3 (type) = 0;
5103 TREE_LANG_FLAG_4 (type) = 0;
5104 TREE_LANG_FLAG_5 (type) = 0;
5105 TREE_LANG_FLAG_6 (type) = 0;
5107 if (TREE_CODE (type) == FUNCTION_TYPE)
5109 /* Remove the const and volatile qualifiers from arguments. The
5110 C++ front end removes them, but the C front end does not,
5111 leading to false ODR violation errors when merging two
5112 instances of the same function signature compiled by
5113 different front ends. */
5114 tree p;
5116 for (p = TYPE_ARG_TYPES (type); p; p = TREE_CHAIN (p))
5118 tree arg_type = TREE_VALUE (p);
5120 if (TYPE_READONLY (arg_type) || TYPE_VOLATILE (arg_type))
5122 int quals = TYPE_QUALS (arg_type)
5123 & ~TYPE_QUAL_CONST
5124 & ~TYPE_QUAL_VOLATILE;
5125 TREE_VALUE (p) = build_qualified_type (arg_type, quals);
5126 free_lang_data_in_type (TREE_VALUE (p));
5128 /* C++ FE uses TREE_PURPOSE to store initial values. */
5129 TREE_PURPOSE (p) = NULL;
5131 /* Java uses TYPE_MINVAL for TYPE_ARGUMENT_SIGNATURE. */
5132 TYPE_MINVAL (type) = NULL;
5134 if (TREE_CODE (type) == METHOD_TYPE)
5136 tree p;
5138 for (p = TYPE_ARG_TYPES (type); p; p = TREE_CHAIN (p))
5140 /* C++ FE uses TREE_PURPOSE to store initial values. */
5141 TREE_PURPOSE (p) = NULL;
5143 /* Java uses TYPE_MINVAL for TYPE_ARGUMENT_SIGNATURE. */
5144 TYPE_MINVAL (type) = NULL;
5147 /* Remove members that are not actually FIELD_DECLs from the field
5148 list of an aggregate. These occur in C++. */
5149 if (RECORD_OR_UNION_TYPE_P (type))
5151 tree prev, member;
5153 /* Note that TYPE_FIELDS can be shared across distinct
5154 TREE_TYPEs. Therefore, if the first field of TYPE_FIELDS is
5155 to be removed, we cannot set its TREE_CHAIN to NULL.
5156 Otherwise, we would not be able to find all the other fields
5157 in the other instances of this TREE_TYPE.
5159 This was causing an ICE in testsuite/g++.dg/lto/20080915.C. */
5160 prev = NULL_TREE;
5161 member = TYPE_FIELDS (type);
5162 while (member)
5164 if (TREE_CODE (member) == FIELD_DECL
5165 || TREE_CODE (member) == TYPE_DECL)
5167 if (prev)
5168 TREE_CHAIN (prev) = member;
5169 else
5170 TYPE_FIELDS (type) = member;
5171 prev = member;
5174 member = TREE_CHAIN (member);
5177 if (prev)
5178 TREE_CHAIN (prev) = NULL_TREE;
5179 else
5180 TYPE_FIELDS (type) = NULL_TREE;
5182 /* FIXME: C FE uses TYPE_VFIELD to record C_TYPE_INCOMPLETE_VARS
5183 and danagle the pointer from time to time. */
5184 if (TYPE_VFIELD (type) && TREE_CODE (TYPE_VFIELD (type)) != FIELD_DECL)
5185 TYPE_VFIELD (type) = NULL_TREE;
5187 /* Remove TYPE_METHODS list. While it would be nice to keep it
5188 to enable ODR warnings about different method lists, doing so
5189 seems to impractically increase size of LTO data streamed.
5190 Keep the infrmation if TYPE_METHODS was non-NULL. This is used
5191 by function.c and pretty printers. */
5192 if (TYPE_METHODS (type))
5193 TYPE_METHODS (type) = error_mark_node;
5194 if (TYPE_BINFO (type))
5196 free_lang_data_in_binfo (TYPE_BINFO (type));
5197 /* We need to preserve link to bases and virtual table for all
5198 polymorphic types to make devirtualization machinery working.
5199 Debug output cares only about bases, but output also
5200 virtual table pointers so merging of -fdevirtualize and
5201 -fno-devirtualize units is easier. */
5202 if ((!BINFO_VTABLE (TYPE_BINFO (type))
5203 || !flag_devirtualize)
5204 && ((!BINFO_N_BASE_BINFOS (TYPE_BINFO (type))
5205 && !BINFO_VTABLE (TYPE_BINFO (type)))
5206 || debug_info_level != DINFO_LEVEL_NONE))
5207 TYPE_BINFO (type) = NULL;
5210 else
5212 /* For non-aggregate types, clear out the language slot (which
5213 overloads TYPE_BINFO). */
5214 TYPE_LANG_SLOT_1 (type) = NULL_TREE;
5216 if (INTEGRAL_TYPE_P (type)
5217 || SCALAR_FLOAT_TYPE_P (type)
5218 || FIXED_POINT_TYPE_P (type))
5220 free_lang_data_in_one_sizepos (&TYPE_MIN_VALUE (type));
5221 free_lang_data_in_one_sizepos (&TYPE_MAX_VALUE (type));
5225 free_lang_data_in_one_sizepos (&TYPE_SIZE (type));
5226 free_lang_data_in_one_sizepos (&TYPE_SIZE_UNIT (type));
5228 if (TYPE_CONTEXT (type)
5229 && TREE_CODE (TYPE_CONTEXT (type)) == BLOCK)
5231 tree ctx = TYPE_CONTEXT (type);
5234 ctx = BLOCK_SUPERCONTEXT (ctx);
5236 while (ctx && TREE_CODE (ctx) == BLOCK);
5237 TYPE_CONTEXT (type) = ctx;
5242 /* Return true if DECL may need an assembler name to be set. */
5244 static inline bool
5245 need_assembler_name_p (tree decl)
5247 /* We use DECL_ASSEMBLER_NAME to hold mangled type names for One Definition
5248 Rule merging. This makes type_odr_p to return true on those types during
5249 LTO and by comparing the mangled name, we can say what types are intended
5250 to be equivalent across compilation unit.
5252 We do not store names of type_in_anonymous_namespace_p.
5254 Record, union and enumeration type have linkage that allows use
5255 to check type_in_anonymous_namespace_p. We do not mangle compound types
5256 that always can be compared structurally.
5258 Similarly for builtin types, we compare properties of their main variant.
5259 A special case are integer types where mangling do make differences
5260 between char/signed char/unsigned char etc. Storing name for these makes
5261 e.g. -fno-signed-char/-fsigned-char mismatches to be handled well.
5262 See cp/mangle.c:write_builtin_type for details. */
5264 if (flag_lto_odr_type_mering
5265 && TREE_CODE (decl) == TYPE_DECL
5266 && DECL_NAME (decl)
5267 && decl == TYPE_NAME (TREE_TYPE (decl))
5268 && !TYPE_ARTIFICIAL (TREE_TYPE (decl))
5269 && (type_with_linkage_p (TREE_TYPE (decl))
5270 || TREE_CODE (TREE_TYPE (decl)) == INTEGER_TYPE)
5271 && !variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
5272 return !DECL_ASSEMBLER_NAME_SET_P (decl);
5273 /* Only FUNCTION_DECLs and VAR_DECLs are considered. */
5274 if (TREE_CODE (decl) != FUNCTION_DECL
5275 && TREE_CODE (decl) != VAR_DECL)
5276 return false;
5278 /* If DECL already has its assembler name set, it does not need a
5279 new one. */
5280 if (!HAS_DECL_ASSEMBLER_NAME_P (decl)
5281 || DECL_ASSEMBLER_NAME_SET_P (decl))
5282 return false;
5284 /* Abstract decls do not need an assembler name. */
5285 if (DECL_ABSTRACT_P (decl))
5286 return false;
5288 /* For VAR_DECLs, only static, public and external symbols need an
5289 assembler name. */
5290 if (TREE_CODE (decl) == VAR_DECL
5291 && !TREE_STATIC (decl)
5292 && !TREE_PUBLIC (decl)
5293 && !DECL_EXTERNAL (decl))
5294 return false;
5296 if (TREE_CODE (decl) == FUNCTION_DECL)
5298 /* Do not set assembler name on builtins. Allow RTL expansion to
5299 decide whether to expand inline or via a regular call. */
5300 if (DECL_BUILT_IN (decl)
5301 && DECL_BUILT_IN_CLASS (decl) != BUILT_IN_FRONTEND)
5302 return false;
5304 /* Functions represented in the callgraph need an assembler name. */
5305 if (cgraph_node::get (decl) != NULL)
5306 return true;
5308 /* Unused and not public functions don't need an assembler name. */
5309 if (!TREE_USED (decl) && !TREE_PUBLIC (decl))
5310 return false;
5313 return true;
5317 /* Reset all language specific information still present in symbol
5318 DECL. */
5320 static void
5321 free_lang_data_in_decl (tree decl)
5323 gcc_assert (DECL_P (decl));
5325 /* Give the FE a chance to remove its own data first. */
5326 lang_hooks.free_lang_data (decl);
5328 TREE_LANG_FLAG_0 (decl) = 0;
5329 TREE_LANG_FLAG_1 (decl) = 0;
5330 TREE_LANG_FLAG_2 (decl) = 0;
5331 TREE_LANG_FLAG_3 (decl) = 0;
5332 TREE_LANG_FLAG_4 (decl) = 0;
5333 TREE_LANG_FLAG_5 (decl) = 0;
5334 TREE_LANG_FLAG_6 (decl) = 0;
5336 free_lang_data_in_one_sizepos (&DECL_SIZE (decl));
5337 free_lang_data_in_one_sizepos (&DECL_SIZE_UNIT (decl));
5338 if (TREE_CODE (decl) == FIELD_DECL)
5340 free_lang_data_in_one_sizepos (&DECL_FIELD_OFFSET (decl));
5341 if (TREE_CODE (DECL_CONTEXT (decl)) == QUAL_UNION_TYPE)
5342 DECL_QUALIFIER (decl) = NULL_TREE;
5345 if (TREE_CODE (decl) == FUNCTION_DECL)
5347 struct cgraph_node *node;
5348 if (!(node = cgraph_node::get (decl))
5349 || (!node->definition && !node->clones))
5351 if (node)
5352 node->release_body ();
5353 else
5355 release_function_body (decl);
5356 DECL_ARGUMENTS (decl) = NULL;
5357 DECL_RESULT (decl) = NULL;
5358 DECL_INITIAL (decl) = error_mark_node;
5361 if (gimple_has_body_p (decl))
5363 tree t;
5365 /* If DECL has a gimple body, then the context for its
5366 arguments must be DECL. Otherwise, it doesn't really
5367 matter, as we will not be emitting any code for DECL. In
5368 general, there may be other instances of DECL created by
5369 the front end and since PARM_DECLs are generally shared,
5370 their DECL_CONTEXT changes as the replicas of DECL are
5371 created. The only time where DECL_CONTEXT is important
5372 is for the FUNCTION_DECLs that have a gimple body (since
5373 the PARM_DECL will be used in the function's body). */
5374 for (t = DECL_ARGUMENTS (decl); t; t = TREE_CHAIN (t))
5375 DECL_CONTEXT (t) = decl;
5376 if (!DECL_FUNCTION_SPECIFIC_TARGET (decl))
5377 DECL_FUNCTION_SPECIFIC_TARGET (decl)
5378 = target_option_default_node;
5379 if (!DECL_FUNCTION_SPECIFIC_OPTIMIZATION (decl))
5380 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (decl)
5381 = optimization_default_node;
5384 /* DECL_SAVED_TREE holds the GENERIC representation for DECL.
5385 At this point, it is not needed anymore. */
5386 DECL_SAVED_TREE (decl) = NULL_TREE;
5388 /* Clear the abstract origin if it refers to a method. Otherwise
5389 dwarf2out.c will ICE as we clear TYPE_METHODS and thus the
5390 origin will not be output correctly. */
5391 if (DECL_ABSTRACT_ORIGIN (decl)
5392 && DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))
5393 && RECORD_OR_UNION_TYPE_P
5394 (DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))))
5395 DECL_ABSTRACT_ORIGIN (decl) = NULL_TREE;
5397 /* Sometimes the C++ frontend doesn't manage to transform a temporary
5398 DECL_VINDEX referring to itself into a vtable slot number as it
5399 should. Happens with functions that are copied and then forgotten
5400 about. Just clear it, it won't matter anymore. */
5401 if (DECL_VINDEX (decl) && !tree_fits_shwi_p (DECL_VINDEX (decl)))
5402 DECL_VINDEX (decl) = NULL_TREE;
5404 else if (TREE_CODE (decl) == VAR_DECL)
5406 if ((DECL_EXTERNAL (decl)
5407 && (!TREE_STATIC (decl) || !TREE_READONLY (decl)))
5408 || (decl_function_context (decl) && !TREE_STATIC (decl)))
5409 DECL_INITIAL (decl) = NULL_TREE;
5411 else if (TREE_CODE (decl) == TYPE_DECL
5412 || TREE_CODE (decl) == FIELD_DECL)
5413 DECL_INITIAL (decl) = NULL_TREE;
5414 else if (TREE_CODE (decl) == TRANSLATION_UNIT_DECL
5415 && DECL_INITIAL (decl)
5416 && TREE_CODE (DECL_INITIAL (decl)) == BLOCK)
5418 /* Strip builtins from the translation-unit BLOCK. We still have targets
5419 without builtin_decl_explicit support and also builtins are shared
5420 nodes and thus we can't use TREE_CHAIN in multiple lists. */
5421 tree *nextp = &BLOCK_VARS (DECL_INITIAL (decl));
5422 while (*nextp)
5424 tree var = *nextp;
5425 if (TREE_CODE (var) == FUNCTION_DECL
5426 && DECL_BUILT_IN (var))
5427 *nextp = TREE_CHAIN (var);
5428 else
5429 nextp = &TREE_CHAIN (var);
5435 /* Data used when collecting DECLs and TYPEs for language data removal. */
5437 struct free_lang_data_d
5439 /* Worklist to avoid excessive recursion. */
5440 vec<tree> worklist;
5442 /* Set of traversed objects. Used to avoid duplicate visits. */
5443 hash_set<tree> *pset;
5445 /* Array of symbols to process with free_lang_data_in_decl. */
5446 vec<tree> decls;
5448 /* Array of types to process with free_lang_data_in_type. */
5449 vec<tree> types;
5453 /* Save all language fields needed to generate proper debug information
5454 for DECL. This saves most fields cleared out by free_lang_data_in_decl. */
5456 static void
5457 save_debug_info_for_decl (tree t)
5459 /*struct saved_debug_info_d *sdi;*/
5461 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && DECL_P (t));
5463 /* FIXME. Partial implementation for saving debug info removed. */
5467 /* Save all language fields needed to generate proper debug information
5468 for TYPE. This saves most fields cleared out by free_lang_data_in_type. */
5470 static void
5471 save_debug_info_for_type (tree t)
5473 /*struct saved_debug_info_d *sdi;*/
5475 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && TYPE_P (t));
5477 /* FIXME. Partial implementation for saving debug info removed. */
5481 /* Add type or decl T to one of the list of tree nodes that need their
5482 language data removed. The lists are held inside FLD. */
5484 static void
5485 add_tree_to_fld_list (tree t, struct free_lang_data_d *fld)
5487 if (DECL_P (t))
5489 fld->decls.safe_push (t);
5490 if (debug_info_level > DINFO_LEVEL_TERSE)
5491 save_debug_info_for_decl (t);
5493 else if (TYPE_P (t))
5495 fld->types.safe_push (t);
5496 if (debug_info_level > DINFO_LEVEL_TERSE)
5497 save_debug_info_for_type (t);
5499 else
5500 gcc_unreachable ();
5503 /* Push tree node T into FLD->WORKLIST. */
5505 static inline void
5506 fld_worklist_push (tree t, struct free_lang_data_d *fld)
5508 if (t && !is_lang_specific (t) && !fld->pset->contains (t))
5509 fld->worklist.safe_push ((t));
5513 /* Operand callback helper for free_lang_data_in_node. *TP is the
5514 subtree operand being considered. */
5516 static tree
5517 find_decls_types_r (tree *tp, int *ws, void *data)
5519 tree t = *tp;
5520 struct free_lang_data_d *fld = (struct free_lang_data_d *) data;
5522 if (TREE_CODE (t) == TREE_LIST)
5523 return NULL_TREE;
5525 /* Language specific nodes will be removed, so there is no need
5526 to gather anything under them. */
5527 if (is_lang_specific (t))
5529 *ws = 0;
5530 return NULL_TREE;
5533 if (DECL_P (t))
5535 /* Note that walk_tree does not traverse every possible field in
5536 decls, so we have to do our own traversals here. */
5537 add_tree_to_fld_list (t, fld);
5539 fld_worklist_push (DECL_NAME (t), fld);
5540 fld_worklist_push (DECL_CONTEXT (t), fld);
5541 fld_worklist_push (DECL_SIZE (t), fld);
5542 fld_worklist_push (DECL_SIZE_UNIT (t), fld);
5544 /* We are going to remove everything under DECL_INITIAL for
5545 TYPE_DECLs. No point walking them. */
5546 if (TREE_CODE (t) != TYPE_DECL)
5547 fld_worklist_push (DECL_INITIAL (t), fld);
5549 fld_worklist_push (DECL_ATTRIBUTES (t), fld);
5550 fld_worklist_push (DECL_ABSTRACT_ORIGIN (t), fld);
5552 if (TREE_CODE (t) == FUNCTION_DECL)
5554 fld_worklist_push (DECL_ARGUMENTS (t), fld);
5555 fld_worklist_push (DECL_RESULT (t), fld);
5557 else if (TREE_CODE (t) == TYPE_DECL)
5559 fld_worklist_push (DECL_ORIGINAL_TYPE (t), fld);
5561 else if (TREE_CODE (t) == FIELD_DECL)
5563 fld_worklist_push (DECL_FIELD_OFFSET (t), fld);
5564 fld_worklist_push (DECL_BIT_FIELD_TYPE (t), fld);
5565 fld_worklist_push (DECL_FIELD_BIT_OFFSET (t), fld);
5566 fld_worklist_push (DECL_FCONTEXT (t), fld);
5569 if ((TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
5570 && DECL_HAS_VALUE_EXPR_P (t))
5571 fld_worklist_push (DECL_VALUE_EXPR (t), fld);
5573 if (TREE_CODE (t) != FIELD_DECL
5574 && TREE_CODE (t) != TYPE_DECL)
5575 fld_worklist_push (TREE_CHAIN (t), fld);
5576 *ws = 0;
5578 else if (TYPE_P (t))
5580 /* Note that walk_tree does not traverse every possible field in
5581 types, so we have to do our own traversals here. */
5582 add_tree_to_fld_list (t, fld);
5584 if (!RECORD_OR_UNION_TYPE_P (t))
5585 fld_worklist_push (TYPE_CACHED_VALUES (t), fld);
5586 fld_worklist_push (TYPE_SIZE (t), fld);
5587 fld_worklist_push (TYPE_SIZE_UNIT (t), fld);
5588 fld_worklist_push (TYPE_ATTRIBUTES (t), fld);
5589 fld_worklist_push (TYPE_POINTER_TO (t), fld);
5590 fld_worklist_push (TYPE_REFERENCE_TO (t), fld);
5591 fld_worklist_push (TYPE_NAME (t), fld);
5592 /* Do not walk TYPE_NEXT_PTR_TO or TYPE_NEXT_REF_TO. We do not stream
5593 them and thus do not and want not to reach unused pointer types
5594 this way. */
5595 if (!POINTER_TYPE_P (t))
5596 fld_worklist_push (TYPE_MINVAL (t), fld);
5597 if (!RECORD_OR_UNION_TYPE_P (t))
5598 fld_worklist_push (TYPE_MAXVAL (t), fld);
5599 fld_worklist_push (TYPE_MAIN_VARIANT (t), fld);
5600 /* Do not walk TYPE_NEXT_VARIANT. We do not stream it and thus
5601 do not and want not to reach unused variants this way. */
5602 if (TYPE_CONTEXT (t))
5604 tree ctx = TYPE_CONTEXT (t);
5605 /* We adjust BLOCK TYPE_CONTEXTs to the innermost non-BLOCK one.
5606 So push that instead. */
5607 while (ctx && TREE_CODE (ctx) == BLOCK)
5608 ctx = BLOCK_SUPERCONTEXT (ctx);
5609 fld_worklist_push (ctx, fld);
5611 /* Do not walk TYPE_CANONICAL. We do not stream it and thus do not
5612 and want not to reach unused types this way. */
5614 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t))
5616 unsigned i;
5617 tree tem;
5618 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (TYPE_BINFO (t)), i, tem)
5619 fld_worklist_push (TREE_TYPE (tem), fld);
5620 tem = BINFO_VIRTUALS (TYPE_BINFO (t));
5621 if (tem
5622 /* The Java FE overloads BINFO_VIRTUALS for its own purpose. */
5623 && TREE_CODE (tem) == TREE_LIST)
5626 fld_worklist_push (TREE_VALUE (tem), fld);
5627 tem = TREE_CHAIN (tem);
5629 while (tem);
5631 if (RECORD_OR_UNION_TYPE_P (t))
5633 tree tem;
5634 /* Push all TYPE_FIELDS - there can be interleaving interesting
5635 and non-interesting things. */
5636 tem = TYPE_FIELDS (t);
5637 while (tem)
5639 if (TREE_CODE (tem) == FIELD_DECL
5640 || TREE_CODE (tem) == TYPE_DECL)
5641 fld_worklist_push (tem, fld);
5642 tem = TREE_CHAIN (tem);
5646 fld_worklist_push (TYPE_STUB_DECL (t), fld);
5647 *ws = 0;
5649 else if (TREE_CODE (t) == BLOCK)
5651 tree tem;
5652 for (tem = BLOCK_VARS (t); tem; tem = TREE_CHAIN (tem))
5653 fld_worklist_push (tem, fld);
5654 for (tem = BLOCK_SUBBLOCKS (t); tem; tem = BLOCK_CHAIN (tem))
5655 fld_worklist_push (tem, fld);
5656 fld_worklist_push (BLOCK_ABSTRACT_ORIGIN (t), fld);
5659 if (TREE_CODE (t) != IDENTIFIER_NODE
5660 && CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED))
5661 fld_worklist_push (TREE_TYPE (t), fld);
5663 return NULL_TREE;
5667 /* Find decls and types in T. */
5669 static void
5670 find_decls_types (tree t, struct free_lang_data_d *fld)
5672 while (1)
5674 if (!fld->pset->contains (t))
5675 walk_tree (&t, find_decls_types_r, fld, fld->pset);
5676 if (fld->worklist.is_empty ())
5677 break;
5678 t = fld->worklist.pop ();
5682 /* Translate all the types in LIST with the corresponding runtime
5683 types. */
5685 static tree
5686 get_eh_types_for_runtime (tree list)
5688 tree head, prev;
5690 if (list == NULL_TREE)
5691 return NULL_TREE;
5693 head = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5694 prev = head;
5695 list = TREE_CHAIN (list);
5696 while (list)
5698 tree n = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5699 TREE_CHAIN (prev) = n;
5700 prev = TREE_CHAIN (prev);
5701 list = TREE_CHAIN (list);
5704 return head;
5708 /* Find decls and types referenced in EH region R and store them in
5709 FLD->DECLS and FLD->TYPES. */
5711 static void
5712 find_decls_types_in_eh_region (eh_region r, struct free_lang_data_d *fld)
5714 switch (r->type)
5716 case ERT_CLEANUP:
5717 break;
5719 case ERT_TRY:
5721 eh_catch c;
5723 /* The types referenced in each catch must first be changed to the
5724 EH types used at runtime. This removes references to FE types
5725 in the region. */
5726 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
5728 c->type_list = get_eh_types_for_runtime (c->type_list);
5729 walk_tree (&c->type_list, find_decls_types_r, fld, fld->pset);
5732 break;
5734 case ERT_ALLOWED_EXCEPTIONS:
5735 r->u.allowed.type_list
5736 = get_eh_types_for_runtime (r->u.allowed.type_list);
5737 walk_tree (&r->u.allowed.type_list, find_decls_types_r, fld, fld->pset);
5738 break;
5740 case ERT_MUST_NOT_THROW:
5741 walk_tree (&r->u.must_not_throw.failure_decl,
5742 find_decls_types_r, fld, fld->pset);
5743 break;
5748 /* Find decls and types referenced in cgraph node N and store them in
5749 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5750 look for *every* kind of DECL and TYPE node reachable from N,
5751 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5752 NAMESPACE_DECLs, etc). */
5754 static void
5755 find_decls_types_in_node (struct cgraph_node *n, struct free_lang_data_d *fld)
5757 basic_block bb;
5758 struct function *fn;
5759 unsigned ix;
5760 tree t;
5762 find_decls_types (n->decl, fld);
5764 if (!gimple_has_body_p (n->decl))
5765 return;
5767 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
5769 fn = DECL_STRUCT_FUNCTION (n->decl);
5771 /* Traverse locals. */
5772 FOR_EACH_LOCAL_DECL (fn, ix, t)
5773 find_decls_types (t, fld);
5775 /* Traverse EH regions in FN. */
5777 eh_region r;
5778 FOR_ALL_EH_REGION_FN (r, fn)
5779 find_decls_types_in_eh_region (r, fld);
5782 /* Traverse every statement in FN. */
5783 FOR_EACH_BB_FN (bb, fn)
5785 gphi_iterator psi;
5786 gimple_stmt_iterator si;
5787 unsigned i;
5789 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
5791 gphi *phi = psi.phi ();
5793 for (i = 0; i < gimple_phi_num_args (phi); i++)
5795 tree *arg_p = gimple_phi_arg_def_ptr (phi, i);
5796 find_decls_types (*arg_p, fld);
5800 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
5802 gimple *stmt = gsi_stmt (si);
5804 if (is_gimple_call (stmt))
5805 find_decls_types (gimple_call_fntype (stmt), fld);
5807 for (i = 0; i < gimple_num_ops (stmt); i++)
5809 tree arg = gimple_op (stmt, i);
5810 find_decls_types (arg, fld);
5817 /* Find decls and types referenced in varpool node N and store them in
5818 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5819 look for *every* kind of DECL and TYPE node reachable from N,
5820 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5821 NAMESPACE_DECLs, etc). */
5823 static void
5824 find_decls_types_in_var (varpool_node *v, struct free_lang_data_d *fld)
5826 find_decls_types (v->decl, fld);
5829 /* If T needs an assembler name, have one created for it. */
5831 void
5832 assign_assembler_name_if_neeeded (tree t)
5834 if (need_assembler_name_p (t))
5836 /* When setting DECL_ASSEMBLER_NAME, the C++ mangler may emit
5837 diagnostics that use input_location to show locus
5838 information. The problem here is that, at this point,
5839 input_location is generally anchored to the end of the file
5840 (since the parser is long gone), so we don't have a good
5841 position to pin it to.
5843 To alleviate this problem, this uses the location of T's
5844 declaration. Examples of this are
5845 testsuite/g++.dg/template/cond2.C and
5846 testsuite/g++.dg/template/pr35240.C. */
5847 location_t saved_location = input_location;
5848 input_location = DECL_SOURCE_LOCATION (t);
5850 decl_assembler_name (t);
5852 input_location = saved_location;
5857 /* Free language specific information for every operand and expression
5858 in every node of the call graph. This process operates in three stages:
5860 1- Every callgraph node and varpool node is traversed looking for
5861 decls and types embedded in them. This is a more exhaustive
5862 search than that done by find_referenced_vars, because it will
5863 also collect individual fields, decls embedded in types, etc.
5865 2- All the decls found are sent to free_lang_data_in_decl.
5867 3- All the types found are sent to free_lang_data_in_type.
5869 The ordering between decls and types is important because
5870 free_lang_data_in_decl sets assembler names, which includes
5871 mangling. So types cannot be freed up until assembler names have
5872 been set up. */
5874 static void
5875 free_lang_data_in_cgraph (void)
5877 struct cgraph_node *n;
5878 varpool_node *v;
5879 struct free_lang_data_d fld;
5880 tree t;
5881 unsigned i;
5882 alias_pair *p;
5884 /* Initialize sets and arrays to store referenced decls and types. */
5885 fld.pset = new hash_set<tree>;
5886 fld.worklist.create (0);
5887 fld.decls.create (100);
5888 fld.types.create (100);
5890 /* Find decls and types in the body of every function in the callgraph. */
5891 FOR_EACH_FUNCTION (n)
5892 find_decls_types_in_node (n, &fld);
5894 FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
5895 find_decls_types (p->decl, &fld);
5897 /* Find decls and types in every varpool symbol. */
5898 FOR_EACH_VARIABLE (v)
5899 find_decls_types_in_var (v, &fld);
5901 /* Set the assembler name on every decl found. We need to do this
5902 now because free_lang_data_in_decl will invalidate data needed
5903 for mangling. This breaks mangling on interdependent decls. */
5904 FOR_EACH_VEC_ELT (fld.decls, i, t)
5905 assign_assembler_name_if_neeeded (t);
5907 /* Traverse every decl found freeing its language data. */
5908 FOR_EACH_VEC_ELT (fld.decls, i, t)
5909 free_lang_data_in_decl (t);
5911 /* Traverse every type found freeing its language data. */
5912 FOR_EACH_VEC_ELT (fld.types, i, t)
5913 free_lang_data_in_type (t);
5914 #ifdef ENABLE_CHECKING
5915 FOR_EACH_VEC_ELT (fld.types, i, t)
5916 verify_type (t);
5917 #endif
5919 delete fld.pset;
5920 fld.worklist.release ();
5921 fld.decls.release ();
5922 fld.types.release ();
5926 /* Free resources that are used by FE but are not needed once they are done. */
5928 static unsigned
5929 free_lang_data (void)
5931 unsigned i;
5933 /* If we are the LTO frontend we have freed lang-specific data already. */
5934 if (in_lto_p
5935 || (!flag_generate_lto && !flag_generate_offload))
5936 return 0;
5938 /* Allocate and assign alias sets to the standard integer types
5939 while the slots are still in the way the frontends generated them. */
5940 for (i = 0; i < itk_none; ++i)
5941 if (integer_types[i])
5942 TYPE_ALIAS_SET (integer_types[i]) = get_alias_set (integer_types[i]);
5944 /* Traverse the IL resetting language specific information for
5945 operands, expressions, etc. */
5946 free_lang_data_in_cgraph ();
5948 /* Create gimple variants for common types. */
5949 ptrdiff_type_node = integer_type_node;
5950 fileptr_type_node = ptr_type_node;
5952 /* Reset some langhooks. Do not reset types_compatible_p, it may
5953 still be used indirectly via the get_alias_set langhook. */
5954 lang_hooks.dwarf_name = lhd_dwarf_name;
5955 lang_hooks.decl_printable_name = gimple_decl_printable_name;
5956 lang_hooks.gimplify_expr = lhd_gimplify_expr;
5958 /* We do not want the default decl_assembler_name implementation,
5959 rather if we have fixed everything we want a wrapper around it
5960 asserting that all non-local symbols already got their assembler
5961 name and only produce assembler names for local symbols. Or rather
5962 make sure we never call decl_assembler_name on local symbols and
5963 devise a separate, middle-end private scheme for it. */
5965 /* Reset diagnostic machinery. */
5966 tree_diagnostics_defaults (global_dc);
5968 return 0;
5972 namespace {
5974 const pass_data pass_data_ipa_free_lang_data =
5976 SIMPLE_IPA_PASS, /* type */
5977 "*free_lang_data", /* name */
5978 OPTGROUP_NONE, /* optinfo_flags */
5979 TV_IPA_FREE_LANG_DATA, /* tv_id */
5980 0, /* properties_required */
5981 0, /* properties_provided */
5982 0, /* properties_destroyed */
5983 0, /* todo_flags_start */
5984 0, /* todo_flags_finish */
5987 class pass_ipa_free_lang_data : public simple_ipa_opt_pass
5989 public:
5990 pass_ipa_free_lang_data (gcc::context *ctxt)
5991 : simple_ipa_opt_pass (pass_data_ipa_free_lang_data, ctxt)
5994 /* opt_pass methods: */
5995 virtual unsigned int execute (function *) { return free_lang_data (); }
5997 }; // class pass_ipa_free_lang_data
5999 } // anon namespace
6001 simple_ipa_opt_pass *
6002 make_pass_ipa_free_lang_data (gcc::context *ctxt)
6004 return new pass_ipa_free_lang_data (ctxt);
6007 /* The backbone of is_attribute_p(). ATTR_LEN is the string length of
6008 ATTR_NAME. Also used internally by remove_attribute(). */
6009 bool
6010 private_is_attribute_p (const char *attr_name, size_t attr_len, const_tree ident)
6012 size_t ident_len = IDENTIFIER_LENGTH (ident);
6014 if (ident_len == attr_len)
6016 if (strcmp (attr_name, IDENTIFIER_POINTER (ident)) == 0)
6017 return true;
6019 else if (ident_len == attr_len + 4)
6021 /* There is the possibility that ATTR is 'text' and IDENT is
6022 '__text__'. */
6023 const char *p = IDENTIFIER_POINTER (ident);
6024 if (p[0] == '_' && p[1] == '_'
6025 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
6026 && strncmp (attr_name, p + 2, attr_len) == 0)
6027 return true;
6030 return false;
6033 /* The backbone of lookup_attribute(). ATTR_LEN is the string length
6034 of ATTR_NAME, and LIST is not NULL_TREE. */
6035 tree
6036 private_lookup_attribute (const char *attr_name, size_t attr_len, tree list)
6038 while (list)
6040 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
6042 if (ident_len == attr_len)
6044 if (!strcmp (attr_name,
6045 IDENTIFIER_POINTER (get_attribute_name (list))))
6046 break;
6048 /* TODO: If we made sure that attributes were stored in the
6049 canonical form without '__...__' (ie, as in 'text' as opposed
6050 to '__text__') then we could avoid the following case. */
6051 else if (ident_len == attr_len + 4)
6053 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
6054 if (p[0] == '_' && p[1] == '_'
6055 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
6056 && strncmp (attr_name, p + 2, attr_len) == 0)
6057 break;
6059 list = TREE_CHAIN (list);
6062 return list;
6065 /* Given an attribute name ATTR_NAME and a list of attributes LIST,
6066 return a pointer to the attribute's list first element if the attribute
6067 starts with ATTR_NAME. ATTR_NAME must be in the form 'text' (not
6068 '__text__'). */
6070 tree
6071 private_lookup_attribute_by_prefix (const char *attr_name, size_t attr_len,
6072 tree list)
6074 while (list)
6076 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
6078 if (attr_len > ident_len)
6080 list = TREE_CHAIN (list);
6081 continue;
6084 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
6086 if (strncmp (attr_name, p, attr_len) == 0)
6087 break;
6089 /* TODO: If we made sure that attributes were stored in the
6090 canonical form without '__...__' (ie, as in 'text' as opposed
6091 to '__text__') then we could avoid the following case. */
6092 if (p[0] == '_' && p[1] == '_' &&
6093 strncmp (attr_name, p + 2, attr_len) == 0)
6094 break;
6096 list = TREE_CHAIN (list);
6099 return list;
6103 /* A variant of lookup_attribute() that can be used with an identifier
6104 as the first argument, and where the identifier can be either
6105 'text' or '__text__'.
6107 Given an attribute ATTR_IDENTIFIER, and a list of attributes LIST,
6108 return a pointer to the attribute's list element if the attribute
6109 is part of the list, or NULL_TREE if not found. If the attribute
6110 appears more than once, this only returns the first occurrence; the
6111 TREE_CHAIN of the return value should be passed back in if further
6112 occurrences are wanted. ATTR_IDENTIFIER must be an identifier but
6113 can be in the form 'text' or '__text__'. */
6114 static tree
6115 lookup_ident_attribute (tree attr_identifier, tree list)
6117 gcc_checking_assert (TREE_CODE (attr_identifier) == IDENTIFIER_NODE);
6119 while (list)
6121 gcc_checking_assert (TREE_CODE (get_attribute_name (list))
6122 == IDENTIFIER_NODE);
6124 if (cmp_attrib_identifiers (attr_identifier,
6125 get_attribute_name (list)))
6126 /* Found it. */
6127 break;
6128 list = TREE_CHAIN (list);
6131 return list;
6134 /* Remove any instances of attribute ATTR_NAME in LIST and return the
6135 modified list. */
6137 tree
6138 remove_attribute (const char *attr_name, tree list)
6140 tree *p;
6141 size_t attr_len = strlen (attr_name);
6143 gcc_checking_assert (attr_name[0] != '_');
6145 for (p = &list; *p; )
6147 tree l = *p;
6148 /* TODO: If we were storing attributes in normalized form, here
6149 we could use a simple strcmp(). */
6150 if (private_is_attribute_p (attr_name, attr_len, get_attribute_name (l)))
6151 *p = TREE_CHAIN (l);
6152 else
6153 p = &TREE_CHAIN (l);
6156 return list;
6159 /* Return an attribute list that is the union of a1 and a2. */
6161 tree
6162 merge_attributes (tree a1, tree a2)
6164 tree attributes;
6166 /* Either one unset? Take the set one. */
6168 if ((attributes = a1) == 0)
6169 attributes = a2;
6171 /* One that completely contains the other? Take it. */
6173 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
6175 if (attribute_list_contained (a2, a1))
6176 attributes = a2;
6177 else
6179 /* Pick the longest list, and hang on the other list. */
6181 if (list_length (a1) < list_length (a2))
6182 attributes = a2, a2 = a1;
6184 for (; a2 != 0; a2 = TREE_CHAIN (a2))
6186 tree a;
6187 for (a = lookup_ident_attribute (get_attribute_name (a2),
6188 attributes);
6189 a != NULL_TREE && !attribute_value_equal (a, a2);
6190 a = lookup_ident_attribute (get_attribute_name (a2),
6191 TREE_CHAIN (a)))
6193 if (a == NULL_TREE)
6195 a1 = copy_node (a2);
6196 TREE_CHAIN (a1) = attributes;
6197 attributes = a1;
6202 return attributes;
6205 /* Given types T1 and T2, merge their attributes and return
6206 the result. */
6208 tree
6209 merge_type_attributes (tree t1, tree t2)
6211 return merge_attributes (TYPE_ATTRIBUTES (t1),
6212 TYPE_ATTRIBUTES (t2));
6215 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
6216 the result. */
6218 tree
6219 merge_decl_attributes (tree olddecl, tree newdecl)
6221 return merge_attributes (DECL_ATTRIBUTES (olddecl),
6222 DECL_ATTRIBUTES (newdecl));
6225 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
6227 /* Specialization of merge_decl_attributes for various Windows targets.
6229 This handles the following situation:
6231 __declspec (dllimport) int foo;
6232 int foo;
6234 The second instance of `foo' nullifies the dllimport. */
6236 tree
6237 merge_dllimport_decl_attributes (tree old, tree new_tree)
6239 tree a;
6240 int delete_dllimport_p = 1;
6242 /* What we need to do here is remove from `old' dllimport if it doesn't
6243 appear in `new'. dllimport behaves like extern: if a declaration is
6244 marked dllimport and a definition appears later, then the object
6245 is not dllimport'd. We also remove a `new' dllimport if the old list
6246 contains dllexport: dllexport always overrides dllimport, regardless
6247 of the order of declaration. */
6248 if (!VAR_OR_FUNCTION_DECL_P (new_tree))
6249 delete_dllimport_p = 0;
6250 else if (DECL_DLLIMPORT_P (new_tree)
6251 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
6253 DECL_DLLIMPORT_P (new_tree) = 0;
6254 warning (OPT_Wattributes, "%q+D already declared with dllexport attribute: "
6255 "dllimport ignored", new_tree);
6257 else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new_tree))
6259 /* Warn about overriding a symbol that has already been used, e.g.:
6260 extern int __attribute__ ((dllimport)) foo;
6261 int* bar () {return &foo;}
6262 int foo;
6264 if (TREE_USED (old))
6266 warning (0, "%q+D redeclared without dllimport attribute "
6267 "after being referenced with dll linkage", new_tree);
6268 /* If we have used a variable's address with dllimport linkage,
6269 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
6270 decl may already have had TREE_CONSTANT computed.
6271 We still remove the attribute so that assembler code refers
6272 to '&foo rather than '_imp__foo'. */
6273 if (TREE_CODE (old) == VAR_DECL && TREE_ADDRESSABLE (old))
6274 DECL_DLLIMPORT_P (new_tree) = 1;
6277 /* Let an inline definition silently override the external reference,
6278 but otherwise warn about attribute inconsistency. */
6279 else if (TREE_CODE (new_tree) == VAR_DECL
6280 || !DECL_DECLARED_INLINE_P (new_tree))
6281 warning (OPT_Wattributes, "%q+D redeclared without dllimport attribute: "
6282 "previous dllimport ignored", new_tree);
6284 else
6285 delete_dllimport_p = 0;
6287 a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new_tree));
6289 if (delete_dllimport_p)
6290 a = remove_attribute ("dllimport", a);
6292 return a;
6295 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
6296 struct attribute_spec.handler. */
6298 tree
6299 handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
6300 bool *no_add_attrs)
6302 tree node = *pnode;
6303 bool is_dllimport;
6305 /* These attributes may apply to structure and union types being created,
6306 but otherwise should pass to the declaration involved. */
6307 if (!DECL_P (node))
6309 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
6310 | (int) ATTR_FLAG_ARRAY_NEXT))
6312 *no_add_attrs = true;
6313 return tree_cons (name, args, NULL_TREE);
6315 if (TREE_CODE (node) == RECORD_TYPE
6316 || TREE_CODE (node) == UNION_TYPE)
6318 node = TYPE_NAME (node);
6319 if (!node)
6320 return NULL_TREE;
6322 else
6324 warning (OPT_Wattributes, "%qE attribute ignored",
6325 name);
6326 *no_add_attrs = true;
6327 return NULL_TREE;
6331 if (TREE_CODE (node) != FUNCTION_DECL
6332 && TREE_CODE (node) != VAR_DECL
6333 && TREE_CODE (node) != TYPE_DECL)
6335 *no_add_attrs = true;
6336 warning (OPT_Wattributes, "%qE attribute ignored",
6337 name);
6338 return NULL_TREE;
6341 if (TREE_CODE (node) == TYPE_DECL
6342 && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE
6343 && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE)
6345 *no_add_attrs = true;
6346 warning (OPT_Wattributes, "%qE attribute ignored",
6347 name);
6348 return NULL_TREE;
6351 is_dllimport = is_attribute_p ("dllimport", name);
6353 /* Report error on dllimport ambiguities seen now before they cause
6354 any damage. */
6355 if (is_dllimport)
6357 /* Honor any target-specific overrides. */
6358 if (!targetm.valid_dllimport_attribute_p (node))
6359 *no_add_attrs = true;
6361 else if (TREE_CODE (node) == FUNCTION_DECL
6362 && DECL_DECLARED_INLINE_P (node))
6364 warning (OPT_Wattributes, "inline function %q+D declared as "
6365 " dllimport: attribute ignored", node);
6366 *no_add_attrs = true;
6368 /* Like MS, treat definition of dllimported variables and
6369 non-inlined functions on declaration as syntax errors. */
6370 else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
6372 error ("function %q+D definition is marked dllimport", node);
6373 *no_add_attrs = true;
6376 else if (TREE_CODE (node) == VAR_DECL)
6378 if (DECL_INITIAL (node))
6380 error ("variable %q+D definition is marked dllimport",
6381 node);
6382 *no_add_attrs = true;
6385 /* `extern' needn't be specified with dllimport.
6386 Specify `extern' now and hope for the best. Sigh. */
6387 DECL_EXTERNAL (node) = 1;
6388 /* Also, implicitly give dllimport'd variables declared within
6389 a function global scope, unless declared static. */
6390 if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
6391 TREE_PUBLIC (node) = 1;
6394 if (*no_add_attrs == false)
6395 DECL_DLLIMPORT_P (node) = 1;
6397 else if (TREE_CODE (node) == FUNCTION_DECL
6398 && DECL_DECLARED_INLINE_P (node)
6399 && flag_keep_inline_dllexport)
6400 /* An exported function, even if inline, must be emitted. */
6401 DECL_EXTERNAL (node) = 0;
6403 /* Report error if symbol is not accessible at global scope. */
6404 if (!TREE_PUBLIC (node)
6405 && (TREE_CODE (node) == VAR_DECL
6406 || TREE_CODE (node) == FUNCTION_DECL))
6408 error ("external linkage required for symbol %q+D because of "
6409 "%qE attribute", node, name);
6410 *no_add_attrs = true;
6413 /* A dllexport'd entity must have default visibility so that other
6414 program units (shared libraries or the main executable) can see
6415 it. A dllimport'd entity must have default visibility so that
6416 the linker knows that undefined references within this program
6417 unit can be resolved by the dynamic linker. */
6418 if (!*no_add_attrs)
6420 if (DECL_VISIBILITY_SPECIFIED (node)
6421 && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT)
6422 error ("%qE implies default visibility, but %qD has already "
6423 "been declared with a different visibility",
6424 name, node);
6425 DECL_VISIBILITY (node) = VISIBILITY_DEFAULT;
6426 DECL_VISIBILITY_SPECIFIED (node) = 1;
6429 return NULL_TREE;
6432 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
6434 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
6435 of the various TYPE_QUAL values. */
6437 static void
6438 set_type_quals (tree type, int type_quals)
6440 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
6441 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
6442 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
6443 TYPE_ATOMIC (type) = (type_quals & TYPE_QUAL_ATOMIC) != 0;
6444 TYPE_ADDR_SPACE (type) = DECODE_QUAL_ADDR_SPACE (type_quals);
6447 /* Returns true iff unqualified CAND and BASE are equivalent. */
6449 bool
6450 check_base_type (const_tree cand, const_tree base)
6452 return (TYPE_NAME (cand) == TYPE_NAME (base)
6453 /* Apparently this is needed for Objective-C. */
6454 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
6455 /* Check alignment. */
6456 && TYPE_ALIGN (cand) == TYPE_ALIGN (base)
6457 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
6458 TYPE_ATTRIBUTES (base)));
6461 /* Returns true iff CAND is equivalent to BASE with TYPE_QUALS. */
6463 bool
6464 check_qualified_type (const_tree cand, const_tree base, int type_quals)
6466 return (TYPE_QUALS (cand) == type_quals
6467 && check_base_type (cand, base));
6470 /* Returns true iff CAND is equivalent to BASE with ALIGN. */
6472 static bool
6473 check_aligned_type (const_tree cand, const_tree base, unsigned int align)
6475 return (TYPE_QUALS (cand) == TYPE_QUALS (base)
6476 && TYPE_NAME (cand) == TYPE_NAME (base)
6477 /* Apparently this is needed for Objective-C. */
6478 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
6479 /* Check alignment. */
6480 && TYPE_ALIGN (cand) == align
6481 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
6482 TYPE_ATTRIBUTES (base)));
6485 /* This function checks to see if TYPE matches the size one of the built-in
6486 atomic types, and returns that core atomic type. */
6488 static tree
6489 find_atomic_core_type (tree type)
6491 tree base_atomic_type;
6493 /* Only handle complete types. */
6494 if (TYPE_SIZE (type) == NULL_TREE)
6495 return NULL_TREE;
6497 HOST_WIDE_INT type_size = tree_to_uhwi (TYPE_SIZE (type));
6498 switch (type_size)
6500 case 8:
6501 base_atomic_type = atomicQI_type_node;
6502 break;
6504 case 16:
6505 base_atomic_type = atomicHI_type_node;
6506 break;
6508 case 32:
6509 base_atomic_type = atomicSI_type_node;
6510 break;
6512 case 64:
6513 base_atomic_type = atomicDI_type_node;
6514 break;
6516 case 128:
6517 base_atomic_type = atomicTI_type_node;
6518 break;
6520 default:
6521 base_atomic_type = NULL_TREE;
6524 return base_atomic_type;
6527 /* Return a version of the TYPE, qualified as indicated by the
6528 TYPE_QUALS, if one exists. If no qualified version exists yet,
6529 return NULL_TREE. */
6531 tree
6532 get_qualified_type (tree type, int type_quals)
6534 tree t;
6536 if (TYPE_QUALS (type) == type_quals)
6537 return type;
6539 /* Search the chain of variants to see if there is already one there just
6540 like the one we need to have. If so, use that existing one. We must
6541 preserve the TYPE_NAME, since there is code that depends on this. */
6542 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6543 if (check_qualified_type (t, type, type_quals))
6544 return t;
6546 return NULL_TREE;
6549 /* Like get_qualified_type, but creates the type if it does not
6550 exist. This function never returns NULL_TREE. */
6552 tree
6553 build_qualified_type (tree type, int type_quals)
6555 tree t;
6557 /* See if we already have the appropriate qualified variant. */
6558 t = get_qualified_type (type, type_quals);
6560 /* If not, build it. */
6561 if (!t)
6563 t = build_variant_type_copy (type);
6564 set_type_quals (t, type_quals);
6566 if (((type_quals & TYPE_QUAL_ATOMIC) == TYPE_QUAL_ATOMIC))
6568 /* See if this object can map to a basic atomic type. */
6569 tree atomic_type = find_atomic_core_type (type);
6570 if (atomic_type)
6572 /* Ensure the alignment of this type is compatible with
6573 the required alignment of the atomic type. */
6574 if (TYPE_ALIGN (atomic_type) > TYPE_ALIGN (t))
6575 TYPE_ALIGN (t) = TYPE_ALIGN (atomic_type);
6579 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6580 /* Propagate structural equality. */
6581 SET_TYPE_STRUCTURAL_EQUALITY (t);
6582 else if (TYPE_CANONICAL (type) != type)
6583 /* Build the underlying canonical type, since it is different
6584 from TYPE. */
6586 tree c = build_qualified_type (TYPE_CANONICAL (type), type_quals);
6587 TYPE_CANONICAL (t) = TYPE_CANONICAL (c);
6589 else
6590 /* T is its own canonical type. */
6591 TYPE_CANONICAL (t) = t;
6595 return t;
6598 /* Create a variant of type T with alignment ALIGN. */
6600 tree
6601 build_aligned_type (tree type, unsigned int align)
6603 tree t;
6605 if (TYPE_PACKED (type)
6606 || TYPE_ALIGN (type) == align)
6607 return type;
6609 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6610 if (check_aligned_type (t, type, align))
6611 return t;
6613 t = build_variant_type_copy (type);
6614 TYPE_ALIGN (t) = align;
6616 return t;
6619 /* Create a new distinct copy of TYPE. The new type is made its own
6620 MAIN_VARIANT. If TYPE requires structural equality checks, the
6621 resulting type requires structural equality checks; otherwise, its
6622 TYPE_CANONICAL points to itself. */
6624 tree
6625 build_distinct_type_copy (tree type)
6627 tree t = copy_node (type);
6629 TYPE_POINTER_TO (t) = 0;
6630 TYPE_REFERENCE_TO (t) = 0;
6632 /* Set the canonical type either to a new equivalence class, or
6633 propagate the need for structural equality checks. */
6634 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6635 SET_TYPE_STRUCTURAL_EQUALITY (t);
6636 else
6637 TYPE_CANONICAL (t) = t;
6639 /* Make it its own variant. */
6640 TYPE_MAIN_VARIANT (t) = t;
6641 TYPE_NEXT_VARIANT (t) = 0;
6643 /* We do not record methods in type copies nor variants
6644 so we do not need to keep them up to date when new method
6645 is inserted. */
6646 if (RECORD_OR_UNION_TYPE_P (t))
6647 TYPE_METHODS (t) = NULL_TREE;
6649 /* Note that it is now possible for TYPE_MIN_VALUE to be a value
6650 whose TREE_TYPE is not t. This can also happen in the Ada
6651 frontend when using subtypes. */
6653 return t;
6656 /* Create a new variant of TYPE, equivalent but distinct. This is so
6657 the caller can modify it. TYPE_CANONICAL for the return type will
6658 be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
6659 are considered equal by the language itself (or that both types
6660 require structural equality checks). */
6662 tree
6663 build_variant_type_copy (tree type)
6665 tree t, m = TYPE_MAIN_VARIANT (type);
6667 t = build_distinct_type_copy (type);
6669 /* Since we're building a variant, assume that it is a non-semantic
6670 variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
6671 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
6673 /* Add the new type to the chain of variants of TYPE. */
6674 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
6675 TYPE_NEXT_VARIANT (m) = t;
6676 TYPE_MAIN_VARIANT (t) = m;
6678 return t;
6681 /* Return true if the from tree in both tree maps are equal. */
6684 tree_map_base_eq (const void *va, const void *vb)
6686 const struct tree_map_base *const a = (const struct tree_map_base *) va,
6687 *const b = (const struct tree_map_base *) vb;
6688 return (a->from == b->from);
6691 /* Hash a from tree in a tree_base_map. */
6693 unsigned int
6694 tree_map_base_hash (const void *item)
6696 return htab_hash_pointer (((const struct tree_map_base *)item)->from);
6699 /* Return true if this tree map structure is marked for garbage collection
6700 purposes. We simply return true if the from tree is marked, so that this
6701 structure goes away when the from tree goes away. */
6704 tree_map_base_marked_p (const void *p)
6706 return ggc_marked_p (((const struct tree_map_base *) p)->from);
6709 /* Hash a from tree in a tree_map. */
6711 unsigned int
6712 tree_map_hash (const void *item)
6714 return (((const struct tree_map *) item)->hash);
6717 /* Hash a from tree in a tree_decl_map. */
6719 unsigned int
6720 tree_decl_map_hash (const void *item)
6722 return DECL_UID (((const struct tree_decl_map *) item)->base.from);
6725 /* Return the initialization priority for DECL. */
6727 priority_type
6728 decl_init_priority_lookup (tree decl)
6730 symtab_node *snode = symtab_node::get (decl);
6732 if (!snode)
6733 return DEFAULT_INIT_PRIORITY;
6734 return
6735 snode->get_init_priority ();
6738 /* Return the finalization priority for DECL. */
6740 priority_type
6741 decl_fini_priority_lookup (tree decl)
6743 cgraph_node *node = cgraph_node::get (decl);
6745 if (!node)
6746 return DEFAULT_INIT_PRIORITY;
6747 return
6748 node->get_fini_priority ();
6751 /* Set the initialization priority for DECL to PRIORITY. */
6753 void
6754 decl_init_priority_insert (tree decl, priority_type priority)
6756 struct symtab_node *snode;
6758 if (priority == DEFAULT_INIT_PRIORITY)
6760 snode = symtab_node::get (decl);
6761 if (!snode)
6762 return;
6764 else if (TREE_CODE (decl) == VAR_DECL)
6765 snode = varpool_node::get_create (decl);
6766 else
6767 snode = cgraph_node::get_create (decl);
6768 snode->set_init_priority (priority);
6771 /* Set the finalization priority for DECL to PRIORITY. */
6773 void
6774 decl_fini_priority_insert (tree decl, priority_type priority)
6776 struct cgraph_node *node;
6778 if (priority == DEFAULT_INIT_PRIORITY)
6780 node = cgraph_node::get (decl);
6781 if (!node)
6782 return;
6784 else
6785 node = cgraph_node::get_create (decl);
6786 node->set_fini_priority (priority);
6789 /* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
6791 static void
6792 print_debug_expr_statistics (void)
6794 fprintf (stderr, "DECL_DEBUG_EXPR hash: size %ld, %ld elements, %f collisions\n",
6795 (long) debug_expr_for_decl->size (),
6796 (long) debug_expr_for_decl->elements (),
6797 debug_expr_for_decl->collisions ());
6800 /* Print out the statistics for the DECL_VALUE_EXPR hash table. */
6802 static void
6803 print_value_expr_statistics (void)
6805 fprintf (stderr, "DECL_VALUE_EXPR hash: size %ld, %ld elements, %f collisions\n",
6806 (long) value_expr_for_decl->size (),
6807 (long) value_expr_for_decl->elements (),
6808 value_expr_for_decl->collisions ());
6811 /* Lookup a debug expression for FROM, and return it if we find one. */
6813 tree
6814 decl_debug_expr_lookup (tree from)
6816 struct tree_decl_map *h, in;
6817 in.base.from = from;
6819 h = debug_expr_for_decl->find_with_hash (&in, DECL_UID (from));
6820 if (h)
6821 return h->to;
6822 return NULL_TREE;
6825 /* Insert a mapping FROM->TO in the debug expression hashtable. */
6827 void
6828 decl_debug_expr_insert (tree from, tree to)
6830 struct tree_decl_map *h;
6832 h = ggc_alloc<tree_decl_map> ();
6833 h->base.from = from;
6834 h->to = to;
6835 *debug_expr_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT) = h;
6838 /* Lookup a value expression for FROM, and return it if we find one. */
6840 tree
6841 decl_value_expr_lookup (tree from)
6843 struct tree_decl_map *h, in;
6844 in.base.from = from;
6846 h = value_expr_for_decl->find_with_hash (&in, DECL_UID (from));
6847 if (h)
6848 return h->to;
6849 return NULL_TREE;
6852 /* Insert a mapping FROM->TO in the value expression hashtable. */
6854 void
6855 decl_value_expr_insert (tree from, tree to)
6857 struct tree_decl_map *h;
6859 h = ggc_alloc<tree_decl_map> ();
6860 h->base.from = from;
6861 h->to = to;
6862 *value_expr_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT) = h;
6865 /* Lookup a vector of debug arguments for FROM, and return it if we
6866 find one. */
6868 vec<tree, va_gc> **
6869 decl_debug_args_lookup (tree from)
6871 struct tree_vec_map *h, in;
6873 if (!DECL_HAS_DEBUG_ARGS_P (from))
6874 return NULL;
6875 gcc_checking_assert (debug_args_for_decl != NULL);
6876 in.base.from = from;
6877 h = debug_args_for_decl->find_with_hash (&in, DECL_UID (from));
6878 if (h)
6879 return &h->to;
6880 return NULL;
6883 /* Insert a mapping FROM->empty vector of debug arguments in the value
6884 expression hashtable. */
6886 vec<tree, va_gc> **
6887 decl_debug_args_insert (tree from)
6889 struct tree_vec_map *h;
6890 tree_vec_map **loc;
6892 if (DECL_HAS_DEBUG_ARGS_P (from))
6893 return decl_debug_args_lookup (from);
6894 if (debug_args_for_decl == NULL)
6895 debug_args_for_decl = hash_table<tree_vec_map_cache_hasher>::create_ggc (64);
6896 h = ggc_alloc<tree_vec_map> ();
6897 h->base.from = from;
6898 h->to = NULL;
6899 loc = debug_args_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT);
6900 *loc = h;
6901 DECL_HAS_DEBUG_ARGS_P (from) = 1;
6902 return &h->to;
6905 /* Hashing of types so that we don't make duplicates.
6906 The entry point is `type_hash_canon'. */
6908 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
6909 with types in the TREE_VALUE slots), by adding the hash codes
6910 of the individual types. */
6912 static void
6913 type_hash_list (const_tree list, inchash::hash &hstate)
6915 const_tree tail;
6917 for (tail = list; tail; tail = TREE_CHAIN (tail))
6918 if (TREE_VALUE (tail) != error_mark_node)
6919 hstate.add_object (TYPE_HASH (TREE_VALUE (tail)));
6922 /* These are the Hashtable callback functions. */
6924 /* Returns true iff the types are equivalent. */
6926 bool
6927 type_cache_hasher::equal (type_hash *a, type_hash *b)
6929 /* First test the things that are the same for all types. */
6930 if (a->hash != b->hash
6931 || TREE_CODE (a->type) != TREE_CODE (b->type)
6932 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
6933 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
6934 TYPE_ATTRIBUTES (b->type))
6935 || (TREE_CODE (a->type) != COMPLEX_TYPE
6936 && TYPE_NAME (a->type) != TYPE_NAME (b->type)))
6937 return 0;
6939 /* Be careful about comparing arrays before and after the element type
6940 has been completed; don't compare TYPE_ALIGN unless both types are
6941 complete. */
6942 if (COMPLETE_TYPE_P (a->type) && COMPLETE_TYPE_P (b->type)
6943 && (TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
6944 || TYPE_MODE (a->type) != TYPE_MODE (b->type)))
6945 return 0;
6947 switch (TREE_CODE (a->type))
6949 case VOID_TYPE:
6950 case COMPLEX_TYPE:
6951 case POINTER_TYPE:
6952 case REFERENCE_TYPE:
6953 case NULLPTR_TYPE:
6954 return 1;
6956 case VECTOR_TYPE:
6957 return TYPE_VECTOR_SUBPARTS (a->type) == TYPE_VECTOR_SUBPARTS (b->type);
6959 case ENUMERAL_TYPE:
6960 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
6961 && !(TYPE_VALUES (a->type)
6962 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
6963 && TYPE_VALUES (b->type)
6964 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
6965 && type_list_equal (TYPE_VALUES (a->type),
6966 TYPE_VALUES (b->type))))
6967 return 0;
6969 /* ... fall through ... */
6971 case INTEGER_TYPE:
6972 case REAL_TYPE:
6973 case BOOLEAN_TYPE:
6974 if (TYPE_PRECISION (a->type) != TYPE_PRECISION (b->type))
6975 return false;
6976 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
6977 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
6978 TYPE_MAX_VALUE (b->type)))
6979 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
6980 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
6981 TYPE_MIN_VALUE (b->type))));
6983 case FIXED_POINT_TYPE:
6984 return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
6986 case OFFSET_TYPE:
6987 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
6989 case METHOD_TYPE:
6990 if (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
6991 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6992 || (TYPE_ARG_TYPES (a->type)
6993 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6994 && TYPE_ARG_TYPES (b->type)
6995 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6996 && type_list_equal (TYPE_ARG_TYPES (a->type),
6997 TYPE_ARG_TYPES (b->type)))))
6998 break;
6999 return 0;
7000 case ARRAY_TYPE:
7001 return TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type);
7003 case RECORD_TYPE:
7004 case UNION_TYPE:
7005 case QUAL_UNION_TYPE:
7006 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
7007 || (TYPE_FIELDS (a->type)
7008 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
7009 && TYPE_FIELDS (b->type)
7010 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
7011 && type_list_equal (TYPE_FIELDS (a->type),
7012 TYPE_FIELDS (b->type))));
7014 case FUNCTION_TYPE:
7015 if (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
7016 || (TYPE_ARG_TYPES (a->type)
7017 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
7018 && TYPE_ARG_TYPES (b->type)
7019 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
7020 && type_list_equal (TYPE_ARG_TYPES (a->type),
7021 TYPE_ARG_TYPES (b->type))))
7022 break;
7023 return 0;
7025 default:
7026 return 0;
7029 if (lang_hooks.types.type_hash_eq != NULL)
7030 return lang_hooks.types.type_hash_eq (a->type, b->type);
7032 return 1;
7035 /* Given TYPE, and HASHCODE its hash code, return the canonical
7036 object for an identical type if one already exists.
7037 Otherwise, return TYPE, and record it as the canonical object.
7039 To use this function, first create a type of the sort you want.
7040 Then compute its hash code from the fields of the type that
7041 make it different from other similar types.
7042 Then call this function and use the value. */
7044 tree
7045 type_hash_canon (unsigned int hashcode, tree type)
7047 type_hash in;
7048 type_hash **loc;
7050 /* The hash table only contains main variants, so ensure that's what we're
7051 being passed. */
7052 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
7054 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
7055 must call that routine before comparing TYPE_ALIGNs. */
7056 layout_type (type);
7058 in.hash = hashcode;
7059 in.type = type;
7061 loc = type_hash_table->find_slot_with_hash (&in, hashcode, INSERT);
7062 if (*loc)
7064 tree t1 = ((type_hash *) *loc)->type;
7065 gcc_assert (TYPE_MAIN_VARIANT (t1) == t1);
7066 if (GATHER_STATISTICS)
7068 tree_code_counts[(int) TREE_CODE (type)]--;
7069 tree_node_counts[(int) t_kind]--;
7070 tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type_non_common);
7072 return t1;
7074 else
7076 struct type_hash *h;
7078 h = ggc_alloc<type_hash> ();
7079 h->hash = hashcode;
7080 h->type = type;
7081 *loc = h;
7083 return type;
7087 static void
7088 print_type_hash_statistics (void)
7090 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
7091 (long) type_hash_table->size (),
7092 (long) type_hash_table->elements (),
7093 type_hash_table->collisions ());
7096 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
7097 with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
7098 by adding the hash codes of the individual attributes. */
7100 static void
7101 attribute_hash_list (const_tree list, inchash::hash &hstate)
7103 const_tree tail;
7105 for (tail = list; tail; tail = TREE_CHAIN (tail))
7106 /* ??? Do we want to add in TREE_VALUE too? */
7107 hstate.add_object (IDENTIFIER_HASH_VALUE (get_attribute_name (tail)));
7110 /* Given two lists of attributes, return true if list l2 is
7111 equivalent to l1. */
7114 attribute_list_equal (const_tree l1, const_tree l2)
7116 if (l1 == l2)
7117 return 1;
7119 return attribute_list_contained (l1, l2)
7120 && attribute_list_contained (l2, l1);
7123 /* Given two lists of attributes, return true if list L2 is
7124 completely contained within L1. */
7125 /* ??? This would be faster if attribute names were stored in a canonicalized
7126 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
7127 must be used to show these elements are equivalent (which they are). */
7128 /* ??? It's not clear that attributes with arguments will always be handled
7129 correctly. */
7132 attribute_list_contained (const_tree l1, const_tree l2)
7134 const_tree t1, t2;
7136 /* First check the obvious, maybe the lists are identical. */
7137 if (l1 == l2)
7138 return 1;
7140 /* Maybe the lists are similar. */
7141 for (t1 = l1, t2 = l2;
7142 t1 != 0 && t2 != 0
7143 && get_attribute_name (t1) == get_attribute_name (t2)
7144 && TREE_VALUE (t1) == TREE_VALUE (t2);
7145 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
7148 /* Maybe the lists are equal. */
7149 if (t1 == 0 && t2 == 0)
7150 return 1;
7152 for (; t2 != 0; t2 = TREE_CHAIN (t2))
7154 const_tree attr;
7155 /* This CONST_CAST is okay because lookup_attribute does not
7156 modify its argument and the return value is assigned to a
7157 const_tree. */
7158 for (attr = lookup_ident_attribute (get_attribute_name (t2),
7159 CONST_CAST_TREE (l1));
7160 attr != NULL_TREE && !attribute_value_equal (t2, attr);
7161 attr = lookup_ident_attribute (get_attribute_name (t2),
7162 TREE_CHAIN (attr)))
7165 if (attr == NULL_TREE)
7166 return 0;
7169 return 1;
7172 /* Given two lists of types
7173 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
7174 return 1 if the lists contain the same types in the same order.
7175 Also, the TREE_PURPOSEs must match. */
7178 type_list_equal (const_tree l1, const_tree l2)
7180 const_tree t1, t2;
7182 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
7183 if (TREE_VALUE (t1) != TREE_VALUE (t2)
7184 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
7185 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
7186 && (TREE_TYPE (TREE_PURPOSE (t1))
7187 == TREE_TYPE (TREE_PURPOSE (t2))))))
7188 return 0;
7190 return t1 == t2;
7193 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
7194 given by TYPE. If the argument list accepts variable arguments,
7195 then this function counts only the ordinary arguments. */
7198 type_num_arguments (const_tree type)
7200 int i = 0;
7201 tree t;
7203 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
7204 /* If the function does not take a variable number of arguments,
7205 the last element in the list will have type `void'. */
7206 if (VOID_TYPE_P (TREE_VALUE (t)))
7207 break;
7208 else
7209 ++i;
7211 return i;
7214 /* Nonzero if integer constants T1 and T2
7215 represent the same constant value. */
7218 tree_int_cst_equal (const_tree t1, const_tree t2)
7220 if (t1 == t2)
7221 return 1;
7223 if (t1 == 0 || t2 == 0)
7224 return 0;
7226 if (TREE_CODE (t1) == INTEGER_CST
7227 && TREE_CODE (t2) == INTEGER_CST
7228 && wi::to_widest (t1) == wi::to_widest (t2))
7229 return 1;
7231 return 0;
7234 /* Return true if T is an INTEGER_CST whose numerical value (extended
7235 according to TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. */
7237 bool
7238 tree_fits_shwi_p (const_tree t)
7240 return (t != NULL_TREE
7241 && TREE_CODE (t) == INTEGER_CST
7242 && wi::fits_shwi_p (wi::to_widest (t)));
7245 /* Return true if T is an INTEGER_CST whose numerical value (extended
7246 according to TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. */
7248 bool
7249 tree_fits_uhwi_p (const_tree t)
7251 return (t != NULL_TREE
7252 && TREE_CODE (t) == INTEGER_CST
7253 && wi::fits_uhwi_p (wi::to_widest (t)));
7256 /* T is an INTEGER_CST whose numerical value (extended according to
7257 TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. Return that
7258 HOST_WIDE_INT. */
7260 HOST_WIDE_INT
7261 tree_to_shwi (const_tree t)
7263 gcc_assert (tree_fits_shwi_p (t));
7264 return TREE_INT_CST_LOW (t);
7267 /* T is an INTEGER_CST whose numerical value (extended according to
7268 TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. Return that
7269 HOST_WIDE_INT. */
7271 unsigned HOST_WIDE_INT
7272 tree_to_uhwi (const_tree t)
7274 gcc_assert (tree_fits_uhwi_p (t));
7275 return TREE_INT_CST_LOW (t);
7278 /* Return the most significant (sign) bit of T. */
7281 tree_int_cst_sign_bit (const_tree t)
7283 unsigned bitno = TYPE_PRECISION (TREE_TYPE (t)) - 1;
7285 return wi::extract_uhwi (t, bitno, 1);
7288 /* Return an indication of the sign of the integer constant T.
7289 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
7290 Note that -1 will never be returned if T's type is unsigned. */
7293 tree_int_cst_sgn (const_tree t)
7295 if (wi::eq_p (t, 0))
7296 return 0;
7297 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
7298 return 1;
7299 else if (wi::neg_p (t))
7300 return -1;
7301 else
7302 return 1;
7305 /* Return the minimum number of bits needed to represent VALUE in a
7306 signed or unsigned type, UNSIGNEDP says which. */
7308 unsigned int
7309 tree_int_cst_min_precision (tree value, signop sgn)
7311 /* If the value is negative, compute its negative minus 1. The latter
7312 adjustment is because the absolute value of the largest negative value
7313 is one larger than the largest positive value. This is equivalent to
7314 a bit-wise negation, so use that operation instead. */
7316 if (tree_int_cst_sgn (value) < 0)
7317 value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
7319 /* Return the number of bits needed, taking into account the fact
7320 that we need one more bit for a signed than unsigned type.
7321 If value is 0 or -1, the minimum precision is 1 no matter
7322 whether unsignedp is true or false. */
7324 if (integer_zerop (value))
7325 return 1;
7326 else
7327 return tree_floor_log2 (value) + 1 + (sgn == SIGNED ? 1 : 0) ;
7330 /* Return truthvalue of whether T1 is the same tree structure as T2.
7331 Return 1 if they are the same.
7332 Return 0 if they are understandably different.
7333 Return -1 if either contains tree structure not understood by
7334 this function. */
7337 simple_cst_equal (const_tree t1, const_tree t2)
7339 enum tree_code code1, code2;
7340 int cmp;
7341 int i;
7343 if (t1 == t2)
7344 return 1;
7345 if (t1 == 0 || t2 == 0)
7346 return 0;
7348 code1 = TREE_CODE (t1);
7349 code2 = TREE_CODE (t2);
7351 if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
7353 if (CONVERT_EXPR_CODE_P (code2)
7354 || code2 == NON_LVALUE_EXPR)
7355 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7356 else
7357 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
7360 else if (CONVERT_EXPR_CODE_P (code2)
7361 || code2 == NON_LVALUE_EXPR)
7362 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
7364 if (code1 != code2)
7365 return 0;
7367 switch (code1)
7369 case INTEGER_CST:
7370 return wi::to_widest (t1) == wi::to_widest (t2);
7372 case REAL_CST:
7373 return real_identical (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
7375 case FIXED_CST:
7376 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
7378 case STRING_CST:
7379 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
7380 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
7381 TREE_STRING_LENGTH (t1)));
7383 case CONSTRUCTOR:
7385 unsigned HOST_WIDE_INT idx;
7386 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (t1);
7387 vec<constructor_elt, va_gc> *v2 = CONSTRUCTOR_ELTS (t2);
7389 if (vec_safe_length (v1) != vec_safe_length (v2))
7390 return false;
7392 for (idx = 0; idx < vec_safe_length (v1); ++idx)
7393 /* ??? Should we handle also fields here? */
7394 if (!simple_cst_equal ((*v1)[idx].value, (*v2)[idx].value))
7395 return false;
7396 return true;
7399 case SAVE_EXPR:
7400 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7402 case CALL_EXPR:
7403 cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
7404 if (cmp <= 0)
7405 return cmp;
7406 if (call_expr_nargs (t1) != call_expr_nargs (t2))
7407 return 0;
7409 const_tree arg1, arg2;
7410 const_call_expr_arg_iterator iter1, iter2;
7411 for (arg1 = first_const_call_expr_arg (t1, &iter1),
7412 arg2 = first_const_call_expr_arg (t2, &iter2);
7413 arg1 && arg2;
7414 arg1 = next_const_call_expr_arg (&iter1),
7415 arg2 = next_const_call_expr_arg (&iter2))
7417 cmp = simple_cst_equal (arg1, arg2);
7418 if (cmp <= 0)
7419 return cmp;
7421 return arg1 == arg2;
7424 case TARGET_EXPR:
7425 /* Special case: if either target is an unallocated VAR_DECL,
7426 it means that it's going to be unified with whatever the
7427 TARGET_EXPR is really supposed to initialize, so treat it
7428 as being equivalent to anything. */
7429 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
7430 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
7431 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
7432 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
7433 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
7434 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
7435 cmp = 1;
7436 else
7437 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7439 if (cmp <= 0)
7440 return cmp;
7442 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
7444 case WITH_CLEANUP_EXPR:
7445 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7446 if (cmp <= 0)
7447 return cmp;
7449 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
7451 case COMPONENT_REF:
7452 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
7453 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7455 return 0;
7457 case VAR_DECL:
7458 case PARM_DECL:
7459 case CONST_DECL:
7460 case FUNCTION_DECL:
7461 return 0;
7463 default:
7464 break;
7467 /* This general rule works for most tree codes. All exceptions should be
7468 handled above. If this is a language-specific tree code, we can't
7469 trust what might be in the operand, so say we don't know
7470 the situation. */
7471 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
7472 return -1;
7474 switch (TREE_CODE_CLASS (code1))
7476 case tcc_unary:
7477 case tcc_binary:
7478 case tcc_comparison:
7479 case tcc_expression:
7480 case tcc_reference:
7481 case tcc_statement:
7482 cmp = 1;
7483 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
7485 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
7486 if (cmp <= 0)
7487 return cmp;
7490 return cmp;
7492 default:
7493 return -1;
7497 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
7498 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
7499 than U, respectively. */
7502 compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
7504 if (tree_int_cst_sgn (t) < 0)
7505 return -1;
7506 else if (!tree_fits_uhwi_p (t))
7507 return 1;
7508 else if (TREE_INT_CST_LOW (t) == u)
7509 return 0;
7510 else if (TREE_INT_CST_LOW (t) < u)
7511 return -1;
7512 else
7513 return 1;
7516 /* Return true if SIZE represents a constant size that is in bounds of
7517 what the middle-end and the backend accepts (covering not more than
7518 half of the address-space). */
7520 bool
7521 valid_constant_size_p (const_tree size)
7523 if (! tree_fits_uhwi_p (size)
7524 || TREE_OVERFLOW (size)
7525 || tree_int_cst_sign_bit (size) != 0)
7526 return false;
7527 return true;
7530 /* Return the precision of the type, or for a complex or vector type the
7531 precision of the type of its elements. */
7533 unsigned int
7534 element_precision (const_tree type)
7536 if (!TYPE_P (type))
7537 type = TREE_TYPE (type);
7538 enum tree_code code = TREE_CODE (type);
7539 if (code == COMPLEX_TYPE || code == VECTOR_TYPE)
7540 type = TREE_TYPE (type);
7542 return TYPE_PRECISION (type);
7545 /* Return true if CODE represents an associative tree code. Otherwise
7546 return false. */
7547 bool
7548 associative_tree_code (enum tree_code code)
7550 switch (code)
7552 case BIT_IOR_EXPR:
7553 case BIT_AND_EXPR:
7554 case BIT_XOR_EXPR:
7555 case PLUS_EXPR:
7556 case MULT_EXPR:
7557 case MIN_EXPR:
7558 case MAX_EXPR:
7559 return true;
7561 default:
7562 break;
7564 return false;
7567 /* Return true if CODE represents a commutative tree code. Otherwise
7568 return false. */
7569 bool
7570 commutative_tree_code (enum tree_code code)
7572 switch (code)
7574 case PLUS_EXPR:
7575 case MULT_EXPR:
7576 case MULT_HIGHPART_EXPR:
7577 case MIN_EXPR:
7578 case MAX_EXPR:
7579 case BIT_IOR_EXPR:
7580 case BIT_XOR_EXPR:
7581 case BIT_AND_EXPR:
7582 case NE_EXPR:
7583 case EQ_EXPR:
7584 case UNORDERED_EXPR:
7585 case ORDERED_EXPR:
7586 case UNEQ_EXPR:
7587 case LTGT_EXPR:
7588 case TRUTH_AND_EXPR:
7589 case TRUTH_XOR_EXPR:
7590 case TRUTH_OR_EXPR:
7591 case WIDEN_MULT_EXPR:
7592 case VEC_WIDEN_MULT_HI_EXPR:
7593 case VEC_WIDEN_MULT_LO_EXPR:
7594 case VEC_WIDEN_MULT_EVEN_EXPR:
7595 case VEC_WIDEN_MULT_ODD_EXPR:
7596 return true;
7598 default:
7599 break;
7601 return false;
7604 /* Return true if CODE represents a ternary tree code for which the
7605 first two operands are commutative. Otherwise return false. */
7606 bool
7607 commutative_ternary_tree_code (enum tree_code code)
7609 switch (code)
7611 case WIDEN_MULT_PLUS_EXPR:
7612 case WIDEN_MULT_MINUS_EXPR:
7613 case DOT_PROD_EXPR:
7614 case FMA_EXPR:
7615 return true;
7617 default:
7618 break;
7620 return false;
7623 /* Returns true if CODE can overflow. */
7625 bool
7626 operation_can_overflow (enum tree_code code)
7628 switch (code)
7630 case PLUS_EXPR:
7631 case MINUS_EXPR:
7632 case MULT_EXPR:
7633 case LSHIFT_EXPR:
7634 /* Can overflow in various ways. */
7635 return true;
7636 case TRUNC_DIV_EXPR:
7637 case EXACT_DIV_EXPR:
7638 case FLOOR_DIV_EXPR:
7639 case CEIL_DIV_EXPR:
7640 /* For INT_MIN / -1. */
7641 return true;
7642 case NEGATE_EXPR:
7643 case ABS_EXPR:
7644 /* For -INT_MIN. */
7645 return true;
7646 default:
7647 /* These operators cannot overflow. */
7648 return false;
7652 /* Returns true if CODE operating on operands of type TYPE doesn't overflow, or
7653 ftrapv doesn't generate trapping insns for CODE. */
7655 bool
7656 operation_no_trapping_overflow (tree type, enum tree_code code)
7658 gcc_checking_assert (ANY_INTEGRAL_TYPE_P (type));
7660 /* We don't generate instructions that trap on overflow for complex or vector
7661 types. */
7662 if (!INTEGRAL_TYPE_P (type))
7663 return true;
7665 if (!TYPE_OVERFLOW_TRAPS (type))
7666 return true;
7668 switch (code)
7670 case PLUS_EXPR:
7671 case MINUS_EXPR:
7672 case MULT_EXPR:
7673 case NEGATE_EXPR:
7674 case ABS_EXPR:
7675 /* These operators can overflow, and -ftrapv generates trapping code for
7676 these. */
7677 return false;
7678 case TRUNC_DIV_EXPR:
7679 case EXACT_DIV_EXPR:
7680 case FLOOR_DIV_EXPR:
7681 case CEIL_DIV_EXPR:
7682 case LSHIFT_EXPR:
7683 /* These operators can overflow, but -ftrapv does not generate trapping
7684 code for these. */
7685 return true;
7686 default:
7687 /* These operators cannot overflow. */
7688 return true;
7692 namespace inchash
7695 /* Generate a hash value for an expression. This can be used iteratively
7696 by passing a previous result as the HSTATE argument.
7698 This function is intended to produce the same hash for expressions which
7699 would compare equal using operand_equal_p. */
7700 void
7701 add_expr (const_tree t, inchash::hash &hstate)
7703 int i;
7704 enum tree_code code;
7705 enum tree_code_class tclass;
7707 if (t == NULL_TREE)
7709 hstate.merge_hash (0);
7710 return;
7713 code = TREE_CODE (t);
7715 switch (code)
7717 /* Alas, constants aren't shared, so we can't rely on pointer
7718 identity. */
7719 case VOID_CST:
7720 hstate.merge_hash (0);
7721 return;
7722 case INTEGER_CST:
7723 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
7724 hstate.add_wide_int (TREE_INT_CST_ELT (t, i));
7725 return;
7726 case REAL_CST:
7728 unsigned int val2 = real_hash (TREE_REAL_CST_PTR (t));
7729 hstate.merge_hash (val2);
7730 return;
7732 case FIXED_CST:
7734 unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
7735 hstate.merge_hash (val2);
7736 return;
7738 case STRING_CST:
7739 hstate.add ((const void *) TREE_STRING_POINTER (t), TREE_STRING_LENGTH (t));
7740 return;
7741 case COMPLEX_CST:
7742 inchash::add_expr (TREE_REALPART (t), hstate);
7743 inchash::add_expr (TREE_IMAGPART (t), hstate);
7744 return;
7745 case VECTOR_CST:
7747 unsigned i;
7748 for (i = 0; i < VECTOR_CST_NELTS (t); ++i)
7749 inchash::add_expr (VECTOR_CST_ELT (t, i), hstate);
7750 return;
7752 case SSA_NAME:
7753 /* We can just compare by pointer. */
7754 hstate.add_wide_int (SSA_NAME_VERSION (t));
7755 return;
7756 case PLACEHOLDER_EXPR:
7757 /* The node itself doesn't matter. */
7758 return;
7759 case TREE_LIST:
7760 /* A list of expressions, for a CALL_EXPR or as the elements of a
7761 VECTOR_CST. */
7762 for (; t; t = TREE_CHAIN (t))
7763 inchash::add_expr (TREE_VALUE (t), hstate);
7764 return;
7765 case CONSTRUCTOR:
7767 unsigned HOST_WIDE_INT idx;
7768 tree field, value;
7769 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
7771 inchash::add_expr (field, hstate);
7772 inchash::add_expr (value, hstate);
7774 return;
7776 case FUNCTION_DECL:
7777 /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
7778 Otherwise nodes that compare equal according to operand_equal_p might
7779 get different hash codes. However, don't do this for machine specific
7780 or front end builtins, since the function code is overloaded in those
7781 cases. */
7782 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
7783 && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
7785 t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
7786 code = TREE_CODE (t);
7788 /* FALL THROUGH */
7789 default:
7790 tclass = TREE_CODE_CLASS (code);
7792 if (tclass == tcc_declaration)
7794 /* DECL's have a unique ID */
7795 hstate.add_wide_int (DECL_UID (t));
7797 else
7799 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
7801 hstate.add_object (code);
7803 /* Don't hash the type, that can lead to having nodes which
7804 compare equal according to operand_equal_p, but which
7805 have different hash codes. */
7806 if (CONVERT_EXPR_CODE_P (code)
7807 || code == NON_LVALUE_EXPR)
7809 /* Make sure to include signness in the hash computation. */
7810 hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
7811 inchash::add_expr (TREE_OPERAND (t, 0), hstate);
7814 else if (commutative_tree_code (code))
7816 /* It's a commutative expression. We want to hash it the same
7817 however it appears. We do this by first hashing both operands
7818 and then rehashing based on the order of their independent
7819 hashes. */
7820 inchash::hash one, two;
7821 inchash::add_expr (TREE_OPERAND (t, 0), one);
7822 inchash::add_expr (TREE_OPERAND (t, 1), two);
7823 hstate.add_commutative (one, two);
7825 else
7826 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
7827 inchash::add_expr (TREE_OPERAND (t, i), hstate);
7829 return;
7835 /* Constructors for pointer, array and function types.
7836 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
7837 constructed by language-dependent code, not here.) */
7839 /* Construct, lay out and return the type of pointers to TO_TYPE with
7840 mode MODE. If CAN_ALIAS_ALL is TRUE, indicate this type can
7841 reference all of memory. If such a type has already been
7842 constructed, reuse it. */
7844 tree
7845 build_pointer_type_for_mode (tree to_type, machine_mode mode,
7846 bool can_alias_all)
7848 tree t;
7849 bool could_alias = can_alias_all;
7851 if (to_type == error_mark_node)
7852 return error_mark_node;
7854 /* If the pointed-to type has the may_alias attribute set, force
7855 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7856 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7857 can_alias_all = true;
7859 /* In some cases, languages will have things that aren't a POINTER_TYPE
7860 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
7861 In that case, return that type without regard to the rest of our
7862 operands.
7864 ??? This is a kludge, but consistent with the way this function has
7865 always operated and there doesn't seem to be a good way to avoid this
7866 at the moment. */
7867 if (TYPE_POINTER_TO (to_type) != 0
7868 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
7869 return TYPE_POINTER_TO (to_type);
7871 /* First, if we already have a type for pointers to TO_TYPE and it's
7872 the proper mode, use it. */
7873 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
7874 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7875 return t;
7877 t = make_node (POINTER_TYPE);
7879 TREE_TYPE (t) = to_type;
7880 SET_TYPE_MODE (t, mode);
7881 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7882 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
7883 TYPE_POINTER_TO (to_type) = t;
7885 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7886 SET_TYPE_STRUCTURAL_EQUALITY (t);
7887 else if (TYPE_CANONICAL (to_type) != to_type || could_alias)
7888 TYPE_CANONICAL (t)
7889 = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
7890 mode, false);
7892 /* Lay out the type. This function has many callers that are concerned
7893 with expression-construction, and this simplifies them all. */
7894 layout_type (t);
7896 return t;
7899 /* By default build pointers in ptr_mode. */
7901 tree
7902 build_pointer_type (tree to_type)
7904 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7905 : TYPE_ADDR_SPACE (to_type);
7906 machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7907 return build_pointer_type_for_mode (to_type, pointer_mode, false);
7910 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
7912 tree
7913 build_reference_type_for_mode (tree to_type, machine_mode mode,
7914 bool can_alias_all)
7916 tree t;
7917 bool could_alias = can_alias_all;
7919 if (to_type == error_mark_node)
7920 return error_mark_node;
7922 /* If the pointed-to type has the may_alias attribute set, force
7923 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7924 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7925 can_alias_all = true;
7927 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
7928 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
7929 In that case, return that type without regard to the rest of our
7930 operands.
7932 ??? This is a kludge, but consistent with the way this function has
7933 always operated and there doesn't seem to be a good way to avoid this
7934 at the moment. */
7935 if (TYPE_REFERENCE_TO (to_type) != 0
7936 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
7937 return TYPE_REFERENCE_TO (to_type);
7939 /* First, if we already have a type for pointers to TO_TYPE and it's
7940 the proper mode, use it. */
7941 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
7942 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7943 return t;
7945 t = make_node (REFERENCE_TYPE);
7947 TREE_TYPE (t) = to_type;
7948 SET_TYPE_MODE (t, mode);
7949 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7950 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
7951 TYPE_REFERENCE_TO (to_type) = t;
7953 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7954 SET_TYPE_STRUCTURAL_EQUALITY (t);
7955 else if (TYPE_CANONICAL (to_type) != to_type || could_alias)
7956 TYPE_CANONICAL (t)
7957 = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
7958 mode, false);
7960 layout_type (t);
7962 return t;
7966 /* Build the node for the type of references-to-TO_TYPE by default
7967 in ptr_mode. */
7969 tree
7970 build_reference_type (tree to_type)
7972 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7973 : TYPE_ADDR_SPACE (to_type);
7974 machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7975 return build_reference_type_for_mode (to_type, pointer_mode, false);
7978 #define MAX_INT_CACHED_PREC \
7979 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
7980 static GTY(()) tree nonstandard_integer_type_cache[2 * MAX_INT_CACHED_PREC + 2];
7982 /* Builds a signed or unsigned integer type of precision PRECISION.
7983 Used for C bitfields whose precision does not match that of
7984 built-in target types. */
7985 tree
7986 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
7987 int unsignedp)
7989 tree itype, ret;
7991 if (unsignedp)
7992 unsignedp = MAX_INT_CACHED_PREC + 1;
7994 if (precision <= MAX_INT_CACHED_PREC)
7996 itype = nonstandard_integer_type_cache[precision + unsignedp];
7997 if (itype)
7998 return itype;
8001 itype = make_node (INTEGER_TYPE);
8002 TYPE_PRECISION (itype) = precision;
8004 if (unsignedp)
8005 fixup_unsigned_type (itype);
8006 else
8007 fixup_signed_type (itype);
8009 ret = itype;
8010 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (itype)))
8011 ret = type_hash_canon (tree_to_uhwi (TYPE_MAX_VALUE (itype)), itype);
8012 if (precision <= MAX_INT_CACHED_PREC)
8013 nonstandard_integer_type_cache[precision + unsignedp] = ret;
8015 return ret;
8018 /* Create a range of some discrete type TYPE (an INTEGER_TYPE, ENUMERAL_TYPE
8019 or BOOLEAN_TYPE) with low bound LOWVAL and high bound HIGHVAL. If SHARED
8020 is true, reuse such a type that has already been constructed. */
8022 static tree
8023 build_range_type_1 (tree type, tree lowval, tree highval, bool shared)
8025 tree itype = make_node (INTEGER_TYPE);
8026 inchash::hash hstate;
8028 TREE_TYPE (itype) = type;
8030 TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
8031 TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
8033 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
8034 SET_TYPE_MODE (itype, TYPE_MODE (type));
8035 TYPE_SIZE (itype) = TYPE_SIZE (type);
8036 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
8037 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
8038 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
8040 if (!shared)
8041 return itype;
8043 if ((TYPE_MIN_VALUE (itype)
8044 && TREE_CODE (TYPE_MIN_VALUE (itype)) != INTEGER_CST)
8045 || (TYPE_MAX_VALUE (itype)
8046 && TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST))
8048 /* Since we cannot reliably merge this type, we need to compare it using
8049 structural equality checks. */
8050 SET_TYPE_STRUCTURAL_EQUALITY (itype);
8051 return itype;
8054 inchash::add_expr (TYPE_MIN_VALUE (itype), hstate);
8055 inchash::add_expr (TYPE_MAX_VALUE (itype), hstate);
8056 hstate.merge_hash (TYPE_HASH (type));
8057 itype = type_hash_canon (hstate.end (), itype);
8059 return itype;
8062 /* Wrapper around build_range_type_1 with SHARED set to true. */
8064 tree
8065 build_range_type (tree type, tree lowval, tree highval)
8067 return build_range_type_1 (type, lowval, highval, true);
8070 /* Wrapper around build_range_type_1 with SHARED set to false. */
8072 tree
8073 build_nonshared_range_type (tree type, tree lowval, tree highval)
8075 return build_range_type_1 (type, lowval, highval, false);
8078 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
8079 MAXVAL should be the maximum value in the domain
8080 (one less than the length of the array).
8082 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
8083 We don't enforce this limit, that is up to caller (e.g. language front end).
8084 The limit exists because the result is a signed type and we don't handle
8085 sizes that use more than one HOST_WIDE_INT. */
8087 tree
8088 build_index_type (tree maxval)
8090 return build_range_type (sizetype, size_zero_node, maxval);
8093 /* Return true if the debug information for TYPE, a subtype, should be emitted
8094 as a subrange type. If so, set LOWVAL to the low bound and HIGHVAL to the
8095 high bound, respectively. Sometimes doing so unnecessarily obfuscates the
8096 debug info and doesn't reflect the source code. */
8098 bool
8099 subrange_type_for_debug_p (const_tree type, tree *lowval, tree *highval)
8101 tree base_type = TREE_TYPE (type), low, high;
8103 /* Subrange types have a base type which is an integral type. */
8104 if (!INTEGRAL_TYPE_P (base_type))
8105 return false;
8107 /* Get the real bounds of the subtype. */
8108 if (lang_hooks.types.get_subrange_bounds)
8109 lang_hooks.types.get_subrange_bounds (type, &low, &high);
8110 else
8112 low = TYPE_MIN_VALUE (type);
8113 high = TYPE_MAX_VALUE (type);
8116 /* If the type and its base type have the same representation and the same
8117 name, then the type is not a subrange but a copy of the base type. */
8118 if ((TREE_CODE (base_type) == INTEGER_TYPE
8119 || TREE_CODE (base_type) == BOOLEAN_TYPE)
8120 && int_size_in_bytes (type) == int_size_in_bytes (base_type)
8121 && tree_int_cst_equal (low, TYPE_MIN_VALUE (base_type))
8122 && tree_int_cst_equal (high, TYPE_MAX_VALUE (base_type))
8123 && TYPE_IDENTIFIER (type) == TYPE_IDENTIFIER (base_type))
8124 return false;
8126 if (lowval)
8127 *lowval = low;
8128 if (highval)
8129 *highval = high;
8130 return true;
8133 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
8134 and number of elements specified by the range of values of INDEX_TYPE.
8135 If SHARED is true, reuse such a type that has already been constructed. */
8137 static tree
8138 build_array_type_1 (tree elt_type, tree index_type, bool shared)
8140 tree t;
8142 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
8144 error ("arrays of functions are not meaningful");
8145 elt_type = integer_type_node;
8148 t = make_node (ARRAY_TYPE);
8149 TREE_TYPE (t) = elt_type;
8150 TYPE_DOMAIN (t) = index_type;
8151 TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (elt_type);
8152 layout_type (t);
8154 /* If the element type is incomplete at this point we get marked for
8155 structural equality. Do not record these types in the canonical
8156 type hashtable. */
8157 if (TYPE_STRUCTURAL_EQUALITY_P (t))
8158 return t;
8160 if (shared)
8162 inchash::hash hstate;
8163 hstate.add_object (TYPE_HASH (elt_type));
8164 if (index_type)
8165 hstate.add_object (TYPE_HASH (index_type));
8166 t = type_hash_canon (hstate.end (), t);
8169 if (TYPE_CANONICAL (t) == t)
8171 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
8172 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
8173 SET_TYPE_STRUCTURAL_EQUALITY (t);
8174 else if (TYPE_CANONICAL (elt_type) != elt_type
8175 || (index_type && TYPE_CANONICAL (index_type) != index_type))
8176 TYPE_CANONICAL (t)
8177 = build_array_type_1 (TYPE_CANONICAL (elt_type),
8178 index_type
8179 ? TYPE_CANONICAL (index_type) : NULL_TREE,
8180 shared);
8183 return t;
8186 /* Wrapper around build_array_type_1 with SHARED set to true. */
8188 tree
8189 build_array_type (tree elt_type, tree index_type)
8191 return build_array_type_1 (elt_type, index_type, true);
8194 /* Wrapper around build_array_type_1 with SHARED set to false. */
8196 tree
8197 build_nonshared_array_type (tree elt_type, tree index_type)
8199 return build_array_type_1 (elt_type, index_type, false);
8202 /* Return a representation of ELT_TYPE[NELTS], using indices of type
8203 sizetype. */
8205 tree
8206 build_array_type_nelts (tree elt_type, unsigned HOST_WIDE_INT nelts)
8208 return build_array_type (elt_type, build_index_type (size_int (nelts - 1)));
8211 /* Recursively examines the array elements of TYPE, until a non-array
8212 element type is found. */
8214 tree
8215 strip_array_types (tree type)
8217 while (TREE_CODE (type) == ARRAY_TYPE)
8218 type = TREE_TYPE (type);
8220 return type;
8223 /* Computes the canonical argument types from the argument type list
8224 ARGTYPES.
8226 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
8227 on entry to this function, or if any of the ARGTYPES are
8228 structural.
8230 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
8231 true on entry to this function, or if any of the ARGTYPES are
8232 non-canonical.
8234 Returns a canonical argument list, which may be ARGTYPES when the
8235 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
8236 true) or would not differ from ARGTYPES. */
8238 static tree
8239 maybe_canonicalize_argtypes (tree argtypes,
8240 bool *any_structural_p,
8241 bool *any_noncanonical_p)
8243 tree arg;
8244 bool any_noncanonical_argtypes_p = false;
8246 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
8248 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
8249 /* Fail gracefully by stating that the type is structural. */
8250 *any_structural_p = true;
8251 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
8252 *any_structural_p = true;
8253 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
8254 || TREE_PURPOSE (arg))
8255 /* If the argument has a default argument, we consider it
8256 non-canonical even though the type itself is canonical.
8257 That way, different variants of function and method types
8258 with default arguments will all point to the variant with
8259 no defaults as their canonical type. */
8260 any_noncanonical_argtypes_p = true;
8263 if (*any_structural_p)
8264 return argtypes;
8266 if (any_noncanonical_argtypes_p)
8268 /* Build the canonical list of argument types. */
8269 tree canon_argtypes = NULL_TREE;
8270 bool is_void = false;
8272 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
8274 if (arg == void_list_node)
8275 is_void = true;
8276 else
8277 canon_argtypes = tree_cons (NULL_TREE,
8278 TYPE_CANONICAL (TREE_VALUE (arg)),
8279 canon_argtypes);
8282 canon_argtypes = nreverse (canon_argtypes);
8283 if (is_void)
8284 canon_argtypes = chainon (canon_argtypes, void_list_node);
8286 /* There is a non-canonical type. */
8287 *any_noncanonical_p = true;
8288 return canon_argtypes;
8291 /* The canonical argument types are the same as ARGTYPES. */
8292 return argtypes;
8295 /* Construct, lay out and return
8296 the type of functions returning type VALUE_TYPE
8297 given arguments of types ARG_TYPES.
8298 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
8299 are data type nodes for the arguments of the function.
8300 If such a type has already been constructed, reuse it. */
8302 tree
8303 build_function_type (tree value_type, tree arg_types)
8305 tree t;
8306 inchash::hash hstate;
8307 bool any_structural_p, any_noncanonical_p;
8308 tree canon_argtypes;
8310 if (TREE_CODE (value_type) == FUNCTION_TYPE)
8312 error ("function return type cannot be function");
8313 value_type = integer_type_node;
8316 /* Make a node of the sort we want. */
8317 t = make_node (FUNCTION_TYPE);
8318 TREE_TYPE (t) = value_type;
8319 TYPE_ARG_TYPES (t) = arg_types;
8321 /* If we already have such a type, use the old one. */
8322 hstate.add_object (TYPE_HASH (value_type));
8323 type_hash_list (arg_types, hstate);
8324 t = type_hash_canon (hstate.end (), t);
8326 /* Set up the canonical type. */
8327 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
8328 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
8329 canon_argtypes = maybe_canonicalize_argtypes (arg_types,
8330 &any_structural_p,
8331 &any_noncanonical_p);
8332 if (any_structural_p)
8333 SET_TYPE_STRUCTURAL_EQUALITY (t);
8334 else if (any_noncanonical_p)
8335 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
8336 canon_argtypes);
8338 if (!COMPLETE_TYPE_P (t))
8339 layout_type (t);
8340 return t;
8343 /* Build a function type. The RETURN_TYPE is the type returned by the
8344 function. If VAARGS is set, no void_type_node is appended to the
8345 the list. ARGP must be always be terminated be a NULL_TREE. */
8347 static tree
8348 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
8350 tree t, args, last;
8352 t = va_arg (argp, tree);
8353 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
8354 args = tree_cons (NULL_TREE, t, args);
8356 if (vaargs)
8358 last = args;
8359 if (args != NULL_TREE)
8360 args = nreverse (args);
8361 gcc_assert (last != void_list_node);
8363 else if (args == NULL_TREE)
8364 args = void_list_node;
8365 else
8367 last = args;
8368 args = nreverse (args);
8369 TREE_CHAIN (last) = void_list_node;
8371 args = build_function_type (return_type, args);
8373 return args;
8376 /* Build a function type. The RETURN_TYPE is the type returned by the
8377 function. If additional arguments are provided, they are
8378 additional argument types. The list of argument types must always
8379 be terminated by NULL_TREE. */
8381 tree
8382 build_function_type_list (tree return_type, ...)
8384 tree args;
8385 va_list p;
8387 va_start (p, return_type);
8388 args = build_function_type_list_1 (false, return_type, p);
8389 va_end (p);
8390 return args;
8393 /* Build a variable argument function type. The RETURN_TYPE is the
8394 type returned by the function. If additional arguments are provided,
8395 they are additional argument types. The list of argument types must
8396 always be terminated by NULL_TREE. */
8398 tree
8399 build_varargs_function_type_list (tree return_type, ...)
8401 tree args;
8402 va_list p;
8404 va_start (p, return_type);
8405 args = build_function_type_list_1 (true, return_type, p);
8406 va_end (p);
8408 return args;
8411 /* Build a function type. RETURN_TYPE is the type returned by the
8412 function; VAARGS indicates whether the function takes varargs. The
8413 function takes N named arguments, the types of which are provided in
8414 ARG_TYPES. */
8416 static tree
8417 build_function_type_array_1 (bool vaargs, tree return_type, int n,
8418 tree *arg_types)
8420 int i;
8421 tree t = vaargs ? NULL_TREE : void_list_node;
8423 for (i = n - 1; i >= 0; i--)
8424 t = tree_cons (NULL_TREE, arg_types[i], t);
8426 return build_function_type (return_type, t);
8429 /* Build a function type. RETURN_TYPE is the type returned by the
8430 function. The function takes N named arguments, the types of which
8431 are provided in ARG_TYPES. */
8433 tree
8434 build_function_type_array (tree return_type, int n, tree *arg_types)
8436 return build_function_type_array_1 (false, return_type, n, arg_types);
8439 /* Build a variable argument function type. RETURN_TYPE is the type
8440 returned by the function. The function takes N named arguments, the
8441 types of which are provided in ARG_TYPES. */
8443 tree
8444 build_varargs_function_type_array (tree return_type, int n, tree *arg_types)
8446 return build_function_type_array_1 (true, return_type, n, arg_types);
8449 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
8450 and ARGTYPES (a TREE_LIST) are the return type and arguments types
8451 for the method. An implicit additional parameter (of type
8452 pointer-to-BASETYPE) is added to the ARGTYPES. */
8454 tree
8455 build_method_type_directly (tree basetype,
8456 tree rettype,
8457 tree argtypes)
8459 tree t;
8460 tree ptype;
8461 inchash::hash hstate;
8462 bool any_structural_p, any_noncanonical_p;
8463 tree canon_argtypes;
8465 /* Make a node of the sort we want. */
8466 t = make_node (METHOD_TYPE);
8468 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8469 TREE_TYPE (t) = rettype;
8470 ptype = build_pointer_type (basetype);
8472 /* The actual arglist for this function includes a "hidden" argument
8473 which is "this". Put it into the list of argument types. */
8474 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
8475 TYPE_ARG_TYPES (t) = argtypes;
8477 /* If we already have such a type, use the old one. */
8478 hstate.add_object (TYPE_HASH (basetype));
8479 hstate.add_object (TYPE_HASH (rettype));
8480 type_hash_list (argtypes, hstate);
8481 t = type_hash_canon (hstate.end (), t);
8483 /* Set up the canonical type. */
8484 any_structural_p
8485 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8486 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
8487 any_noncanonical_p
8488 = (TYPE_CANONICAL (basetype) != basetype
8489 || TYPE_CANONICAL (rettype) != rettype);
8490 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
8491 &any_structural_p,
8492 &any_noncanonical_p);
8493 if (any_structural_p)
8494 SET_TYPE_STRUCTURAL_EQUALITY (t);
8495 else if (any_noncanonical_p)
8496 TYPE_CANONICAL (t)
8497 = build_method_type_directly (TYPE_CANONICAL (basetype),
8498 TYPE_CANONICAL (rettype),
8499 canon_argtypes);
8500 if (!COMPLETE_TYPE_P (t))
8501 layout_type (t);
8503 return t;
8506 /* Construct, lay out and return the type of methods belonging to class
8507 BASETYPE and whose arguments and values are described by TYPE.
8508 If that type exists already, reuse it.
8509 TYPE must be a FUNCTION_TYPE node. */
8511 tree
8512 build_method_type (tree basetype, tree type)
8514 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8516 return build_method_type_directly (basetype,
8517 TREE_TYPE (type),
8518 TYPE_ARG_TYPES (type));
8521 /* Construct, lay out and return the type of offsets to a value
8522 of type TYPE, within an object of type BASETYPE.
8523 If a suitable offset type exists already, reuse it. */
8525 tree
8526 build_offset_type (tree basetype, tree type)
8528 tree t;
8529 inchash::hash hstate;
8531 /* Make a node of the sort we want. */
8532 t = make_node (OFFSET_TYPE);
8534 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8535 TREE_TYPE (t) = type;
8537 /* If we already have such a type, use the old one. */
8538 hstate.add_object (TYPE_HASH (basetype));
8539 hstate.add_object (TYPE_HASH (type));
8540 t = type_hash_canon (hstate.end (), t);
8542 if (!COMPLETE_TYPE_P (t))
8543 layout_type (t);
8545 if (TYPE_CANONICAL (t) == t)
8547 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8548 || TYPE_STRUCTURAL_EQUALITY_P (type))
8549 SET_TYPE_STRUCTURAL_EQUALITY (t);
8550 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
8551 || TYPE_CANONICAL (type) != type)
8552 TYPE_CANONICAL (t)
8553 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
8554 TYPE_CANONICAL (type));
8557 return t;
8560 /* Create a complex type whose components are COMPONENT_TYPE. */
8562 tree
8563 build_complex_type (tree component_type)
8565 tree t;
8566 inchash::hash hstate;
8568 gcc_assert (INTEGRAL_TYPE_P (component_type)
8569 || SCALAR_FLOAT_TYPE_P (component_type)
8570 || FIXED_POINT_TYPE_P (component_type));
8572 /* Make a node of the sort we want. */
8573 t = make_node (COMPLEX_TYPE);
8575 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
8577 /* If we already have such a type, use the old one. */
8578 hstate.add_object (TYPE_HASH (component_type));
8579 t = type_hash_canon (hstate.end (), t);
8581 if (!COMPLETE_TYPE_P (t))
8582 layout_type (t);
8584 if (TYPE_CANONICAL (t) == t)
8586 if (TYPE_STRUCTURAL_EQUALITY_P (component_type))
8587 SET_TYPE_STRUCTURAL_EQUALITY (t);
8588 else if (TYPE_CANONICAL (component_type) != component_type)
8589 TYPE_CANONICAL (t)
8590 = build_complex_type (TYPE_CANONICAL (component_type));
8593 /* We need to create a name, since complex is a fundamental type. */
8594 if (! TYPE_NAME (t))
8596 const char *name;
8597 if (component_type == char_type_node)
8598 name = "complex char";
8599 else if (component_type == signed_char_type_node)
8600 name = "complex signed char";
8601 else if (component_type == unsigned_char_type_node)
8602 name = "complex unsigned char";
8603 else if (component_type == short_integer_type_node)
8604 name = "complex short int";
8605 else if (component_type == short_unsigned_type_node)
8606 name = "complex short unsigned int";
8607 else if (component_type == integer_type_node)
8608 name = "complex int";
8609 else if (component_type == unsigned_type_node)
8610 name = "complex unsigned int";
8611 else if (component_type == long_integer_type_node)
8612 name = "complex long int";
8613 else if (component_type == long_unsigned_type_node)
8614 name = "complex long unsigned int";
8615 else if (component_type == long_long_integer_type_node)
8616 name = "complex long long int";
8617 else if (component_type == long_long_unsigned_type_node)
8618 name = "complex long long unsigned int";
8619 else
8620 name = 0;
8622 if (name != 0)
8623 TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
8624 get_identifier (name), t);
8627 return build_qualified_type (t, TYPE_QUALS (component_type));
8630 /* If TYPE is a real or complex floating-point type and the target
8631 does not directly support arithmetic on TYPE then return the wider
8632 type to be used for arithmetic on TYPE. Otherwise, return
8633 NULL_TREE. */
8635 tree
8636 excess_precision_type (tree type)
8638 if (flag_excess_precision != EXCESS_PRECISION_FAST)
8640 int flt_eval_method = TARGET_FLT_EVAL_METHOD;
8641 switch (TREE_CODE (type))
8643 case REAL_TYPE:
8644 switch (flt_eval_method)
8646 case 1:
8647 if (TYPE_MODE (type) == TYPE_MODE (float_type_node))
8648 return double_type_node;
8649 break;
8650 case 2:
8651 if (TYPE_MODE (type) == TYPE_MODE (float_type_node)
8652 || TYPE_MODE (type) == TYPE_MODE (double_type_node))
8653 return long_double_type_node;
8654 break;
8655 default:
8656 gcc_unreachable ();
8658 break;
8659 case COMPLEX_TYPE:
8660 if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
8661 return NULL_TREE;
8662 switch (flt_eval_method)
8664 case 1:
8665 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node))
8666 return complex_double_type_node;
8667 break;
8668 case 2:
8669 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node)
8670 || (TYPE_MODE (TREE_TYPE (type))
8671 == TYPE_MODE (double_type_node)))
8672 return complex_long_double_type_node;
8673 break;
8674 default:
8675 gcc_unreachable ();
8677 break;
8678 default:
8679 break;
8682 return NULL_TREE;
8685 /* Return OP, stripped of any conversions to wider types as much as is safe.
8686 Converting the value back to OP's type makes a value equivalent to OP.
8688 If FOR_TYPE is nonzero, we return a value which, if converted to
8689 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
8691 OP must have integer, real or enumeral type. Pointers are not allowed!
8693 There are some cases where the obvious value we could return
8694 would regenerate to OP if converted to OP's type,
8695 but would not extend like OP to wider types.
8696 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
8697 For example, if OP is (unsigned short)(signed char)-1,
8698 we avoid returning (signed char)-1 if FOR_TYPE is int,
8699 even though extending that to an unsigned short would regenerate OP,
8700 since the result of extending (signed char)-1 to (int)
8701 is different from (int) OP. */
8703 tree
8704 get_unwidened (tree op, tree for_type)
8706 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
8707 tree type = TREE_TYPE (op);
8708 unsigned final_prec
8709 = TYPE_PRECISION (for_type != 0 ? for_type : type);
8710 int uns
8711 = (for_type != 0 && for_type != type
8712 && final_prec > TYPE_PRECISION (type)
8713 && TYPE_UNSIGNED (type));
8714 tree win = op;
8716 while (CONVERT_EXPR_P (op))
8718 int bitschange;
8720 /* TYPE_PRECISION on vector types has different meaning
8721 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
8722 so avoid them here. */
8723 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
8724 break;
8726 bitschange = TYPE_PRECISION (TREE_TYPE (op))
8727 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
8729 /* Truncations are many-one so cannot be removed.
8730 Unless we are later going to truncate down even farther. */
8731 if (bitschange < 0
8732 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
8733 break;
8735 /* See what's inside this conversion. If we decide to strip it,
8736 we will set WIN. */
8737 op = TREE_OPERAND (op, 0);
8739 /* If we have not stripped any zero-extensions (uns is 0),
8740 we can strip any kind of extension.
8741 If we have previously stripped a zero-extension,
8742 only zero-extensions can safely be stripped.
8743 Any extension can be stripped if the bits it would produce
8744 are all going to be discarded later by truncating to FOR_TYPE. */
8746 if (bitschange > 0)
8748 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
8749 win = op;
8750 /* TYPE_UNSIGNED says whether this is a zero-extension.
8751 Let's avoid computing it if it does not affect WIN
8752 and if UNS will not be needed again. */
8753 if ((uns
8754 || CONVERT_EXPR_P (op))
8755 && TYPE_UNSIGNED (TREE_TYPE (op)))
8757 uns = 1;
8758 win = op;
8763 /* If we finally reach a constant see if it fits in for_type and
8764 in that case convert it. */
8765 if (for_type
8766 && TREE_CODE (win) == INTEGER_CST
8767 && TREE_TYPE (win) != for_type
8768 && int_fits_type_p (win, for_type))
8769 win = fold_convert (for_type, win);
8771 return win;
8774 /* Return OP or a simpler expression for a narrower value
8775 which can be sign-extended or zero-extended to give back OP.
8776 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
8777 or 0 if the value should be sign-extended. */
8779 tree
8780 get_narrower (tree op, int *unsignedp_ptr)
8782 int uns = 0;
8783 int first = 1;
8784 tree win = op;
8785 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
8787 while (TREE_CODE (op) == NOP_EXPR)
8789 int bitschange
8790 = (TYPE_PRECISION (TREE_TYPE (op))
8791 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
8793 /* Truncations are many-one so cannot be removed. */
8794 if (bitschange < 0)
8795 break;
8797 /* See what's inside this conversion. If we decide to strip it,
8798 we will set WIN. */
8800 if (bitschange > 0)
8802 op = TREE_OPERAND (op, 0);
8803 /* An extension: the outermost one can be stripped,
8804 but remember whether it is zero or sign extension. */
8805 if (first)
8806 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8807 /* Otherwise, if a sign extension has been stripped,
8808 only sign extensions can now be stripped;
8809 if a zero extension has been stripped, only zero-extensions. */
8810 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
8811 break;
8812 first = 0;
8814 else /* bitschange == 0 */
8816 /* A change in nominal type can always be stripped, but we must
8817 preserve the unsignedness. */
8818 if (first)
8819 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8820 first = 0;
8821 op = TREE_OPERAND (op, 0);
8822 /* Keep trying to narrow, but don't assign op to win if it
8823 would turn an integral type into something else. */
8824 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
8825 continue;
8828 win = op;
8831 if (TREE_CODE (op) == COMPONENT_REF
8832 /* Since type_for_size always gives an integer type. */
8833 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
8834 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
8835 /* Ensure field is laid out already. */
8836 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
8837 && tree_fits_uhwi_p (DECL_SIZE (TREE_OPERAND (op, 1))))
8839 unsigned HOST_WIDE_INT innerprec
8840 = tree_to_uhwi (DECL_SIZE (TREE_OPERAND (op, 1)));
8841 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
8842 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
8843 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
8845 /* We can get this structure field in a narrower type that fits it,
8846 but the resulting extension to its nominal type (a fullword type)
8847 must satisfy the same conditions as for other extensions.
8849 Do this only for fields that are aligned (not bit-fields),
8850 because when bit-field insns will be used there is no
8851 advantage in doing this. */
8853 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
8854 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
8855 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
8856 && type != 0)
8858 if (first)
8859 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
8860 win = fold_convert (type, op);
8864 *unsignedp_ptr = uns;
8865 return win;
8868 /* Returns true if integer constant C has a value that is permissible
8869 for type TYPE (an INTEGER_TYPE). */
8871 bool
8872 int_fits_type_p (const_tree c, const_tree type)
8874 tree type_low_bound, type_high_bound;
8875 bool ok_for_low_bound, ok_for_high_bound;
8876 signop sgn_c = TYPE_SIGN (TREE_TYPE (c));
8878 retry:
8879 type_low_bound = TYPE_MIN_VALUE (type);
8880 type_high_bound = TYPE_MAX_VALUE (type);
8882 /* If at least one bound of the type is a constant integer, we can check
8883 ourselves and maybe make a decision. If no such decision is possible, but
8884 this type is a subtype, try checking against that. Otherwise, use
8885 fits_to_tree_p, which checks against the precision.
8887 Compute the status for each possibly constant bound, and return if we see
8888 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
8889 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
8890 for "constant known to fit". */
8892 /* Check if c >= type_low_bound. */
8893 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
8895 if (tree_int_cst_lt (c, type_low_bound))
8896 return false;
8897 ok_for_low_bound = true;
8899 else
8900 ok_for_low_bound = false;
8902 /* Check if c <= type_high_bound. */
8903 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
8905 if (tree_int_cst_lt (type_high_bound, c))
8906 return false;
8907 ok_for_high_bound = true;
8909 else
8910 ok_for_high_bound = false;
8912 /* If the constant fits both bounds, the result is known. */
8913 if (ok_for_low_bound && ok_for_high_bound)
8914 return true;
8916 /* Perform some generic filtering which may allow making a decision
8917 even if the bounds are not constant. First, negative integers
8918 never fit in unsigned types, */
8919 if (TYPE_UNSIGNED (type) && sgn_c == SIGNED && wi::neg_p (c))
8920 return false;
8922 /* Second, narrower types always fit in wider ones. */
8923 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
8924 return true;
8926 /* Third, unsigned integers with top bit set never fit signed types. */
8927 if (!TYPE_UNSIGNED (type) && sgn_c == UNSIGNED)
8929 int prec = GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (c))) - 1;
8930 if (prec < TYPE_PRECISION (TREE_TYPE (c)))
8932 /* When a tree_cst is converted to a wide-int, the precision
8933 is taken from the type. However, if the precision of the
8934 mode underneath the type is smaller than that, it is
8935 possible that the value will not fit. The test below
8936 fails if any bit is set between the sign bit of the
8937 underlying mode and the top bit of the type. */
8938 if (wi::ne_p (wi::zext (c, prec - 1), c))
8939 return false;
8941 else if (wi::neg_p (c))
8942 return false;
8945 /* If we haven't been able to decide at this point, there nothing more we
8946 can check ourselves here. Look at the base type if we have one and it
8947 has the same precision. */
8948 if (TREE_CODE (type) == INTEGER_TYPE
8949 && TREE_TYPE (type) != 0
8950 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
8952 type = TREE_TYPE (type);
8953 goto retry;
8956 /* Or to fits_to_tree_p, if nothing else. */
8957 return wi::fits_to_tree_p (c, type);
8960 /* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
8961 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
8962 represented (assuming two's-complement arithmetic) within the bit
8963 precision of the type are returned instead. */
8965 void
8966 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
8968 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
8969 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
8970 wi::to_mpz (TYPE_MIN_VALUE (type), min, TYPE_SIGN (type));
8971 else
8973 if (TYPE_UNSIGNED (type))
8974 mpz_set_ui (min, 0);
8975 else
8977 wide_int mn = wi::min_value (TYPE_PRECISION (type), SIGNED);
8978 wi::to_mpz (mn, min, SIGNED);
8982 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
8983 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
8984 wi::to_mpz (TYPE_MAX_VALUE (type), max, TYPE_SIGN (type));
8985 else
8987 wide_int mn = wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
8988 wi::to_mpz (mn, max, TYPE_SIGN (type));
8992 /* Return true if VAR is an automatic variable defined in function FN. */
8994 bool
8995 auto_var_in_fn_p (const_tree var, const_tree fn)
8997 return (DECL_P (var) && DECL_CONTEXT (var) == fn
8998 && ((((TREE_CODE (var) == VAR_DECL && ! DECL_EXTERNAL (var))
8999 || TREE_CODE (var) == PARM_DECL)
9000 && ! TREE_STATIC (var))
9001 || TREE_CODE (var) == LABEL_DECL
9002 || TREE_CODE (var) == RESULT_DECL));
9005 /* Subprogram of following function. Called by walk_tree.
9007 Return *TP if it is an automatic variable or parameter of the
9008 function passed in as DATA. */
9010 static tree
9011 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
9013 tree fn = (tree) data;
9015 if (TYPE_P (*tp))
9016 *walk_subtrees = 0;
9018 else if (DECL_P (*tp)
9019 && auto_var_in_fn_p (*tp, fn))
9020 return *tp;
9022 return NULL_TREE;
9025 /* Returns true if T is, contains, or refers to a type with variable
9026 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
9027 arguments, but not the return type. If FN is nonzero, only return
9028 true if a modifier of the type or position of FN is a variable or
9029 parameter inside FN.
9031 This concept is more general than that of C99 'variably modified types':
9032 in C99, a struct type is never variably modified because a VLA may not
9033 appear as a structure member. However, in GNU C code like:
9035 struct S { int i[f()]; };
9037 is valid, and other languages may define similar constructs. */
9039 bool
9040 variably_modified_type_p (tree type, tree fn)
9042 tree t;
9044 /* Test if T is either variable (if FN is zero) or an expression containing
9045 a variable in FN. If TYPE isn't gimplified, return true also if
9046 gimplify_one_sizepos would gimplify the expression into a local
9047 variable. */
9048 #define RETURN_TRUE_IF_VAR(T) \
9049 do { tree _t = (T); \
9050 if (_t != NULL_TREE \
9051 && _t != error_mark_node \
9052 && TREE_CODE (_t) != INTEGER_CST \
9053 && TREE_CODE (_t) != PLACEHOLDER_EXPR \
9054 && (!fn \
9055 || (!TYPE_SIZES_GIMPLIFIED (type) \
9056 && !is_gimple_sizepos (_t)) \
9057 || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
9058 return true; } while (0)
9060 if (type == error_mark_node)
9061 return false;
9063 /* If TYPE itself has variable size, it is variably modified. */
9064 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
9065 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
9067 switch (TREE_CODE (type))
9069 case POINTER_TYPE:
9070 case REFERENCE_TYPE:
9071 case VECTOR_TYPE:
9072 if (variably_modified_type_p (TREE_TYPE (type), fn))
9073 return true;
9074 break;
9076 case FUNCTION_TYPE:
9077 case METHOD_TYPE:
9078 /* If TYPE is a function type, it is variably modified if the
9079 return type is variably modified. */
9080 if (variably_modified_type_p (TREE_TYPE (type), fn))
9081 return true;
9082 break;
9084 case INTEGER_TYPE:
9085 case REAL_TYPE:
9086 case FIXED_POINT_TYPE:
9087 case ENUMERAL_TYPE:
9088 case BOOLEAN_TYPE:
9089 /* Scalar types are variably modified if their end points
9090 aren't constant. */
9091 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
9092 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
9093 break;
9095 case RECORD_TYPE:
9096 case UNION_TYPE:
9097 case QUAL_UNION_TYPE:
9098 /* We can't see if any of the fields are variably-modified by the
9099 definition we normally use, since that would produce infinite
9100 recursion via pointers. */
9101 /* This is variably modified if some field's type is. */
9102 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
9103 if (TREE_CODE (t) == FIELD_DECL)
9105 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
9106 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
9107 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
9109 if (TREE_CODE (type) == QUAL_UNION_TYPE)
9110 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
9112 break;
9114 case ARRAY_TYPE:
9115 /* Do not call ourselves to avoid infinite recursion. This is
9116 variably modified if the element type is. */
9117 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
9118 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
9119 break;
9121 default:
9122 break;
9125 /* The current language may have other cases to check, but in general,
9126 all other types are not variably modified. */
9127 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
9129 #undef RETURN_TRUE_IF_VAR
9132 /* Given a DECL or TYPE, return the scope in which it was declared, or
9133 NULL_TREE if there is no containing scope. */
9135 tree
9136 get_containing_scope (const_tree t)
9138 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
9141 /* Return the innermost context enclosing DECL that is
9142 a FUNCTION_DECL, or zero if none. */
9144 tree
9145 decl_function_context (const_tree decl)
9147 tree context;
9149 if (TREE_CODE (decl) == ERROR_MARK)
9150 return 0;
9152 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
9153 where we look up the function at runtime. Such functions always take
9154 a first argument of type 'pointer to real context'.
9156 C++ should really be fixed to use DECL_CONTEXT for the real context,
9157 and use something else for the "virtual context". */
9158 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
9159 context
9160 = TYPE_MAIN_VARIANT
9161 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
9162 else
9163 context = DECL_CONTEXT (decl);
9165 while (context && TREE_CODE (context) != FUNCTION_DECL)
9167 if (TREE_CODE (context) == BLOCK)
9168 context = BLOCK_SUPERCONTEXT (context);
9169 else
9170 context = get_containing_scope (context);
9173 return context;
9176 /* Return the innermost context enclosing DECL that is
9177 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
9178 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
9180 tree
9181 decl_type_context (const_tree decl)
9183 tree context = DECL_CONTEXT (decl);
9185 while (context)
9186 switch (TREE_CODE (context))
9188 case NAMESPACE_DECL:
9189 case TRANSLATION_UNIT_DECL:
9190 return NULL_TREE;
9192 case RECORD_TYPE:
9193 case UNION_TYPE:
9194 case QUAL_UNION_TYPE:
9195 return context;
9197 case TYPE_DECL:
9198 case FUNCTION_DECL:
9199 context = DECL_CONTEXT (context);
9200 break;
9202 case BLOCK:
9203 context = BLOCK_SUPERCONTEXT (context);
9204 break;
9206 default:
9207 gcc_unreachable ();
9210 return NULL_TREE;
9213 /* CALL is a CALL_EXPR. Return the declaration for the function
9214 called, or NULL_TREE if the called function cannot be
9215 determined. */
9217 tree
9218 get_callee_fndecl (const_tree call)
9220 tree addr;
9222 if (call == error_mark_node)
9223 return error_mark_node;
9225 /* It's invalid to call this function with anything but a
9226 CALL_EXPR. */
9227 gcc_assert (TREE_CODE (call) == CALL_EXPR);
9229 /* The first operand to the CALL is the address of the function
9230 called. */
9231 addr = CALL_EXPR_FN (call);
9233 /* If there is no function, return early. */
9234 if (addr == NULL_TREE)
9235 return NULL_TREE;
9237 STRIP_NOPS (addr);
9239 /* If this is a readonly function pointer, extract its initial value. */
9240 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
9241 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
9242 && DECL_INITIAL (addr))
9243 addr = DECL_INITIAL (addr);
9245 /* If the address is just `&f' for some function `f', then we know
9246 that `f' is being called. */
9247 if (TREE_CODE (addr) == ADDR_EXPR
9248 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
9249 return TREE_OPERAND (addr, 0);
9251 /* We couldn't figure out what was being called. */
9252 return NULL_TREE;
9255 #define TREE_MEM_USAGE_SPACES 40
9257 /* Print debugging information about tree nodes generated during the compile,
9258 and any language-specific information. */
9260 void
9261 dump_tree_statistics (void)
9263 if (GATHER_STATISTICS)
9265 int i;
9266 int total_nodes, total_bytes;
9267 fprintf (stderr, "\nKind Nodes Bytes\n");
9268 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9269 total_nodes = total_bytes = 0;
9270 for (i = 0; i < (int) all_kinds; i++)
9272 fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
9273 tree_node_counts[i], tree_node_sizes[i]);
9274 total_nodes += tree_node_counts[i];
9275 total_bytes += tree_node_sizes[i];
9277 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9278 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
9279 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9280 fprintf (stderr, "Code Nodes\n");
9281 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9282 for (i = 0; i < (int) MAX_TREE_CODES; i++)
9283 fprintf (stderr, "%-32s %7d\n", get_tree_code_name ((enum tree_code) i),
9284 tree_code_counts[i]);
9285 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9286 fprintf (stderr, "\n");
9287 ssanames_print_statistics ();
9288 fprintf (stderr, "\n");
9289 phinodes_print_statistics ();
9290 fprintf (stderr, "\n");
9292 else
9293 fprintf (stderr, "(No per-node statistics)\n");
9295 print_type_hash_statistics ();
9296 print_debug_expr_statistics ();
9297 print_value_expr_statistics ();
9298 lang_hooks.print_statistics ();
9301 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
9303 /* Generate a crc32 of a byte. */
9305 static unsigned
9306 crc32_unsigned_bits (unsigned chksum, unsigned value, unsigned bits)
9308 unsigned ix;
9310 for (ix = bits; ix--; value <<= 1)
9312 unsigned feedback;
9314 feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
9315 chksum <<= 1;
9316 chksum ^= feedback;
9318 return chksum;
9321 /* Generate a crc32 of a 32-bit unsigned. */
9323 unsigned
9324 crc32_unsigned (unsigned chksum, unsigned value)
9326 return crc32_unsigned_bits (chksum, value, 32);
9329 /* Generate a crc32 of a byte. */
9331 unsigned
9332 crc32_byte (unsigned chksum, char byte)
9334 return crc32_unsigned_bits (chksum, (unsigned) byte << 24, 8);
9337 /* Generate a crc32 of a string. */
9339 unsigned
9340 crc32_string (unsigned chksum, const char *string)
9344 chksum = crc32_byte (chksum, *string);
9346 while (*string++);
9347 return chksum;
9350 /* P is a string that will be used in a symbol. Mask out any characters
9351 that are not valid in that context. */
9353 void
9354 clean_symbol_name (char *p)
9356 for (; *p; p++)
9357 if (! (ISALNUM (*p)
9358 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
9359 || *p == '$'
9360 #endif
9361 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
9362 || *p == '.'
9363 #endif
9365 *p = '_';
9368 /* For anonymous aggregate types, we need some sort of name to
9369 hold on to. In practice, this should not appear, but it should
9370 not be harmful if it does. */
9371 bool
9372 anon_aggrname_p(const_tree id_node)
9374 #ifndef NO_DOT_IN_LABEL
9375 return (IDENTIFIER_POINTER (id_node)[0] == '.'
9376 && IDENTIFIER_POINTER (id_node)[1] == '_');
9377 #else /* NO_DOT_IN_LABEL */
9378 #ifndef NO_DOLLAR_IN_LABEL
9379 return (IDENTIFIER_POINTER (id_node)[0] == '$' \
9380 && IDENTIFIER_POINTER (id_node)[1] == '_');
9381 #else /* NO_DOLLAR_IN_LABEL */
9382 #define ANON_AGGRNAME_PREFIX "__anon_"
9383 return (!strncmp (IDENTIFIER_POINTER (id_node), ANON_AGGRNAME_PREFIX,
9384 sizeof (ANON_AGGRNAME_PREFIX) - 1));
9385 #endif /* NO_DOLLAR_IN_LABEL */
9386 #endif /* NO_DOT_IN_LABEL */
9389 /* Return a format for an anonymous aggregate name. */
9390 const char *
9391 anon_aggrname_format()
9393 #ifndef NO_DOT_IN_LABEL
9394 return "._%d";
9395 #else /* NO_DOT_IN_LABEL */
9396 #ifndef NO_DOLLAR_IN_LABEL
9397 return "$_%d";
9398 #else /* NO_DOLLAR_IN_LABEL */
9399 return "__anon_%d";
9400 #endif /* NO_DOLLAR_IN_LABEL */
9401 #endif /* NO_DOT_IN_LABEL */
9404 /* Generate a name for a special-purpose function.
9405 The generated name may need to be unique across the whole link.
9406 Changes to this function may also require corresponding changes to
9407 xstrdup_mask_random.
9408 TYPE is some string to identify the purpose of this function to the
9409 linker or collect2; it must start with an uppercase letter,
9410 one of:
9411 I - for constructors
9412 D - for destructors
9413 N - for C++ anonymous namespaces
9414 F - for DWARF unwind frame information. */
9416 tree
9417 get_file_function_name (const char *type)
9419 char *buf;
9420 const char *p;
9421 char *q;
9423 /* If we already have a name we know to be unique, just use that. */
9424 if (first_global_object_name)
9425 p = q = ASTRDUP (first_global_object_name);
9426 /* If the target is handling the constructors/destructors, they
9427 will be local to this file and the name is only necessary for
9428 debugging purposes.
9429 We also assign sub_I and sub_D sufixes to constructors called from
9430 the global static constructors. These are always local. */
9431 else if (((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
9432 || (strncmp (type, "sub_", 4) == 0
9433 && (type[4] == 'I' || type[4] == 'D')))
9435 const char *file = main_input_filename;
9436 if (! file)
9437 file = LOCATION_FILE (input_location);
9438 /* Just use the file's basename, because the full pathname
9439 might be quite long. */
9440 p = q = ASTRDUP (lbasename (file));
9442 else
9444 /* Otherwise, the name must be unique across the entire link.
9445 We don't have anything that we know to be unique to this translation
9446 unit, so use what we do have and throw in some randomness. */
9447 unsigned len;
9448 const char *name = weak_global_object_name;
9449 const char *file = main_input_filename;
9451 if (! name)
9452 name = "";
9453 if (! file)
9454 file = LOCATION_FILE (input_location);
9456 len = strlen (file);
9457 q = (char *) alloca (9 + 17 + len + 1);
9458 memcpy (q, file, len + 1);
9460 snprintf (q + len, 9 + 17 + 1, "_%08X_" HOST_WIDE_INT_PRINT_HEX,
9461 crc32_string (0, name), get_random_seed (false));
9463 p = q;
9466 clean_symbol_name (q);
9467 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
9468 + strlen (type));
9470 /* Set up the name of the file-level functions we may need.
9471 Use a global object (which is already required to be unique over
9472 the program) rather than the file name (which imposes extra
9473 constraints). */
9474 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
9476 return get_identifier (buf);
9479 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
9481 /* Complain that the tree code of NODE does not match the expected 0
9482 terminated list of trailing codes. The trailing code list can be
9483 empty, for a more vague error message. FILE, LINE, and FUNCTION
9484 are of the caller. */
9486 void
9487 tree_check_failed (const_tree node, const char *file,
9488 int line, const char *function, ...)
9490 va_list args;
9491 const char *buffer;
9492 unsigned length = 0;
9493 enum tree_code code;
9495 va_start (args, function);
9496 while ((code = (enum tree_code) va_arg (args, int)))
9497 length += 4 + strlen (get_tree_code_name (code));
9498 va_end (args);
9499 if (length)
9501 char *tmp;
9502 va_start (args, function);
9503 length += strlen ("expected ");
9504 buffer = tmp = (char *) alloca (length);
9505 length = 0;
9506 while ((code = (enum tree_code) va_arg (args, int)))
9508 const char *prefix = length ? " or " : "expected ";
9510 strcpy (tmp + length, prefix);
9511 length += strlen (prefix);
9512 strcpy (tmp + length, get_tree_code_name (code));
9513 length += strlen (get_tree_code_name (code));
9515 va_end (args);
9517 else
9518 buffer = "unexpected node";
9520 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9521 buffer, get_tree_code_name (TREE_CODE (node)),
9522 function, trim_filename (file), line);
9525 /* Complain that the tree code of NODE does match the expected 0
9526 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
9527 the caller. */
9529 void
9530 tree_not_check_failed (const_tree node, const char *file,
9531 int line, const char *function, ...)
9533 va_list args;
9534 char *buffer;
9535 unsigned length = 0;
9536 enum tree_code code;
9538 va_start (args, function);
9539 while ((code = (enum tree_code) va_arg (args, int)))
9540 length += 4 + strlen (get_tree_code_name (code));
9541 va_end (args);
9542 va_start (args, function);
9543 buffer = (char *) alloca (length);
9544 length = 0;
9545 while ((code = (enum tree_code) va_arg (args, int)))
9547 if (length)
9549 strcpy (buffer + length, " or ");
9550 length += 4;
9552 strcpy (buffer + length, get_tree_code_name (code));
9553 length += strlen (get_tree_code_name (code));
9555 va_end (args);
9557 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
9558 buffer, get_tree_code_name (TREE_CODE (node)),
9559 function, trim_filename (file), line);
9562 /* Similar to tree_check_failed, except that we check for a class of tree
9563 code, given in CL. */
9565 void
9566 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
9567 const char *file, int line, const char *function)
9569 internal_error
9570 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
9571 TREE_CODE_CLASS_STRING (cl),
9572 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9573 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9576 /* Similar to tree_check_failed, except that instead of specifying a
9577 dozen codes, use the knowledge that they're all sequential. */
9579 void
9580 tree_range_check_failed (const_tree node, const char *file, int line,
9581 const char *function, enum tree_code c1,
9582 enum tree_code c2)
9584 char *buffer;
9585 unsigned length = 0;
9586 unsigned int c;
9588 for (c = c1; c <= c2; ++c)
9589 length += 4 + strlen (get_tree_code_name ((enum tree_code) c));
9591 length += strlen ("expected ");
9592 buffer = (char *) alloca (length);
9593 length = 0;
9595 for (c = c1; c <= c2; ++c)
9597 const char *prefix = length ? " or " : "expected ";
9599 strcpy (buffer + length, prefix);
9600 length += strlen (prefix);
9601 strcpy (buffer + length, get_tree_code_name ((enum tree_code) c));
9602 length += strlen (get_tree_code_name ((enum tree_code) c));
9605 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9606 buffer, get_tree_code_name (TREE_CODE (node)),
9607 function, trim_filename (file), line);
9611 /* Similar to tree_check_failed, except that we check that a tree does
9612 not have the specified code, given in CL. */
9614 void
9615 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
9616 const char *file, int line, const char *function)
9618 internal_error
9619 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
9620 TREE_CODE_CLASS_STRING (cl),
9621 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9622 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9626 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
9628 void
9629 omp_clause_check_failed (const_tree node, const char *file, int line,
9630 const char *function, enum omp_clause_code code)
9632 internal_error ("tree check: expected omp_clause %s, have %s in %s, at %s:%d",
9633 omp_clause_code_name[code], get_tree_code_name (TREE_CODE (node)),
9634 function, trim_filename (file), line);
9638 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
9640 void
9641 omp_clause_range_check_failed (const_tree node, const char *file, int line,
9642 const char *function, enum omp_clause_code c1,
9643 enum omp_clause_code c2)
9645 char *buffer;
9646 unsigned length = 0;
9647 unsigned int c;
9649 for (c = c1; c <= c2; ++c)
9650 length += 4 + strlen (omp_clause_code_name[c]);
9652 length += strlen ("expected ");
9653 buffer = (char *) alloca (length);
9654 length = 0;
9656 for (c = c1; c <= c2; ++c)
9658 const char *prefix = length ? " or " : "expected ";
9660 strcpy (buffer + length, prefix);
9661 length += strlen (prefix);
9662 strcpy (buffer + length, omp_clause_code_name[c]);
9663 length += strlen (omp_clause_code_name[c]);
9666 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9667 buffer, omp_clause_code_name[TREE_CODE (node)],
9668 function, trim_filename (file), line);
9672 #undef DEFTREESTRUCT
9673 #define DEFTREESTRUCT(VAL, NAME) NAME,
9675 static const char *ts_enum_names[] = {
9676 #include "treestruct.def"
9678 #undef DEFTREESTRUCT
9680 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
9682 /* Similar to tree_class_check_failed, except that we check for
9683 whether CODE contains the tree structure identified by EN. */
9685 void
9686 tree_contains_struct_check_failed (const_tree node,
9687 const enum tree_node_structure_enum en,
9688 const char *file, int line,
9689 const char *function)
9691 internal_error
9692 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
9693 TS_ENUM_NAME (en),
9694 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9698 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9699 (dynamically sized) vector. */
9701 void
9702 tree_int_cst_elt_check_failed (int idx, int len, const char *file, int line,
9703 const char *function)
9705 internal_error
9706 ("tree check: accessed elt %d of tree_int_cst with %d elts in %s, at %s:%d",
9707 idx + 1, len, function, trim_filename (file), line);
9710 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9711 (dynamically sized) vector. */
9713 void
9714 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
9715 const char *function)
9717 internal_error
9718 ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
9719 idx + 1, len, function, trim_filename (file), line);
9722 /* Similar to above, except that the check is for the bounds of the operand
9723 vector of an expression node EXP. */
9725 void
9726 tree_operand_check_failed (int idx, const_tree exp, const char *file,
9727 int line, const char *function)
9729 enum tree_code code = TREE_CODE (exp);
9730 internal_error
9731 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
9732 idx + 1, get_tree_code_name (code), TREE_OPERAND_LENGTH (exp),
9733 function, trim_filename (file), line);
9736 /* Similar to above, except that the check is for the number of
9737 operands of an OMP_CLAUSE node. */
9739 void
9740 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
9741 int line, const char *function)
9743 internal_error
9744 ("tree check: accessed operand %d of omp_clause %s with %d operands "
9745 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
9746 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
9747 trim_filename (file), line);
9749 #endif /* ENABLE_TREE_CHECKING */
9751 /* Create a new vector type node holding SUBPARTS units of type INNERTYPE,
9752 and mapped to the machine mode MODE. Initialize its fields and build
9753 the information necessary for debugging output. */
9755 static tree
9756 make_vector_type (tree innertype, int nunits, machine_mode mode)
9758 tree t;
9759 inchash::hash hstate;
9761 t = make_node (VECTOR_TYPE);
9762 TREE_TYPE (t) = TYPE_MAIN_VARIANT (innertype);
9763 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
9764 SET_TYPE_MODE (t, mode);
9766 if (TYPE_STRUCTURAL_EQUALITY_P (innertype))
9767 SET_TYPE_STRUCTURAL_EQUALITY (t);
9768 else if (TYPE_CANONICAL (innertype) != innertype
9769 || mode != VOIDmode)
9770 TYPE_CANONICAL (t)
9771 = make_vector_type (TYPE_CANONICAL (innertype), nunits, VOIDmode);
9773 layout_type (t);
9775 hstate.add_wide_int (VECTOR_TYPE);
9776 hstate.add_wide_int (nunits);
9777 hstate.add_wide_int (mode);
9778 hstate.add_object (TYPE_HASH (TREE_TYPE (t)));
9779 t = type_hash_canon (hstate.end (), t);
9781 /* We have built a main variant, based on the main variant of the
9782 inner type. Use it to build the variant we return. */
9783 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
9784 && TREE_TYPE (t) != innertype)
9785 return build_type_attribute_qual_variant (t,
9786 TYPE_ATTRIBUTES (innertype),
9787 TYPE_QUALS (innertype));
9789 return t;
9792 static tree
9793 make_or_reuse_type (unsigned size, int unsignedp)
9795 int i;
9797 if (size == INT_TYPE_SIZE)
9798 return unsignedp ? unsigned_type_node : integer_type_node;
9799 if (size == CHAR_TYPE_SIZE)
9800 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
9801 if (size == SHORT_TYPE_SIZE)
9802 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
9803 if (size == LONG_TYPE_SIZE)
9804 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
9805 if (size == LONG_LONG_TYPE_SIZE)
9806 return (unsignedp ? long_long_unsigned_type_node
9807 : long_long_integer_type_node);
9809 for (i = 0; i < NUM_INT_N_ENTS; i ++)
9810 if (size == int_n_data[i].bitsize
9811 && int_n_enabled_p[i])
9812 return (unsignedp ? int_n_trees[i].unsigned_type
9813 : int_n_trees[i].signed_type);
9815 if (unsignedp)
9816 return make_unsigned_type (size);
9817 else
9818 return make_signed_type (size);
9821 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
9823 static tree
9824 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
9826 if (satp)
9828 if (size == SHORT_FRACT_TYPE_SIZE)
9829 return unsignedp ? sat_unsigned_short_fract_type_node
9830 : sat_short_fract_type_node;
9831 if (size == FRACT_TYPE_SIZE)
9832 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
9833 if (size == LONG_FRACT_TYPE_SIZE)
9834 return unsignedp ? sat_unsigned_long_fract_type_node
9835 : sat_long_fract_type_node;
9836 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9837 return unsignedp ? sat_unsigned_long_long_fract_type_node
9838 : sat_long_long_fract_type_node;
9840 else
9842 if (size == SHORT_FRACT_TYPE_SIZE)
9843 return unsignedp ? unsigned_short_fract_type_node
9844 : short_fract_type_node;
9845 if (size == FRACT_TYPE_SIZE)
9846 return unsignedp ? unsigned_fract_type_node : fract_type_node;
9847 if (size == LONG_FRACT_TYPE_SIZE)
9848 return unsignedp ? unsigned_long_fract_type_node
9849 : long_fract_type_node;
9850 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9851 return unsignedp ? unsigned_long_long_fract_type_node
9852 : long_long_fract_type_node;
9855 return make_fract_type (size, unsignedp, satp);
9858 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
9860 static tree
9861 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
9863 if (satp)
9865 if (size == SHORT_ACCUM_TYPE_SIZE)
9866 return unsignedp ? sat_unsigned_short_accum_type_node
9867 : sat_short_accum_type_node;
9868 if (size == ACCUM_TYPE_SIZE)
9869 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
9870 if (size == LONG_ACCUM_TYPE_SIZE)
9871 return unsignedp ? sat_unsigned_long_accum_type_node
9872 : sat_long_accum_type_node;
9873 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9874 return unsignedp ? sat_unsigned_long_long_accum_type_node
9875 : sat_long_long_accum_type_node;
9877 else
9879 if (size == SHORT_ACCUM_TYPE_SIZE)
9880 return unsignedp ? unsigned_short_accum_type_node
9881 : short_accum_type_node;
9882 if (size == ACCUM_TYPE_SIZE)
9883 return unsignedp ? unsigned_accum_type_node : accum_type_node;
9884 if (size == LONG_ACCUM_TYPE_SIZE)
9885 return unsignedp ? unsigned_long_accum_type_node
9886 : long_accum_type_node;
9887 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9888 return unsignedp ? unsigned_long_long_accum_type_node
9889 : long_long_accum_type_node;
9892 return make_accum_type (size, unsignedp, satp);
9896 /* Create an atomic variant node for TYPE. This routine is called
9897 during initialization of data types to create the 5 basic atomic
9898 types. The generic build_variant_type function requires these to
9899 already be set up in order to function properly, so cannot be
9900 called from there. If ALIGN is non-zero, then ensure alignment is
9901 overridden to this value. */
9903 static tree
9904 build_atomic_base (tree type, unsigned int align)
9906 tree t;
9908 /* Make sure its not already registered. */
9909 if ((t = get_qualified_type (type, TYPE_QUAL_ATOMIC)))
9910 return t;
9912 t = build_variant_type_copy (type);
9913 set_type_quals (t, TYPE_QUAL_ATOMIC);
9915 if (align)
9916 TYPE_ALIGN (t) = align;
9918 return t;
9921 /* Create nodes for all integer types (and error_mark_node) using the sizes
9922 of C datatypes. SIGNED_CHAR specifies whether char is signed,
9923 SHORT_DOUBLE specifies whether double should be of the same precision
9924 as float. */
9926 void
9927 build_common_tree_nodes (bool signed_char, bool short_double)
9929 int i;
9931 error_mark_node = make_node (ERROR_MARK);
9932 TREE_TYPE (error_mark_node) = error_mark_node;
9934 initialize_sizetypes ();
9936 /* Define both `signed char' and `unsigned char'. */
9937 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
9938 TYPE_STRING_FLAG (signed_char_type_node) = 1;
9939 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
9940 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
9942 /* Define `char', which is like either `signed char' or `unsigned char'
9943 but not the same as either. */
9944 char_type_node
9945 = (signed_char
9946 ? make_signed_type (CHAR_TYPE_SIZE)
9947 : make_unsigned_type (CHAR_TYPE_SIZE));
9948 TYPE_STRING_FLAG (char_type_node) = 1;
9950 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
9951 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
9952 integer_type_node = make_signed_type (INT_TYPE_SIZE);
9953 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
9954 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
9955 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
9956 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
9957 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
9959 for (i = 0; i < NUM_INT_N_ENTS; i ++)
9961 int_n_trees[i].signed_type = make_signed_type (int_n_data[i].bitsize);
9962 int_n_trees[i].unsigned_type = make_unsigned_type (int_n_data[i].bitsize);
9963 TYPE_SIZE (int_n_trees[i].signed_type) = bitsize_int (int_n_data[i].bitsize);
9964 TYPE_SIZE (int_n_trees[i].unsigned_type) = bitsize_int (int_n_data[i].bitsize);
9966 if (int_n_data[i].bitsize > LONG_LONG_TYPE_SIZE
9967 && int_n_enabled_p[i])
9969 integer_types[itk_intN_0 + i * 2] = int_n_trees[i].signed_type;
9970 integer_types[itk_unsigned_intN_0 + i * 2] = int_n_trees[i].unsigned_type;
9974 /* Define a boolean type. This type only represents boolean values but
9975 may be larger than char depending on the value of BOOL_TYPE_SIZE. */
9976 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
9977 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
9978 TYPE_PRECISION (boolean_type_node) = 1;
9979 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
9981 /* Define what type to use for size_t. */
9982 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
9983 size_type_node = unsigned_type_node;
9984 else if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
9985 size_type_node = long_unsigned_type_node;
9986 else if (strcmp (SIZE_TYPE, "long long unsigned int") == 0)
9987 size_type_node = long_long_unsigned_type_node;
9988 else if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
9989 size_type_node = short_unsigned_type_node;
9990 else
9992 int i;
9994 size_type_node = NULL_TREE;
9995 for (i = 0; i < NUM_INT_N_ENTS; i++)
9996 if (int_n_enabled_p[i])
9998 char name[50];
9999 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
10001 if (strcmp (name, SIZE_TYPE) == 0)
10003 size_type_node = int_n_trees[i].unsigned_type;
10006 if (size_type_node == NULL_TREE)
10007 gcc_unreachable ();
10010 /* Fill in the rest of the sized types. Reuse existing type nodes
10011 when possible. */
10012 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
10013 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
10014 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
10015 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
10016 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
10018 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
10019 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
10020 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
10021 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
10022 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
10024 /* Don't call build_qualified type for atomics. That routine does
10025 special processing for atomics, and until they are initialized
10026 it's better not to make that call.
10028 Check to see if there is a target override for atomic types. */
10030 atomicQI_type_node = build_atomic_base (unsigned_intQI_type_node,
10031 targetm.atomic_align_for_mode (QImode));
10032 atomicHI_type_node = build_atomic_base (unsigned_intHI_type_node,
10033 targetm.atomic_align_for_mode (HImode));
10034 atomicSI_type_node = build_atomic_base (unsigned_intSI_type_node,
10035 targetm.atomic_align_for_mode (SImode));
10036 atomicDI_type_node = build_atomic_base (unsigned_intDI_type_node,
10037 targetm.atomic_align_for_mode (DImode));
10038 atomicTI_type_node = build_atomic_base (unsigned_intTI_type_node,
10039 targetm.atomic_align_for_mode (TImode));
10041 access_public_node = get_identifier ("public");
10042 access_protected_node = get_identifier ("protected");
10043 access_private_node = get_identifier ("private");
10045 /* Define these next since types below may used them. */
10046 integer_zero_node = build_int_cst (integer_type_node, 0);
10047 integer_one_node = build_int_cst (integer_type_node, 1);
10048 integer_three_node = build_int_cst (integer_type_node, 3);
10049 integer_minus_one_node = build_int_cst (integer_type_node, -1);
10051 size_zero_node = size_int (0);
10052 size_one_node = size_int (1);
10053 bitsize_zero_node = bitsize_int (0);
10054 bitsize_one_node = bitsize_int (1);
10055 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
10057 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
10058 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
10060 void_type_node = make_node (VOID_TYPE);
10061 layout_type (void_type_node);
10063 pointer_bounds_type_node = targetm.chkp_bound_type ();
10065 /* We are not going to have real types in C with less than byte alignment,
10066 so we might as well not have any types that claim to have it. */
10067 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
10068 TYPE_USER_ALIGN (void_type_node) = 0;
10070 void_node = make_node (VOID_CST);
10071 TREE_TYPE (void_node) = void_type_node;
10073 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
10074 layout_type (TREE_TYPE (null_pointer_node));
10076 ptr_type_node = build_pointer_type (void_type_node);
10077 const_ptr_type_node
10078 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
10079 fileptr_type_node = ptr_type_node;
10081 pointer_sized_int_node = build_nonstandard_integer_type (POINTER_SIZE, 1);
10083 float_type_node = make_node (REAL_TYPE);
10084 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
10085 layout_type (float_type_node);
10087 double_type_node = make_node (REAL_TYPE);
10088 if (short_double)
10089 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
10090 else
10091 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
10092 layout_type (double_type_node);
10094 long_double_type_node = make_node (REAL_TYPE);
10095 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
10096 layout_type (long_double_type_node);
10098 float_ptr_type_node = build_pointer_type (float_type_node);
10099 double_ptr_type_node = build_pointer_type (double_type_node);
10100 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
10101 integer_ptr_type_node = build_pointer_type (integer_type_node);
10103 /* Fixed size integer types. */
10104 uint16_type_node = make_or_reuse_type (16, 1);
10105 uint32_type_node = make_or_reuse_type (32, 1);
10106 uint64_type_node = make_or_reuse_type (64, 1);
10108 /* Decimal float types. */
10109 dfloat32_type_node = make_node (REAL_TYPE);
10110 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
10111 layout_type (dfloat32_type_node);
10112 SET_TYPE_MODE (dfloat32_type_node, SDmode);
10113 dfloat32_ptr_type_node = build_pointer_type (dfloat32_type_node);
10115 dfloat64_type_node = make_node (REAL_TYPE);
10116 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
10117 layout_type (dfloat64_type_node);
10118 SET_TYPE_MODE (dfloat64_type_node, DDmode);
10119 dfloat64_ptr_type_node = build_pointer_type (dfloat64_type_node);
10121 dfloat128_type_node = make_node (REAL_TYPE);
10122 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
10123 layout_type (dfloat128_type_node);
10124 SET_TYPE_MODE (dfloat128_type_node, TDmode);
10125 dfloat128_ptr_type_node = build_pointer_type (dfloat128_type_node);
10127 complex_integer_type_node = build_complex_type (integer_type_node);
10128 complex_float_type_node = build_complex_type (float_type_node);
10129 complex_double_type_node = build_complex_type (double_type_node);
10130 complex_long_double_type_node = build_complex_type (long_double_type_node);
10132 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
10133 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
10134 sat_ ## KIND ## _type_node = \
10135 make_sat_signed_ ## KIND ## _type (SIZE); \
10136 sat_unsigned_ ## KIND ## _type_node = \
10137 make_sat_unsigned_ ## KIND ## _type (SIZE); \
10138 KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
10139 unsigned_ ## KIND ## _type_node = \
10140 make_unsigned_ ## KIND ## _type (SIZE);
10142 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
10143 sat_ ## WIDTH ## KIND ## _type_node = \
10144 make_sat_signed_ ## KIND ## _type (SIZE); \
10145 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
10146 make_sat_unsigned_ ## KIND ## _type (SIZE); \
10147 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
10148 unsigned_ ## WIDTH ## KIND ## _type_node = \
10149 make_unsigned_ ## KIND ## _type (SIZE);
10151 /* Make fixed-point type nodes based on four different widths. */
10152 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
10153 MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
10154 MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
10155 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
10156 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
10158 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
10159 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
10160 NAME ## _type_node = \
10161 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
10162 u ## NAME ## _type_node = \
10163 make_or_reuse_unsigned_ ## KIND ## _type \
10164 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
10165 sat_ ## NAME ## _type_node = \
10166 make_or_reuse_sat_signed_ ## KIND ## _type \
10167 (GET_MODE_BITSIZE (MODE ## mode)); \
10168 sat_u ## NAME ## _type_node = \
10169 make_or_reuse_sat_unsigned_ ## KIND ## _type \
10170 (GET_MODE_BITSIZE (U ## MODE ## mode));
10172 /* Fixed-point type and mode nodes. */
10173 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
10174 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
10175 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
10176 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
10177 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
10178 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
10179 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
10180 MAKE_FIXED_MODE_NODE (accum, ha, HA)
10181 MAKE_FIXED_MODE_NODE (accum, sa, SA)
10182 MAKE_FIXED_MODE_NODE (accum, da, DA)
10183 MAKE_FIXED_MODE_NODE (accum, ta, TA)
10186 tree t = targetm.build_builtin_va_list ();
10188 /* Many back-ends define record types without setting TYPE_NAME.
10189 If we copied the record type here, we'd keep the original
10190 record type without a name. This breaks name mangling. So,
10191 don't copy record types and let c_common_nodes_and_builtins()
10192 declare the type to be __builtin_va_list. */
10193 if (TREE_CODE (t) != RECORD_TYPE)
10194 t = build_variant_type_copy (t);
10196 va_list_type_node = t;
10200 /* Modify DECL for given flags.
10201 TM_PURE attribute is set only on types, so the function will modify
10202 DECL's type when ECF_TM_PURE is used. */
10204 void
10205 set_call_expr_flags (tree decl, int flags)
10207 if (flags & ECF_NOTHROW)
10208 TREE_NOTHROW (decl) = 1;
10209 if (flags & ECF_CONST)
10210 TREE_READONLY (decl) = 1;
10211 if (flags & ECF_PURE)
10212 DECL_PURE_P (decl) = 1;
10213 if (flags & ECF_LOOPING_CONST_OR_PURE)
10214 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
10215 if (flags & ECF_NOVOPS)
10216 DECL_IS_NOVOPS (decl) = 1;
10217 if (flags & ECF_NORETURN)
10218 TREE_THIS_VOLATILE (decl) = 1;
10219 if (flags & ECF_MALLOC)
10220 DECL_IS_MALLOC (decl) = 1;
10221 if (flags & ECF_RETURNS_TWICE)
10222 DECL_IS_RETURNS_TWICE (decl) = 1;
10223 if (flags & ECF_LEAF)
10224 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("leaf"),
10225 NULL, DECL_ATTRIBUTES (decl));
10226 if ((flags & ECF_TM_PURE) && flag_tm)
10227 apply_tm_attr (decl, get_identifier ("transaction_pure"));
10228 /* Looping const or pure is implied by noreturn.
10229 There is currently no way to declare looping const or looping pure alone. */
10230 gcc_assert (!(flags & ECF_LOOPING_CONST_OR_PURE)
10231 || ((flags & ECF_NORETURN) && (flags & (ECF_CONST | ECF_PURE))));
10235 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
10237 static void
10238 local_define_builtin (const char *name, tree type, enum built_in_function code,
10239 const char *library_name, int ecf_flags)
10241 tree decl;
10243 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
10244 library_name, NULL_TREE);
10245 set_call_expr_flags (decl, ecf_flags);
10247 set_builtin_decl (code, decl, true);
10250 /* Call this function after instantiating all builtins that the language
10251 front end cares about. This will build the rest of the builtins
10252 and internal functions that are relied upon by the tree optimizers and
10253 the middle-end. */
10255 void
10256 build_common_builtin_nodes (void)
10258 tree tmp, ftype;
10259 int ecf_flags;
10261 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE))
10263 ftype = build_function_type (void_type_node, void_list_node);
10264 local_define_builtin ("__builtin_unreachable", ftype, BUILT_IN_UNREACHABLE,
10265 "__builtin_unreachable",
10266 ECF_NOTHROW | ECF_LEAF | ECF_NORETURN
10267 | ECF_CONST);
10270 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY)
10271 || !builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
10273 ftype = build_function_type_list (ptr_type_node,
10274 ptr_type_node, const_ptr_type_node,
10275 size_type_node, NULL_TREE);
10277 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY))
10278 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
10279 "memcpy", ECF_NOTHROW | ECF_LEAF);
10280 if (!builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
10281 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
10282 "memmove", ECF_NOTHROW | ECF_LEAF);
10285 if (!builtin_decl_explicit_p (BUILT_IN_MEMCMP))
10287 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
10288 const_ptr_type_node, size_type_node,
10289 NULL_TREE);
10290 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
10291 "memcmp", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10294 if (!builtin_decl_explicit_p (BUILT_IN_MEMSET))
10296 ftype = build_function_type_list (ptr_type_node,
10297 ptr_type_node, integer_type_node,
10298 size_type_node, NULL_TREE);
10299 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
10300 "memset", ECF_NOTHROW | ECF_LEAF);
10303 if (!builtin_decl_explicit_p (BUILT_IN_ALLOCA))
10305 ftype = build_function_type_list (ptr_type_node,
10306 size_type_node, NULL_TREE);
10307 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
10308 "alloca", ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
10311 ftype = build_function_type_list (ptr_type_node, size_type_node,
10312 size_type_node, NULL_TREE);
10313 local_define_builtin ("__builtin_alloca_with_align", ftype,
10314 BUILT_IN_ALLOCA_WITH_ALIGN,
10315 "__builtin_alloca_with_align",
10316 ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
10318 /* If we're checking the stack, `alloca' can throw. */
10319 if (flag_stack_check)
10321 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA)) = 0;
10322 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN)) = 0;
10325 ftype = build_function_type_list (void_type_node,
10326 ptr_type_node, ptr_type_node,
10327 ptr_type_node, NULL_TREE);
10328 local_define_builtin ("__builtin_init_trampoline", ftype,
10329 BUILT_IN_INIT_TRAMPOLINE,
10330 "__builtin_init_trampoline", ECF_NOTHROW | ECF_LEAF);
10331 local_define_builtin ("__builtin_init_heap_trampoline", ftype,
10332 BUILT_IN_INIT_HEAP_TRAMPOLINE,
10333 "__builtin_init_heap_trampoline",
10334 ECF_NOTHROW | ECF_LEAF);
10336 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
10337 local_define_builtin ("__builtin_adjust_trampoline", ftype,
10338 BUILT_IN_ADJUST_TRAMPOLINE,
10339 "__builtin_adjust_trampoline",
10340 ECF_CONST | ECF_NOTHROW);
10342 ftype = build_function_type_list (void_type_node,
10343 ptr_type_node, ptr_type_node, NULL_TREE);
10344 local_define_builtin ("__builtin_nonlocal_goto", ftype,
10345 BUILT_IN_NONLOCAL_GOTO,
10346 "__builtin_nonlocal_goto",
10347 ECF_NORETURN | ECF_NOTHROW);
10349 ftype = build_function_type_list (void_type_node,
10350 ptr_type_node, ptr_type_node, NULL_TREE);
10351 local_define_builtin ("__builtin_setjmp_setup", ftype,
10352 BUILT_IN_SETJMP_SETUP,
10353 "__builtin_setjmp_setup", ECF_NOTHROW);
10355 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10356 local_define_builtin ("__builtin_setjmp_receiver", ftype,
10357 BUILT_IN_SETJMP_RECEIVER,
10358 "__builtin_setjmp_receiver", ECF_NOTHROW | ECF_LEAF);
10360 ftype = build_function_type_list (ptr_type_node, NULL_TREE);
10361 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
10362 "__builtin_stack_save", ECF_NOTHROW | ECF_LEAF);
10364 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10365 local_define_builtin ("__builtin_stack_restore", ftype,
10366 BUILT_IN_STACK_RESTORE,
10367 "__builtin_stack_restore", ECF_NOTHROW | ECF_LEAF);
10369 /* If there's a possibility that we might use the ARM EABI, build the
10370 alternate __cxa_end_cleanup node used to resume from C++ and Java. */
10371 if (targetm.arm_eabi_unwinder)
10373 ftype = build_function_type_list (void_type_node, NULL_TREE);
10374 local_define_builtin ("__builtin_cxa_end_cleanup", ftype,
10375 BUILT_IN_CXA_END_CLEANUP,
10376 "__cxa_end_cleanup", ECF_NORETURN | ECF_LEAF);
10379 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10380 local_define_builtin ("__builtin_unwind_resume", ftype,
10381 BUILT_IN_UNWIND_RESUME,
10382 ((targetm_common.except_unwind_info (&global_options)
10383 == UI_SJLJ)
10384 ? "_Unwind_SjLj_Resume" : "_Unwind_Resume"),
10385 ECF_NORETURN);
10387 if (builtin_decl_explicit (BUILT_IN_RETURN_ADDRESS) == NULL_TREE)
10389 ftype = build_function_type_list (ptr_type_node, integer_type_node,
10390 NULL_TREE);
10391 local_define_builtin ("__builtin_return_address", ftype,
10392 BUILT_IN_RETURN_ADDRESS,
10393 "__builtin_return_address",
10394 ECF_NOTHROW);
10397 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER)
10398 || !builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
10400 ftype = build_function_type_list (void_type_node, ptr_type_node,
10401 ptr_type_node, NULL_TREE);
10402 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER))
10403 local_define_builtin ("__cyg_profile_func_enter", ftype,
10404 BUILT_IN_PROFILE_FUNC_ENTER,
10405 "__cyg_profile_func_enter", 0);
10406 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
10407 local_define_builtin ("__cyg_profile_func_exit", ftype,
10408 BUILT_IN_PROFILE_FUNC_EXIT,
10409 "__cyg_profile_func_exit", 0);
10412 /* The exception object and filter values from the runtime. The argument
10413 must be zero before exception lowering, i.e. from the front end. After
10414 exception lowering, it will be the region number for the exception
10415 landing pad. These functions are PURE instead of CONST to prevent
10416 them from being hoisted past the exception edge that will initialize
10417 its value in the landing pad. */
10418 ftype = build_function_type_list (ptr_type_node,
10419 integer_type_node, NULL_TREE);
10420 ecf_flags = ECF_PURE | ECF_NOTHROW | ECF_LEAF;
10421 /* Only use TM_PURE if we have TM language support. */
10422 if (builtin_decl_explicit_p (BUILT_IN_TM_LOAD_1))
10423 ecf_flags |= ECF_TM_PURE;
10424 local_define_builtin ("__builtin_eh_pointer", ftype, BUILT_IN_EH_POINTER,
10425 "__builtin_eh_pointer", ecf_flags);
10427 tmp = lang_hooks.types.type_for_mode (targetm.eh_return_filter_mode (), 0);
10428 ftype = build_function_type_list (tmp, integer_type_node, NULL_TREE);
10429 local_define_builtin ("__builtin_eh_filter", ftype, BUILT_IN_EH_FILTER,
10430 "__builtin_eh_filter", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10432 ftype = build_function_type_list (void_type_node,
10433 integer_type_node, integer_type_node,
10434 NULL_TREE);
10435 local_define_builtin ("__builtin_eh_copy_values", ftype,
10436 BUILT_IN_EH_COPY_VALUES,
10437 "__builtin_eh_copy_values", ECF_NOTHROW);
10439 /* Complex multiplication and division. These are handled as builtins
10440 rather than optabs because emit_library_call_value doesn't support
10441 complex. Further, we can do slightly better with folding these
10442 beasties if the real and complex parts of the arguments are separate. */
10444 int mode;
10446 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
10448 char mode_name_buf[4], *q;
10449 const char *p;
10450 enum built_in_function mcode, dcode;
10451 tree type, inner_type;
10452 const char *prefix = "__";
10454 if (targetm.libfunc_gnu_prefix)
10455 prefix = "__gnu_";
10457 type = lang_hooks.types.type_for_mode ((machine_mode) mode, 0);
10458 if (type == NULL)
10459 continue;
10460 inner_type = TREE_TYPE (type);
10462 ftype = build_function_type_list (type, inner_type, inner_type,
10463 inner_type, inner_type, NULL_TREE);
10465 mcode = ((enum built_in_function)
10466 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10467 dcode = ((enum built_in_function)
10468 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10470 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
10471 *q = TOLOWER (*p);
10472 *q = '\0';
10474 built_in_names[mcode] = concat (prefix, "mul", mode_name_buf, "3",
10475 NULL);
10476 local_define_builtin (built_in_names[mcode], ftype, mcode,
10477 built_in_names[mcode],
10478 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
10480 built_in_names[dcode] = concat (prefix, "div", mode_name_buf, "3",
10481 NULL);
10482 local_define_builtin (built_in_names[dcode], ftype, dcode,
10483 built_in_names[dcode],
10484 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
10488 init_internal_fns ();
10491 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
10492 better way.
10494 If we requested a pointer to a vector, build up the pointers that
10495 we stripped off while looking for the inner type. Similarly for
10496 return values from functions.
10498 The argument TYPE is the top of the chain, and BOTTOM is the
10499 new type which we will point to. */
10501 tree
10502 reconstruct_complex_type (tree type, tree bottom)
10504 tree inner, outer;
10506 if (TREE_CODE (type) == POINTER_TYPE)
10508 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10509 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
10510 TYPE_REF_CAN_ALIAS_ALL (type));
10512 else if (TREE_CODE (type) == REFERENCE_TYPE)
10514 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10515 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
10516 TYPE_REF_CAN_ALIAS_ALL (type));
10518 else if (TREE_CODE (type) == ARRAY_TYPE)
10520 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10521 outer = build_array_type (inner, TYPE_DOMAIN (type));
10523 else if (TREE_CODE (type) == FUNCTION_TYPE)
10525 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10526 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
10528 else if (TREE_CODE (type) == METHOD_TYPE)
10530 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10531 /* The build_method_type_directly() routine prepends 'this' to argument list,
10532 so we must compensate by getting rid of it. */
10533 outer
10534 = build_method_type_directly
10535 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
10536 inner,
10537 TREE_CHAIN (TYPE_ARG_TYPES (type)));
10539 else if (TREE_CODE (type) == OFFSET_TYPE)
10541 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10542 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
10544 else
10545 return bottom;
10547 return build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
10548 TYPE_QUALS (type));
10551 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
10552 the inner type. */
10553 tree
10554 build_vector_type_for_mode (tree innertype, machine_mode mode)
10556 int nunits;
10558 switch (GET_MODE_CLASS (mode))
10560 case MODE_VECTOR_INT:
10561 case MODE_VECTOR_FLOAT:
10562 case MODE_VECTOR_FRACT:
10563 case MODE_VECTOR_UFRACT:
10564 case MODE_VECTOR_ACCUM:
10565 case MODE_VECTOR_UACCUM:
10566 nunits = GET_MODE_NUNITS (mode);
10567 break;
10569 case MODE_INT:
10570 /* Check that there are no leftover bits. */
10571 gcc_assert (GET_MODE_BITSIZE (mode)
10572 % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
10574 nunits = GET_MODE_BITSIZE (mode)
10575 / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
10576 break;
10578 default:
10579 gcc_unreachable ();
10582 return make_vector_type (innertype, nunits, mode);
10585 /* Similarly, but takes the inner type and number of units, which must be
10586 a power of two. */
10588 tree
10589 build_vector_type (tree innertype, int nunits)
10591 return make_vector_type (innertype, nunits, VOIDmode);
10594 /* Similarly, but builds a variant type with TYPE_VECTOR_OPAQUE set. */
10596 tree
10597 build_opaque_vector_type (tree innertype, int nunits)
10599 tree t = make_vector_type (innertype, nunits, VOIDmode);
10600 tree cand;
10601 /* We always build the non-opaque variant before the opaque one,
10602 so if it already exists, it is TYPE_NEXT_VARIANT of this one. */
10603 cand = TYPE_NEXT_VARIANT (t);
10604 if (cand
10605 && TYPE_VECTOR_OPAQUE (cand)
10606 && check_qualified_type (cand, t, TYPE_QUALS (t)))
10607 return cand;
10608 /* Othewise build a variant type and make sure to queue it after
10609 the non-opaque type. */
10610 cand = build_distinct_type_copy (t);
10611 TYPE_VECTOR_OPAQUE (cand) = true;
10612 TYPE_CANONICAL (cand) = TYPE_CANONICAL (t);
10613 TYPE_NEXT_VARIANT (cand) = TYPE_NEXT_VARIANT (t);
10614 TYPE_NEXT_VARIANT (t) = cand;
10615 TYPE_MAIN_VARIANT (cand) = TYPE_MAIN_VARIANT (t);
10616 return cand;
10620 /* Given an initializer INIT, return TRUE if INIT is zero or some
10621 aggregate of zeros. Otherwise return FALSE. */
10622 bool
10623 initializer_zerop (const_tree init)
10625 tree elt;
10627 STRIP_NOPS (init);
10629 switch (TREE_CODE (init))
10631 case INTEGER_CST:
10632 return integer_zerop (init);
10634 case REAL_CST:
10635 /* ??? Note that this is not correct for C4X float formats. There,
10636 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
10637 negative exponent. */
10638 return real_zerop (init)
10639 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
10641 case FIXED_CST:
10642 return fixed_zerop (init);
10644 case COMPLEX_CST:
10645 return integer_zerop (init)
10646 || (real_zerop (init)
10647 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
10648 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
10650 case VECTOR_CST:
10652 unsigned i;
10653 for (i = 0; i < VECTOR_CST_NELTS (init); ++i)
10654 if (!initializer_zerop (VECTOR_CST_ELT (init, i)))
10655 return false;
10656 return true;
10659 case CONSTRUCTOR:
10661 unsigned HOST_WIDE_INT idx;
10663 if (TREE_CLOBBER_P (init))
10664 return false;
10665 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
10666 if (!initializer_zerop (elt))
10667 return false;
10668 return true;
10671 case STRING_CST:
10673 int i;
10675 /* We need to loop through all elements to handle cases like
10676 "\0" and "\0foobar". */
10677 for (i = 0; i < TREE_STRING_LENGTH (init); ++i)
10678 if (TREE_STRING_POINTER (init)[i] != '\0')
10679 return false;
10681 return true;
10684 default:
10685 return false;
10689 /* Check if vector VEC consists of all the equal elements and
10690 that the number of elements corresponds to the type of VEC.
10691 The function returns first element of the vector
10692 or NULL_TREE if the vector is not uniform. */
10693 tree
10694 uniform_vector_p (const_tree vec)
10696 tree first, t;
10697 unsigned i;
10699 if (vec == NULL_TREE)
10700 return NULL_TREE;
10702 gcc_assert (VECTOR_TYPE_P (TREE_TYPE (vec)));
10704 if (TREE_CODE (vec) == VECTOR_CST)
10706 first = VECTOR_CST_ELT (vec, 0);
10707 for (i = 1; i < VECTOR_CST_NELTS (vec); ++i)
10708 if (!operand_equal_p (first, VECTOR_CST_ELT (vec, i), 0))
10709 return NULL_TREE;
10711 return first;
10714 else if (TREE_CODE (vec) == CONSTRUCTOR)
10716 first = error_mark_node;
10718 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (vec), i, t)
10720 if (i == 0)
10722 first = t;
10723 continue;
10725 if (!operand_equal_p (first, t, 0))
10726 return NULL_TREE;
10728 if (i != TYPE_VECTOR_SUBPARTS (TREE_TYPE (vec)))
10729 return NULL_TREE;
10731 return first;
10734 return NULL_TREE;
10737 /* Build an empty statement at location LOC. */
10739 tree
10740 build_empty_stmt (location_t loc)
10742 tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
10743 SET_EXPR_LOCATION (t, loc);
10744 return t;
10748 /* Build an OpenMP clause with code CODE. LOC is the location of the
10749 clause. */
10751 tree
10752 build_omp_clause (location_t loc, enum omp_clause_code code)
10754 tree t;
10755 int size, length;
10757 length = omp_clause_num_ops[code];
10758 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
10760 record_node_allocation_statistics (OMP_CLAUSE, size);
10762 t = (tree) ggc_internal_alloc (size);
10763 memset (t, 0, size);
10764 TREE_SET_CODE (t, OMP_CLAUSE);
10765 OMP_CLAUSE_SET_CODE (t, code);
10766 OMP_CLAUSE_LOCATION (t) = loc;
10768 return t;
10771 /* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
10772 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
10773 Except for the CODE and operand count field, other storage for the
10774 object is initialized to zeros. */
10776 tree
10777 build_vl_exp_stat (enum tree_code code, int len MEM_STAT_DECL)
10779 tree t;
10780 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
10782 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
10783 gcc_assert (len >= 1);
10785 record_node_allocation_statistics (code, length);
10787 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
10789 TREE_SET_CODE (t, code);
10791 /* Can't use TREE_OPERAND to store the length because if checking is
10792 enabled, it will try to check the length before we store it. :-P */
10793 t->exp.operands[0] = build_int_cst (sizetype, len);
10795 return t;
10798 /* Helper function for build_call_* functions; build a CALL_EXPR with
10799 indicated RETURN_TYPE, FN, and NARGS, but do not initialize any of
10800 the argument slots. */
10802 static tree
10803 build_call_1 (tree return_type, tree fn, int nargs)
10805 tree t;
10807 t = build_vl_exp (CALL_EXPR, nargs + 3);
10808 TREE_TYPE (t) = return_type;
10809 CALL_EXPR_FN (t) = fn;
10810 CALL_EXPR_STATIC_CHAIN (t) = NULL;
10812 return t;
10815 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10816 FN and a null static chain slot. NARGS is the number of call arguments
10817 which are specified as "..." arguments. */
10819 tree
10820 build_call_nary (tree return_type, tree fn, int nargs, ...)
10822 tree ret;
10823 va_list args;
10824 va_start (args, nargs);
10825 ret = build_call_valist (return_type, fn, nargs, args);
10826 va_end (args);
10827 return ret;
10830 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10831 FN and a null static chain slot. NARGS is the number of call arguments
10832 which are specified as a va_list ARGS. */
10834 tree
10835 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
10837 tree t;
10838 int i;
10840 t = build_call_1 (return_type, fn, nargs);
10841 for (i = 0; i < nargs; i++)
10842 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
10843 process_call_operands (t);
10844 return t;
10847 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10848 FN and a null static chain slot. NARGS is the number of call arguments
10849 which are specified as a tree array ARGS. */
10851 tree
10852 build_call_array_loc (location_t loc, tree return_type, tree fn,
10853 int nargs, const tree *args)
10855 tree t;
10856 int i;
10858 t = build_call_1 (return_type, fn, nargs);
10859 for (i = 0; i < nargs; i++)
10860 CALL_EXPR_ARG (t, i) = args[i];
10861 process_call_operands (t);
10862 SET_EXPR_LOCATION (t, loc);
10863 return t;
10866 /* Like build_call_array, but takes a vec. */
10868 tree
10869 build_call_vec (tree return_type, tree fn, vec<tree, va_gc> *args)
10871 tree ret, t;
10872 unsigned int ix;
10874 ret = build_call_1 (return_type, fn, vec_safe_length (args));
10875 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
10876 CALL_EXPR_ARG (ret, ix) = t;
10877 process_call_operands (ret);
10878 return ret;
10881 /* Conveniently construct a function call expression. FNDECL names the
10882 function to be called and N arguments are passed in the array
10883 ARGARRAY. */
10885 tree
10886 build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
10888 tree fntype = TREE_TYPE (fndecl);
10889 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
10891 return fold_build_call_array_loc (loc, TREE_TYPE (fntype), fn, n, argarray);
10894 /* Conveniently construct a function call expression. FNDECL names the
10895 function to be called and the arguments are passed in the vector
10896 VEC. */
10898 tree
10899 build_call_expr_loc_vec (location_t loc, tree fndecl, vec<tree, va_gc> *vec)
10901 return build_call_expr_loc_array (loc, fndecl, vec_safe_length (vec),
10902 vec_safe_address (vec));
10906 /* Conveniently construct a function call expression. FNDECL names the
10907 function to be called, N is the number of arguments, and the "..."
10908 parameters are the argument expressions. */
10910 tree
10911 build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
10913 va_list ap;
10914 tree *argarray = XALLOCAVEC (tree, n);
10915 int i;
10917 va_start (ap, n);
10918 for (i = 0; i < n; i++)
10919 argarray[i] = va_arg (ap, tree);
10920 va_end (ap);
10921 return build_call_expr_loc_array (loc, fndecl, n, argarray);
10924 /* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
10925 varargs macros aren't supported by all bootstrap compilers. */
10927 tree
10928 build_call_expr (tree fndecl, int n, ...)
10930 va_list ap;
10931 tree *argarray = XALLOCAVEC (tree, n);
10932 int i;
10934 va_start (ap, n);
10935 for (i = 0; i < n; i++)
10936 argarray[i] = va_arg (ap, tree);
10937 va_end (ap);
10938 return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
10941 /* Build internal call expression. This is just like CALL_EXPR, except
10942 its CALL_EXPR_FN is NULL. It will get gimplified later into ordinary
10943 internal function. */
10945 tree
10946 build_call_expr_internal_loc (location_t loc, enum internal_fn ifn,
10947 tree type, int n, ...)
10949 va_list ap;
10950 int i;
10952 tree fn = build_call_1 (type, NULL_TREE, n);
10953 va_start (ap, n);
10954 for (i = 0; i < n; i++)
10955 CALL_EXPR_ARG (fn, i) = va_arg (ap, tree);
10956 va_end (ap);
10957 SET_EXPR_LOCATION (fn, loc);
10958 CALL_EXPR_IFN (fn) = ifn;
10959 return fn;
10962 /* Create a new constant string literal and return a char* pointer to it.
10963 The STRING_CST value is the LEN characters at STR. */
10964 tree
10965 build_string_literal (int len, const char *str)
10967 tree t, elem, index, type;
10969 t = build_string (len, str);
10970 elem = build_type_variant (char_type_node, 1, 0);
10971 index = build_index_type (size_int (len - 1));
10972 type = build_array_type (elem, index);
10973 TREE_TYPE (t) = type;
10974 TREE_CONSTANT (t) = 1;
10975 TREE_READONLY (t) = 1;
10976 TREE_STATIC (t) = 1;
10978 type = build_pointer_type (elem);
10979 t = build1 (ADDR_EXPR, type,
10980 build4 (ARRAY_REF, elem,
10981 t, integer_zero_node, NULL_TREE, NULL_TREE));
10982 return t;
10987 /* Return true if T (assumed to be a DECL) must be assigned a memory
10988 location. */
10990 bool
10991 needs_to_live_in_memory (const_tree t)
10993 return (TREE_ADDRESSABLE (t)
10994 || is_global_var (t)
10995 || (TREE_CODE (t) == RESULT_DECL
10996 && !DECL_BY_REFERENCE (t)
10997 && aggregate_value_p (t, current_function_decl)));
11000 /* Return value of a constant X and sign-extend it. */
11002 HOST_WIDE_INT
11003 int_cst_value (const_tree x)
11005 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
11006 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
11008 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
11009 gcc_assert (cst_and_fits_in_hwi (x));
11011 if (bits < HOST_BITS_PER_WIDE_INT)
11013 bool negative = ((val >> (bits - 1)) & 1) != 0;
11014 if (negative)
11015 val |= (~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1;
11016 else
11017 val &= ~((~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1);
11020 return val;
11023 /* If TYPE is an integral or pointer type, return an integer type with
11024 the same precision which is unsigned iff UNSIGNEDP is true, or itself
11025 if TYPE is already an integer type of signedness UNSIGNEDP. */
11027 tree
11028 signed_or_unsigned_type_for (int unsignedp, tree type)
11030 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type) == unsignedp)
11031 return type;
11033 if (TREE_CODE (type) == VECTOR_TYPE)
11035 tree inner = TREE_TYPE (type);
11036 tree inner2 = signed_or_unsigned_type_for (unsignedp, inner);
11037 if (!inner2)
11038 return NULL_TREE;
11039 if (inner == inner2)
11040 return type;
11041 return build_vector_type (inner2, TYPE_VECTOR_SUBPARTS (type));
11044 if (!INTEGRAL_TYPE_P (type)
11045 && !POINTER_TYPE_P (type)
11046 && TREE_CODE (type) != OFFSET_TYPE)
11047 return NULL_TREE;
11049 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
11052 /* If TYPE is an integral or pointer type, return an integer type with
11053 the same precision which is unsigned, or itself if TYPE is already an
11054 unsigned integer type. */
11056 tree
11057 unsigned_type_for (tree type)
11059 return signed_or_unsigned_type_for (1, type);
11062 /* If TYPE is an integral or pointer type, return an integer type with
11063 the same precision which is signed, or itself if TYPE is already a
11064 signed integer type. */
11066 tree
11067 signed_type_for (tree type)
11069 return signed_or_unsigned_type_for (0, type);
11072 /* If TYPE is a vector type, return a signed integer vector type with the
11073 same width and number of subparts. Otherwise return boolean_type_node. */
11075 tree
11076 truth_type_for (tree type)
11078 if (TREE_CODE (type) == VECTOR_TYPE)
11080 tree elem = lang_hooks.types.type_for_size
11081 (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))), 0);
11082 return build_opaque_vector_type (elem, TYPE_VECTOR_SUBPARTS (type));
11084 else
11085 return boolean_type_node;
11088 /* Returns the largest value obtainable by casting something in INNER type to
11089 OUTER type. */
11091 tree
11092 upper_bound_in_type (tree outer, tree inner)
11094 unsigned int det = 0;
11095 unsigned oprec = TYPE_PRECISION (outer);
11096 unsigned iprec = TYPE_PRECISION (inner);
11097 unsigned prec;
11099 /* Compute a unique number for every combination. */
11100 det |= (oprec > iprec) ? 4 : 0;
11101 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
11102 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
11104 /* Determine the exponent to use. */
11105 switch (det)
11107 case 0:
11108 case 1:
11109 /* oprec <= iprec, outer: signed, inner: don't care. */
11110 prec = oprec - 1;
11111 break;
11112 case 2:
11113 case 3:
11114 /* oprec <= iprec, outer: unsigned, inner: don't care. */
11115 prec = oprec;
11116 break;
11117 case 4:
11118 /* oprec > iprec, outer: signed, inner: signed. */
11119 prec = iprec - 1;
11120 break;
11121 case 5:
11122 /* oprec > iprec, outer: signed, inner: unsigned. */
11123 prec = iprec;
11124 break;
11125 case 6:
11126 /* oprec > iprec, outer: unsigned, inner: signed. */
11127 prec = oprec;
11128 break;
11129 case 7:
11130 /* oprec > iprec, outer: unsigned, inner: unsigned. */
11131 prec = iprec;
11132 break;
11133 default:
11134 gcc_unreachable ();
11137 return wide_int_to_tree (outer,
11138 wi::mask (prec, false, TYPE_PRECISION (outer)));
11141 /* Returns the smallest value obtainable by casting something in INNER type to
11142 OUTER type. */
11144 tree
11145 lower_bound_in_type (tree outer, tree inner)
11147 unsigned oprec = TYPE_PRECISION (outer);
11148 unsigned iprec = TYPE_PRECISION (inner);
11150 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
11151 and obtain 0. */
11152 if (TYPE_UNSIGNED (outer)
11153 /* If we are widening something of an unsigned type, OUTER type
11154 contains all values of INNER type. In particular, both INNER
11155 and OUTER types have zero in common. */
11156 || (oprec > iprec && TYPE_UNSIGNED (inner)))
11157 return build_int_cst (outer, 0);
11158 else
11160 /* If we are widening a signed type to another signed type, we
11161 want to obtain -2^^(iprec-1). If we are keeping the
11162 precision or narrowing to a signed type, we want to obtain
11163 -2^(oprec-1). */
11164 unsigned prec = oprec > iprec ? iprec : oprec;
11165 return wide_int_to_tree (outer,
11166 wi::mask (prec - 1, true,
11167 TYPE_PRECISION (outer)));
11171 /* Return nonzero if two operands that are suitable for PHI nodes are
11172 necessarily equal. Specifically, both ARG0 and ARG1 must be either
11173 SSA_NAME or invariant. Note that this is strictly an optimization.
11174 That is, callers of this function can directly call operand_equal_p
11175 and get the same result, only slower. */
11178 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
11180 if (arg0 == arg1)
11181 return 1;
11182 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
11183 return 0;
11184 return operand_equal_p (arg0, arg1, 0);
11187 /* Returns number of zeros at the end of binary representation of X. */
11189 tree
11190 num_ending_zeros (const_tree x)
11192 return build_int_cst (TREE_TYPE (x), wi::ctz (x));
11196 #define WALK_SUBTREE(NODE) \
11197 do \
11199 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
11200 if (result) \
11201 return result; \
11203 while (0)
11205 /* This is a subroutine of walk_tree that walks field of TYPE that are to
11206 be walked whenever a type is seen in the tree. Rest of operands and return
11207 value are as for walk_tree. */
11209 static tree
11210 walk_type_fields (tree type, walk_tree_fn func, void *data,
11211 hash_set<tree> *pset, walk_tree_lh lh)
11213 tree result = NULL_TREE;
11215 switch (TREE_CODE (type))
11217 case POINTER_TYPE:
11218 case REFERENCE_TYPE:
11219 case VECTOR_TYPE:
11220 /* We have to worry about mutually recursive pointers. These can't
11221 be written in C. They can in Ada. It's pathological, but
11222 there's an ACATS test (c38102a) that checks it. Deal with this
11223 by checking if we're pointing to another pointer, that one
11224 points to another pointer, that one does too, and we have no htab.
11225 If so, get a hash table. We check three levels deep to avoid
11226 the cost of the hash table if we don't need one. */
11227 if (POINTER_TYPE_P (TREE_TYPE (type))
11228 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
11229 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
11230 && !pset)
11232 result = walk_tree_without_duplicates (&TREE_TYPE (type),
11233 func, data);
11234 if (result)
11235 return result;
11237 break;
11240 /* ... fall through ... */
11242 case COMPLEX_TYPE:
11243 WALK_SUBTREE (TREE_TYPE (type));
11244 break;
11246 case METHOD_TYPE:
11247 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
11249 /* Fall through. */
11251 case FUNCTION_TYPE:
11252 WALK_SUBTREE (TREE_TYPE (type));
11254 tree arg;
11256 /* We never want to walk into default arguments. */
11257 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
11258 WALK_SUBTREE (TREE_VALUE (arg));
11260 break;
11262 case ARRAY_TYPE:
11263 /* Don't follow this nodes's type if a pointer for fear that
11264 we'll have infinite recursion. If we have a PSET, then we
11265 need not fear. */
11266 if (pset
11267 || (!POINTER_TYPE_P (TREE_TYPE (type))
11268 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
11269 WALK_SUBTREE (TREE_TYPE (type));
11270 WALK_SUBTREE (TYPE_DOMAIN (type));
11271 break;
11273 case OFFSET_TYPE:
11274 WALK_SUBTREE (TREE_TYPE (type));
11275 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
11276 break;
11278 default:
11279 break;
11282 return NULL_TREE;
11285 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
11286 called with the DATA and the address of each sub-tree. If FUNC returns a
11287 non-NULL value, the traversal is stopped, and the value returned by FUNC
11288 is returned. If PSET is non-NULL it is used to record the nodes visited,
11289 and to avoid visiting a node more than once. */
11291 tree
11292 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
11293 hash_set<tree> *pset, walk_tree_lh lh)
11295 enum tree_code code;
11296 int walk_subtrees;
11297 tree result;
11299 #define WALK_SUBTREE_TAIL(NODE) \
11300 do \
11302 tp = & (NODE); \
11303 goto tail_recurse; \
11305 while (0)
11307 tail_recurse:
11308 /* Skip empty subtrees. */
11309 if (!*tp)
11310 return NULL_TREE;
11312 /* Don't walk the same tree twice, if the user has requested
11313 that we avoid doing so. */
11314 if (pset && pset->add (*tp))
11315 return NULL_TREE;
11317 /* Call the function. */
11318 walk_subtrees = 1;
11319 result = (*func) (tp, &walk_subtrees, data);
11321 /* If we found something, return it. */
11322 if (result)
11323 return result;
11325 code = TREE_CODE (*tp);
11327 /* Even if we didn't, FUNC may have decided that there was nothing
11328 interesting below this point in the tree. */
11329 if (!walk_subtrees)
11331 /* But we still need to check our siblings. */
11332 if (code == TREE_LIST)
11333 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
11334 else if (code == OMP_CLAUSE)
11335 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11336 else
11337 return NULL_TREE;
11340 if (lh)
11342 result = (*lh) (tp, &walk_subtrees, func, data, pset);
11343 if (result || !walk_subtrees)
11344 return result;
11347 switch (code)
11349 case ERROR_MARK:
11350 case IDENTIFIER_NODE:
11351 case INTEGER_CST:
11352 case REAL_CST:
11353 case FIXED_CST:
11354 case VECTOR_CST:
11355 case STRING_CST:
11356 case BLOCK:
11357 case PLACEHOLDER_EXPR:
11358 case SSA_NAME:
11359 case FIELD_DECL:
11360 case RESULT_DECL:
11361 /* None of these have subtrees other than those already walked
11362 above. */
11363 break;
11365 case TREE_LIST:
11366 WALK_SUBTREE (TREE_VALUE (*tp));
11367 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
11368 break;
11370 case TREE_VEC:
11372 int len = TREE_VEC_LENGTH (*tp);
11374 if (len == 0)
11375 break;
11377 /* Walk all elements but the first. */
11378 while (--len)
11379 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
11381 /* Now walk the first one as a tail call. */
11382 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
11385 case COMPLEX_CST:
11386 WALK_SUBTREE (TREE_REALPART (*tp));
11387 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
11389 case CONSTRUCTOR:
11391 unsigned HOST_WIDE_INT idx;
11392 constructor_elt *ce;
11394 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (*tp), idx, &ce);
11395 idx++)
11396 WALK_SUBTREE (ce->value);
11398 break;
11400 case SAVE_EXPR:
11401 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
11403 case BIND_EXPR:
11405 tree decl;
11406 for (decl = BIND_EXPR_VARS (*tp); decl; decl = DECL_CHAIN (decl))
11408 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
11409 into declarations that are just mentioned, rather than
11410 declared; they don't really belong to this part of the tree.
11411 And, we can see cycles: the initializer for a declaration
11412 can refer to the declaration itself. */
11413 WALK_SUBTREE (DECL_INITIAL (decl));
11414 WALK_SUBTREE (DECL_SIZE (decl));
11415 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
11417 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
11420 case STATEMENT_LIST:
11422 tree_stmt_iterator i;
11423 for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
11424 WALK_SUBTREE (*tsi_stmt_ptr (i));
11426 break;
11428 case OMP_CLAUSE:
11429 switch (OMP_CLAUSE_CODE (*tp))
11431 case OMP_CLAUSE_GANG:
11432 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 1));
11433 /* FALLTHRU */
11435 case OMP_CLAUSE_DEVICE_RESIDENT:
11436 case OMP_CLAUSE_USE_DEVICE:
11437 case OMP_CLAUSE_ASYNC:
11438 case OMP_CLAUSE_WAIT:
11439 case OMP_CLAUSE_WORKER:
11440 case OMP_CLAUSE_VECTOR:
11441 case OMP_CLAUSE_NUM_GANGS:
11442 case OMP_CLAUSE_NUM_WORKERS:
11443 case OMP_CLAUSE_VECTOR_LENGTH:
11444 case OMP_CLAUSE_PRIVATE:
11445 case OMP_CLAUSE_SHARED:
11446 case OMP_CLAUSE_FIRSTPRIVATE:
11447 case OMP_CLAUSE_COPYIN:
11448 case OMP_CLAUSE_COPYPRIVATE:
11449 case OMP_CLAUSE_FINAL:
11450 case OMP_CLAUSE_IF:
11451 case OMP_CLAUSE_NUM_THREADS:
11452 case OMP_CLAUSE_SCHEDULE:
11453 case OMP_CLAUSE_UNIFORM:
11454 case OMP_CLAUSE_DEPEND:
11455 case OMP_CLAUSE_NUM_TEAMS:
11456 case OMP_CLAUSE_THREAD_LIMIT:
11457 case OMP_CLAUSE_DEVICE:
11458 case OMP_CLAUSE_DIST_SCHEDULE:
11459 case OMP_CLAUSE_SAFELEN:
11460 case OMP_CLAUSE_SIMDLEN:
11461 case OMP_CLAUSE__LOOPTEMP_:
11462 case OMP_CLAUSE__SIMDUID_:
11463 case OMP_CLAUSE__CILK_FOR_COUNT_:
11464 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0));
11465 /* FALLTHRU */
11467 case OMP_CLAUSE_INDEPENDENT:
11468 case OMP_CLAUSE_NOWAIT:
11469 case OMP_CLAUSE_ORDERED:
11470 case OMP_CLAUSE_DEFAULT:
11471 case OMP_CLAUSE_UNTIED:
11472 case OMP_CLAUSE_MERGEABLE:
11473 case OMP_CLAUSE_PROC_BIND:
11474 case OMP_CLAUSE_INBRANCH:
11475 case OMP_CLAUSE_NOTINBRANCH:
11476 case OMP_CLAUSE_FOR:
11477 case OMP_CLAUSE_PARALLEL:
11478 case OMP_CLAUSE_SECTIONS:
11479 case OMP_CLAUSE_TASKGROUP:
11480 case OMP_CLAUSE_AUTO:
11481 case OMP_CLAUSE_SEQ:
11482 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11484 case OMP_CLAUSE_LASTPRIVATE:
11485 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11486 WALK_SUBTREE (OMP_CLAUSE_LASTPRIVATE_STMT (*tp));
11487 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11489 case OMP_CLAUSE_COLLAPSE:
11491 int i;
11492 for (i = 0; i < 3; i++)
11493 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
11494 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11497 case OMP_CLAUSE_LINEAR:
11498 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11499 WALK_SUBTREE (OMP_CLAUSE_LINEAR_STEP (*tp));
11500 WALK_SUBTREE (OMP_CLAUSE_LINEAR_STMT (*tp));
11501 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11503 case OMP_CLAUSE_ALIGNED:
11504 case OMP_CLAUSE_FROM:
11505 case OMP_CLAUSE_TO:
11506 case OMP_CLAUSE_MAP:
11507 case OMP_CLAUSE__CACHE_:
11508 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11509 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 1));
11510 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11512 case OMP_CLAUSE_REDUCTION:
11514 int i;
11515 for (i = 0; i < 4; i++)
11516 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
11517 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11520 default:
11521 gcc_unreachable ();
11523 break;
11525 case TARGET_EXPR:
11527 int i, len;
11529 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
11530 But, we only want to walk once. */
11531 len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
11532 for (i = 0; i < len; ++i)
11533 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11534 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
11537 case DECL_EXPR:
11538 /* If this is a TYPE_DECL, walk into the fields of the type that it's
11539 defining. We only want to walk into these fields of a type in this
11540 case and not in the general case of a mere reference to the type.
11542 The criterion is as follows: if the field can be an expression, it
11543 must be walked only here. This should be in keeping with the fields
11544 that are directly gimplified in gimplify_type_sizes in order for the
11545 mark/copy-if-shared/unmark machinery of the gimplifier to work with
11546 variable-sized types.
11548 Note that DECLs get walked as part of processing the BIND_EXPR. */
11549 if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
11551 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
11552 if (TREE_CODE (*type_p) == ERROR_MARK)
11553 return NULL_TREE;
11555 /* Call the function for the type. See if it returns anything or
11556 doesn't want us to continue. If we are to continue, walk both
11557 the normal fields and those for the declaration case. */
11558 result = (*func) (type_p, &walk_subtrees, data);
11559 if (result || !walk_subtrees)
11560 return result;
11562 /* But do not walk a pointed-to type since it may itself need to
11563 be walked in the declaration case if it isn't anonymous. */
11564 if (!POINTER_TYPE_P (*type_p))
11566 result = walk_type_fields (*type_p, func, data, pset, lh);
11567 if (result)
11568 return result;
11571 /* If this is a record type, also walk the fields. */
11572 if (RECORD_OR_UNION_TYPE_P (*type_p))
11574 tree field;
11576 for (field = TYPE_FIELDS (*type_p); field;
11577 field = DECL_CHAIN (field))
11579 /* We'd like to look at the type of the field, but we can
11580 easily get infinite recursion. So assume it's pointed
11581 to elsewhere in the tree. Also, ignore things that
11582 aren't fields. */
11583 if (TREE_CODE (field) != FIELD_DECL)
11584 continue;
11586 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
11587 WALK_SUBTREE (DECL_SIZE (field));
11588 WALK_SUBTREE (DECL_SIZE_UNIT (field));
11589 if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
11590 WALK_SUBTREE (DECL_QUALIFIER (field));
11594 /* Same for scalar types. */
11595 else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
11596 || TREE_CODE (*type_p) == ENUMERAL_TYPE
11597 || TREE_CODE (*type_p) == INTEGER_TYPE
11598 || TREE_CODE (*type_p) == FIXED_POINT_TYPE
11599 || TREE_CODE (*type_p) == REAL_TYPE)
11601 WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
11602 WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
11605 WALK_SUBTREE (TYPE_SIZE (*type_p));
11606 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
11608 /* FALLTHRU */
11610 default:
11611 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
11613 int i, len;
11615 /* Walk over all the sub-trees of this operand. */
11616 len = TREE_OPERAND_LENGTH (*tp);
11618 /* Go through the subtrees. We need to do this in forward order so
11619 that the scope of a FOR_EXPR is handled properly. */
11620 if (len)
11622 for (i = 0; i < len - 1; ++i)
11623 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11624 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
11627 /* If this is a type, walk the needed fields in the type. */
11628 else if (TYPE_P (*tp))
11629 return walk_type_fields (*tp, func, data, pset, lh);
11630 break;
11633 /* We didn't find what we were looking for. */
11634 return NULL_TREE;
11636 #undef WALK_SUBTREE_TAIL
11638 #undef WALK_SUBTREE
11640 /* Like walk_tree, but does not walk duplicate nodes more than once. */
11642 tree
11643 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
11644 walk_tree_lh lh)
11646 tree result;
11648 hash_set<tree> pset;
11649 result = walk_tree_1 (tp, func, data, &pset, lh);
11650 return result;
11654 tree
11655 tree_block (tree t)
11657 const enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
11659 if (IS_EXPR_CODE_CLASS (c))
11660 return LOCATION_BLOCK (t->exp.locus);
11661 gcc_unreachable ();
11662 return NULL;
11665 void
11666 tree_set_block (tree t, tree b)
11668 const enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
11670 if (IS_EXPR_CODE_CLASS (c))
11672 if (b)
11673 t->exp.locus = COMBINE_LOCATION_DATA (line_table, t->exp.locus, b);
11674 else
11675 t->exp.locus = LOCATION_LOCUS (t->exp.locus);
11677 else
11678 gcc_unreachable ();
11681 /* Create a nameless artificial label and put it in the current
11682 function context. The label has a location of LOC. Returns the
11683 newly created label. */
11685 tree
11686 create_artificial_label (location_t loc)
11688 tree lab = build_decl (loc,
11689 LABEL_DECL, NULL_TREE, void_type_node);
11691 DECL_ARTIFICIAL (lab) = 1;
11692 DECL_IGNORED_P (lab) = 1;
11693 DECL_CONTEXT (lab) = current_function_decl;
11694 return lab;
11697 /* Given a tree, try to return a useful variable name that we can use
11698 to prefix a temporary that is being assigned the value of the tree.
11699 I.E. given <temp> = &A, return A. */
11701 const char *
11702 get_name (tree t)
11704 tree stripped_decl;
11706 stripped_decl = t;
11707 STRIP_NOPS (stripped_decl);
11708 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
11709 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
11710 else if (TREE_CODE (stripped_decl) == SSA_NAME)
11712 tree name = SSA_NAME_IDENTIFIER (stripped_decl);
11713 if (!name)
11714 return NULL;
11715 return IDENTIFIER_POINTER (name);
11717 else
11719 switch (TREE_CODE (stripped_decl))
11721 case ADDR_EXPR:
11722 return get_name (TREE_OPERAND (stripped_decl, 0));
11723 default:
11724 return NULL;
11729 /* Return true if TYPE has a variable argument list. */
11731 bool
11732 stdarg_p (const_tree fntype)
11734 function_args_iterator args_iter;
11735 tree n = NULL_TREE, t;
11737 if (!fntype)
11738 return false;
11740 FOREACH_FUNCTION_ARGS (fntype, t, args_iter)
11742 n = t;
11745 return n != NULL_TREE && n != void_type_node;
11748 /* Return true if TYPE has a prototype. */
11750 bool
11751 prototype_p (const_tree fntype)
11753 tree t;
11755 gcc_assert (fntype != NULL_TREE);
11757 t = TYPE_ARG_TYPES (fntype);
11758 return (t != NULL_TREE);
11761 /* If BLOCK is inlined from an __attribute__((__artificial__))
11762 routine, return pointer to location from where it has been
11763 called. */
11764 location_t *
11765 block_nonartificial_location (tree block)
11767 location_t *ret = NULL;
11769 while (block && TREE_CODE (block) == BLOCK
11770 && BLOCK_ABSTRACT_ORIGIN (block))
11772 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
11774 while (TREE_CODE (ao) == BLOCK
11775 && BLOCK_ABSTRACT_ORIGIN (ao)
11776 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
11777 ao = BLOCK_ABSTRACT_ORIGIN (ao);
11779 if (TREE_CODE (ao) == FUNCTION_DECL)
11781 /* If AO is an artificial inline, point RET to the
11782 call site locus at which it has been inlined and continue
11783 the loop, in case AO's caller is also an artificial
11784 inline. */
11785 if (DECL_DECLARED_INLINE_P (ao)
11786 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
11787 ret = &BLOCK_SOURCE_LOCATION (block);
11788 else
11789 break;
11791 else if (TREE_CODE (ao) != BLOCK)
11792 break;
11794 block = BLOCK_SUPERCONTEXT (block);
11796 return ret;
11800 /* If EXP is inlined from an __attribute__((__artificial__))
11801 function, return the location of the original call expression. */
11803 location_t
11804 tree_nonartificial_location (tree exp)
11806 location_t *loc = block_nonartificial_location (TREE_BLOCK (exp));
11808 if (loc)
11809 return *loc;
11810 else
11811 return EXPR_LOCATION (exp);
11815 /* These are the hash table functions for the hash table of OPTIMIZATION_NODEq
11816 nodes. */
11818 /* Return the hash code X, an OPTIMIZATION_NODE or TARGET_OPTION code. */
11820 hashval_t
11821 cl_option_hasher::hash (tree x)
11823 const_tree const t = x;
11824 const char *p;
11825 size_t i;
11826 size_t len = 0;
11827 hashval_t hash = 0;
11829 if (TREE_CODE (t) == OPTIMIZATION_NODE)
11831 p = (const char *)TREE_OPTIMIZATION (t);
11832 len = sizeof (struct cl_optimization);
11835 else if (TREE_CODE (t) == TARGET_OPTION_NODE)
11836 return cl_target_option_hash (TREE_TARGET_OPTION (t));
11838 else
11839 gcc_unreachable ();
11841 /* assume most opt flags are just 0/1, some are 2-3, and a few might be
11842 something else. */
11843 for (i = 0; i < len; i++)
11844 if (p[i])
11845 hash = (hash << 4) ^ ((i << 2) | p[i]);
11847 return hash;
11850 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
11851 TARGET_OPTION tree node) is the same as that given by *Y, which is the
11852 same. */
11854 bool
11855 cl_option_hasher::equal (tree x, tree y)
11857 const_tree const xt = x;
11858 const_tree const yt = y;
11859 const char *xp;
11860 const char *yp;
11861 size_t len;
11863 if (TREE_CODE (xt) != TREE_CODE (yt))
11864 return 0;
11866 if (TREE_CODE (xt) == OPTIMIZATION_NODE)
11868 xp = (const char *)TREE_OPTIMIZATION (xt);
11869 yp = (const char *)TREE_OPTIMIZATION (yt);
11870 len = sizeof (struct cl_optimization);
11873 else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
11875 return cl_target_option_eq (TREE_TARGET_OPTION (xt),
11876 TREE_TARGET_OPTION (yt));
11879 else
11880 gcc_unreachable ();
11882 return (memcmp (xp, yp, len) == 0);
11885 /* Build an OPTIMIZATION_NODE based on the options in OPTS. */
11887 tree
11888 build_optimization_node (struct gcc_options *opts)
11890 tree t;
11892 /* Use the cache of optimization nodes. */
11894 cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node),
11895 opts);
11897 tree *slot = cl_option_hash_table->find_slot (cl_optimization_node, INSERT);
11898 t = *slot;
11899 if (!t)
11901 /* Insert this one into the hash table. */
11902 t = cl_optimization_node;
11903 *slot = t;
11905 /* Make a new node for next time round. */
11906 cl_optimization_node = make_node (OPTIMIZATION_NODE);
11909 return t;
11912 /* Build a TARGET_OPTION_NODE based on the options in OPTS. */
11914 tree
11915 build_target_option_node (struct gcc_options *opts)
11917 tree t;
11919 /* Use the cache of optimization nodes. */
11921 cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node),
11922 opts);
11924 tree *slot = cl_option_hash_table->find_slot (cl_target_option_node, INSERT);
11925 t = *slot;
11926 if (!t)
11928 /* Insert this one into the hash table. */
11929 t = cl_target_option_node;
11930 *slot = t;
11932 /* Make a new node for next time round. */
11933 cl_target_option_node = make_node (TARGET_OPTION_NODE);
11936 return t;
11939 /* Clear TREE_TARGET_GLOBALS of all TARGET_OPTION_NODE trees,
11940 so that they aren't saved during PCH writing. */
11942 void
11943 prepare_target_option_nodes_for_pch (void)
11945 hash_table<cl_option_hasher>::iterator iter = cl_option_hash_table->begin ();
11946 for (; iter != cl_option_hash_table->end (); ++iter)
11947 if (TREE_CODE (*iter) == TARGET_OPTION_NODE)
11948 TREE_TARGET_GLOBALS (*iter) = NULL;
11951 /* Determine the "ultimate origin" of a block. The block may be an inlined
11952 instance of an inlined instance of a block which is local to an inline
11953 function, so we have to trace all of the way back through the origin chain
11954 to find out what sort of node actually served as the original seed for the
11955 given block. */
11957 tree
11958 block_ultimate_origin (const_tree block)
11960 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
11962 /* BLOCK_ABSTRACT_ORIGIN can point to itself; ignore that if
11963 we're trying to output the abstract instance of this function. */
11964 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
11965 return NULL_TREE;
11967 if (immediate_origin == NULL_TREE)
11968 return NULL_TREE;
11969 else
11971 tree ret_val;
11972 tree lookahead = immediate_origin;
11976 ret_val = lookahead;
11977 lookahead = (TREE_CODE (ret_val) == BLOCK
11978 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
11980 while (lookahead != NULL && lookahead != ret_val);
11982 /* The block's abstract origin chain may not be the *ultimate* origin of
11983 the block. It could lead to a DECL that has an abstract origin set.
11984 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
11985 will give us if it has one). Note that DECL's abstract origins are
11986 supposed to be the most distant ancestor (or so decl_ultimate_origin
11987 claims), so we don't need to loop following the DECL origins. */
11988 if (DECL_P (ret_val))
11989 return DECL_ORIGIN (ret_val);
11991 return ret_val;
11995 /* Return true iff conversion from INNER_TYPE to OUTER_TYPE generates
11996 no instruction. */
11998 bool
11999 tree_nop_conversion_p (const_tree outer_type, const_tree inner_type)
12001 /* Use precision rather then machine mode when we can, which gives
12002 the correct answer even for submode (bit-field) types. */
12003 if ((INTEGRAL_TYPE_P (outer_type)
12004 || POINTER_TYPE_P (outer_type)
12005 || TREE_CODE (outer_type) == OFFSET_TYPE)
12006 && (INTEGRAL_TYPE_P (inner_type)
12007 || POINTER_TYPE_P (inner_type)
12008 || TREE_CODE (inner_type) == OFFSET_TYPE))
12009 return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
12011 /* Otherwise fall back on comparing machine modes (e.g. for
12012 aggregate types, floats). */
12013 return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
12016 /* Return true iff conversion in EXP generates no instruction. Mark
12017 it inline so that we fully inline into the stripping functions even
12018 though we have two uses of this function. */
12020 static inline bool
12021 tree_nop_conversion (const_tree exp)
12023 tree outer_type, inner_type;
12025 if (!CONVERT_EXPR_P (exp)
12026 && TREE_CODE (exp) != NON_LVALUE_EXPR)
12027 return false;
12028 if (TREE_OPERAND (exp, 0) == error_mark_node)
12029 return false;
12031 outer_type = TREE_TYPE (exp);
12032 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
12034 if (!inner_type)
12035 return false;
12037 return tree_nop_conversion_p (outer_type, inner_type);
12040 /* Return true iff conversion in EXP generates no instruction. Don't
12041 consider conversions changing the signedness. */
12043 static bool
12044 tree_sign_nop_conversion (const_tree exp)
12046 tree outer_type, inner_type;
12048 if (!tree_nop_conversion (exp))
12049 return false;
12051 outer_type = TREE_TYPE (exp);
12052 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
12054 return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
12055 && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
12058 /* Strip conversions from EXP according to tree_nop_conversion and
12059 return the resulting expression. */
12061 tree
12062 tree_strip_nop_conversions (tree exp)
12064 while (tree_nop_conversion (exp))
12065 exp = TREE_OPERAND (exp, 0);
12066 return exp;
12069 /* Strip conversions from EXP according to tree_sign_nop_conversion
12070 and return the resulting expression. */
12072 tree
12073 tree_strip_sign_nop_conversions (tree exp)
12075 while (tree_sign_nop_conversion (exp))
12076 exp = TREE_OPERAND (exp, 0);
12077 return exp;
12080 /* Avoid any floating point extensions from EXP. */
12081 tree
12082 strip_float_extensions (tree exp)
12084 tree sub, expt, subt;
12086 /* For floating point constant look up the narrowest type that can hold
12087 it properly and handle it like (type)(narrowest_type)constant.
12088 This way we can optimize for instance a=a*2.0 where "a" is float
12089 but 2.0 is double constant. */
12090 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
12092 REAL_VALUE_TYPE orig;
12093 tree type = NULL;
12095 orig = TREE_REAL_CST (exp);
12096 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
12097 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
12098 type = float_type_node;
12099 else if (TYPE_PRECISION (TREE_TYPE (exp))
12100 > TYPE_PRECISION (double_type_node)
12101 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
12102 type = double_type_node;
12103 if (type)
12104 return build_real_truncate (type, orig);
12107 if (!CONVERT_EXPR_P (exp))
12108 return exp;
12110 sub = TREE_OPERAND (exp, 0);
12111 subt = TREE_TYPE (sub);
12112 expt = TREE_TYPE (exp);
12114 if (!FLOAT_TYPE_P (subt))
12115 return exp;
12117 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
12118 return exp;
12120 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
12121 return exp;
12123 return strip_float_extensions (sub);
12126 /* Strip out all handled components that produce invariant
12127 offsets. */
12129 const_tree
12130 strip_invariant_refs (const_tree op)
12132 while (handled_component_p (op))
12134 switch (TREE_CODE (op))
12136 case ARRAY_REF:
12137 case ARRAY_RANGE_REF:
12138 if (!is_gimple_constant (TREE_OPERAND (op, 1))
12139 || TREE_OPERAND (op, 2) != NULL_TREE
12140 || TREE_OPERAND (op, 3) != NULL_TREE)
12141 return NULL;
12142 break;
12144 case COMPONENT_REF:
12145 if (TREE_OPERAND (op, 2) != NULL_TREE)
12146 return NULL;
12147 break;
12149 default:;
12151 op = TREE_OPERAND (op, 0);
12154 return op;
12157 static GTY(()) tree gcc_eh_personality_decl;
12159 /* Return the GCC personality function decl. */
12161 tree
12162 lhd_gcc_personality (void)
12164 if (!gcc_eh_personality_decl)
12165 gcc_eh_personality_decl = build_personality_function ("gcc");
12166 return gcc_eh_personality_decl;
12169 /* TARGET is a call target of GIMPLE call statement
12170 (obtained by gimple_call_fn). Return true if it is
12171 OBJ_TYPE_REF representing an virtual call of C++ method.
12172 (As opposed to OBJ_TYPE_REF representing objc calls
12173 through a cast where middle-end devirtualization machinery
12174 can't apply.) */
12176 bool
12177 virtual_method_call_p (const_tree target)
12179 if (TREE_CODE (target) != OBJ_TYPE_REF)
12180 return false;
12181 tree t = TREE_TYPE (target);
12182 gcc_checking_assert (TREE_CODE (t) == POINTER_TYPE);
12183 t = TREE_TYPE (t);
12184 if (TREE_CODE (t) == FUNCTION_TYPE)
12185 return false;
12186 gcc_checking_assert (TREE_CODE (t) == METHOD_TYPE);
12187 /* If we do not have BINFO associated, it means that type was built
12188 without devirtualization enabled. Do not consider this a virtual
12189 call. */
12190 if (!TYPE_BINFO (obj_type_ref_class (target)))
12191 return false;
12192 return true;
12195 /* REF is OBJ_TYPE_REF, return the class the ref corresponds to. */
12197 tree
12198 obj_type_ref_class (const_tree ref)
12200 gcc_checking_assert (TREE_CODE (ref) == OBJ_TYPE_REF);
12201 ref = TREE_TYPE (ref);
12202 gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
12203 ref = TREE_TYPE (ref);
12204 /* We look for type THIS points to. ObjC also builds
12205 OBJ_TYPE_REF with non-method calls, Their first parameter
12206 ID however also corresponds to class type. */
12207 gcc_checking_assert (TREE_CODE (ref) == METHOD_TYPE
12208 || TREE_CODE (ref) == FUNCTION_TYPE);
12209 ref = TREE_VALUE (TYPE_ARG_TYPES (ref));
12210 gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
12211 return TREE_TYPE (ref);
12214 /* Lookup sub-BINFO of BINFO of TYPE at offset POS. */
12216 static tree
12217 lookup_binfo_at_offset (tree binfo, tree type, HOST_WIDE_INT pos)
12219 unsigned int i;
12220 tree base_binfo, b;
12222 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
12223 if (pos == tree_to_shwi (BINFO_OFFSET (base_binfo))
12224 && types_same_for_odr (TREE_TYPE (base_binfo), type))
12225 return base_binfo;
12226 else if ((b = lookup_binfo_at_offset (base_binfo, type, pos)) != NULL)
12227 return b;
12228 return NULL;
12231 /* Try to find a base info of BINFO that would have its field decl at offset
12232 OFFSET within the BINFO type and which is of EXPECTED_TYPE. If it can be
12233 found, return, otherwise return NULL_TREE. */
12235 tree
12236 get_binfo_at_offset (tree binfo, HOST_WIDE_INT offset, tree expected_type)
12238 tree type = BINFO_TYPE (binfo);
12240 while (true)
12242 HOST_WIDE_INT pos, size;
12243 tree fld;
12244 int i;
12246 if (types_same_for_odr (type, expected_type))
12247 return binfo;
12248 if (offset < 0)
12249 return NULL_TREE;
12251 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
12253 if (TREE_CODE (fld) != FIELD_DECL || !DECL_ARTIFICIAL (fld))
12254 continue;
12256 pos = int_bit_position (fld);
12257 size = tree_to_uhwi (DECL_SIZE (fld));
12258 if (pos <= offset && (pos + size) > offset)
12259 break;
12261 if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE)
12262 return NULL_TREE;
12264 /* Offset 0 indicates the primary base, whose vtable contents are
12265 represented in the binfo for the derived class. */
12266 else if (offset != 0)
12268 tree found_binfo = NULL, base_binfo;
12269 /* Offsets in BINFO are in bytes relative to the whole structure
12270 while POS is in bits relative to the containing field. */
12271 int binfo_offset = (tree_to_shwi (BINFO_OFFSET (binfo)) + pos
12272 / BITS_PER_UNIT);
12274 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
12275 if (tree_to_shwi (BINFO_OFFSET (base_binfo)) == binfo_offset
12276 && types_same_for_odr (TREE_TYPE (base_binfo), TREE_TYPE (fld)))
12278 found_binfo = base_binfo;
12279 break;
12281 if (found_binfo)
12282 binfo = found_binfo;
12283 else
12284 binfo = lookup_binfo_at_offset (binfo, TREE_TYPE (fld),
12285 binfo_offset);
12288 type = TREE_TYPE (fld);
12289 offset -= pos;
12293 /* Returns true if X is a typedef decl. */
12295 bool
12296 is_typedef_decl (const_tree x)
12298 return (x && TREE_CODE (x) == TYPE_DECL
12299 && DECL_ORIGINAL_TYPE (x) != NULL_TREE);
12302 /* Returns true iff TYPE is a type variant created for a typedef. */
12304 bool
12305 typedef_variant_p (const_tree type)
12307 return is_typedef_decl (TYPE_NAME (type));
12310 /* Warn about a use of an identifier which was marked deprecated. */
12311 void
12312 warn_deprecated_use (tree node, tree attr)
12314 const char *msg;
12316 if (node == 0 || !warn_deprecated_decl)
12317 return;
12319 if (!attr)
12321 if (DECL_P (node))
12322 attr = DECL_ATTRIBUTES (node);
12323 else if (TYPE_P (node))
12325 tree decl = TYPE_STUB_DECL (node);
12326 if (decl)
12327 attr = lookup_attribute ("deprecated",
12328 TYPE_ATTRIBUTES (TREE_TYPE (decl)));
12332 if (attr)
12333 attr = lookup_attribute ("deprecated", attr);
12335 if (attr)
12336 msg = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr)));
12337 else
12338 msg = NULL;
12340 bool w;
12341 if (DECL_P (node))
12343 if (msg)
12344 w = warning (OPT_Wdeprecated_declarations,
12345 "%qD is deprecated: %s", node, msg);
12346 else
12347 w = warning (OPT_Wdeprecated_declarations,
12348 "%qD is deprecated", node);
12349 if (w)
12350 inform (DECL_SOURCE_LOCATION (node), "declared here");
12352 else if (TYPE_P (node))
12354 tree what = NULL_TREE;
12355 tree decl = TYPE_STUB_DECL (node);
12357 if (TYPE_NAME (node))
12359 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
12360 what = TYPE_NAME (node);
12361 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
12362 && DECL_NAME (TYPE_NAME (node)))
12363 what = DECL_NAME (TYPE_NAME (node));
12366 if (decl)
12368 if (what)
12370 if (msg)
12371 w = warning (OPT_Wdeprecated_declarations,
12372 "%qE is deprecated: %s", what, msg);
12373 else
12374 w = warning (OPT_Wdeprecated_declarations,
12375 "%qE is deprecated", what);
12377 else
12379 if (msg)
12380 w = warning (OPT_Wdeprecated_declarations,
12381 "type is deprecated: %s", msg);
12382 else
12383 w = warning (OPT_Wdeprecated_declarations,
12384 "type is deprecated");
12386 if (w)
12387 inform (DECL_SOURCE_LOCATION (decl), "declared here");
12389 else
12391 if (what)
12393 if (msg)
12394 warning (OPT_Wdeprecated_declarations, "%qE is deprecated: %s",
12395 what, msg);
12396 else
12397 warning (OPT_Wdeprecated_declarations, "%qE is deprecated", what);
12399 else
12401 if (msg)
12402 warning (OPT_Wdeprecated_declarations, "type is deprecated: %s",
12403 msg);
12404 else
12405 warning (OPT_Wdeprecated_declarations, "type is deprecated");
12411 /* Return true if REF has a COMPONENT_REF with a bit-field field declaration
12412 somewhere in it. */
12414 bool
12415 contains_bitfld_component_ref_p (const_tree ref)
12417 while (handled_component_p (ref))
12419 if (TREE_CODE (ref) == COMPONENT_REF
12420 && DECL_BIT_FIELD (TREE_OPERAND (ref, 1)))
12421 return true;
12422 ref = TREE_OPERAND (ref, 0);
12425 return false;
12428 /* Try to determine whether a TRY_CATCH expression can fall through.
12429 This is a subroutine of block_may_fallthru. */
12431 static bool
12432 try_catch_may_fallthru (const_tree stmt)
12434 tree_stmt_iterator i;
12436 /* If the TRY block can fall through, the whole TRY_CATCH can
12437 fall through. */
12438 if (block_may_fallthru (TREE_OPERAND (stmt, 0)))
12439 return true;
12441 i = tsi_start (TREE_OPERAND (stmt, 1));
12442 switch (TREE_CODE (tsi_stmt (i)))
12444 case CATCH_EXPR:
12445 /* We expect to see a sequence of CATCH_EXPR trees, each with a
12446 catch expression and a body. The whole TRY_CATCH may fall
12447 through iff any of the catch bodies falls through. */
12448 for (; !tsi_end_p (i); tsi_next (&i))
12450 if (block_may_fallthru (CATCH_BODY (tsi_stmt (i))))
12451 return true;
12453 return false;
12455 case EH_FILTER_EXPR:
12456 /* The exception filter expression only matters if there is an
12457 exception. If the exception does not match EH_FILTER_TYPES,
12458 we will execute EH_FILTER_FAILURE, and we will fall through
12459 if that falls through. If the exception does match
12460 EH_FILTER_TYPES, the stack unwinder will continue up the
12461 stack, so we will not fall through. We don't know whether we
12462 will throw an exception which matches EH_FILTER_TYPES or not,
12463 so we just ignore EH_FILTER_TYPES and assume that we might
12464 throw an exception which doesn't match. */
12465 return block_may_fallthru (EH_FILTER_FAILURE (tsi_stmt (i)));
12467 default:
12468 /* This case represents statements to be executed when an
12469 exception occurs. Those statements are implicitly followed
12470 by a RESX statement to resume execution after the exception.
12471 So in this case the TRY_CATCH never falls through. */
12472 return false;
12476 /* Try to determine if we can fall out of the bottom of BLOCK. This guess
12477 need not be 100% accurate; simply be conservative and return true if we
12478 don't know. This is used only to avoid stupidly generating extra code.
12479 If we're wrong, we'll just delete the extra code later. */
12481 bool
12482 block_may_fallthru (const_tree block)
12484 /* This CONST_CAST is okay because expr_last returns its argument
12485 unmodified and we assign it to a const_tree. */
12486 const_tree stmt = expr_last (CONST_CAST_TREE (block));
12488 switch (stmt ? TREE_CODE (stmt) : ERROR_MARK)
12490 case GOTO_EXPR:
12491 case RETURN_EXPR:
12492 /* Easy cases. If the last statement of the block implies
12493 control transfer, then we can't fall through. */
12494 return false;
12496 case SWITCH_EXPR:
12497 /* If SWITCH_LABELS is set, this is lowered, and represents a
12498 branch to a selected label and hence can not fall through.
12499 Otherwise SWITCH_BODY is set, and the switch can fall
12500 through. */
12501 return SWITCH_LABELS (stmt) == NULL_TREE;
12503 case COND_EXPR:
12504 if (block_may_fallthru (COND_EXPR_THEN (stmt)))
12505 return true;
12506 return block_may_fallthru (COND_EXPR_ELSE (stmt));
12508 case BIND_EXPR:
12509 return block_may_fallthru (BIND_EXPR_BODY (stmt));
12511 case TRY_CATCH_EXPR:
12512 return try_catch_may_fallthru (stmt);
12514 case TRY_FINALLY_EXPR:
12515 /* The finally clause is always executed after the try clause,
12516 so if it does not fall through, then the try-finally will not
12517 fall through. Otherwise, if the try clause does not fall
12518 through, then when the finally clause falls through it will
12519 resume execution wherever the try clause was going. So the
12520 whole try-finally will only fall through if both the try
12521 clause and the finally clause fall through. */
12522 return (block_may_fallthru (TREE_OPERAND (stmt, 0))
12523 && block_may_fallthru (TREE_OPERAND (stmt, 1)));
12525 case MODIFY_EXPR:
12526 if (TREE_CODE (TREE_OPERAND (stmt, 1)) == CALL_EXPR)
12527 stmt = TREE_OPERAND (stmt, 1);
12528 else
12529 return true;
12530 /* FALLTHRU */
12532 case CALL_EXPR:
12533 /* Functions that do not return do not fall through. */
12534 return (call_expr_flags (stmt) & ECF_NORETURN) == 0;
12536 case CLEANUP_POINT_EXPR:
12537 return block_may_fallthru (TREE_OPERAND (stmt, 0));
12539 case TARGET_EXPR:
12540 return block_may_fallthru (TREE_OPERAND (stmt, 1));
12542 case ERROR_MARK:
12543 return true;
12545 default:
12546 return lang_hooks.block_may_fallthru (stmt);
12550 /* True if we are using EH to handle cleanups. */
12551 static bool using_eh_for_cleanups_flag = false;
12553 /* This routine is called from front ends to indicate eh should be used for
12554 cleanups. */
12555 void
12556 using_eh_for_cleanups (void)
12558 using_eh_for_cleanups_flag = true;
12561 /* Query whether EH is used for cleanups. */
12562 bool
12563 using_eh_for_cleanups_p (void)
12565 return using_eh_for_cleanups_flag;
12568 /* Wrapper for tree_code_name to ensure that tree code is valid */
12569 const char *
12570 get_tree_code_name (enum tree_code code)
12572 const char *invalid = "<invalid tree code>";
12574 if (code >= MAX_TREE_CODES)
12575 return invalid;
12577 return tree_code_name[code];
12580 /* Drops the TREE_OVERFLOW flag from T. */
12582 tree
12583 drop_tree_overflow (tree t)
12585 gcc_checking_assert (TREE_OVERFLOW (t));
12587 /* For tree codes with a sharing machinery re-build the result. */
12588 if (TREE_CODE (t) == INTEGER_CST)
12589 return wide_int_to_tree (TREE_TYPE (t), t);
12591 /* Otherwise, as all tcc_constants are possibly shared, copy the node
12592 and drop the flag. */
12593 t = copy_node (t);
12594 TREE_OVERFLOW (t) = 0;
12595 return t;
12598 /* Given a memory reference expression T, return its base address.
12599 The base address of a memory reference expression is the main
12600 object being referenced. For instance, the base address for
12601 'array[i].fld[j]' is 'array'. You can think of this as stripping
12602 away the offset part from a memory address.
12604 This function calls handled_component_p to strip away all the inner
12605 parts of the memory reference until it reaches the base object. */
12607 tree
12608 get_base_address (tree t)
12610 while (handled_component_p (t))
12611 t = TREE_OPERAND (t, 0);
12613 if ((TREE_CODE (t) == MEM_REF
12614 || TREE_CODE (t) == TARGET_MEM_REF)
12615 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR)
12616 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
12618 /* ??? Either the alias oracle or all callers need to properly deal
12619 with WITH_SIZE_EXPRs before we can look through those. */
12620 if (TREE_CODE (t) == WITH_SIZE_EXPR)
12621 return NULL_TREE;
12623 return t;
12626 /* Return a tree of sizetype representing the size, in bytes, of the element
12627 of EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
12629 tree
12630 array_ref_element_size (tree exp)
12632 tree aligned_size = TREE_OPERAND (exp, 3);
12633 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)));
12634 location_t loc = EXPR_LOCATION (exp);
12636 /* If a size was specified in the ARRAY_REF, it's the size measured
12637 in alignment units of the element type. So multiply by that value. */
12638 if (aligned_size)
12640 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
12641 sizetype from another type of the same width and signedness. */
12642 if (TREE_TYPE (aligned_size) != sizetype)
12643 aligned_size = fold_convert_loc (loc, sizetype, aligned_size);
12644 return size_binop_loc (loc, MULT_EXPR, aligned_size,
12645 size_int (TYPE_ALIGN_UNIT (elmt_type)));
12648 /* Otherwise, take the size from that of the element type. Substitute
12649 any PLACEHOLDER_EXPR that we have. */
12650 else
12651 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (elmt_type), exp);
12654 /* Return a tree representing the lower bound of the array mentioned in
12655 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
12657 tree
12658 array_ref_low_bound (tree exp)
12660 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
12662 /* If a lower bound is specified in EXP, use it. */
12663 if (TREE_OPERAND (exp, 2))
12664 return TREE_OPERAND (exp, 2);
12666 /* Otherwise, if there is a domain type and it has a lower bound, use it,
12667 substituting for a PLACEHOLDER_EXPR as needed. */
12668 if (domain_type && TYPE_MIN_VALUE (domain_type))
12669 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MIN_VALUE (domain_type), exp);
12671 /* Otherwise, return a zero of the appropriate type. */
12672 return build_int_cst (TREE_TYPE (TREE_OPERAND (exp, 1)), 0);
12675 /* Return a tree representing the upper bound of the array mentioned in
12676 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
12678 tree
12679 array_ref_up_bound (tree exp)
12681 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
12683 /* If there is a domain type and it has an upper bound, use it, substituting
12684 for a PLACEHOLDER_EXPR as needed. */
12685 if (domain_type && TYPE_MAX_VALUE (domain_type))
12686 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MAX_VALUE (domain_type), exp);
12688 /* Otherwise fail. */
12689 return NULL_TREE;
12692 /* Returns true if REF is an array reference to an array at the end of
12693 a structure. If this is the case, the array may be allocated larger
12694 than its upper bound implies. */
12696 bool
12697 array_at_struct_end_p (tree ref)
12699 if (TREE_CODE (ref) != ARRAY_REF
12700 && TREE_CODE (ref) != ARRAY_RANGE_REF)
12701 return false;
12703 while (handled_component_p (ref))
12705 /* If the reference chain contains a component reference to a
12706 non-union type and there follows another field the reference
12707 is not at the end of a structure. */
12708 if (TREE_CODE (ref) == COMPONENT_REF
12709 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
12711 tree nextf = DECL_CHAIN (TREE_OPERAND (ref, 1));
12712 while (nextf && TREE_CODE (nextf) != FIELD_DECL)
12713 nextf = DECL_CHAIN (nextf);
12714 if (nextf)
12715 return false;
12718 ref = TREE_OPERAND (ref, 0);
12721 /* If the reference is based on a declared entity, the size of the array
12722 is constrained by its given domain. */
12723 if (DECL_P (ref))
12724 return false;
12726 return true;
12729 /* Return a tree representing the offset, in bytes, of the field referenced
12730 by EXP. This does not include any offset in DECL_FIELD_BIT_OFFSET. */
12732 tree
12733 component_ref_field_offset (tree exp)
12735 tree aligned_offset = TREE_OPERAND (exp, 2);
12736 tree field = TREE_OPERAND (exp, 1);
12737 location_t loc = EXPR_LOCATION (exp);
12739 /* If an offset was specified in the COMPONENT_REF, it's the offset measured
12740 in units of DECL_OFFSET_ALIGN / BITS_PER_UNIT. So multiply by that
12741 value. */
12742 if (aligned_offset)
12744 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
12745 sizetype from another type of the same width and signedness. */
12746 if (TREE_TYPE (aligned_offset) != sizetype)
12747 aligned_offset = fold_convert_loc (loc, sizetype, aligned_offset);
12748 return size_binop_loc (loc, MULT_EXPR, aligned_offset,
12749 size_int (DECL_OFFSET_ALIGN (field)
12750 / BITS_PER_UNIT));
12753 /* Otherwise, take the offset from that of the field. Substitute
12754 any PLACEHOLDER_EXPR that we have. */
12755 else
12756 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (DECL_FIELD_OFFSET (field), exp);
12759 /* Return the machine mode of T. For vectors, returns the mode of the
12760 inner type. The main use case is to feed the result to HONOR_NANS,
12761 avoiding the BLKmode that a direct TYPE_MODE (T) might return. */
12763 machine_mode
12764 element_mode (const_tree t)
12766 if (!TYPE_P (t))
12767 t = TREE_TYPE (t);
12768 if (VECTOR_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
12769 t = TREE_TYPE (t);
12770 return TYPE_MODE (t);
12774 /* Veirfy that basic properties of T match TV and thus T can be a variant of
12775 TV. TV should be the more specified variant (i.e. the main variant). */
12777 static bool
12778 verify_type_variant (const_tree t, tree tv)
12780 /* Type variant can differ by:
12782 - TYPE_QUALS: TYPE_READONLY, TYPE_VOLATILE, TYPE_ATOMIC, TYPE_RESTRICT,
12783 ENCODE_QUAL_ADDR_SPACE.
12784 - main variant may be TYPE_COMPLETE_P and variant types !TYPE_COMPLETE_P
12785 in this case some values may not be set in the variant types
12786 (see TYPE_COMPLETE_P checks).
12787 - it is possible to have TYPE_ARTIFICIAL variant of non-artifical type
12788 - by TYPE_NAME and attributes (i.e. when variant originate by typedef)
12789 - TYPE_CANONICAL (TYPE_ALIAS_SET is the same among variants)
12790 - by the alignment: TYPE_ALIGN and TYPE_USER_ALIGN
12791 - during LTO by TYPE_CONTEXT if type is TYPE_FILE_SCOPE_P
12792 this is necessary to make it possible to merge types form different TUs
12793 - arrays, pointers and references may have TREE_TYPE that is a variant
12794 of TREE_TYPE of their main variants.
12795 - aggregates may have new TYPE_FIELDS list that list variants of
12796 the main variant TYPE_FIELDS.
12797 - vector types may differ by TYPE_VECTOR_OPAQUE
12798 - TYPE_METHODS is always NULL for vairant types and maintained for
12799 main variant only.
12802 /* Convenience macro for matching individual fields. */
12803 #define verify_variant_match(flag) \
12804 do { \
12805 if (flag (tv) != flag (t)) \
12807 error ("type variant differs by " #flag "."); \
12808 debug_tree (tv); \
12809 return false; \
12811 } while (false)
12813 /* tree_base checks. */
12815 verify_variant_match (TREE_CODE);
12816 /* FIXME: Ada builds non-artificial variants of artificial types. */
12817 if (TYPE_ARTIFICIAL (tv) && 0)
12818 verify_variant_match (TYPE_ARTIFICIAL);
12819 if (POINTER_TYPE_P (tv))
12820 verify_variant_match (TYPE_REF_CAN_ALIAS_ALL);
12821 /* FIXME: TYPE_SIZES_GIMPLIFIED may differs for Ada build. */
12822 verify_variant_match (TYPE_UNSIGNED);
12823 verify_variant_match (TYPE_ALIGN_OK);
12824 verify_variant_match (TYPE_PACKED);
12825 if (TREE_CODE (t) == REFERENCE_TYPE)
12826 verify_variant_match (TYPE_REF_IS_RVALUE);
12827 verify_variant_match (TYPE_SATURATING);
12828 /* FIXME: This check trigger during libstdc++ build. */
12829 if (RECORD_OR_UNION_TYPE_P (t) && COMPLETE_TYPE_P (t) && 0)
12830 verify_variant_match (TYPE_FINAL_P);
12832 /* tree_type_common checks. */
12834 if (COMPLETE_TYPE_P (t))
12836 verify_variant_match (TYPE_SIZE);
12837 verify_variant_match (TYPE_MODE);
12838 if (TYPE_SIZE_UNIT (t) != TYPE_SIZE_UNIT (tv)
12839 /* FIXME: ideally we should compare pointer equality, but java FE
12840 produce variants where size is INTEGER_CST of different type (int
12841 wrt size_type) during libjava biuld. */
12842 && !operand_equal_p (TYPE_SIZE_UNIT (t), TYPE_SIZE_UNIT (tv), 0))
12844 error ("type variant has different TYPE_SIZE_UNIT");
12845 debug_tree (tv);
12846 error ("type variant's TYPE_SIZE_UNIT");
12847 debug_tree (TYPE_SIZE_UNIT (tv));
12848 error ("type's TYPE_SIZE_UNIT");
12849 debug_tree (TYPE_SIZE_UNIT (t));
12850 return false;
12853 verify_variant_match (TYPE_PRECISION);
12854 verify_variant_match (TYPE_NEEDS_CONSTRUCTING);
12855 if (RECORD_OR_UNION_TYPE_P (t))
12856 verify_variant_match (TYPE_TRANSPARENT_AGGR);
12857 else if (TREE_CODE (t) == ARRAY_TYPE)
12858 verify_variant_match (TYPE_NONALIASED_COMPONENT);
12859 /* During LTO we merge variant lists from diferent translation units
12860 that may differ BY TYPE_CONTEXT that in turn may point
12861 to TRANSLATION_UNIT_DECL.
12862 Ada also builds variants of types with different TYPE_CONTEXT. */
12863 if ((!in_lto_p || !TYPE_FILE_SCOPE_P (t)) && 0)
12864 verify_variant_match (TYPE_CONTEXT);
12865 verify_variant_match (TYPE_STRING_FLAG);
12866 if (TYPE_ALIAS_SET_KNOWN_P (t) && TYPE_ALIAS_SET_KNOWN_P (tv))
12867 verify_variant_match (TYPE_ALIAS_SET);
12869 /* tree_type_non_common checks. */
12871 /* FIXME: C FE uses TYPE_VFIELD to record C_TYPE_INCOMPLETE_VARS
12872 and dangle the pointer from time to time. */
12873 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_VFIELD (t) != TYPE_VFIELD (tv)
12874 && (in_lto_p || !TYPE_VFIELD (tv)
12875 || TREE_CODE (TYPE_VFIELD (tv)) != TREE_LIST))
12877 error ("type variant has different TYPE_VFIELD");
12878 debug_tree (tv);
12879 return false;
12881 if ((TREE_CODE (t) == ENUMERAL_TYPE && COMPLETE_TYPE_P (t))
12882 || TREE_CODE (t) == INTEGER_TYPE
12883 || TREE_CODE (t) == BOOLEAN_TYPE
12884 || TREE_CODE (t) == REAL_TYPE
12885 || TREE_CODE (t) == FIXED_POINT_TYPE)
12887 verify_variant_match (TYPE_MAX_VALUE);
12888 verify_variant_match (TYPE_MIN_VALUE);
12890 if (TREE_CODE (t) == METHOD_TYPE)
12891 verify_variant_match (TYPE_METHOD_BASETYPE);
12892 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_METHODS (t))
12894 error ("type variant has TYPE_METHODS");
12895 debug_tree (tv);
12896 return false;
12898 if (TREE_CODE (t) == OFFSET_TYPE)
12899 verify_variant_match (TYPE_OFFSET_BASETYPE);
12900 if (TREE_CODE (t) == ARRAY_TYPE)
12901 verify_variant_match (TYPE_ARRAY_MAX_SIZE);
12902 /* FIXME: Be lax and allow TYPE_BINFO to be missing in variant types
12903 or even type's main variant. This is needed to make bootstrap pass
12904 and the bug seems new in GCC 5.
12905 C++ FE should be updated to make this consistent and we should check
12906 that TYPE_BINFO is always NULL for !COMPLETE_TYPE_P and otherwise there
12907 is a match with main variant.
12909 Also disable the check for Java for now because of parser hack that builds
12910 first an dummy BINFO and then sometimes replace it by real BINFO in some
12911 of the copies. */
12912 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t) && TYPE_BINFO (tv)
12913 && TYPE_BINFO (t) != TYPE_BINFO (tv)
12914 /* FIXME: Java sometimes keep dump TYPE_BINFOs on variant types.
12915 Since there is no cheap way to tell C++/Java type w/o LTO, do checking
12916 at LTO time only. */
12917 && (in_lto_p && odr_type_p (t)))
12919 error ("type variant has different TYPE_BINFO");
12920 debug_tree (tv);
12921 error ("type variant's TYPE_BINFO");
12922 debug_tree (TYPE_BINFO (tv));
12923 error ("type's TYPE_BINFO");
12924 debug_tree (TYPE_BINFO (t));
12925 return false;
12928 /* Check various uses of TYPE_VALUES_RAW. */
12929 if (TREE_CODE (t) == ENUMERAL_TYPE)
12930 verify_variant_match (TYPE_VALUES);
12931 else if (TREE_CODE (t) == ARRAY_TYPE)
12932 verify_variant_match (TYPE_DOMAIN);
12933 /* Permit incomplete variants of complete type. While FEs may complete
12934 all variants, this does not happen for C++ templates in all cases. */
12935 else if (RECORD_OR_UNION_TYPE_P (t)
12936 && COMPLETE_TYPE_P (t)
12937 && TYPE_FIELDS (t) != TYPE_FIELDS (tv))
12939 tree f1, f2;
12941 /* Fortran builds qualified variants as new records with items of
12942 qualified type. Verify that they looks same. */
12943 for (f1 = TYPE_FIELDS (t), f2 = TYPE_FIELDS (tv);
12944 f1 && f2;
12945 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
12946 if (TREE_CODE (f1) != FIELD_DECL || TREE_CODE (f2) != FIELD_DECL
12947 || (TYPE_MAIN_VARIANT (TREE_TYPE (f1))
12948 != TYPE_MAIN_VARIANT (TREE_TYPE (f2))
12949 /* FIXME: gfc_nonrestricted_type builds all types as variants
12950 with exception of pointer types. It deeply copies the type
12951 which means that we may end up with a variant type
12952 referring non-variant pointer. We may change it to
12953 produce types as variants, too, like
12954 objc_get_protocol_qualified_type does. */
12955 && !POINTER_TYPE_P (TREE_TYPE (f1)))
12956 || DECL_FIELD_OFFSET (f1) != DECL_FIELD_OFFSET (f2)
12957 || DECL_FIELD_BIT_OFFSET (f1) != DECL_FIELD_BIT_OFFSET (f2))
12958 break;
12959 if (f1 || f2)
12961 error ("type variant has different TYPE_FIELDS");
12962 debug_tree (tv);
12963 error ("first mismatch is field");
12964 debug_tree (f1);
12965 error ("and field");
12966 debug_tree (f2);
12967 return false;
12970 else if ((TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE))
12971 verify_variant_match (TYPE_ARG_TYPES);
12972 /* For C++ the qualified variant of array type is really an array type
12973 of qualified TREE_TYPE.
12974 objc builds variants of pointer where pointer to type is a variant, too
12975 in objc_get_protocol_qualified_type. */
12976 if (TREE_TYPE (t) != TREE_TYPE (tv)
12977 && ((TREE_CODE (t) != ARRAY_TYPE
12978 && !POINTER_TYPE_P (t))
12979 || TYPE_MAIN_VARIANT (TREE_TYPE (t))
12980 != TYPE_MAIN_VARIANT (TREE_TYPE (tv))))
12982 error ("type variant has different TREE_TYPE");
12983 debug_tree (tv);
12984 error ("type variant's TREE_TYPE");
12985 debug_tree (TREE_TYPE (tv));
12986 error ("type's TREE_TYPE");
12987 debug_tree (TREE_TYPE (t));
12988 return false;
12990 if (type_with_alias_set_p (t)
12991 && !gimple_canonical_types_compatible_p (t, tv, false))
12993 error ("type is not compatible with its vairant");
12994 debug_tree (tv);
12995 error ("type variant's TREE_TYPE");
12996 debug_tree (TREE_TYPE (tv));
12997 error ("type's TREE_TYPE");
12998 debug_tree (TREE_TYPE (t));
12999 return false;
13001 return true;
13002 #undef verify_variant_match
13006 /* The TYPE_CANONICAL merging machinery. It should closely resemble
13007 the middle-end types_compatible_p function. It needs to avoid
13008 claiming types are different for types that should be treated
13009 the same with respect to TBAA. Canonical types are also used
13010 for IL consistency checks via the useless_type_conversion_p
13011 predicate which does not handle all type kinds itself but falls
13012 back to pointer-comparison of TYPE_CANONICAL for aggregates
13013 for example. */
13015 /* Return true iff T1 and T2 are structurally identical for what
13016 TBAA is concerned.
13017 This function is used both by lto.c canonical type merging and by the
13018 verifier. If TRUST_TYPE_CANONICAL we do not look into structure of types
13019 that have TYPE_CANONICAL defined and assume them equivalent. */
13021 bool
13022 gimple_canonical_types_compatible_p (const_tree t1, const_tree t2,
13023 bool trust_type_canonical)
13025 /* Type variants should be same as the main variant. When not doing sanity
13026 checking to verify this fact, go to main variants and save some work. */
13027 if (trust_type_canonical)
13029 t1 = TYPE_MAIN_VARIANT (t1);
13030 t2 = TYPE_MAIN_VARIANT (t2);
13033 /* Check first for the obvious case of pointer identity. */
13034 if (t1 == t2)
13035 return true;
13037 /* Check that we have two types to compare. */
13038 if (t1 == NULL_TREE || t2 == NULL_TREE)
13039 return false;
13041 /* We consider complete types always compatible with incomplete type.
13042 This does not make sense for canonical type calculation and thus we
13043 need to ensure that we are never called on it.
13045 FIXME: For more correctness the function probably should have three modes
13046 1) mode assuming that types are complete mathcing their structure
13047 2) mode allowing incomplete types but producing equivalence classes
13048 and thus ignoring all info from complete types
13049 3) mode allowing incomplete types to match complete but checking
13050 compatibility between complete types.
13052 1 and 2 can be used for canonical type calculation. 3 is the real
13053 definition of type compatibility that can be used i.e. for warnings during
13054 declaration merging. */
13056 gcc_assert (!trust_type_canonical
13057 || (type_with_alias_set_p (t1) && type_with_alias_set_p (t2)));
13058 /* If the types have been previously registered and found equal
13059 they still are. */
13060 if (TYPE_CANONICAL (t1) && TYPE_CANONICAL (t2)
13061 && trust_type_canonical)
13062 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
13064 /* Can't be the same type if the types don't have the same code. */
13065 if (tree_code_for_canonical_type_merging (TREE_CODE (t1))
13066 != tree_code_for_canonical_type_merging (TREE_CODE (t2)))
13067 return false;
13069 /* Qualifiers do not matter for canonical type comparison purposes. */
13071 /* Void types and nullptr types are always the same. */
13072 if (TREE_CODE (t1) == VOID_TYPE
13073 || TREE_CODE (t1) == NULLPTR_TYPE)
13074 return true;
13076 /* Can't be the same type if they have different mode. */
13077 if (TYPE_MODE (t1) != TYPE_MODE (t2))
13078 return false;
13080 /* Non-aggregate types can be handled cheaply. */
13081 if (INTEGRAL_TYPE_P (t1)
13082 || SCALAR_FLOAT_TYPE_P (t1)
13083 || FIXED_POINT_TYPE_P (t1)
13084 || TREE_CODE (t1) == VECTOR_TYPE
13085 || TREE_CODE (t1) == COMPLEX_TYPE
13086 || TREE_CODE (t1) == OFFSET_TYPE
13087 || POINTER_TYPE_P (t1))
13089 /* Can't be the same type if they have different sign or precision. */
13090 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2)
13091 || TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
13092 return false;
13094 /* Fortran's C_SIGNED_CHAR is !TYPE_STRING_FLAG but needs to be
13095 interoperable with "signed char". Unless all frontends are revisited
13096 to agree on these types, we must ignore the flag completely. */
13098 /* Fortran standard define C_PTR type that is compatible with every
13099 C pointer. For this reason we need to glob all pointers into one.
13100 Still pointers in different address spaces are not compatible. */
13101 if (POINTER_TYPE_P (t1))
13103 if (TYPE_ADDR_SPACE (TREE_TYPE (t1))
13104 != TYPE_ADDR_SPACE (TREE_TYPE (t2)))
13105 return false;
13108 /* Tail-recurse to components. */
13109 if (TREE_CODE (t1) == VECTOR_TYPE
13110 || TREE_CODE (t1) == COMPLEX_TYPE)
13111 return gimple_canonical_types_compatible_p (TREE_TYPE (t1),
13112 TREE_TYPE (t2),
13113 trust_type_canonical);
13115 return true;
13118 /* Do type-specific comparisons. */
13119 switch (TREE_CODE (t1))
13121 case ARRAY_TYPE:
13122 /* Array types are the same if the element types are the same and
13123 the number of elements are the same. */
13124 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2),
13125 trust_type_canonical)
13126 || TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2)
13127 || TYPE_NONALIASED_COMPONENT (t1) != TYPE_NONALIASED_COMPONENT (t2))
13128 return false;
13129 else
13131 tree i1 = TYPE_DOMAIN (t1);
13132 tree i2 = TYPE_DOMAIN (t2);
13134 /* For an incomplete external array, the type domain can be
13135 NULL_TREE. Check this condition also. */
13136 if (i1 == NULL_TREE && i2 == NULL_TREE)
13137 return true;
13138 else if (i1 == NULL_TREE || i2 == NULL_TREE)
13139 return false;
13140 else
13142 tree min1 = TYPE_MIN_VALUE (i1);
13143 tree min2 = TYPE_MIN_VALUE (i2);
13144 tree max1 = TYPE_MAX_VALUE (i1);
13145 tree max2 = TYPE_MAX_VALUE (i2);
13147 /* The minimum/maximum values have to be the same. */
13148 if ((min1 == min2
13149 || (min1 && min2
13150 && ((TREE_CODE (min1) == PLACEHOLDER_EXPR
13151 && TREE_CODE (min2) == PLACEHOLDER_EXPR)
13152 || operand_equal_p (min1, min2, 0))))
13153 && (max1 == max2
13154 || (max1 && max2
13155 && ((TREE_CODE (max1) == PLACEHOLDER_EXPR
13156 && TREE_CODE (max2) == PLACEHOLDER_EXPR)
13157 || operand_equal_p (max1, max2, 0)))))
13158 return true;
13159 else
13160 return false;
13164 case METHOD_TYPE:
13165 case FUNCTION_TYPE:
13166 /* Function types are the same if the return type and arguments types
13167 are the same. */
13168 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2),
13169 trust_type_canonical))
13170 return false;
13172 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2))
13173 return true;
13174 else
13176 tree parms1, parms2;
13178 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
13179 parms1 && parms2;
13180 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
13182 if (!gimple_canonical_types_compatible_p
13183 (TREE_VALUE (parms1), TREE_VALUE (parms2),
13184 trust_type_canonical))
13185 return false;
13188 if (parms1 || parms2)
13189 return false;
13191 return true;
13194 case RECORD_TYPE:
13195 case UNION_TYPE:
13196 case QUAL_UNION_TYPE:
13198 tree f1, f2;
13200 /* For aggregate types, all the fields must be the same. */
13201 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
13202 f1 || f2;
13203 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
13205 /* Skip non-fields. */
13206 while (f1 && TREE_CODE (f1) != FIELD_DECL)
13207 f1 = TREE_CHAIN (f1);
13208 while (f2 && TREE_CODE (f2) != FIELD_DECL)
13209 f2 = TREE_CHAIN (f2);
13210 if (!f1 || !f2)
13211 break;
13212 /* The fields must have the same name, offset and type. */
13213 if (DECL_NONADDRESSABLE_P (f1) != DECL_NONADDRESSABLE_P (f2)
13214 || !gimple_compare_field_offset (f1, f2)
13215 || !gimple_canonical_types_compatible_p
13216 (TREE_TYPE (f1), TREE_TYPE (f2),
13217 trust_type_canonical))
13218 return false;
13221 /* If one aggregate has more fields than the other, they
13222 are not the same. */
13223 if (f1 || f2)
13224 return false;
13226 return true;
13229 default:
13230 /* Consider all types with language specific trees in them mutually
13231 compatible. This is executed only from verify_type and false
13232 positives can be tolerated. */
13233 gcc_assert (!in_lto_p);
13234 return true;
13238 /* Verify type T. */
13240 void
13241 verify_type (const_tree t)
13243 bool error_found = false;
13244 tree mv = TYPE_MAIN_VARIANT (t);
13245 if (!mv)
13247 error ("Main variant is not defined");
13248 error_found = true;
13250 else if (mv != TYPE_MAIN_VARIANT (mv))
13252 error ("TYPE_MAIN_VARIANT has different TYPE_MAIN_VARIANT");
13253 debug_tree (mv);
13254 error_found = true;
13256 else if (t != mv && !verify_type_variant (t, mv))
13257 error_found = true;
13259 tree ct = TYPE_CANONICAL (t);
13260 if (!ct)
13262 else if (TYPE_CANONICAL (t) != ct)
13264 error ("TYPE_CANONICAL has different TYPE_CANONICAL");
13265 debug_tree (ct);
13266 error_found = true;
13268 /* Method and function types can not be used to address memory and thus
13269 TYPE_CANONICAL really matters only for determining useless conversions.
13271 FIXME: C++ FE produce declarations of builtin functions that are not
13272 compatible with main variants. */
13273 else if (TREE_CODE (t) == FUNCTION_TYPE)
13275 else if (t != ct
13276 /* FIXME: gimple_canonical_types_compatible_p can not compare types
13277 with variably sized arrays because their sizes possibly
13278 gimplified to different variables. */
13279 && !variably_modified_type_p (ct, NULL)
13280 && !gimple_canonical_types_compatible_p (t, ct, false))
13282 error ("TYPE_CANONICAL is not compatible");
13283 debug_tree (ct);
13284 error_found = true;
13288 /* Check various uses of TYPE_MINVAL. */
13289 if (RECORD_OR_UNION_TYPE_P (t))
13291 /* FIXME: C FE uses TYPE_VFIELD to record C_TYPE_INCOMPLETE_VARS
13292 and danagle the pointer from time to time. */
13293 if (TYPE_VFIELD (t)
13294 && TREE_CODE (TYPE_VFIELD (t)) != FIELD_DECL
13295 && TREE_CODE (TYPE_VFIELD (t)) != TREE_LIST)
13297 error ("TYPE_VFIELD is not FIELD_DECL nor TREE_LIST");
13298 debug_tree (TYPE_VFIELD (t));
13299 error_found = true;
13302 else if (TREE_CODE (t) == POINTER_TYPE)
13304 if (TYPE_NEXT_PTR_TO (t)
13305 && TREE_CODE (TYPE_NEXT_PTR_TO (t)) != POINTER_TYPE)
13307 error ("TYPE_NEXT_PTR_TO is not POINTER_TYPE");
13308 debug_tree (TYPE_NEXT_PTR_TO (t));
13309 error_found = true;
13312 else if (TREE_CODE (t) == REFERENCE_TYPE)
13314 if (TYPE_NEXT_REF_TO (t)
13315 && TREE_CODE (TYPE_NEXT_REF_TO (t)) != REFERENCE_TYPE)
13317 error ("TYPE_NEXT_REF_TO is not REFERENCE_TYPE");
13318 debug_tree (TYPE_NEXT_REF_TO (t));
13319 error_found = true;
13322 else if (INTEGRAL_TYPE_P (t) || TREE_CODE (t) == REAL_TYPE
13323 || TREE_CODE (t) == FIXED_POINT_TYPE)
13325 /* FIXME: The following check should pass:
13326 useless_type_conversion_p (const_cast <tree> (t),
13327 TREE_TYPE (TYPE_MIN_VALUE (t))
13328 but does not for C sizetypes in LTO. */
13330 /* Java uses TYPE_MINVAL for TYPE_ARGUMENT_SIGNATURE. */
13331 else if (TYPE_MINVAL (t)
13332 && ((TREE_CODE (t) != METHOD_TYPE && TREE_CODE (t) != FUNCTION_TYPE)
13333 || in_lto_p))
13335 error ("TYPE_MINVAL non-NULL");
13336 debug_tree (TYPE_MINVAL (t));
13337 error_found = true;
13340 /* Check various uses of TYPE_MAXVAL. */
13341 if (RECORD_OR_UNION_TYPE_P (t))
13343 if (TYPE_METHODS (t) && TREE_CODE (TYPE_METHODS (t)) != FUNCTION_DECL
13344 && TREE_CODE (TYPE_METHODS (t)) != TEMPLATE_DECL
13345 && TYPE_METHODS (t) != error_mark_node)
13347 error ("TYPE_METHODS is not FUNCTION_DECL, TEMPLATE_DECL nor error_mark_node");
13348 debug_tree (TYPE_METHODS (t));
13349 error_found = true;
13352 else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
13354 if (TYPE_METHOD_BASETYPE (t)
13355 && TREE_CODE (TYPE_METHOD_BASETYPE (t)) != RECORD_TYPE
13356 && TREE_CODE (TYPE_METHOD_BASETYPE (t)) != UNION_TYPE)
13358 error ("TYPE_METHOD_BASETYPE is not record nor union");
13359 debug_tree (TYPE_METHOD_BASETYPE (t));
13360 error_found = true;
13363 else if (TREE_CODE (t) == OFFSET_TYPE)
13365 if (TYPE_OFFSET_BASETYPE (t)
13366 && TREE_CODE (TYPE_OFFSET_BASETYPE (t)) != RECORD_TYPE
13367 && TREE_CODE (TYPE_OFFSET_BASETYPE (t)) != UNION_TYPE)
13369 error ("TYPE_OFFSET_BASETYPE is not record nor union");
13370 debug_tree (TYPE_OFFSET_BASETYPE (t));
13371 error_found = true;
13374 else if (INTEGRAL_TYPE_P (t) || TREE_CODE (t) == REAL_TYPE
13375 || TREE_CODE (t) == FIXED_POINT_TYPE)
13377 /* FIXME: The following check should pass:
13378 useless_type_conversion_p (const_cast <tree> (t),
13379 TREE_TYPE (TYPE_MAX_VALUE (t))
13380 but does not for C sizetypes in LTO. */
13382 else if (TREE_CODE (t) == ARRAY_TYPE)
13384 if (TYPE_ARRAY_MAX_SIZE (t)
13385 && TREE_CODE (TYPE_ARRAY_MAX_SIZE (t)) != INTEGER_CST)
13387 error ("TYPE_ARRAY_MAX_SIZE not INTEGER_CST");
13388 debug_tree (TYPE_ARRAY_MAX_SIZE (t));
13389 error_found = true;
13392 else if (TYPE_MAXVAL (t))
13394 error ("TYPE_MAXVAL non-NULL");
13395 debug_tree (TYPE_MAXVAL (t));
13396 error_found = true;
13399 /* Check various uses of TYPE_BINFO. */
13400 if (RECORD_OR_UNION_TYPE_P (t))
13402 if (!TYPE_BINFO (t))
13404 else if (TREE_CODE (TYPE_BINFO (t)) != TREE_BINFO)
13406 error ("TYPE_BINFO is not TREE_BINFO");
13407 debug_tree (TYPE_BINFO (t));
13408 error_found = true;
13410 /* FIXME: Java builds invalid empty binfos that do not have
13411 TREE_TYPE set. */
13412 else if (TREE_TYPE (TYPE_BINFO (t)) != TYPE_MAIN_VARIANT (t) && 0)
13414 error ("TYPE_BINFO type is not TYPE_MAIN_VARIANT");
13415 debug_tree (TREE_TYPE (TYPE_BINFO (t)));
13416 error_found = true;
13419 else if (TYPE_LANG_SLOT_1 (t) && in_lto_p)
13421 error ("TYPE_LANG_SLOT_1 (binfo) field is non-NULL");
13422 debug_tree (TYPE_LANG_SLOT_1 (t));
13423 error_found = true;
13426 /* Check various uses of TYPE_VALUES_RAW. */
13427 if (TREE_CODE (t) == ENUMERAL_TYPE)
13428 for (tree l = TYPE_VALUES (t); l; l = TREE_CHAIN (l))
13430 tree value = TREE_VALUE (l);
13431 tree name = TREE_PURPOSE (l);
13433 /* C FE porduce INTEGER_CST of INTEGER_TYPE, while C++ FE uses
13434 CONST_DECL of ENUMERAL TYPE. */
13435 if (TREE_CODE (value) != INTEGER_CST && TREE_CODE (value) != CONST_DECL)
13437 error ("Enum value is not CONST_DECL or INTEGER_CST");
13438 debug_tree (value);
13439 debug_tree (name);
13440 error_found = true;
13442 if (TREE_CODE (TREE_TYPE (value)) != INTEGER_TYPE
13443 && !useless_type_conversion_p (const_cast <tree> (t), TREE_TYPE (value)))
13445 error ("Enum value type is not INTEGER_TYPE nor convertible to the enum");
13446 debug_tree (value);
13447 debug_tree (name);
13448 error_found = true;
13450 if (TREE_CODE (name) != IDENTIFIER_NODE)
13452 error ("Enum value name is not IDENTIFIER_NODE");
13453 debug_tree (value);
13454 debug_tree (name);
13455 error_found = true;
13458 else if (TREE_CODE (t) == ARRAY_TYPE)
13460 if (TYPE_DOMAIN (t) && TREE_CODE (TYPE_DOMAIN (t)) != INTEGER_TYPE)
13462 error ("Array TYPE_DOMAIN is not integer type");
13463 debug_tree (TYPE_DOMAIN (t));
13464 error_found = true;
13467 else if (RECORD_OR_UNION_TYPE_P (t))
13468 for (tree fld = TYPE_FIELDS (t); fld; fld = TREE_CHAIN (fld))
13470 /* TODO: verify properties of decls. */
13471 if (TREE_CODE (fld) == FIELD_DECL)
13473 else if (TREE_CODE (fld) == TYPE_DECL)
13475 else if (TREE_CODE (fld) == CONST_DECL)
13477 else if (TREE_CODE (fld) == VAR_DECL)
13479 else if (TREE_CODE (fld) == TEMPLATE_DECL)
13481 else if (TREE_CODE (fld) == USING_DECL)
13483 else
13485 error ("Wrong tree in TYPE_FIELDS list");
13486 debug_tree (fld);
13487 error_found = true;
13490 else if (TREE_CODE (t) == INTEGER_TYPE
13491 || TREE_CODE (t) == BOOLEAN_TYPE
13492 || TREE_CODE (t) == OFFSET_TYPE
13493 || TREE_CODE (t) == REFERENCE_TYPE
13494 || TREE_CODE (t) == NULLPTR_TYPE
13495 || TREE_CODE (t) == POINTER_TYPE)
13497 if (TYPE_CACHED_VALUES_P (t) != (TYPE_CACHED_VALUES (t) != NULL))
13499 error ("TYPE_CACHED_VALUES_P is %i while TYPE_CACHED_VALUES is %p",
13500 TYPE_CACHED_VALUES_P (t), (void *)TYPE_CACHED_VALUES (t));
13501 error_found = true;
13503 else if (TYPE_CACHED_VALUES_P (t) && TREE_CODE (TYPE_CACHED_VALUES (t)) != TREE_VEC)
13505 error ("TYPE_CACHED_VALUES is not TREE_VEC");
13506 debug_tree (TYPE_CACHED_VALUES (t));
13507 error_found = true;
13509 /* Verify just enough of cache to ensure that no one copied it to new type.
13510 All copying should go by copy_node that should clear it. */
13511 else if (TYPE_CACHED_VALUES_P (t))
13513 int i;
13514 for (i = 0; i < TREE_VEC_LENGTH (TYPE_CACHED_VALUES (t)); i++)
13515 if (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i)
13516 && TREE_TYPE (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i)) != t)
13518 error ("wrong TYPE_CACHED_VALUES entry");
13519 debug_tree (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i));
13520 error_found = true;
13521 break;
13525 else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
13526 for (tree l = TYPE_ARG_TYPES (t); l; l = TREE_CHAIN (l))
13528 /* C++ FE uses TREE_PURPOSE to store initial values. */
13529 if (TREE_PURPOSE (l) && in_lto_p)
13531 error ("TREE_PURPOSE is non-NULL in TYPE_ARG_TYPES list");
13532 debug_tree (l);
13533 error_found = true;
13535 if (!TYPE_P (TREE_VALUE (l)))
13537 error ("Wrong entry in TYPE_ARG_TYPES list");
13538 debug_tree (l);
13539 error_found = true;
13542 else if (!is_lang_specific (t) && TYPE_VALUES_RAW (t))
13544 error ("TYPE_VALUES_RAW field is non-NULL");
13545 debug_tree (TYPE_VALUES_RAW (t));
13546 error_found = true;
13548 if (TREE_CODE (t) != INTEGER_TYPE
13549 && TREE_CODE (t) != BOOLEAN_TYPE
13550 && TREE_CODE (t) != OFFSET_TYPE
13551 && TREE_CODE (t) != REFERENCE_TYPE
13552 && TREE_CODE (t) != NULLPTR_TYPE
13553 && TREE_CODE (t) != POINTER_TYPE
13554 && TYPE_CACHED_VALUES_P (t))
13556 error ("TYPE_CACHED_VALUES_P is set while it should not");
13557 error_found = true;
13559 if (TYPE_STRING_FLAG (t)
13560 && TREE_CODE (t) != ARRAY_TYPE && TREE_CODE (t) != INTEGER_TYPE)
13562 error ("TYPE_STRING_FLAG is set on wrong type code");
13563 error_found = true;
13565 else if (TYPE_STRING_FLAG (t))
13567 const_tree b = t;
13568 if (TREE_CODE (b) == ARRAY_TYPE)
13569 b = TREE_TYPE (t);
13570 /* Java builds arrays with TYPE_STRING_FLAG of promoted_char_type
13571 that is 32bits. */
13572 if (TREE_CODE (b) != INTEGER_TYPE)
13574 error ("TYPE_STRING_FLAG is set on type that does not look like "
13575 "char nor array of chars");
13576 error_found = true;
13580 /* ipa-devirt makes an assumption that TYPE_METHOD_BASETYPE is always
13581 TYPE_MAIN_VARIANT and it would be odd to add methods only to variatns
13582 of a type. */
13583 if (TREE_CODE (t) == METHOD_TYPE
13584 && TYPE_MAIN_VARIANT (TYPE_METHOD_BASETYPE (t)) != TYPE_METHOD_BASETYPE (t))
13586 error ("TYPE_METHOD_BASETYPE is not main variant");
13587 error_found = true;
13590 if (error_found)
13592 debug_tree (const_cast <tree> (t));
13593 internal_error ("verify_type failed");
13598 /* Return true if ARG is marked with the nonnull attribute in the
13599 current function signature. */
13601 bool
13602 nonnull_arg_p (const_tree arg)
13604 tree t, attrs, fntype;
13605 unsigned HOST_WIDE_INT arg_num;
13607 gcc_assert (TREE_CODE (arg) == PARM_DECL && POINTER_TYPE_P (TREE_TYPE (arg)));
13609 /* The static chain decl is always non null. */
13610 if (arg == cfun->static_chain_decl)
13611 return true;
13613 /* THIS argument of method is always non-NULL. */
13614 if (TREE_CODE (TREE_TYPE (cfun->decl)) == METHOD_TYPE
13615 && arg == DECL_ARGUMENTS (cfun->decl)
13616 && flag_delete_null_pointer_checks)
13617 return true;
13619 /* Values passed by reference are always non-NULL. */
13620 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE
13621 && flag_delete_null_pointer_checks)
13622 return true;
13624 fntype = TREE_TYPE (cfun->decl);
13625 for (attrs = TYPE_ATTRIBUTES (fntype); attrs; attrs = TREE_CHAIN (attrs))
13627 attrs = lookup_attribute ("nonnull", attrs);
13629 /* If "nonnull" wasn't specified, we know nothing about the argument. */
13630 if (attrs == NULL_TREE)
13631 return false;
13633 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
13634 if (TREE_VALUE (attrs) == NULL_TREE)
13635 return true;
13637 /* Get the position number for ARG in the function signature. */
13638 for (arg_num = 1, t = DECL_ARGUMENTS (cfun->decl);
13640 t = DECL_CHAIN (t), arg_num++)
13642 if (t == arg)
13643 break;
13646 gcc_assert (t == arg);
13648 /* Now see if ARG_NUM is mentioned in the nonnull list. */
13649 for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
13651 if (compare_tree_int (TREE_VALUE (t), arg_num) == 0)
13652 return true;
13656 return false;
13660 #include "gt-tree.h"