IPA ICF, part 4/5
[official-gcc.git] / gcc / tree.c
blobe7032065da01eb8dc25e87ea915fff50738e2531
1 /* Language-independent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This file contains the low level primitives for operating on tree nodes,
21 including allocation, list operations, interning of identifiers,
22 construction of data type nodes and statement nodes,
23 and construction of type conversion nodes. It also contains
24 tables index by tree code that describe how to take apart
25 nodes of that code.
27 It is intended to be language-independent, but occasionally
28 calls language-dependent routines defined (for C) in typecheck.c. */
30 #include "config.h"
31 #include "system.h"
32 #include "coretypes.h"
33 #include "tm.h"
34 #include "flags.h"
35 #include "tree.h"
36 #include "stor-layout.h"
37 #include "calls.h"
38 #include "attribs.h"
39 #include "varasm.h"
40 #include "tm_p.h"
41 #include "function.h"
42 #include "obstack.h"
43 #include "toplev.h" /* get_random_seed */
44 #include "hashtab.h"
45 #include "inchash.h"
46 #include "filenames.h"
47 #include "output.h"
48 #include "target.h"
49 #include "common/common-target.h"
50 #include "langhooks.h"
51 #include "tree-inline.h"
52 #include "tree-iterator.h"
53 #include "basic-block.h"
54 #include "bitmap.h"
55 #include "tree-ssa-alias.h"
56 #include "internal-fn.h"
57 #include "gimple-expr.h"
58 #include "is-a.h"
59 #include "gimple.h"
60 #include "gimple-iterator.h"
61 #include "gimplify.h"
62 #include "gimple-ssa.h"
63 #include "cgraph.h"
64 #include "tree-phinodes.h"
65 #include "stringpool.h"
66 #include "tree-ssanames.h"
67 #include "expr.h"
68 #include "tree-dfa.h"
69 #include "params.h"
70 #include "tree-pass.h"
71 #include "langhooks-def.h"
72 #include "diagnostic.h"
73 #include "tree-diagnostic.h"
74 #include "tree-pretty-print.h"
75 #include "except.h"
76 #include "debug.h"
77 #include "intl.h"
78 #include "wide-int.h"
79 #include "builtins.h"
81 /* Tree code classes. */
83 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
84 #define END_OF_BASE_TREE_CODES tcc_exceptional,
86 const enum tree_code_class tree_code_type[] = {
87 #include "all-tree.def"
90 #undef DEFTREECODE
91 #undef END_OF_BASE_TREE_CODES
93 /* Table indexed by tree code giving number of expression
94 operands beyond the fixed part of the node structure.
95 Not used for types or decls. */
97 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
98 #define END_OF_BASE_TREE_CODES 0,
100 const unsigned char tree_code_length[] = {
101 #include "all-tree.def"
104 #undef DEFTREECODE
105 #undef END_OF_BASE_TREE_CODES
107 /* Names of tree components.
108 Used for printing out the tree and error messages. */
109 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
110 #define END_OF_BASE_TREE_CODES "@dummy",
112 static const char *const tree_code_name[] = {
113 #include "all-tree.def"
116 #undef DEFTREECODE
117 #undef END_OF_BASE_TREE_CODES
119 /* Each tree code class has an associated string representation.
120 These must correspond to the tree_code_class entries. */
122 const char *const tree_code_class_strings[] =
124 "exceptional",
125 "constant",
126 "type",
127 "declaration",
128 "reference",
129 "comparison",
130 "unary",
131 "binary",
132 "statement",
133 "vl_exp",
134 "expression"
137 /* obstack.[ch] explicitly declined to prototype this. */
138 extern int _obstack_allocated_p (struct obstack *h, void *obj);
140 /* Statistics-gathering stuff. */
142 static int tree_code_counts[MAX_TREE_CODES];
143 int tree_node_counts[(int) all_kinds];
144 int tree_node_sizes[(int) all_kinds];
146 /* Keep in sync with tree.h:enum tree_node_kind. */
147 static const char * const tree_node_kind_names[] = {
148 "decls",
149 "types",
150 "blocks",
151 "stmts",
152 "refs",
153 "exprs",
154 "constants",
155 "identifiers",
156 "vecs",
157 "binfos",
158 "ssa names",
159 "constructors",
160 "random kinds",
161 "lang_decl kinds",
162 "lang_type kinds",
163 "omp clauses",
166 /* Unique id for next decl created. */
167 static GTY(()) int next_decl_uid;
168 /* Unique id for next type created. */
169 static GTY(()) int next_type_uid = 1;
170 /* Unique id for next debug decl created. Use negative numbers,
171 to catch erroneous uses. */
172 static GTY(()) int next_debug_decl_uid;
174 /* Since we cannot rehash a type after it is in the table, we have to
175 keep the hash code. */
177 struct GTY(()) type_hash {
178 unsigned long hash;
179 tree type;
182 /* Initial size of the hash table (rounded to next prime). */
183 #define TYPE_HASH_INITIAL_SIZE 1000
185 /* Now here is the hash table. When recording a type, it is added to
186 the slot whose index is the hash code. Note that the hash table is
187 used for several kinds of types (function types, array types and
188 array index range types, for now). While all these live in the
189 same table, they are completely independent, and the hash code is
190 computed differently for each of these. */
192 static GTY ((if_marked ("type_hash_marked_p"), param_is (struct type_hash)))
193 htab_t type_hash_table;
195 /* Hash table and temporary node for larger integer const values. */
196 static GTY (()) tree int_cst_node;
197 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node)))
198 htab_t int_cst_hash_table;
200 /* Hash table for optimization flags and target option flags. Use the same
201 hash table for both sets of options. Nodes for building the current
202 optimization and target option nodes. The assumption is most of the time
203 the options created will already be in the hash table, so we avoid
204 allocating and freeing up a node repeatably. */
205 static GTY (()) tree cl_optimization_node;
206 static GTY (()) tree cl_target_option_node;
207 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node)))
208 htab_t cl_option_hash_table;
210 /* General tree->tree mapping structure for use in hash tables. */
213 static GTY ((if_marked ("tree_decl_map_marked_p"), param_is (struct tree_decl_map)))
214 htab_t debug_expr_for_decl;
216 static GTY ((if_marked ("tree_decl_map_marked_p"), param_is (struct tree_decl_map)))
217 htab_t value_expr_for_decl;
219 static GTY ((if_marked ("tree_vec_map_marked_p"), param_is (struct tree_vec_map)))
220 htab_t debug_args_for_decl;
222 static void set_type_quals (tree, int);
223 static int type_hash_eq (const void *, const void *);
224 static hashval_t type_hash_hash (const void *);
225 static hashval_t int_cst_hash_hash (const void *);
226 static int int_cst_hash_eq (const void *, const void *);
227 static hashval_t cl_option_hash_hash (const void *);
228 static int cl_option_hash_eq (const void *, const void *);
229 static void print_type_hash_statistics (void);
230 static void print_debug_expr_statistics (void);
231 static void print_value_expr_statistics (void);
232 static int type_hash_marked_p (const void *);
233 static void type_hash_list (const_tree, inchash::hash &);
234 static void attribute_hash_list (const_tree, inchash::hash &);
236 tree global_trees[TI_MAX];
237 tree integer_types[itk_none];
239 bool int_n_enabled_p[NUM_INT_N_ENTS];
240 struct int_n_trees_t int_n_trees [NUM_INT_N_ENTS];
242 unsigned char tree_contains_struct[MAX_TREE_CODES][64];
244 /* Number of operands for each OpenMP clause. */
245 unsigned const char omp_clause_num_ops[] =
247 0, /* OMP_CLAUSE_ERROR */
248 1, /* OMP_CLAUSE_PRIVATE */
249 1, /* OMP_CLAUSE_SHARED */
250 1, /* OMP_CLAUSE_FIRSTPRIVATE */
251 2, /* OMP_CLAUSE_LASTPRIVATE */
252 4, /* OMP_CLAUSE_REDUCTION */
253 1, /* OMP_CLAUSE_COPYIN */
254 1, /* OMP_CLAUSE_COPYPRIVATE */
255 3, /* OMP_CLAUSE_LINEAR */
256 2, /* OMP_CLAUSE_ALIGNED */
257 1, /* OMP_CLAUSE_DEPEND */
258 1, /* OMP_CLAUSE_UNIFORM */
259 2, /* OMP_CLAUSE_FROM */
260 2, /* OMP_CLAUSE_TO */
261 2, /* OMP_CLAUSE_MAP */
262 1, /* OMP_CLAUSE__LOOPTEMP_ */
263 1, /* OMP_CLAUSE_IF */
264 1, /* OMP_CLAUSE_NUM_THREADS */
265 1, /* OMP_CLAUSE_SCHEDULE */
266 0, /* OMP_CLAUSE_NOWAIT */
267 0, /* OMP_CLAUSE_ORDERED */
268 0, /* OMP_CLAUSE_DEFAULT */
269 3, /* OMP_CLAUSE_COLLAPSE */
270 0, /* OMP_CLAUSE_UNTIED */
271 1, /* OMP_CLAUSE_FINAL */
272 0, /* OMP_CLAUSE_MERGEABLE */
273 1, /* OMP_CLAUSE_DEVICE */
274 1, /* OMP_CLAUSE_DIST_SCHEDULE */
275 0, /* OMP_CLAUSE_INBRANCH */
276 0, /* OMP_CLAUSE_NOTINBRANCH */
277 1, /* OMP_CLAUSE_NUM_TEAMS */
278 1, /* OMP_CLAUSE_THREAD_LIMIT */
279 0, /* OMP_CLAUSE_PROC_BIND */
280 1, /* OMP_CLAUSE_SAFELEN */
281 1, /* OMP_CLAUSE_SIMDLEN */
282 0, /* OMP_CLAUSE_FOR */
283 0, /* OMP_CLAUSE_PARALLEL */
284 0, /* OMP_CLAUSE_SECTIONS */
285 0, /* OMP_CLAUSE_TASKGROUP */
286 1, /* OMP_CLAUSE__SIMDUID_ */
287 1, /* OMP_CLAUSE__CILK_FOR_COUNT_ */
290 const char * const omp_clause_code_name[] =
292 "error_clause",
293 "private",
294 "shared",
295 "firstprivate",
296 "lastprivate",
297 "reduction",
298 "copyin",
299 "copyprivate",
300 "linear",
301 "aligned",
302 "depend",
303 "uniform",
304 "from",
305 "to",
306 "map",
307 "_looptemp_",
308 "if",
309 "num_threads",
310 "schedule",
311 "nowait",
312 "ordered",
313 "default",
314 "collapse",
315 "untied",
316 "final",
317 "mergeable",
318 "device",
319 "dist_schedule",
320 "inbranch",
321 "notinbranch",
322 "num_teams",
323 "thread_limit",
324 "proc_bind",
325 "safelen",
326 "simdlen",
327 "for",
328 "parallel",
329 "sections",
330 "taskgroup",
331 "_simduid_",
332 "_Cilk_for_count_"
336 /* Return the tree node structure used by tree code CODE. */
338 static inline enum tree_node_structure_enum
339 tree_node_structure_for_code (enum tree_code code)
341 switch (TREE_CODE_CLASS (code))
343 case tcc_declaration:
345 switch (code)
347 case FIELD_DECL:
348 return TS_FIELD_DECL;
349 case PARM_DECL:
350 return TS_PARM_DECL;
351 case VAR_DECL:
352 return TS_VAR_DECL;
353 case LABEL_DECL:
354 return TS_LABEL_DECL;
355 case RESULT_DECL:
356 return TS_RESULT_DECL;
357 case DEBUG_EXPR_DECL:
358 return TS_DECL_WRTL;
359 case CONST_DECL:
360 return TS_CONST_DECL;
361 case TYPE_DECL:
362 return TS_TYPE_DECL;
363 case FUNCTION_DECL:
364 return TS_FUNCTION_DECL;
365 case TRANSLATION_UNIT_DECL:
366 return TS_TRANSLATION_UNIT_DECL;
367 default:
368 return TS_DECL_NON_COMMON;
371 case tcc_type:
372 return TS_TYPE_NON_COMMON;
373 case tcc_reference:
374 case tcc_comparison:
375 case tcc_unary:
376 case tcc_binary:
377 case tcc_expression:
378 case tcc_statement:
379 case tcc_vl_exp:
380 return TS_EXP;
381 default: /* tcc_constant and tcc_exceptional */
382 break;
384 switch (code)
386 /* tcc_constant cases. */
387 case VOID_CST: return TS_TYPED;
388 case INTEGER_CST: return TS_INT_CST;
389 case REAL_CST: return TS_REAL_CST;
390 case FIXED_CST: return TS_FIXED_CST;
391 case COMPLEX_CST: return TS_COMPLEX;
392 case VECTOR_CST: return TS_VECTOR;
393 case STRING_CST: return TS_STRING;
394 /* tcc_exceptional cases. */
395 case ERROR_MARK: return TS_COMMON;
396 case IDENTIFIER_NODE: return TS_IDENTIFIER;
397 case TREE_LIST: return TS_LIST;
398 case TREE_VEC: return TS_VEC;
399 case SSA_NAME: return TS_SSA_NAME;
400 case PLACEHOLDER_EXPR: return TS_COMMON;
401 case STATEMENT_LIST: return TS_STATEMENT_LIST;
402 case BLOCK: return TS_BLOCK;
403 case CONSTRUCTOR: return TS_CONSTRUCTOR;
404 case TREE_BINFO: return TS_BINFO;
405 case OMP_CLAUSE: return TS_OMP_CLAUSE;
406 case OPTIMIZATION_NODE: return TS_OPTIMIZATION;
407 case TARGET_OPTION_NODE: return TS_TARGET_OPTION;
409 default:
410 gcc_unreachable ();
415 /* Initialize tree_contains_struct to describe the hierarchy of tree
416 nodes. */
418 static void
419 initialize_tree_contains_struct (void)
421 unsigned i;
423 for (i = ERROR_MARK; i < LAST_AND_UNUSED_TREE_CODE; i++)
425 enum tree_code code;
426 enum tree_node_structure_enum ts_code;
428 code = (enum tree_code) i;
429 ts_code = tree_node_structure_for_code (code);
431 /* Mark the TS structure itself. */
432 tree_contains_struct[code][ts_code] = 1;
434 /* Mark all the structures that TS is derived from. */
435 switch (ts_code)
437 case TS_TYPED:
438 case TS_BLOCK:
439 MARK_TS_BASE (code);
440 break;
442 case TS_COMMON:
443 case TS_INT_CST:
444 case TS_REAL_CST:
445 case TS_FIXED_CST:
446 case TS_VECTOR:
447 case TS_STRING:
448 case TS_COMPLEX:
449 case TS_SSA_NAME:
450 case TS_CONSTRUCTOR:
451 case TS_EXP:
452 case TS_STATEMENT_LIST:
453 MARK_TS_TYPED (code);
454 break;
456 case TS_IDENTIFIER:
457 case TS_DECL_MINIMAL:
458 case TS_TYPE_COMMON:
459 case TS_LIST:
460 case TS_VEC:
461 case TS_BINFO:
462 case TS_OMP_CLAUSE:
463 case TS_OPTIMIZATION:
464 case TS_TARGET_OPTION:
465 MARK_TS_COMMON (code);
466 break;
468 case TS_TYPE_WITH_LANG_SPECIFIC:
469 MARK_TS_TYPE_COMMON (code);
470 break;
472 case TS_TYPE_NON_COMMON:
473 MARK_TS_TYPE_WITH_LANG_SPECIFIC (code);
474 break;
476 case TS_DECL_COMMON:
477 MARK_TS_DECL_MINIMAL (code);
478 break;
480 case TS_DECL_WRTL:
481 case TS_CONST_DECL:
482 MARK_TS_DECL_COMMON (code);
483 break;
485 case TS_DECL_NON_COMMON:
486 MARK_TS_DECL_WITH_VIS (code);
487 break;
489 case TS_DECL_WITH_VIS:
490 case TS_PARM_DECL:
491 case TS_LABEL_DECL:
492 case TS_RESULT_DECL:
493 MARK_TS_DECL_WRTL (code);
494 break;
496 case TS_FIELD_DECL:
497 MARK_TS_DECL_COMMON (code);
498 break;
500 case TS_VAR_DECL:
501 MARK_TS_DECL_WITH_VIS (code);
502 break;
504 case TS_TYPE_DECL:
505 case TS_FUNCTION_DECL:
506 MARK_TS_DECL_NON_COMMON (code);
507 break;
509 case TS_TRANSLATION_UNIT_DECL:
510 MARK_TS_DECL_COMMON (code);
511 break;
513 default:
514 gcc_unreachable ();
518 /* Basic consistency checks for attributes used in fold. */
519 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_NON_COMMON]);
520 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_NON_COMMON]);
521 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_COMMON]);
522 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_COMMON]);
523 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_COMMON]);
524 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_COMMON]);
525 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_COMMON]);
526 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_COMMON]);
527 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_COMMON]);
528 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_COMMON]);
529 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_COMMON]);
530 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WRTL]);
531 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_WRTL]);
532 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_WRTL]);
533 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WRTL]);
534 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_WRTL]);
535 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_MINIMAL]);
536 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_MINIMAL]);
537 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_MINIMAL]);
538 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_MINIMAL]);
539 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_MINIMAL]);
540 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_MINIMAL]);
541 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_MINIMAL]);
542 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_MINIMAL]);
543 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_MINIMAL]);
544 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WITH_VIS]);
545 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WITH_VIS]);
546 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_WITH_VIS]);
547 gcc_assert (tree_contains_struct[VAR_DECL][TS_VAR_DECL]);
548 gcc_assert (tree_contains_struct[FIELD_DECL][TS_FIELD_DECL]);
549 gcc_assert (tree_contains_struct[PARM_DECL][TS_PARM_DECL]);
550 gcc_assert (tree_contains_struct[LABEL_DECL][TS_LABEL_DECL]);
551 gcc_assert (tree_contains_struct[RESULT_DECL][TS_RESULT_DECL]);
552 gcc_assert (tree_contains_struct[CONST_DECL][TS_CONST_DECL]);
553 gcc_assert (tree_contains_struct[TYPE_DECL][TS_TYPE_DECL]);
554 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_FUNCTION_DECL]);
555 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_MINIMAL]);
556 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_COMMON]);
557 gcc_assert (tree_contains_struct[NAMELIST_DECL][TS_DECL_MINIMAL]);
558 gcc_assert (tree_contains_struct[NAMELIST_DECL][TS_DECL_COMMON]);
562 /* Init tree.c. */
564 void
565 init_ttree (void)
567 /* Initialize the hash table of types. */
568 type_hash_table = htab_create_ggc (TYPE_HASH_INITIAL_SIZE, type_hash_hash,
569 type_hash_eq, 0);
571 debug_expr_for_decl = htab_create_ggc (512, tree_decl_map_hash,
572 tree_decl_map_eq, 0);
574 value_expr_for_decl = htab_create_ggc (512, tree_decl_map_hash,
575 tree_decl_map_eq, 0);
577 int_cst_hash_table = htab_create_ggc (1024, int_cst_hash_hash,
578 int_cst_hash_eq, NULL);
580 int_cst_node = make_int_cst (1, 1);
582 cl_option_hash_table = htab_create_ggc (64, cl_option_hash_hash,
583 cl_option_hash_eq, NULL);
585 cl_optimization_node = make_node (OPTIMIZATION_NODE);
586 cl_target_option_node = make_node (TARGET_OPTION_NODE);
588 /* Initialize the tree_contains_struct array. */
589 initialize_tree_contains_struct ();
590 lang_hooks.init_ts ();
594 /* The name of the object as the assembler will see it (but before any
595 translations made by ASM_OUTPUT_LABELREF). Often this is the same
596 as DECL_NAME. It is an IDENTIFIER_NODE. */
597 tree
598 decl_assembler_name (tree decl)
600 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
601 lang_hooks.set_decl_assembler_name (decl);
602 return DECL_WITH_VIS_CHECK (decl)->decl_with_vis.assembler_name;
605 /* When the target supports COMDAT groups, this indicates which group the
606 DECL is associated with. This can be either an IDENTIFIER_NODE or a
607 decl, in which case its DECL_ASSEMBLER_NAME identifies the group. */
608 tree
609 decl_comdat_group (const_tree node)
611 struct symtab_node *snode = symtab_node::get (node);
612 if (!snode)
613 return NULL;
614 return snode->get_comdat_group ();
617 /* Likewise, but make sure it's been reduced to an IDENTIFIER_NODE. */
618 tree
619 decl_comdat_group_id (const_tree node)
621 struct symtab_node *snode = symtab_node::get (node);
622 if (!snode)
623 return NULL;
624 return snode->get_comdat_group_id ();
627 /* When the target supports named section, return its name as IDENTIFIER_NODE
628 or NULL if it is in no section. */
629 const char *
630 decl_section_name (const_tree node)
632 struct symtab_node *snode = symtab_node::get (node);
633 if (!snode)
634 return NULL;
635 return snode->get_section ();
638 /* Set section section name of NODE to VALUE (that is expected to
639 be identifier node) */
640 void
641 set_decl_section_name (tree node, const char *value)
643 struct symtab_node *snode;
645 if (value == NULL)
647 snode = symtab_node::get (node);
648 if (!snode)
649 return;
651 else if (TREE_CODE (node) == VAR_DECL)
652 snode = varpool_node::get_create (node);
653 else
654 snode = cgraph_node::get_create (node);
655 snode->set_section (value);
658 /* Return TLS model of a variable NODE. */
659 enum tls_model
660 decl_tls_model (const_tree node)
662 struct varpool_node *snode = varpool_node::get (node);
663 if (!snode)
664 return TLS_MODEL_NONE;
665 return snode->tls_model;
668 /* Set TLS model of variable NODE to MODEL. */
669 void
670 set_decl_tls_model (tree node, enum tls_model model)
672 struct varpool_node *vnode;
674 if (model == TLS_MODEL_NONE)
676 vnode = varpool_node::get (node);
677 if (!vnode)
678 return;
680 else
681 vnode = varpool_node::get_create (node);
682 vnode->tls_model = model;
685 /* Compute the number of bytes occupied by a tree with code CODE.
686 This function cannot be used for nodes that have variable sizes,
687 including TREE_VEC, INTEGER_CST, STRING_CST, and CALL_EXPR. */
688 size_t
689 tree_code_size (enum tree_code code)
691 switch (TREE_CODE_CLASS (code))
693 case tcc_declaration: /* A decl node */
695 switch (code)
697 case FIELD_DECL:
698 return sizeof (struct tree_field_decl);
699 case PARM_DECL:
700 return sizeof (struct tree_parm_decl);
701 case VAR_DECL:
702 return sizeof (struct tree_var_decl);
703 case LABEL_DECL:
704 return sizeof (struct tree_label_decl);
705 case RESULT_DECL:
706 return sizeof (struct tree_result_decl);
707 case CONST_DECL:
708 return sizeof (struct tree_const_decl);
709 case TYPE_DECL:
710 return sizeof (struct tree_type_decl);
711 case FUNCTION_DECL:
712 return sizeof (struct tree_function_decl);
713 case DEBUG_EXPR_DECL:
714 return sizeof (struct tree_decl_with_rtl);
715 case TRANSLATION_UNIT_DECL:
716 return sizeof (struct tree_translation_unit_decl);
717 case NAMESPACE_DECL:
718 case IMPORTED_DECL:
719 case NAMELIST_DECL:
720 return sizeof (struct tree_decl_non_common);
721 default:
722 return lang_hooks.tree_size (code);
726 case tcc_type: /* a type node */
727 return sizeof (struct tree_type_non_common);
729 case tcc_reference: /* a reference */
730 case tcc_expression: /* an expression */
731 case tcc_statement: /* an expression with side effects */
732 case tcc_comparison: /* a comparison expression */
733 case tcc_unary: /* a unary arithmetic expression */
734 case tcc_binary: /* a binary arithmetic expression */
735 return (sizeof (struct tree_exp)
736 + (TREE_CODE_LENGTH (code) - 1) * sizeof (tree));
738 case tcc_constant: /* a constant */
739 switch (code)
741 case VOID_CST: return sizeof (struct tree_typed);
742 case INTEGER_CST: gcc_unreachable ();
743 case REAL_CST: return sizeof (struct tree_real_cst);
744 case FIXED_CST: return sizeof (struct tree_fixed_cst);
745 case COMPLEX_CST: return sizeof (struct tree_complex);
746 case VECTOR_CST: return sizeof (struct tree_vector);
747 case STRING_CST: gcc_unreachable ();
748 default:
749 return lang_hooks.tree_size (code);
752 case tcc_exceptional: /* something random, like an identifier. */
753 switch (code)
755 case IDENTIFIER_NODE: return lang_hooks.identifier_size;
756 case TREE_LIST: return sizeof (struct tree_list);
758 case ERROR_MARK:
759 case PLACEHOLDER_EXPR: return sizeof (struct tree_common);
761 case TREE_VEC:
762 case OMP_CLAUSE: gcc_unreachable ();
764 case SSA_NAME: return sizeof (struct tree_ssa_name);
766 case STATEMENT_LIST: return sizeof (struct tree_statement_list);
767 case BLOCK: return sizeof (struct tree_block);
768 case CONSTRUCTOR: return sizeof (struct tree_constructor);
769 case OPTIMIZATION_NODE: return sizeof (struct tree_optimization_option);
770 case TARGET_OPTION_NODE: return sizeof (struct tree_target_option);
772 default:
773 return lang_hooks.tree_size (code);
776 default:
777 gcc_unreachable ();
781 /* Compute the number of bytes occupied by NODE. This routine only
782 looks at TREE_CODE, except for those nodes that have variable sizes. */
783 size_t
784 tree_size (const_tree node)
786 const enum tree_code code = TREE_CODE (node);
787 switch (code)
789 case INTEGER_CST:
790 return (sizeof (struct tree_int_cst)
791 + (TREE_INT_CST_EXT_NUNITS (node) - 1) * sizeof (HOST_WIDE_INT));
793 case TREE_BINFO:
794 return (offsetof (struct tree_binfo, base_binfos)
795 + vec<tree, va_gc>
796 ::embedded_size (BINFO_N_BASE_BINFOS (node)));
798 case TREE_VEC:
799 return (sizeof (struct tree_vec)
800 + (TREE_VEC_LENGTH (node) - 1) * sizeof (tree));
802 case VECTOR_CST:
803 return (sizeof (struct tree_vector)
804 + (TYPE_VECTOR_SUBPARTS (TREE_TYPE (node)) - 1) * sizeof (tree));
806 case STRING_CST:
807 return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
809 case OMP_CLAUSE:
810 return (sizeof (struct tree_omp_clause)
811 + (omp_clause_num_ops[OMP_CLAUSE_CODE (node)] - 1)
812 * sizeof (tree));
814 default:
815 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
816 return (sizeof (struct tree_exp)
817 + (VL_EXP_OPERAND_LENGTH (node) - 1) * sizeof (tree));
818 else
819 return tree_code_size (code);
823 /* Record interesting allocation statistics for a tree node with CODE
824 and LENGTH. */
826 static void
827 record_node_allocation_statistics (enum tree_code code ATTRIBUTE_UNUSED,
828 size_t length ATTRIBUTE_UNUSED)
830 enum tree_code_class type = TREE_CODE_CLASS (code);
831 tree_node_kind kind;
833 if (!GATHER_STATISTICS)
834 return;
836 switch (type)
838 case tcc_declaration: /* A decl node */
839 kind = d_kind;
840 break;
842 case tcc_type: /* a type node */
843 kind = t_kind;
844 break;
846 case tcc_statement: /* an expression with side effects */
847 kind = s_kind;
848 break;
850 case tcc_reference: /* a reference */
851 kind = r_kind;
852 break;
854 case tcc_expression: /* an expression */
855 case tcc_comparison: /* a comparison expression */
856 case tcc_unary: /* a unary arithmetic expression */
857 case tcc_binary: /* a binary arithmetic expression */
858 kind = e_kind;
859 break;
861 case tcc_constant: /* a constant */
862 kind = c_kind;
863 break;
865 case tcc_exceptional: /* something random, like an identifier. */
866 switch (code)
868 case IDENTIFIER_NODE:
869 kind = id_kind;
870 break;
872 case TREE_VEC:
873 kind = vec_kind;
874 break;
876 case TREE_BINFO:
877 kind = binfo_kind;
878 break;
880 case SSA_NAME:
881 kind = ssa_name_kind;
882 break;
884 case BLOCK:
885 kind = b_kind;
886 break;
888 case CONSTRUCTOR:
889 kind = constr_kind;
890 break;
892 case OMP_CLAUSE:
893 kind = omp_clause_kind;
894 break;
896 default:
897 kind = x_kind;
898 break;
900 break;
902 case tcc_vl_exp:
903 kind = e_kind;
904 break;
906 default:
907 gcc_unreachable ();
910 tree_code_counts[(int) code]++;
911 tree_node_counts[(int) kind]++;
912 tree_node_sizes[(int) kind] += length;
915 /* Allocate and return a new UID from the DECL_UID namespace. */
918 allocate_decl_uid (void)
920 return next_decl_uid++;
923 /* Return a newly allocated node of code CODE. For decl and type
924 nodes, some other fields are initialized. The rest of the node is
925 initialized to zero. This function cannot be used for TREE_VEC,
926 INTEGER_CST or OMP_CLAUSE nodes, which is enforced by asserts in
927 tree_code_size.
929 Achoo! I got a code in the node. */
931 tree
932 make_node_stat (enum tree_code code MEM_STAT_DECL)
934 tree t;
935 enum tree_code_class type = TREE_CODE_CLASS (code);
936 size_t length = tree_code_size (code);
938 record_node_allocation_statistics (code, length);
940 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
941 TREE_SET_CODE (t, code);
943 switch (type)
945 case tcc_statement:
946 TREE_SIDE_EFFECTS (t) = 1;
947 break;
949 case tcc_declaration:
950 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
952 if (code == FUNCTION_DECL)
954 DECL_ALIGN (t) = FUNCTION_BOUNDARY;
955 DECL_MODE (t) = FUNCTION_MODE;
957 else
958 DECL_ALIGN (t) = 1;
960 DECL_SOURCE_LOCATION (t) = input_location;
961 if (TREE_CODE (t) == DEBUG_EXPR_DECL)
962 DECL_UID (t) = --next_debug_decl_uid;
963 else
965 DECL_UID (t) = allocate_decl_uid ();
966 SET_DECL_PT_UID (t, -1);
968 if (TREE_CODE (t) == LABEL_DECL)
969 LABEL_DECL_UID (t) = -1;
971 break;
973 case tcc_type:
974 TYPE_UID (t) = next_type_uid++;
975 TYPE_ALIGN (t) = BITS_PER_UNIT;
976 TYPE_USER_ALIGN (t) = 0;
977 TYPE_MAIN_VARIANT (t) = t;
978 TYPE_CANONICAL (t) = t;
980 /* Default to no attributes for type, but let target change that. */
981 TYPE_ATTRIBUTES (t) = NULL_TREE;
982 targetm.set_default_type_attributes (t);
984 /* We have not yet computed the alias set for this type. */
985 TYPE_ALIAS_SET (t) = -1;
986 break;
988 case tcc_constant:
989 TREE_CONSTANT (t) = 1;
990 break;
992 case tcc_expression:
993 switch (code)
995 case INIT_EXPR:
996 case MODIFY_EXPR:
997 case VA_ARG_EXPR:
998 case PREDECREMENT_EXPR:
999 case PREINCREMENT_EXPR:
1000 case POSTDECREMENT_EXPR:
1001 case POSTINCREMENT_EXPR:
1002 /* All of these have side-effects, no matter what their
1003 operands are. */
1004 TREE_SIDE_EFFECTS (t) = 1;
1005 break;
1007 default:
1008 break;
1010 break;
1012 default:
1013 /* Other classes need no special treatment. */
1014 break;
1017 return t;
1020 /* Return a new node with the same contents as NODE except that its
1021 TREE_CHAIN, if it has one, is zero and it has a fresh uid. */
1023 tree
1024 copy_node_stat (tree node MEM_STAT_DECL)
1026 tree t;
1027 enum tree_code code = TREE_CODE (node);
1028 size_t length;
1030 gcc_assert (code != STATEMENT_LIST);
1032 length = tree_size (node);
1033 record_node_allocation_statistics (code, length);
1034 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
1035 memcpy (t, node, length);
1037 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
1038 TREE_CHAIN (t) = 0;
1039 TREE_ASM_WRITTEN (t) = 0;
1040 TREE_VISITED (t) = 0;
1042 if (TREE_CODE_CLASS (code) == tcc_declaration)
1044 if (code == DEBUG_EXPR_DECL)
1045 DECL_UID (t) = --next_debug_decl_uid;
1046 else
1048 DECL_UID (t) = allocate_decl_uid ();
1049 if (DECL_PT_UID_SET_P (node))
1050 SET_DECL_PT_UID (t, DECL_PT_UID (node));
1052 if ((TREE_CODE (node) == PARM_DECL || TREE_CODE (node) == VAR_DECL)
1053 && DECL_HAS_VALUE_EXPR_P (node))
1055 SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (node));
1056 DECL_HAS_VALUE_EXPR_P (t) = 1;
1058 /* DECL_DEBUG_EXPR is copied explicitely by callers. */
1059 if (TREE_CODE (node) == VAR_DECL)
1061 DECL_HAS_DEBUG_EXPR_P (t) = 0;
1062 t->decl_with_vis.symtab_node = NULL;
1064 if (TREE_CODE (node) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (node))
1066 SET_DECL_INIT_PRIORITY (t, DECL_INIT_PRIORITY (node));
1067 DECL_HAS_INIT_PRIORITY_P (t) = 1;
1069 if (TREE_CODE (node) == FUNCTION_DECL)
1071 DECL_STRUCT_FUNCTION (t) = NULL;
1072 t->decl_with_vis.symtab_node = NULL;
1075 else if (TREE_CODE_CLASS (code) == tcc_type)
1077 TYPE_UID (t) = next_type_uid++;
1078 /* The following is so that the debug code for
1079 the copy is different from the original type.
1080 The two statements usually duplicate each other
1081 (because they clear fields of the same union),
1082 but the optimizer should catch that. */
1083 TYPE_SYMTAB_POINTER (t) = 0;
1084 TYPE_SYMTAB_ADDRESS (t) = 0;
1086 /* Do not copy the values cache. */
1087 if (TYPE_CACHED_VALUES_P (t))
1089 TYPE_CACHED_VALUES_P (t) = 0;
1090 TYPE_CACHED_VALUES (t) = NULL_TREE;
1094 return t;
1097 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
1098 For example, this can copy a list made of TREE_LIST nodes. */
1100 tree
1101 copy_list (tree list)
1103 tree head;
1104 tree prev, next;
1106 if (list == 0)
1107 return 0;
1109 head = prev = copy_node (list);
1110 next = TREE_CHAIN (list);
1111 while (next)
1113 TREE_CHAIN (prev) = copy_node (next);
1114 prev = TREE_CHAIN (prev);
1115 next = TREE_CHAIN (next);
1117 return head;
1121 /* Return the value that TREE_INT_CST_EXT_NUNITS should have for an
1122 INTEGER_CST with value CST and type TYPE. */
1124 static unsigned int
1125 get_int_cst_ext_nunits (tree type, const wide_int &cst)
1127 gcc_checking_assert (cst.get_precision () == TYPE_PRECISION (type));
1128 /* We need an extra zero HWI if CST is an unsigned integer with its
1129 upper bit set, and if CST occupies a whole number of HWIs. */
1130 if (TYPE_UNSIGNED (type)
1131 && wi::neg_p (cst)
1132 && (cst.get_precision () % HOST_BITS_PER_WIDE_INT) == 0)
1133 return cst.get_precision () / HOST_BITS_PER_WIDE_INT + 1;
1134 return cst.get_len ();
1137 /* Return a new INTEGER_CST with value CST and type TYPE. */
1139 static tree
1140 build_new_int_cst (tree type, const wide_int &cst)
1142 unsigned int len = cst.get_len ();
1143 unsigned int ext_len = get_int_cst_ext_nunits (type, cst);
1144 tree nt = make_int_cst (len, ext_len);
1146 if (len < ext_len)
1148 --ext_len;
1149 TREE_INT_CST_ELT (nt, ext_len) = 0;
1150 for (unsigned int i = len; i < ext_len; ++i)
1151 TREE_INT_CST_ELT (nt, i) = -1;
1153 else if (TYPE_UNSIGNED (type)
1154 && cst.get_precision () < len * HOST_BITS_PER_WIDE_INT)
1156 len--;
1157 TREE_INT_CST_ELT (nt, len)
1158 = zext_hwi (cst.elt (len),
1159 cst.get_precision () % HOST_BITS_PER_WIDE_INT);
1162 for (unsigned int i = 0; i < len; i++)
1163 TREE_INT_CST_ELT (nt, i) = cst.elt (i);
1164 TREE_TYPE (nt) = type;
1165 return nt;
1168 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1170 tree
1171 build_int_cst (tree type, HOST_WIDE_INT low)
1173 /* Support legacy code. */
1174 if (!type)
1175 type = integer_type_node;
1177 return wide_int_to_tree (type, wi::shwi (low, TYPE_PRECISION (type)));
1180 tree
1181 build_int_cstu (tree type, unsigned HOST_WIDE_INT cst)
1183 return wide_int_to_tree (type, wi::uhwi (cst, TYPE_PRECISION (type)));
1186 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1188 tree
1189 build_int_cst_type (tree type, HOST_WIDE_INT low)
1191 gcc_assert (type);
1192 return wide_int_to_tree (type, wi::shwi (low, TYPE_PRECISION (type)));
1195 /* Constructs tree in type TYPE from with value given by CST. Signedness
1196 of CST is assumed to be the same as the signedness of TYPE. */
1198 tree
1199 double_int_to_tree (tree type, double_int cst)
1201 return wide_int_to_tree (type, widest_int::from (cst, TYPE_SIGN (type)));
1204 /* We force the wide_int CST to the range of the type TYPE by sign or
1205 zero extending it. OVERFLOWABLE indicates if we are interested in
1206 overflow of the value, when >0 we are only interested in signed
1207 overflow, for <0 we are interested in any overflow. OVERFLOWED
1208 indicates whether overflow has already occurred. CONST_OVERFLOWED
1209 indicates whether constant overflow has already occurred. We force
1210 T's value to be within range of T's type (by setting to 0 or 1 all
1211 the bits outside the type's range). We set TREE_OVERFLOWED if,
1212 OVERFLOWED is nonzero,
1213 or OVERFLOWABLE is >0 and signed overflow occurs
1214 or OVERFLOWABLE is <0 and any overflow occurs
1215 We return a new tree node for the extended wide_int. The node
1216 is shared if no overflow flags are set. */
1219 tree
1220 force_fit_type (tree type, const wide_int_ref &cst,
1221 int overflowable, bool overflowed)
1223 signop sign = TYPE_SIGN (type);
1225 /* If we need to set overflow flags, return a new unshared node. */
1226 if (overflowed || !wi::fits_to_tree_p (cst, type))
1228 if (overflowed
1229 || overflowable < 0
1230 || (overflowable > 0 && sign == SIGNED))
1232 wide_int tmp = wide_int::from (cst, TYPE_PRECISION (type), sign);
1233 tree t = build_new_int_cst (type, tmp);
1234 TREE_OVERFLOW (t) = 1;
1235 return t;
1239 /* Else build a shared node. */
1240 return wide_int_to_tree (type, cst);
1243 /* These are the hash table functions for the hash table of INTEGER_CST
1244 nodes of a sizetype. */
1246 /* Return the hash code code X, an INTEGER_CST. */
1248 static hashval_t
1249 int_cst_hash_hash (const void *x)
1251 const_tree const t = (const_tree) x;
1252 hashval_t code = htab_hash_pointer (TREE_TYPE (t));
1253 int i;
1255 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
1256 code ^= TREE_INT_CST_ELT (t, i);
1258 return code;
1261 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
1262 is the same as that given by *Y, which is the same. */
1264 static int
1265 int_cst_hash_eq (const void *x, const void *y)
1267 const_tree const xt = (const_tree) x;
1268 const_tree const yt = (const_tree) y;
1270 if (TREE_TYPE (xt) != TREE_TYPE (yt)
1271 || TREE_INT_CST_NUNITS (xt) != TREE_INT_CST_NUNITS (yt)
1272 || TREE_INT_CST_EXT_NUNITS (xt) != TREE_INT_CST_EXT_NUNITS (yt))
1273 return false;
1275 for (int i = 0; i < TREE_INT_CST_NUNITS (xt); i++)
1276 if (TREE_INT_CST_ELT (xt, i) != TREE_INT_CST_ELT (yt, i))
1277 return false;
1279 return true;
1282 /* Create an INT_CST node of TYPE and value CST.
1283 The returned node is always shared. For small integers we use a
1284 per-type vector cache, for larger ones we use a single hash table.
1285 The value is extended from its precision according to the sign of
1286 the type to be a multiple of HOST_BITS_PER_WIDE_INT. This defines
1287 the upper bits and ensures that hashing and value equality based
1288 upon the underlying HOST_WIDE_INTs works without masking. */
1290 tree
1291 wide_int_to_tree (tree type, const wide_int_ref &pcst)
1293 tree t;
1294 int ix = -1;
1295 int limit = 0;
1297 gcc_assert (type);
1298 unsigned int prec = TYPE_PRECISION (type);
1299 signop sgn = TYPE_SIGN (type);
1301 /* Verify that everything is canonical. */
1302 int l = pcst.get_len ();
1303 if (l > 1)
1305 if (pcst.elt (l - 1) == 0)
1306 gcc_checking_assert (pcst.elt (l - 2) < 0);
1307 if (pcst.elt (l - 1) == (HOST_WIDE_INT) -1)
1308 gcc_checking_assert (pcst.elt (l - 2) >= 0);
1311 wide_int cst = wide_int::from (pcst, prec, sgn);
1312 unsigned int ext_len = get_int_cst_ext_nunits (type, cst);
1314 if (ext_len == 1)
1316 /* We just need to store a single HOST_WIDE_INT. */
1317 HOST_WIDE_INT hwi;
1318 if (TYPE_UNSIGNED (type))
1319 hwi = cst.to_uhwi ();
1320 else
1321 hwi = cst.to_shwi ();
1323 switch (TREE_CODE (type))
1325 case NULLPTR_TYPE:
1326 gcc_assert (hwi == 0);
1327 /* Fallthru. */
1329 case POINTER_TYPE:
1330 case REFERENCE_TYPE:
1331 /* Cache NULL pointer. */
1332 if (hwi == 0)
1334 limit = 1;
1335 ix = 0;
1337 break;
1339 case BOOLEAN_TYPE:
1340 /* Cache false or true. */
1341 limit = 2;
1342 if (hwi < 2)
1343 ix = hwi;
1344 break;
1346 case INTEGER_TYPE:
1347 case OFFSET_TYPE:
1348 if (TYPE_SIGN (type) == UNSIGNED)
1350 /* Cache [0, N). */
1351 limit = INTEGER_SHARE_LIMIT;
1352 if (IN_RANGE (hwi, 0, INTEGER_SHARE_LIMIT - 1))
1353 ix = hwi;
1355 else
1357 /* Cache [-1, N). */
1358 limit = INTEGER_SHARE_LIMIT + 1;
1359 if (IN_RANGE (hwi, -1, INTEGER_SHARE_LIMIT - 1))
1360 ix = hwi + 1;
1362 break;
1364 case ENUMERAL_TYPE:
1365 break;
1367 default:
1368 gcc_unreachable ();
1371 if (ix >= 0)
1373 /* Look for it in the type's vector of small shared ints. */
1374 if (!TYPE_CACHED_VALUES_P (type))
1376 TYPE_CACHED_VALUES_P (type) = 1;
1377 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1380 t = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix);
1381 if (t)
1382 /* Make sure no one is clobbering the shared constant. */
1383 gcc_checking_assert (TREE_TYPE (t) == type
1384 && TREE_INT_CST_NUNITS (t) == 1
1385 && TREE_INT_CST_OFFSET_NUNITS (t) == 1
1386 && TREE_INT_CST_EXT_NUNITS (t) == 1
1387 && TREE_INT_CST_ELT (t, 0) == hwi);
1388 else
1390 /* Create a new shared int. */
1391 t = build_new_int_cst (type, cst);
1392 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1395 else
1397 /* Use the cache of larger shared ints, using int_cst_node as
1398 a temporary. */
1399 void **slot;
1401 TREE_INT_CST_ELT (int_cst_node, 0) = hwi;
1402 TREE_TYPE (int_cst_node) = type;
1404 slot = htab_find_slot (int_cst_hash_table, int_cst_node, INSERT);
1405 t = (tree) *slot;
1406 if (!t)
1408 /* Insert this one into the hash table. */
1409 t = int_cst_node;
1410 *slot = t;
1411 /* Make a new node for next time round. */
1412 int_cst_node = make_int_cst (1, 1);
1416 else
1418 /* The value either hashes properly or we drop it on the floor
1419 for the gc to take care of. There will not be enough of them
1420 to worry about. */
1421 void **slot;
1423 tree nt = build_new_int_cst (type, cst);
1424 slot = htab_find_slot (int_cst_hash_table, nt, INSERT);
1425 t = (tree) *slot;
1426 if (!t)
1428 /* Insert this one into the hash table. */
1429 t = nt;
1430 *slot = t;
1434 return t;
1437 void
1438 cache_integer_cst (tree t)
1440 tree type = TREE_TYPE (t);
1441 int ix = -1;
1442 int limit = 0;
1443 int prec = TYPE_PRECISION (type);
1445 gcc_assert (!TREE_OVERFLOW (t));
1447 switch (TREE_CODE (type))
1449 case NULLPTR_TYPE:
1450 gcc_assert (integer_zerop (t));
1451 /* Fallthru. */
1453 case POINTER_TYPE:
1454 case REFERENCE_TYPE:
1455 /* Cache NULL pointer. */
1456 if (integer_zerop (t))
1458 limit = 1;
1459 ix = 0;
1461 break;
1463 case BOOLEAN_TYPE:
1464 /* Cache false or true. */
1465 limit = 2;
1466 if (wi::ltu_p (t, 2))
1467 ix = TREE_INT_CST_ELT (t, 0);
1468 break;
1470 case INTEGER_TYPE:
1471 case OFFSET_TYPE:
1472 if (TYPE_UNSIGNED (type))
1474 /* Cache 0..N */
1475 limit = INTEGER_SHARE_LIMIT;
1477 /* This is a little hokie, but if the prec is smaller than
1478 what is necessary to hold INTEGER_SHARE_LIMIT, then the
1479 obvious test will not get the correct answer. */
1480 if (prec < HOST_BITS_PER_WIDE_INT)
1482 if (tree_to_uhwi (t) < (unsigned HOST_WIDE_INT) INTEGER_SHARE_LIMIT)
1483 ix = tree_to_uhwi (t);
1485 else if (wi::ltu_p (t, INTEGER_SHARE_LIMIT))
1486 ix = tree_to_uhwi (t);
1488 else
1490 /* Cache -1..N */
1491 limit = INTEGER_SHARE_LIMIT + 1;
1493 if (integer_minus_onep (t))
1494 ix = 0;
1495 else if (!wi::neg_p (t))
1497 if (prec < HOST_BITS_PER_WIDE_INT)
1499 if (tree_to_shwi (t) < INTEGER_SHARE_LIMIT)
1500 ix = tree_to_shwi (t) + 1;
1502 else if (wi::ltu_p (t, INTEGER_SHARE_LIMIT))
1503 ix = tree_to_shwi (t) + 1;
1506 break;
1508 case ENUMERAL_TYPE:
1509 break;
1511 default:
1512 gcc_unreachable ();
1515 if (ix >= 0)
1517 /* Look for it in the type's vector of small shared ints. */
1518 if (!TYPE_CACHED_VALUES_P (type))
1520 TYPE_CACHED_VALUES_P (type) = 1;
1521 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1524 gcc_assert (TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) == NULL_TREE);
1525 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1527 else
1529 /* Use the cache of larger shared ints. */
1530 void **slot;
1532 slot = htab_find_slot (int_cst_hash_table, t, INSERT);
1533 /* If there is already an entry for the number verify it's the
1534 same. */
1535 if (*slot)
1536 gcc_assert (wi::eq_p (tree (*slot), t));
1537 else
1538 /* Otherwise insert this one into the hash table. */
1539 *slot = t;
1544 /* Builds an integer constant in TYPE such that lowest BITS bits are ones
1545 and the rest are zeros. */
1547 tree
1548 build_low_bits_mask (tree type, unsigned bits)
1550 gcc_assert (bits <= TYPE_PRECISION (type));
1552 return wide_int_to_tree (type, wi::mask (bits, false,
1553 TYPE_PRECISION (type)));
1556 /* Checks that X is integer constant that can be expressed in (unsigned)
1557 HOST_WIDE_INT without loss of precision. */
1559 bool
1560 cst_and_fits_in_hwi (const_tree x)
1562 if (TREE_CODE (x) != INTEGER_CST)
1563 return false;
1565 if (TYPE_PRECISION (TREE_TYPE (x)) > HOST_BITS_PER_WIDE_INT)
1566 return false;
1568 return TREE_INT_CST_NUNITS (x) == 1;
1571 /* Build a newly constructed TREE_VEC node of length LEN. */
1573 tree
1574 make_vector_stat (unsigned len MEM_STAT_DECL)
1576 tree t;
1577 unsigned length = (len - 1) * sizeof (tree) + sizeof (struct tree_vector);
1579 record_node_allocation_statistics (VECTOR_CST, length);
1581 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1583 TREE_SET_CODE (t, VECTOR_CST);
1584 TREE_CONSTANT (t) = 1;
1586 return t;
1589 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1590 are in a list pointed to by VALS. */
1592 tree
1593 build_vector_stat (tree type, tree *vals MEM_STAT_DECL)
1595 int over = 0;
1596 unsigned cnt = 0;
1597 tree v = make_vector (TYPE_VECTOR_SUBPARTS (type));
1598 TREE_TYPE (v) = type;
1600 /* Iterate through elements and check for overflow. */
1601 for (cnt = 0; cnt < TYPE_VECTOR_SUBPARTS (type); ++cnt)
1603 tree value = vals[cnt];
1605 VECTOR_CST_ELT (v, cnt) = value;
1607 /* Don't crash if we get an address constant. */
1608 if (!CONSTANT_CLASS_P (value))
1609 continue;
1611 over |= TREE_OVERFLOW (value);
1614 TREE_OVERFLOW (v) = over;
1615 return v;
1618 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1619 are extracted from V, a vector of CONSTRUCTOR_ELT. */
1621 tree
1622 build_vector_from_ctor (tree type, vec<constructor_elt, va_gc> *v)
1624 tree *vec = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
1625 unsigned HOST_WIDE_INT idx;
1626 tree value;
1628 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1629 vec[idx] = value;
1630 for (; idx < TYPE_VECTOR_SUBPARTS (type); ++idx)
1631 vec[idx] = build_zero_cst (TREE_TYPE (type));
1633 return build_vector (type, vec);
1636 /* Build a vector of type VECTYPE where all the elements are SCs. */
1637 tree
1638 build_vector_from_val (tree vectype, tree sc)
1640 int i, nunits = TYPE_VECTOR_SUBPARTS (vectype);
1642 if (sc == error_mark_node)
1643 return sc;
1645 /* Verify that the vector type is suitable for SC. Note that there
1646 is some inconsistency in the type-system with respect to restrict
1647 qualifications of pointers. Vector types always have a main-variant
1648 element type and the qualification is applied to the vector-type.
1649 So TREE_TYPE (vector-type) does not return a properly qualified
1650 vector element-type. */
1651 gcc_checking_assert (types_compatible_p (TYPE_MAIN_VARIANT (TREE_TYPE (sc)),
1652 TREE_TYPE (vectype)));
1654 if (CONSTANT_CLASS_P (sc))
1656 tree *v = XALLOCAVEC (tree, nunits);
1657 for (i = 0; i < nunits; ++i)
1658 v[i] = sc;
1659 return build_vector (vectype, v);
1661 else
1663 vec<constructor_elt, va_gc> *v;
1664 vec_alloc (v, nunits);
1665 for (i = 0; i < nunits; ++i)
1666 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, sc);
1667 return build_constructor (vectype, v);
1671 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1672 are in the vec pointed to by VALS. */
1673 tree
1674 build_constructor (tree type, vec<constructor_elt, va_gc> *vals)
1676 tree c = make_node (CONSTRUCTOR);
1677 unsigned int i;
1678 constructor_elt *elt;
1679 bool constant_p = true;
1680 bool side_effects_p = false;
1682 TREE_TYPE (c) = type;
1683 CONSTRUCTOR_ELTS (c) = vals;
1685 FOR_EACH_VEC_SAFE_ELT (vals, i, elt)
1687 /* Mostly ctors will have elts that don't have side-effects, so
1688 the usual case is to scan all the elements. Hence a single
1689 loop for both const and side effects, rather than one loop
1690 each (with early outs). */
1691 if (!TREE_CONSTANT (elt->value))
1692 constant_p = false;
1693 if (TREE_SIDE_EFFECTS (elt->value))
1694 side_effects_p = true;
1697 TREE_SIDE_EFFECTS (c) = side_effects_p;
1698 TREE_CONSTANT (c) = constant_p;
1700 return c;
1703 /* Build a CONSTRUCTOR node made of a single initializer, with the specified
1704 INDEX and VALUE. */
1705 tree
1706 build_constructor_single (tree type, tree index, tree value)
1708 vec<constructor_elt, va_gc> *v;
1709 constructor_elt elt = {index, value};
1711 vec_alloc (v, 1);
1712 v->quick_push (elt);
1714 return build_constructor (type, v);
1718 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1719 are in a list pointed to by VALS. */
1720 tree
1721 build_constructor_from_list (tree type, tree vals)
1723 tree t;
1724 vec<constructor_elt, va_gc> *v = NULL;
1726 if (vals)
1728 vec_alloc (v, list_length (vals));
1729 for (t = vals; t; t = TREE_CHAIN (t))
1730 CONSTRUCTOR_APPEND_ELT (v, TREE_PURPOSE (t), TREE_VALUE (t));
1733 return build_constructor (type, v);
1736 /* Return a new CONSTRUCTOR node whose type is TYPE. NELTS is the number
1737 of elements, provided as index/value pairs. */
1739 tree
1740 build_constructor_va (tree type, int nelts, ...)
1742 vec<constructor_elt, va_gc> *v = NULL;
1743 va_list p;
1745 va_start (p, nelts);
1746 vec_alloc (v, nelts);
1747 while (nelts--)
1749 tree index = va_arg (p, tree);
1750 tree value = va_arg (p, tree);
1751 CONSTRUCTOR_APPEND_ELT (v, index, value);
1753 va_end (p);
1754 return build_constructor (type, v);
1757 /* Return a new FIXED_CST node whose type is TYPE and value is F. */
1759 tree
1760 build_fixed (tree type, FIXED_VALUE_TYPE f)
1762 tree v;
1763 FIXED_VALUE_TYPE *fp;
1765 v = make_node (FIXED_CST);
1766 fp = ggc_alloc<fixed_value> ();
1767 memcpy (fp, &f, sizeof (FIXED_VALUE_TYPE));
1769 TREE_TYPE (v) = type;
1770 TREE_FIXED_CST_PTR (v) = fp;
1771 return v;
1774 /* Return a new REAL_CST node whose type is TYPE and value is D. */
1776 tree
1777 build_real (tree type, REAL_VALUE_TYPE d)
1779 tree v;
1780 REAL_VALUE_TYPE *dp;
1781 int overflow = 0;
1783 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
1784 Consider doing it via real_convert now. */
1786 v = make_node (REAL_CST);
1787 dp = ggc_alloc<real_value> ();
1788 memcpy (dp, &d, sizeof (REAL_VALUE_TYPE));
1790 TREE_TYPE (v) = type;
1791 TREE_REAL_CST_PTR (v) = dp;
1792 TREE_OVERFLOW (v) = overflow;
1793 return v;
1796 /* Return a new REAL_CST node whose type is TYPE
1797 and whose value is the integer value of the INTEGER_CST node I. */
1799 REAL_VALUE_TYPE
1800 real_value_from_int_cst (const_tree type, const_tree i)
1802 REAL_VALUE_TYPE d;
1804 /* Clear all bits of the real value type so that we can later do
1805 bitwise comparisons to see if two values are the same. */
1806 memset (&d, 0, sizeof d);
1808 real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode, i,
1809 TYPE_SIGN (TREE_TYPE (i)));
1810 return d;
1813 /* Given a tree representing an integer constant I, return a tree
1814 representing the same value as a floating-point constant of type TYPE. */
1816 tree
1817 build_real_from_int_cst (tree type, const_tree i)
1819 tree v;
1820 int overflow = TREE_OVERFLOW (i);
1822 v = build_real (type, real_value_from_int_cst (type, i));
1824 TREE_OVERFLOW (v) |= overflow;
1825 return v;
1828 /* Return a newly constructed STRING_CST node whose value is
1829 the LEN characters at STR.
1830 Note that for a C string literal, LEN should include the trailing NUL.
1831 The TREE_TYPE is not initialized. */
1833 tree
1834 build_string (int len, const char *str)
1836 tree s;
1837 size_t length;
1839 /* Do not waste bytes provided by padding of struct tree_string. */
1840 length = len + offsetof (struct tree_string, str) + 1;
1842 record_node_allocation_statistics (STRING_CST, length);
1844 s = (tree) ggc_internal_alloc (length);
1846 memset (s, 0, sizeof (struct tree_typed));
1847 TREE_SET_CODE (s, STRING_CST);
1848 TREE_CONSTANT (s) = 1;
1849 TREE_STRING_LENGTH (s) = len;
1850 memcpy (s->string.str, str, len);
1851 s->string.str[len] = '\0';
1853 return s;
1856 /* Return a newly constructed COMPLEX_CST node whose value is
1857 specified by the real and imaginary parts REAL and IMAG.
1858 Both REAL and IMAG should be constant nodes. TYPE, if specified,
1859 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
1861 tree
1862 build_complex (tree type, tree real, tree imag)
1864 tree t = make_node (COMPLEX_CST);
1866 TREE_REALPART (t) = real;
1867 TREE_IMAGPART (t) = imag;
1868 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
1869 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
1870 return t;
1873 /* Return a constant of arithmetic type TYPE which is the
1874 multiplicative identity of the set TYPE. */
1876 tree
1877 build_one_cst (tree type)
1879 switch (TREE_CODE (type))
1881 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1882 case POINTER_TYPE: case REFERENCE_TYPE:
1883 case OFFSET_TYPE:
1884 return build_int_cst (type, 1);
1886 case REAL_TYPE:
1887 return build_real (type, dconst1);
1889 case FIXED_POINT_TYPE:
1890 /* We can only generate 1 for accum types. */
1891 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
1892 return build_fixed (type, FCONST1 (TYPE_MODE (type)));
1894 case VECTOR_TYPE:
1896 tree scalar = build_one_cst (TREE_TYPE (type));
1898 return build_vector_from_val (type, scalar);
1901 case COMPLEX_TYPE:
1902 return build_complex (type,
1903 build_one_cst (TREE_TYPE (type)),
1904 build_zero_cst (TREE_TYPE (type)));
1906 default:
1907 gcc_unreachable ();
1911 /* Return an integer of type TYPE containing all 1's in as much precision as
1912 it contains, or a complex or vector whose subparts are such integers. */
1914 tree
1915 build_all_ones_cst (tree type)
1917 if (TREE_CODE (type) == COMPLEX_TYPE)
1919 tree scalar = build_all_ones_cst (TREE_TYPE (type));
1920 return build_complex (type, scalar, scalar);
1922 else
1923 return build_minus_one_cst (type);
1926 /* Return a constant of arithmetic type TYPE which is the
1927 opposite of the multiplicative identity of the set TYPE. */
1929 tree
1930 build_minus_one_cst (tree type)
1932 switch (TREE_CODE (type))
1934 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1935 case POINTER_TYPE: case REFERENCE_TYPE:
1936 case OFFSET_TYPE:
1937 return build_int_cst (type, -1);
1939 case REAL_TYPE:
1940 return build_real (type, dconstm1);
1942 case FIXED_POINT_TYPE:
1943 /* We can only generate 1 for accum types. */
1944 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
1945 return build_fixed (type, fixed_from_double_int (double_int_minus_one,
1946 TYPE_MODE (type)));
1948 case VECTOR_TYPE:
1950 tree scalar = build_minus_one_cst (TREE_TYPE (type));
1952 return build_vector_from_val (type, scalar);
1955 case COMPLEX_TYPE:
1956 return build_complex (type,
1957 build_minus_one_cst (TREE_TYPE (type)),
1958 build_zero_cst (TREE_TYPE (type)));
1960 default:
1961 gcc_unreachable ();
1965 /* Build 0 constant of type TYPE. This is used by constructor folding
1966 and thus the constant should be represented in memory by
1967 zero(es). */
1969 tree
1970 build_zero_cst (tree type)
1972 switch (TREE_CODE (type))
1974 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1975 case POINTER_TYPE: case REFERENCE_TYPE:
1976 case OFFSET_TYPE: case NULLPTR_TYPE:
1977 return build_int_cst (type, 0);
1979 case REAL_TYPE:
1980 return build_real (type, dconst0);
1982 case FIXED_POINT_TYPE:
1983 return build_fixed (type, FCONST0 (TYPE_MODE (type)));
1985 case VECTOR_TYPE:
1987 tree scalar = build_zero_cst (TREE_TYPE (type));
1989 return build_vector_from_val (type, scalar);
1992 case COMPLEX_TYPE:
1994 tree zero = build_zero_cst (TREE_TYPE (type));
1996 return build_complex (type, zero, zero);
1999 default:
2000 if (!AGGREGATE_TYPE_P (type))
2001 return fold_convert (type, integer_zero_node);
2002 return build_constructor (type, NULL);
2007 /* Build a BINFO with LEN language slots. */
2009 tree
2010 make_tree_binfo_stat (unsigned base_binfos MEM_STAT_DECL)
2012 tree t;
2013 size_t length = (offsetof (struct tree_binfo, base_binfos)
2014 + vec<tree, va_gc>::embedded_size (base_binfos));
2016 record_node_allocation_statistics (TREE_BINFO, length);
2018 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
2020 memset (t, 0, offsetof (struct tree_binfo, base_binfos));
2022 TREE_SET_CODE (t, TREE_BINFO);
2024 BINFO_BASE_BINFOS (t)->embedded_init (base_binfos);
2026 return t;
2029 /* Create a CASE_LABEL_EXPR tree node and return it. */
2031 tree
2032 build_case_label (tree low_value, tree high_value, tree label_decl)
2034 tree t = make_node (CASE_LABEL_EXPR);
2036 TREE_TYPE (t) = void_type_node;
2037 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (label_decl));
2039 CASE_LOW (t) = low_value;
2040 CASE_HIGH (t) = high_value;
2041 CASE_LABEL (t) = label_decl;
2042 CASE_CHAIN (t) = NULL_TREE;
2044 return t;
2047 /* Build a newly constructed INTEGER_CST node. LEN and EXT_LEN are the
2048 values of TREE_INT_CST_NUNITS and TREE_INT_CST_EXT_NUNITS respectively.
2049 The latter determines the length of the HOST_WIDE_INT vector. */
2051 tree
2052 make_int_cst_stat (int len, int ext_len MEM_STAT_DECL)
2054 tree t;
2055 int length = ((ext_len - 1) * sizeof (HOST_WIDE_INT)
2056 + sizeof (struct tree_int_cst));
2058 gcc_assert (len);
2059 record_node_allocation_statistics (INTEGER_CST, length);
2061 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2063 TREE_SET_CODE (t, INTEGER_CST);
2064 TREE_INT_CST_NUNITS (t) = len;
2065 TREE_INT_CST_EXT_NUNITS (t) = ext_len;
2066 /* to_offset can only be applied to trees that are offset_int-sized
2067 or smaller. EXT_LEN is correct if it fits, otherwise the constant
2068 must be exactly the precision of offset_int and so LEN is correct. */
2069 if (ext_len <= OFFSET_INT_ELTS)
2070 TREE_INT_CST_OFFSET_NUNITS (t) = ext_len;
2071 else
2072 TREE_INT_CST_OFFSET_NUNITS (t) = len;
2074 TREE_CONSTANT (t) = 1;
2076 return t;
2079 /* Build a newly constructed TREE_VEC node of length LEN. */
2081 tree
2082 make_tree_vec_stat (int len MEM_STAT_DECL)
2084 tree t;
2085 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
2087 record_node_allocation_statistics (TREE_VEC, length);
2089 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2091 TREE_SET_CODE (t, TREE_VEC);
2092 TREE_VEC_LENGTH (t) = len;
2094 return t;
2097 /* Grow a TREE_VEC node to new length LEN. */
2099 tree
2100 grow_tree_vec_stat (tree v, int len MEM_STAT_DECL)
2102 gcc_assert (TREE_CODE (v) == TREE_VEC);
2104 int oldlen = TREE_VEC_LENGTH (v);
2105 gcc_assert (len > oldlen);
2107 int oldlength = (oldlen - 1) * sizeof (tree) + sizeof (struct tree_vec);
2108 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
2110 record_node_allocation_statistics (TREE_VEC, length - oldlength);
2112 v = (tree) ggc_realloc (v, length PASS_MEM_STAT);
2114 TREE_VEC_LENGTH (v) = len;
2116 return v;
2119 /* Return 1 if EXPR is the integer constant zero or a complex constant
2120 of zero. */
2123 integer_zerop (const_tree expr)
2125 STRIP_NOPS (expr);
2127 switch (TREE_CODE (expr))
2129 case INTEGER_CST:
2130 return wi::eq_p (expr, 0);
2131 case COMPLEX_CST:
2132 return (integer_zerop (TREE_REALPART (expr))
2133 && integer_zerop (TREE_IMAGPART (expr)));
2134 case VECTOR_CST:
2136 unsigned i;
2137 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2138 if (!integer_zerop (VECTOR_CST_ELT (expr, i)))
2139 return false;
2140 return true;
2142 default:
2143 return false;
2147 /* Return 1 if EXPR is the integer constant one or the corresponding
2148 complex constant. */
2151 integer_onep (const_tree expr)
2153 STRIP_NOPS (expr);
2155 switch (TREE_CODE (expr))
2157 case INTEGER_CST:
2158 return wi::eq_p (wi::to_widest (expr), 1);
2159 case COMPLEX_CST:
2160 return (integer_onep (TREE_REALPART (expr))
2161 && integer_zerop (TREE_IMAGPART (expr)));
2162 case VECTOR_CST:
2164 unsigned i;
2165 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2166 if (!integer_onep (VECTOR_CST_ELT (expr, i)))
2167 return false;
2168 return true;
2170 default:
2171 return false;
2175 /* Return 1 if EXPR is the integer constant one. For complex and vector,
2176 return 1 if every piece is the integer constant one. */
2179 integer_each_onep (const_tree expr)
2181 STRIP_NOPS (expr);
2183 if (TREE_CODE (expr) == COMPLEX_CST)
2184 return (integer_onep (TREE_REALPART (expr))
2185 && integer_onep (TREE_IMAGPART (expr)));
2186 else
2187 return integer_onep (expr);
2190 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
2191 it contains, or a complex or vector whose subparts are such integers. */
2194 integer_all_onesp (const_tree expr)
2196 STRIP_NOPS (expr);
2198 if (TREE_CODE (expr) == COMPLEX_CST
2199 && integer_all_onesp (TREE_REALPART (expr))
2200 && integer_all_onesp (TREE_IMAGPART (expr)))
2201 return 1;
2203 else if (TREE_CODE (expr) == VECTOR_CST)
2205 unsigned i;
2206 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2207 if (!integer_all_onesp (VECTOR_CST_ELT (expr, i)))
2208 return 0;
2209 return 1;
2212 else if (TREE_CODE (expr) != INTEGER_CST)
2213 return 0;
2215 return wi::max_value (TYPE_PRECISION (TREE_TYPE (expr)), UNSIGNED) == expr;
2218 /* Return 1 if EXPR is the integer constant minus one. */
2221 integer_minus_onep (const_tree expr)
2223 STRIP_NOPS (expr);
2225 if (TREE_CODE (expr) == COMPLEX_CST)
2226 return (integer_all_onesp (TREE_REALPART (expr))
2227 && integer_zerop (TREE_IMAGPART (expr)));
2228 else
2229 return integer_all_onesp (expr);
2232 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
2233 one bit on). */
2236 integer_pow2p (const_tree expr)
2238 STRIP_NOPS (expr);
2240 if (TREE_CODE (expr) == COMPLEX_CST
2241 && integer_pow2p (TREE_REALPART (expr))
2242 && integer_zerop (TREE_IMAGPART (expr)))
2243 return 1;
2245 if (TREE_CODE (expr) != INTEGER_CST)
2246 return 0;
2248 return wi::popcount (expr) == 1;
2251 /* Return 1 if EXPR is an integer constant other than zero or a
2252 complex constant other than zero. */
2255 integer_nonzerop (const_tree expr)
2257 STRIP_NOPS (expr);
2259 return ((TREE_CODE (expr) == INTEGER_CST
2260 && !wi::eq_p (expr, 0))
2261 || (TREE_CODE (expr) == COMPLEX_CST
2262 && (integer_nonzerop (TREE_REALPART (expr))
2263 || integer_nonzerop (TREE_IMAGPART (expr)))));
2266 /* Return 1 if EXPR is the fixed-point constant zero. */
2269 fixed_zerop (const_tree expr)
2271 return (TREE_CODE (expr) == FIXED_CST
2272 && TREE_FIXED_CST (expr).data.is_zero ());
2275 /* Return the power of two represented by a tree node known to be a
2276 power of two. */
2279 tree_log2 (const_tree expr)
2281 STRIP_NOPS (expr);
2283 if (TREE_CODE (expr) == COMPLEX_CST)
2284 return tree_log2 (TREE_REALPART (expr));
2286 return wi::exact_log2 (expr);
2289 /* Similar, but return the largest integer Y such that 2 ** Y is less
2290 than or equal to EXPR. */
2293 tree_floor_log2 (const_tree expr)
2295 STRIP_NOPS (expr);
2297 if (TREE_CODE (expr) == COMPLEX_CST)
2298 return tree_log2 (TREE_REALPART (expr));
2300 return wi::floor_log2 (expr);
2303 /* Return number of known trailing zero bits in EXPR, or, if the value of
2304 EXPR is known to be zero, the precision of it's type. */
2306 unsigned int
2307 tree_ctz (const_tree expr)
2309 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
2310 && !POINTER_TYPE_P (TREE_TYPE (expr)))
2311 return 0;
2313 unsigned int ret1, ret2, prec = TYPE_PRECISION (TREE_TYPE (expr));
2314 switch (TREE_CODE (expr))
2316 case INTEGER_CST:
2317 ret1 = wi::ctz (expr);
2318 return MIN (ret1, prec);
2319 case SSA_NAME:
2320 ret1 = wi::ctz (get_nonzero_bits (expr));
2321 return MIN (ret1, prec);
2322 case PLUS_EXPR:
2323 case MINUS_EXPR:
2324 case BIT_IOR_EXPR:
2325 case BIT_XOR_EXPR:
2326 case MIN_EXPR:
2327 case MAX_EXPR:
2328 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2329 if (ret1 == 0)
2330 return ret1;
2331 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2332 return MIN (ret1, ret2);
2333 case POINTER_PLUS_EXPR:
2334 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2335 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2336 /* Second operand is sizetype, which could be in theory
2337 wider than pointer's precision. Make sure we never
2338 return more than prec. */
2339 ret2 = MIN (ret2, prec);
2340 return MIN (ret1, ret2);
2341 case BIT_AND_EXPR:
2342 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2343 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2344 return MAX (ret1, ret2);
2345 case MULT_EXPR:
2346 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2347 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2348 return MIN (ret1 + ret2, prec);
2349 case LSHIFT_EXPR:
2350 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2351 if (tree_fits_uhwi_p (TREE_OPERAND (expr, 1))
2352 && (tree_to_uhwi (TREE_OPERAND (expr, 1)) < prec))
2354 ret2 = tree_to_uhwi (TREE_OPERAND (expr, 1));
2355 return MIN (ret1 + ret2, prec);
2357 return ret1;
2358 case RSHIFT_EXPR:
2359 if (tree_fits_uhwi_p (TREE_OPERAND (expr, 1))
2360 && (tree_to_uhwi (TREE_OPERAND (expr, 1)) < prec))
2362 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2363 ret2 = tree_to_uhwi (TREE_OPERAND (expr, 1));
2364 if (ret1 > ret2)
2365 return ret1 - ret2;
2367 return 0;
2368 case TRUNC_DIV_EXPR:
2369 case CEIL_DIV_EXPR:
2370 case FLOOR_DIV_EXPR:
2371 case ROUND_DIV_EXPR:
2372 case EXACT_DIV_EXPR:
2373 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
2374 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) == 1)
2376 int l = tree_log2 (TREE_OPERAND (expr, 1));
2377 if (l >= 0)
2379 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2380 ret2 = l;
2381 if (ret1 > ret2)
2382 return ret1 - ret2;
2385 return 0;
2386 CASE_CONVERT:
2387 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2388 if (ret1 && ret1 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2389 ret1 = prec;
2390 return MIN (ret1, prec);
2391 case SAVE_EXPR:
2392 return tree_ctz (TREE_OPERAND (expr, 0));
2393 case COND_EXPR:
2394 ret1 = tree_ctz (TREE_OPERAND (expr, 1));
2395 if (ret1 == 0)
2396 return 0;
2397 ret2 = tree_ctz (TREE_OPERAND (expr, 2));
2398 return MIN (ret1, ret2);
2399 case COMPOUND_EXPR:
2400 return tree_ctz (TREE_OPERAND (expr, 1));
2401 case ADDR_EXPR:
2402 ret1 = get_pointer_alignment (CONST_CAST_TREE (expr));
2403 if (ret1 > BITS_PER_UNIT)
2405 ret1 = ctz_hwi (ret1 / BITS_PER_UNIT);
2406 return MIN (ret1, prec);
2408 return 0;
2409 default:
2410 return 0;
2414 /* Return 1 if EXPR is the real constant zero. Trailing zeroes matter for
2415 decimal float constants, so don't return 1 for them. */
2418 real_zerop (const_tree expr)
2420 STRIP_NOPS (expr);
2422 switch (TREE_CODE (expr))
2424 case REAL_CST:
2425 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0)
2426 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2427 case COMPLEX_CST:
2428 return real_zerop (TREE_REALPART (expr))
2429 && real_zerop (TREE_IMAGPART (expr));
2430 case VECTOR_CST:
2432 unsigned i;
2433 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2434 if (!real_zerop (VECTOR_CST_ELT (expr, i)))
2435 return false;
2436 return true;
2438 default:
2439 return false;
2443 /* Return 1 if EXPR is the real constant one in real or complex form.
2444 Trailing zeroes matter for decimal float constants, so don't return
2445 1 for them. */
2448 real_onep (const_tree expr)
2450 STRIP_NOPS (expr);
2452 switch (TREE_CODE (expr))
2454 case REAL_CST:
2455 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1)
2456 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2457 case COMPLEX_CST:
2458 return real_onep (TREE_REALPART (expr))
2459 && real_zerop (TREE_IMAGPART (expr));
2460 case VECTOR_CST:
2462 unsigned i;
2463 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2464 if (!real_onep (VECTOR_CST_ELT (expr, i)))
2465 return false;
2466 return true;
2468 default:
2469 return false;
2473 /* Return 1 if EXPR is the real constant minus one. Trailing zeroes
2474 matter for decimal float constants, so don't return 1 for them. */
2477 real_minus_onep (const_tree expr)
2479 STRIP_NOPS (expr);
2481 switch (TREE_CODE (expr))
2483 case REAL_CST:
2484 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconstm1)
2485 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2486 case COMPLEX_CST:
2487 return real_minus_onep (TREE_REALPART (expr))
2488 && real_zerop (TREE_IMAGPART (expr));
2489 case VECTOR_CST:
2491 unsigned i;
2492 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2493 if (!real_minus_onep (VECTOR_CST_ELT (expr, i)))
2494 return false;
2495 return true;
2497 default:
2498 return false;
2502 /* Nonzero if EXP is a constant or a cast of a constant. */
2505 really_constant_p (const_tree exp)
2507 /* This is not quite the same as STRIP_NOPS. It does more. */
2508 while (CONVERT_EXPR_P (exp)
2509 || TREE_CODE (exp) == NON_LVALUE_EXPR)
2510 exp = TREE_OPERAND (exp, 0);
2511 return TREE_CONSTANT (exp);
2514 /* Return first list element whose TREE_VALUE is ELEM.
2515 Return 0 if ELEM is not in LIST. */
2517 tree
2518 value_member (tree elem, tree list)
2520 while (list)
2522 if (elem == TREE_VALUE (list))
2523 return list;
2524 list = TREE_CHAIN (list);
2526 return NULL_TREE;
2529 /* Return first list element whose TREE_PURPOSE is ELEM.
2530 Return 0 if ELEM is not in LIST. */
2532 tree
2533 purpose_member (const_tree elem, tree list)
2535 while (list)
2537 if (elem == TREE_PURPOSE (list))
2538 return list;
2539 list = TREE_CHAIN (list);
2541 return NULL_TREE;
2544 /* Return true if ELEM is in V. */
2546 bool
2547 vec_member (const_tree elem, vec<tree, va_gc> *v)
2549 unsigned ix;
2550 tree t;
2551 FOR_EACH_VEC_SAFE_ELT (v, ix, t)
2552 if (elem == t)
2553 return true;
2554 return false;
2557 /* Returns element number IDX (zero-origin) of chain CHAIN, or
2558 NULL_TREE. */
2560 tree
2561 chain_index (int idx, tree chain)
2563 for (; chain && idx > 0; --idx)
2564 chain = TREE_CHAIN (chain);
2565 return chain;
2568 /* Return nonzero if ELEM is part of the chain CHAIN. */
2571 chain_member (const_tree elem, const_tree chain)
2573 while (chain)
2575 if (elem == chain)
2576 return 1;
2577 chain = DECL_CHAIN (chain);
2580 return 0;
2583 /* Return the length of a chain of nodes chained through TREE_CHAIN.
2584 We expect a null pointer to mark the end of the chain.
2585 This is the Lisp primitive `length'. */
2588 list_length (const_tree t)
2590 const_tree p = t;
2591 #ifdef ENABLE_TREE_CHECKING
2592 const_tree q = t;
2593 #endif
2594 int len = 0;
2596 while (p)
2598 p = TREE_CHAIN (p);
2599 #ifdef ENABLE_TREE_CHECKING
2600 if (len % 2)
2601 q = TREE_CHAIN (q);
2602 gcc_assert (p != q);
2603 #endif
2604 len++;
2607 return len;
2610 /* Returns the first FIELD_DECL in the TYPE_FIELDS of the RECORD_TYPE or
2611 UNION_TYPE TYPE, or NULL_TREE if none. */
2613 tree
2614 first_field (const_tree type)
2616 tree t = TYPE_FIELDS (type);
2617 while (t && TREE_CODE (t) != FIELD_DECL)
2618 t = TREE_CHAIN (t);
2619 return t;
2622 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
2623 by modifying the last node in chain 1 to point to chain 2.
2624 This is the Lisp primitive `nconc'. */
2626 tree
2627 chainon (tree op1, tree op2)
2629 tree t1;
2631 if (!op1)
2632 return op2;
2633 if (!op2)
2634 return op1;
2636 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
2637 continue;
2638 TREE_CHAIN (t1) = op2;
2640 #ifdef ENABLE_TREE_CHECKING
2642 tree t2;
2643 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
2644 gcc_assert (t2 != t1);
2646 #endif
2648 return op1;
2651 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
2653 tree
2654 tree_last (tree chain)
2656 tree next;
2657 if (chain)
2658 while ((next = TREE_CHAIN (chain)))
2659 chain = next;
2660 return chain;
2663 /* Reverse the order of elements in the chain T,
2664 and return the new head of the chain (old last element). */
2666 tree
2667 nreverse (tree t)
2669 tree prev = 0, decl, next;
2670 for (decl = t; decl; decl = next)
2672 /* We shouldn't be using this function to reverse BLOCK chains; we
2673 have blocks_nreverse for that. */
2674 gcc_checking_assert (TREE_CODE (decl) != BLOCK);
2675 next = TREE_CHAIN (decl);
2676 TREE_CHAIN (decl) = prev;
2677 prev = decl;
2679 return prev;
2682 /* Return a newly created TREE_LIST node whose
2683 purpose and value fields are PARM and VALUE. */
2685 tree
2686 build_tree_list_stat (tree parm, tree value MEM_STAT_DECL)
2688 tree t = make_node_stat (TREE_LIST PASS_MEM_STAT);
2689 TREE_PURPOSE (t) = parm;
2690 TREE_VALUE (t) = value;
2691 return t;
2694 /* Build a chain of TREE_LIST nodes from a vector. */
2696 tree
2697 build_tree_list_vec_stat (const vec<tree, va_gc> *vec MEM_STAT_DECL)
2699 tree ret = NULL_TREE;
2700 tree *pp = &ret;
2701 unsigned int i;
2702 tree t;
2703 FOR_EACH_VEC_SAFE_ELT (vec, i, t)
2705 *pp = build_tree_list_stat (NULL, t PASS_MEM_STAT);
2706 pp = &TREE_CHAIN (*pp);
2708 return ret;
2711 /* Return a newly created TREE_LIST node whose
2712 purpose and value fields are PURPOSE and VALUE
2713 and whose TREE_CHAIN is CHAIN. */
2715 tree
2716 tree_cons_stat (tree purpose, tree value, tree chain MEM_STAT_DECL)
2718 tree node;
2720 node = ggc_alloc_tree_node_stat (sizeof (struct tree_list) PASS_MEM_STAT);
2721 memset (node, 0, sizeof (struct tree_common));
2723 record_node_allocation_statistics (TREE_LIST, sizeof (struct tree_list));
2725 TREE_SET_CODE (node, TREE_LIST);
2726 TREE_CHAIN (node) = chain;
2727 TREE_PURPOSE (node) = purpose;
2728 TREE_VALUE (node) = value;
2729 return node;
2732 /* Return the values of the elements of a CONSTRUCTOR as a vector of
2733 trees. */
2735 vec<tree, va_gc> *
2736 ctor_to_vec (tree ctor)
2738 vec<tree, va_gc> *vec;
2739 vec_alloc (vec, CONSTRUCTOR_NELTS (ctor));
2740 unsigned int ix;
2741 tree val;
2743 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), ix, val)
2744 vec->quick_push (val);
2746 return vec;
2749 /* Return the size nominally occupied by an object of type TYPE
2750 when it resides in memory. The value is measured in units of bytes,
2751 and its data type is that normally used for type sizes
2752 (which is the first type created by make_signed_type or
2753 make_unsigned_type). */
2755 tree
2756 size_in_bytes (const_tree type)
2758 tree t;
2760 if (type == error_mark_node)
2761 return integer_zero_node;
2763 type = TYPE_MAIN_VARIANT (type);
2764 t = TYPE_SIZE_UNIT (type);
2766 if (t == 0)
2768 lang_hooks.types.incomplete_type_error (NULL_TREE, type);
2769 return size_zero_node;
2772 return t;
2775 /* Return the size of TYPE (in bytes) as a wide integer
2776 or return -1 if the size can vary or is larger than an integer. */
2778 HOST_WIDE_INT
2779 int_size_in_bytes (const_tree type)
2781 tree t;
2783 if (type == error_mark_node)
2784 return 0;
2786 type = TYPE_MAIN_VARIANT (type);
2787 t = TYPE_SIZE_UNIT (type);
2789 if (t && tree_fits_uhwi_p (t))
2790 return TREE_INT_CST_LOW (t);
2791 else
2792 return -1;
2795 /* Return the maximum size of TYPE (in bytes) as a wide integer
2796 or return -1 if the size can vary or is larger than an integer. */
2798 HOST_WIDE_INT
2799 max_int_size_in_bytes (const_tree type)
2801 HOST_WIDE_INT size = -1;
2802 tree size_tree;
2804 /* If this is an array type, check for a possible MAX_SIZE attached. */
2806 if (TREE_CODE (type) == ARRAY_TYPE)
2808 size_tree = TYPE_ARRAY_MAX_SIZE (type);
2810 if (size_tree && tree_fits_uhwi_p (size_tree))
2811 size = tree_to_uhwi (size_tree);
2814 /* If we still haven't been able to get a size, see if the language
2815 can compute a maximum size. */
2817 if (size == -1)
2819 size_tree = lang_hooks.types.max_size (type);
2821 if (size_tree && tree_fits_uhwi_p (size_tree))
2822 size = tree_to_uhwi (size_tree);
2825 return size;
2828 /* Return the bit position of FIELD, in bits from the start of the record.
2829 This is a tree of type bitsizetype. */
2831 tree
2832 bit_position (const_tree field)
2834 return bit_from_pos (DECL_FIELD_OFFSET (field),
2835 DECL_FIELD_BIT_OFFSET (field));
2838 /* Return the byte position of FIELD, in bytes from the start of the record.
2839 This is a tree of type sizetype. */
2841 tree
2842 byte_position (const_tree field)
2844 return byte_from_pos (DECL_FIELD_OFFSET (field),
2845 DECL_FIELD_BIT_OFFSET (field));
2848 /* Likewise, but return as an integer. It must be representable in
2849 that way (since it could be a signed value, we don't have the
2850 option of returning -1 like int_size_in_byte can. */
2852 HOST_WIDE_INT
2853 int_byte_position (const_tree field)
2855 return tree_to_shwi (byte_position (field));
2858 /* Return the strictest alignment, in bits, that T is known to have. */
2860 unsigned int
2861 expr_align (const_tree t)
2863 unsigned int align0, align1;
2865 switch (TREE_CODE (t))
2867 CASE_CONVERT: case NON_LVALUE_EXPR:
2868 /* If we have conversions, we know that the alignment of the
2869 object must meet each of the alignments of the types. */
2870 align0 = expr_align (TREE_OPERAND (t, 0));
2871 align1 = TYPE_ALIGN (TREE_TYPE (t));
2872 return MAX (align0, align1);
2874 case SAVE_EXPR: case COMPOUND_EXPR: case MODIFY_EXPR:
2875 case INIT_EXPR: case TARGET_EXPR: case WITH_CLEANUP_EXPR:
2876 case CLEANUP_POINT_EXPR:
2877 /* These don't change the alignment of an object. */
2878 return expr_align (TREE_OPERAND (t, 0));
2880 case COND_EXPR:
2881 /* The best we can do is say that the alignment is the least aligned
2882 of the two arms. */
2883 align0 = expr_align (TREE_OPERAND (t, 1));
2884 align1 = expr_align (TREE_OPERAND (t, 2));
2885 return MIN (align0, align1);
2887 /* FIXME: LABEL_DECL and CONST_DECL never have DECL_ALIGN set
2888 meaningfully, it's always 1. */
2889 case LABEL_DECL: case CONST_DECL:
2890 case VAR_DECL: case PARM_DECL: case RESULT_DECL:
2891 case FUNCTION_DECL:
2892 gcc_assert (DECL_ALIGN (t) != 0);
2893 return DECL_ALIGN (t);
2895 default:
2896 break;
2899 /* Otherwise take the alignment from that of the type. */
2900 return TYPE_ALIGN (TREE_TYPE (t));
2903 /* Return, as a tree node, the number of elements for TYPE (which is an
2904 ARRAY_TYPE) minus one. This counts only elements of the top array. */
2906 tree
2907 array_type_nelts (const_tree type)
2909 tree index_type, min, max;
2911 /* If they did it with unspecified bounds, then we should have already
2912 given an error about it before we got here. */
2913 if (! TYPE_DOMAIN (type))
2914 return error_mark_node;
2916 index_type = TYPE_DOMAIN (type);
2917 min = TYPE_MIN_VALUE (index_type);
2918 max = TYPE_MAX_VALUE (index_type);
2920 /* TYPE_MAX_VALUE may not be set if the array has unknown length. */
2921 if (!max)
2922 return error_mark_node;
2924 return (integer_zerop (min)
2925 ? max
2926 : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
2929 /* If arg is static -- a reference to an object in static storage -- then
2930 return the object. This is not the same as the C meaning of `static'.
2931 If arg isn't static, return NULL. */
2933 tree
2934 staticp (tree arg)
2936 switch (TREE_CODE (arg))
2938 case FUNCTION_DECL:
2939 /* Nested functions are static, even though taking their address will
2940 involve a trampoline as we unnest the nested function and create
2941 the trampoline on the tree level. */
2942 return arg;
2944 case VAR_DECL:
2945 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2946 && ! DECL_THREAD_LOCAL_P (arg)
2947 && ! DECL_DLLIMPORT_P (arg)
2948 ? arg : NULL);
2950 case CONST_DECL:
2951 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2952 ? arg : NULL);
2954 case CONSTRUCTOR:
2955 return TREE_STATIC (arg) ? arg : NULL;
2957 case LABEL_DECL:
2958 case STRING_CST:
2959 return arg;
2961 case COMPONENT_REF:
2962 /* If the thing being referenced is not a field, then it is
2963 something language specific. */
2964 gcc_assert (TREE_CODE (TREE_OPERAND (arg, 1)) == FIELD_DECL);
2966 /* If we are referencing a bitfield, we can't evaluate an
2967 ADDR_EXPR at compile time and so it isn't a constant. */
2968 if (DECL_BIT_FIELD (TREE_OPERAND (arg, 1)))
2969 return NULL;
2971 return staticp (TREE_OPERAND (arg, 0));
2973 case BIT_FIELD_REF:
2974 return NULL;
2976 case INDIRECT_REF:
2977 return TREE_CONSTANT (TREE_OPERAND (arg, 0)) ? arg : NULL;
2979 case ARRAY_REF:
2980 case ARRAY_RANGE_REF:
2981 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
2982 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
2983 return staticp (TREE_OPERAND (arg, 0));
2984 else
2985 return NULL;
2987 case COMPOUND_LITERAL_EXPR:
2988 return TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (arg)) ? arg : NULL;
2990 default:
2991 return NULL;
2998 /* Return whether OP is a DECL whose address is function-invariant. */
3000 bool
3001 decl_address_invariant_p (const_tree op)
3003 /* The conditions below are slightly less strict than the one in
3004 staticp. */
3006 switch (TREE_CODE (op))
3008 case PARM_DECL:
3009 case RESULT_DECL:
3010 case LABEL_DECL:
3011 case FUNCTION_DECL:
3012 return true;
3014 case VAR_DECL:
3015 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
3016 || DECL_THREAD_LOCAL_P (op)
3017 || DECL_CONTEXT (op) == current_function_decl
3018 || decl_function_context (op) == current_function_decl)
3019 return true;
3020 break;
3022 case CONST_DECL:
3023 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
3024 || decl_function_context (op) == current_function_decl)
3025 return true;
3026 break;
3028 default:
3029 break;
3032 return false;
3035 /* Return whether OP is a DECL whose address is interprocedural-invariant. */
3037 bool
3038 decl_address_ip_invariant_p (const_tree op)
3040 /* The conditions below are slightly less strict than the one in
3041 staticp. */
3043 switch (TREE_CODE (op))
3045 case LABEL_DECL:
3046 case FUNCTION_DECL:
3047 case STRING_CST:
3048 return true;
3050 case VAR_DECL:
3051 if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
3052 && !DECL_DLLIMPORT_P (op))
3053 || DECL_THREAD_LOCAL_P (op))
3054 return true;
3055 break;
3057 case CONST_DECL:
3058 if ((TREE_STATIC (op) || DECL_EXTERNAL (op)))
3059 return true;
3060 break;
3062 default:
3063 break;
3066 return false;
3070 /* Return true if T is function-invariant (internal function, does
3071 not handle arithmetic; that's handled in skip_simple_arithmetic and
3072 tree_invariant_p). */
3074 static bool tree_invariant_p (tree t);
3076 static bool
3077 tree_invariant_p_1 (tree t)
3079 tree op;
3081 if (TREE_CONSTANT (t)
3082 || (TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t)))
3083 return true;
3085 switch (TREE_CODE (t))
3087 case SAVE_EXPR:
3088 return true;
3090 case ADDR_EXPR:
3091 op = TREE_OPERAND (t, 0);
3092 while (handled_component_p (op))
3094 switch (TREE_CODE (op))
3096 case ARRAY_REF:
3097 case ARRAY_RANGE_REF:
3098 if (!tree_invariant_p (TREE_OPERAND (op, 1))
3099 || TREE_OPERAND (op, 2) != NULL_TREE
3100 || TREE_OPERAND (op, 3) != NULL_TREE)
3101 return false;
3102 break;
3104 case COMPONENT_REF:
3105 if (TREE_OPERAND (op, 2) != NULL_TREE)
3106 return false;
3107 break;
3109 default:;
3111 op = TREE_OPERAND (op, 0);
3114 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
3116 default:
3117 break;
3120 return false;
3123 /* Return true if T is function-invariant. */
3125 static bool
3126 tree_invariant_p (tree t)
3128 tree inner = skip_simple_arithmetic (t);
3129 return tree_invariant_p_1 (inner);
3132 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
3133 Do this to any expression which may be used in more than one place,
3134 but must be evaluated only once.
3136 Normally, expand_expr would reevaluate the expression each time.
3137 Calling save_expr produces something that is evaluated and recorded
3138 the first time expand_expr is called on it. Subsequent calls to
3139 expand_expr just reuse the recorded value.
3141 The call to expand_expr that generates code that actually computes
3142 the value is the first call *at compile time*. Subsequent calls
3143 *at compile time* generate code to use the saved value.
3144 This produces correct result provided that *at run time* control
3145 always flows through the insns made by the first expand_expr
3146 before reaching the other places where the save_expr was evaluated.
3147 You, the caller of save_expr, must make sure this is so.
3149 Constants, and certain read-only nodes, are returned with no
3150 SAVE_EXPR because that is safe. Expressions containing placeholders
3151 are not touched; see tree.def for an explanation of what these
3152 are used for. */
3154 tree
3155 save_expr (tree expr)
3157 tree t = fold (expr);
3158 tree inner;
3160 /* If the tree evaluates to a constant, then we don't want to hide that
3161 fact (i.e. this allows further folding, and direct checks for constants).
3162 However, a read-only object that has side effects cannot be bypassed.
3163 Since it is no problem to reevaluate literals, we just return the
3164 literal node. */
3165 inner = skip_simple_arithmetic (t);
3166 if (TREE_CODE (inner) == ERROR_MARK)
3167 return inner;
3169 if (tree_invariant_p_1 (inner))
3170 return t;
3172 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
3173 it means that the size or offset of some field of an object depends on
3174 the value within another field.
3176 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
3177 and some variable since it would then need to be both evaluated once and
3178 evaluated more than once. Front-ends must assure this case cannot
3179 happen by surrounding any such subexpressions in their own SAVE_EXPR
3180 and forcing evaluation at the proper time. */
3181 if (contains_placeholder_p (inner))
3182 return t;
3184 t = build1 (SAVE_EXPR, TREE_TYPE (expr), t);
3185 SET_EXPR_LOCATION (t, EXPR_LOCATION (expr));
3187 /* This expression might be placed ahead of a jump to ensure that the
3188 value was computed on both sides of the jump. So make sure it isn't
3189 eliminated as dead. */
3190 TREE_SIDE_EFFECTS (t) = 1;
3191 return t;
3194 /* Look inside EXPR into any simple arithmetic operations. Return the
3195 outermost non-arithmetic or non-invariant node. */
3197 tree
3198 skip_simple_arithmetic (tree expr)
3200 /* We don't care about whether this can be used as an lvalue in this
3201 context. */
3202 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3203 expr = TREE_OPERAND (expr, 0);
3205 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
3206 a constant, it will be more efficient to not make another SAVE_EXPR since
3207 it will allow better simplification and GCSE will be able to merge the
3208 computations if they actually occur. */
3209 while (true)
3211 if (UNARY_CLASS_P (expr))
3212 expr = TREE_OPERAND (expr, 0);
3213 else if (BINARY_CLASS_P (expr))
3215 if (tree_invariant_p (TREE_OPERAND (expr, 1)))
3216 expr = TREE_OPERAND (expr, 0);
3217 else if (tree_invariant_p (TREE_OPERAND (expr, 0)))
3218 expr = TREE_OPERAND (expr, 1);
3219 else
3220 break;
3222 else
3223 break;
3226 return expr;
3229 /* Look inside EXPR into simple arithmetic operations involving constants.
3230 Return the outermost non-arithmetic or non-constant node. */
3232 tree
3233 skip_simple_constant_arithmetic (tree expr)
3235 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3236 expr = TREE_OPERAND (expr, 0);
3238 while (true)
3240 if (UNARY_CLASS_P (expr))
3241 expr = TREE_OPERAND (expr, 0);
3242 else if (BINARY_CLASS_P (expr))
3244 if (TREE_CONSTANT (TREE_OPERAND (expr, 1)))
3245 expr = TREE_OPERAND (expr, 0);
3246 else if (TREE_CONSTANT (TREE_OPERAND (expr, 0)))
3247 expr = TREE_OPERAND (expr, 1);
3248 else
3249 break;
3251 else
3252 break;
3255 return expr;
3258 /* Return which tree structure is used by T. */
3260 enum tree_node_structure_enum
3261 tree_node_structure (const_tree t)
3263 const enum tree_code code = TREE_CODE (t);
3264 return tree_node_structure_for_code (code);
3267 /* Set various status flags when building a CALL_EXPR object T. */
3269 static void
3270 process_call_operands (tree t)
3272 bool side_effects = TREE_SIDE_EFFECTS (t);
3273 bool read_only = false;
3274 int i = call_expr_flags (t);
3276 /* Calls have side-effects, except those to const or pure functions. */
3277 if ((i & ECF_LOOPING_CONST_OR_PURE) || !(i & (ECF_CONST | ECF_PURE)))
3278 side_effects = true;
3279 /* Propagate TREE_READONLY of arguments for const functions. */
3280 if (i & ECF_CONST)
3281 read_only = true;
3283 if (!side_effects || read_only)
3284 for (i = 1; i < TREE_OPERAND_LENGTH (t); i++)
3286 tree op = TREE_OPERAND (t, i);
3287 if (op && TREE_SIDE_EFFECTS (op))
3288 side_effects = true;
3289 if (op && !TREE_READONLY (op) && !CONSTANT_CLASS_P (op))
3290 read_only = false;
3293 TREE_SIDE_EFFECTS (t) = side_effects;
3294 TREE_READONLY (t) = read_only;
3297 /* Return true if EXP contains a PLACEHOLDER_EXPR, i.e. if it represents a
3298 size or offset that depends on a field within a record. */
3300 bool
3301 contains_placeholder_p (const_tree exp)
3303 enum tree_code code;
3305 if (!exp)
3306 return 0;
3308 code = TREE_CODE (exp);
3309 if (code == PLACEHOLDER_EXPR)
3310 return 1;
3312 switch (TREE_CODE_CLASS (code))
3314 case tcc_reference:
3315 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
3316 position computations since they will be converted into a
3317 WITH_RECORD_EXPR involving the reference, which will assume
3318 here will be valid. */
3319 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3321 case tcc_exceptional:
3322 if (code == TREE_LIST)
3323 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
3324 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
3325 break;
3327 case tcc_unary:
3328 case tcc_binary:
3329 case tcc_comparison:
3330 case tcc_expression:
3331 switch (code)
3333 case COMPOUND_EXPR:
3334 /* Ignoring the first operand isn't quite right, but works best. */
3335 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
3337 case COND_EXPR:
3338 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3339 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
3340 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
3342 case SAVE_EXPR:
3343 /* The save_expr function never wraps anything containing
3344 a PLACEHOLDER_EXPR. */
3345 return 0;
3347 default:
3348 break;
3351 switch (TREE_CODE_LENGTH (code))
3353 case 1:
3354 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3355 case 2:
3356 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3357 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
3358 default:
3359 return 0;
3362 case tcc_vl_exp:
3363 switch (code)
3365 case CALL_EXPR:
3367 const_tree arg;
3368 const_call_expr_arg_iterator iter;
3369 FOR_EACH_CONST_CALL_EXPR_ARG (arg, iter, exp)
3370 if (CONTAINS_PLACEHOLDER_P (arg))
3371 return 1;
3372 return 0;
3374 default:
3375 return 0;
3378 default:
3379 return 0;
3381 return 0;
3384 /* Return true if any part of the structure of TYPE involves a PLACEHOLDER_EXPR
3385 directly. This includes size, bounds, qualifiers (for QUAL_UNION_TYPE) and
3386 field positions. */
3388 static bool
3389 type_contains_placeholder_1 (const_tree type)
3391 /* If the size contains a placeholder or the parent type (component type in
3392 the case of arrays) type involves a placeholder, this type does. */
3393 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
3394 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
3395 || (!POINTER_TYPE_P (type)
3396 && TREE_TYPE (type)
3397 && type_contains_placeholder_p (TREE_TYPE (type))))
3398 return true;
3400 /* Now do type-specific checks. Note that the last part of the check above
3401 greatly limits what we have to do below. */
3402 switch (TREE_CODE (type))
3404 case VOID_TYPE:
3405 case COMPLEX_TYPE:
3406 case ENUMERAL_TYPE:
3407 case BOOLEAN_TYPE:
3408 case POINTER_TYPE:
3409 case OFFSET_TYPE:
3410 case REFERENCE_TYPE:
3411 case METHOD_TYPE:
3412 case FUNCTION_TYPE:
3413 case VECTOR_TYPE:
3414 case NULLPTR_TYPE:
3415 return false;
3417 case INTEGER_TYPE:
3418 case REAL_TYPE:
3419 case FIXED_POINT_TYPE:
3420 /* Here we just check the bounds. */
3421 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
3422 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
3424 case ARRAY_TYPE:
3425 /* We have already checked the component type above, so just check the
3426 domain type. */
3427 return type_contains_placeholder_p (TYPE_DOMAIN (type));
3429 case RECORD_TYPE:
3430 case UNION_TYPE:
3431 case QUAL_UNION_TYPE:
3433 tree field;
3435 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3436 if (TREE_CODE (field) == FIELD_DECL
3437 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
3438 || (TREE_CODE (type) == QUAL_UNION_TYPE
3439 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
3440 || type_contains_placeholder_p (TREE_TYPE (field))))
3441 return true;
3443 return false;
3446 default:
3447 gcc_unreachable ();
3451 /* Wrapper around above function used to cache its result. */
3453 bool
3454 type_contains_placeholder_p (tree type)
3456 bool result;
3458 /* If the contains_placeholder_bits field has been initialized,
3459 then we know the answer. */
3460 if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
3461 return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
3463 /* Indicate that we've seen this type node, and the answer is false.
3464 This is what we want to return if we run into recursion via fields. */
3465 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
3467 /* Compute the real value. */
3468 result = type_contains_placeholder_1 (type);
3470 /* Store the real value. */
3471 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
3473 return result;
3476 /* Push tree EXP onto vector QUEUE if it is not already present. */
3478 static void
3479 push_without_duplicates (tree exp, vec<tree> *queue)
3481 unsigned int i;
3482 tree iter;
3484 FOR_EACH_VEC_ELT (*queue, i, iter)
3485 if (simple_cst_equal (iter, exp) == 1)
3486 break;
3488 if (!iter)
3489 queue->safe_push (exp);
3492 /* Given a tree EXP, find all occurrences of references to fields
3493 in a PLACEHOLDER_EXPR and place them in vector REFS without
3494 duplicates. Also record VAR_DECLs and CONST_DECLs. Note that
3495 we assume here that EXP contains only arithmetic expressions
3496 or CALL_EXPRs with PLACEHOLDER_EXPRs occurring only in their
3497 argument list. */
3499 void
3500 find_placeholder_in_expr (tree exp, vec<tree> *refs)
3502 enum tree_code code = TREE_CODE (exp);
3503 tree inner;
3504 int i;
3506 /* We handle TREE_LIST and COMPONENT_REF separately. */
3507 if (code == TREE_LIST)
3509 FIND_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), refs);
3510 FIND_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), refs);
3512 else if (code == COMPONENT_REF)
3514 for (inner = TREE_OPERAND (exp, 0);
3515 REFERENCE_CLASS_P (inner);
3516 inner = TREE_OPERAND (inner, 0))
3519 if (TREE_CODE (inner) == PLACEHOLDER_EXPR)
3520 push_without_duplicates (exp, refs);
3521 else
3522 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), refs);
3524 else
3525 switch (TREE_CODE_CLASS (code))
3527 case tcc_constant:
3528 break;
3530 case tcc_declaration:
3531 /* Variables allocated to static storage can stay. */
3532 if (!TREE_STATIC (exp))
3533 push_without_duplicates (exp, refs);
3534 break;
3536 case tcc_expression:
3537 /* This is the pattern built in ada/make_aligning_type. */
3538 if (code == ADDR_EXPR
3539 && TREE_CODE (TREE_OPERAND (exp, 0)) == PLACEHOLDER_EXPR)
3541 push_without_duplicates (exp, refs);
3542 break;
3545 /* Fall through... */
3547 case tcc_exceptional:
3548 case tcc_unary:
3549 case tcc_binary:
3550 case tcc_comparison:
3551 case tcc_reference:
3552 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
3553 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3554 break;
3556 case tcc_vl_exp:
3557 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3558 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3559 break;
3561 default:
3562 gcc_unreachable ();
3566 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
3567 return a tree with all occurrences of references to F in a
3568 PLACEHOLDER_EXPR replaced by R. Also handle VAR_DECLs and
3569 CONST_DECLs. Note that we assume here that EXP contains only
3570 arithmetic expressions or CALL_EXPRs with PLACEHOLDER_EXPRs
3571 occurring only in their argument list. */
3573 tree
3574 substitute_in_expr (tree exp, tree f, tree r)
3576 enum tree_code code = TREE_CODE (exp);
3577 tree op0, op1, op2, op3;
3578 tree new_tree;
3580 /* We handle TREE_LIST and COMPONENT_REF separately. */
3581 if (code == TREE_LIST)
3583 op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
3584 op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
3585 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3586 return exp;
3588 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3590 else if (code == COMPONENT_REF)
3592 tree inner;
3594 /* If this expression is getting a value from a PLACEHOLDER_EXPR
3595 and it is the right field, replace it with R. */
3596 for (inner = TREE_OPERAND (exp, 0);
3597 REFERENCE_CLASS_P (inner);
3598 inner = TREE_OPERAND (inner, 0))
3601 /* The field. */
3602 op1 = TREE_OPERAND (exp, 1);
3604 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f)
3605 return r;
3607 /* If this expression hasn't been completed let, leave it alone. */
3608 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && !TREE_TYPE (inner))
3609 return exp;
3611 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3612 if (op0 == TREE_OPERAND (exp, 0))
3613 return exp;
3615 new_tree
3616 = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE);
3618 else
3619 switch (TREE_CODE_CLASS (code))
3621 case tcc_constant:
3622 return exp;
3624 case tcc_declaration:
3625 if (exp == f)
3626 return r;
3627 else
3628 return exp;
3630 case tcc_expression:
3631 if (exp == f)
3632 return r;
3634 /* Fall through... */
3636 case tcc_exceptional:
3637 case tcc_unary:
3638 case tcc_binary:
3639 case tcc_comparison:
3640 case tcc_reference:
3641 switch (TREE_CODE_LENGTH (code))
3643 case 0:
3644 return exp;
3646 case 1:
3647 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3648 if (op0 == TREE_OPERAND (exp, 0))
3649 return exp;
3651 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3652 break;
3654 case 2:
3655 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3656 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3658 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3659 return exp;
3661 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3662 break;
3664 case 3:
3665 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3666 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3667 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3669 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3670 && op2 == TREE_OPERAND (exp, 2))
3671 return exp;
3673 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3674 break;
3676 case 4:
3677 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3678 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3679 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3680 op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
3682 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3683 && op2 == TREE_OPERAND (exp, 2)
3684 && op3 == TREE_OPERAND (exp, 3))
3685 return exp;
3687 new_tree
3688 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3689 break;
3691 default:
3692 gcc_unreachable ();
3694 break;
3696 case tcc_vl_exp:
3698 int i;
3700 new_tree = NULL_TREE;
3702 /* If we are trying to replace F with a constant, inline back
3703 functions which do nothing else than computing a value from
3704 the arguments they are passed. This makes it possible to
3705 fold partially or entirely the replacement expression. */
3706 if (CONSTANT_CLASS_P (r) && code == CALL_EXPR)
3708 tree t = maybe_inline_call_in_expr (exp);
3709 if (t)
3710 return SUBSTITUTE_IN_EXPR (t, f, r);
3713 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3715 tree op = TREE_OPERAND (exp, i);
3716 tree new_op = SUBSTITUTE_IN_EXPR (op, f, r);
3717 if (new_op != op)
3719 if (!new_tree)
3720 new_tree = copy_node (exp);
3721 TREE_OPERAND (new_tree, i) = new_op;
3725 if (new_tree)
3727 new_tree = fold (new_tree);
3728 if (TREE_CODE (new_tree) == CALL_EXPR)
3729 process_call_operands (new_tree);
3731 else
3732 return exp;
3734 break;
3736 default:
3737 gcc_unreachable ();
3740 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3742 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3743 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3745 return new_tree;
3748 /* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
3749 for it within OBJ, a tree that is an object or a chain of references. */
3751 tree
3752 substitute_placeholder_in_expr (tree exp, tree obj)
3754 enum tree_code code = TREE_CODE (exp);
3755 tree op0, op1, op2, op3;
3756 tree new_tree;
3758 /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
3759 in the chain of OBJ. */
3760 if (code == PLACEHOLDER_EXPR)
3762 tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
3763 tree elt;
3765 for (elt = obj; elt != 0;
3766 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3767 || TREE_CODE (elt) == COND_EXPR)
3768 ? TREE_OPERAND (elt, 1)
3769 : (REFERENCE_CLASS_P (elt)
3770 || UNARY_CLASS_P (elt)
3771 || BINARY_CLASS_P (elt)
3772 || VL_EXP_CLASS_P (elt)
3773 || EXPRESSION_CLASS_P (elt))
3774 ? TREE_OPERAND (elt, 0) : 0))
3775 if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
3776 return elt;
3778 for (elt = obj; elt != 0;
3779 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3780 || TREE_CODE (elt) == COND_EXPR)
3781 ? TREE_OPERAND (elt, 1)
3782 : (REFERENCE_CLASS_P (elt)
3783 || UNARY_CLASS_P (elt)
3784 || BINARY_CLASS_P (elt)
3785 || VL_EXP_CLASS_P (elt)
3786 || EXPRESSION_CLASS_P (elt))
3787 ? TREE_OPERAND (elt, 0) : 0))
3788 if (POINTER_TYPE_P (TREE_TYPE (elt))
3789 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
3790 == need_type))
3791 return fold_build1 (INDIRECT_REF, need_type, elt);
3793 /* If we didn't find it, return the original PLACEHOLDER_EXPR. If it
3794 survives until RTL generation, there will be an error. */
3795 return exp;
3798 /* TREE_LIST is special because we need to look at TREE_VALUE
3799 and TREE_CHAIN, not TREE_OPERANDS. */
3800 else if (code == TREE_LIST)
3802 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
3803 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
3804 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3805 return exp;
3807 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3809 else
3810 switch (TREE_CODE_CLASS (code))
3812 case tcc_constant:
3813 case tcc_declaration:
3814 return exp;
3816 case tcc_exceptional:
3817 case tcc_unary:
3818 case tcc_binary:
3819 case tcc_comparison:
3820 case tcc_expression:
3821 case tcc_reference:
3822 case tcc_statement:
3823 switch (TREE_CODE_LENGTH (code))
3825 case 0:
3826 return exp;
3828 case 1:
3829 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3830 if (op0 == TREE_OPERAND (exp, 0))
3831 return exp;
3833 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3834 break;
3836 case 2:
3837 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3838 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3840 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3841 return exp;
3843 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3844 break;
3846 case 3:
3847 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3848 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3849 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3851 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3852 && op2 == TREE_OPERAND (exp, 2))
3853 return exp;
3855 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3856 break;
3858 case 4:
3859 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3860 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3861 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3862 op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
3864 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3865 && op2 == TREE_OPERAND (exp, 2)
3866 && op3 == TREE_OPERAND (exp, 3))
3867 return exp;
3869 new_tree
3870 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3871 break;
3873 default:
3874 gcc_unreachable ();
3876 break;
3878 case tcc_vl_exp:
3880 int i;
3882 new_tree = NULL_TREE;
3884 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3886 tree op = TREE_OPERAND (exp, i);
3887 tree new_op = SUBSTITUTE_PLACEHOLDER_IN_EXPR (op, obj);
3888 if (new_op != op)
3890 if (!new_tree)
3891 new_tree = copy_node (exp);
3892 TREE_OPERAND (new_tree, i) = new_op;
3896 if (new_tree)
3898 new_tree = fold (new_tree);
3899 if (TREE_CODE (new_tree) == CALL_EXPR)
3900 process_call_operands (new_tree);
3902 else
3903 return exp;
3905 break;
3907 default:
3908 gcc_unreachable ();
3911 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3913 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3914 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3916 return new_tree;
3920 /* Subroutine of stabilize_reference; this is called for subtrees of
3921 references. Any expression with side-effects must be put in a SAVE_EXPR
3922 to ensure that it is only evaluated once.
3924 We don't put SAVE_EXPR nodes around everything, because assigning very
3925 simple expressions to temporaries causes us to miss good opportunities
3926 for optimizations. Among other things, the opportunity to fold in the
3927 addition of a constant into an addressing mode often gets lost, e.g.
3928 "y[i+1] += x;". In general, we take the approach that we should not make
3929 an assignment unless we are forced into it - i.e., that any non-side effect
3930 operator should be allowed, and that cse should take care of coalescing
3931 multiple utterances of the same expression should that prove fruitful. */
3933 static tree
3934 stabilize_reference_1 (tree e)
3936 tree result;
3937 enum tree_code code = TREE_CODE (e);
3939 /* We cannot ignore const expressions because it might be a reference
3940 to a const array but whose index contains side-effects. But we can
3941 ignore things that are actual constant or that already have been
3942 handled by this function. */
3944 if (tree_invariant_p (e))
3945 return e;
3947 switch (TREE_CODE_CLASS (code))
3949 case tcc_exceptional:
3950 case tcc_type:
3951 case tcc_declaration:
3952 case tcc_comparison:
3953 case tcc_statement:
3954 case tcc_expression:
3955 case tcc_reference:
3956 case tcc_vl_exp:
3957 /* If the expression has side-effects, then encase it in a SAVE_EXPR
3958 so that it will only be evaluated once. */
3959 /* The reference (r) and comparison (<) classes could be handled as
3960 below, but it is generally faster to only evaluate them once. */
3961 if (TREE_SIDE_EFFECTS (e))
3962 return save_expr (e);
3963 return e;
3965 case tcc_constant:
3966 /* Constants need no processing. In fact, we should never reach
3967 here. */
3968 return e;
3970 case tcc_binary:
3971 /* Division is slow and tends to be compiled with jumps,
3972 especially the division by powers of 2 that is often
3973 found inside of an array reference. So do it just once. */
3974 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
3975 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
3976 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
3977 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
3978 return save_expr (e);
3979 /* Recursively stabilize each operand. */
3980 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
3981 stabilize_reference_1 (TREE_OPERAND (e, 1)));
3982 break;
3984 case tcc_unary:
3985 /* Recursively stabilize each operand. */
3986 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
3987 break;
3989 default:
3990 gcc_unreachable ();
3993 TREE_TYPE (result) = TREE_TYPE (e);
3994 TREE_READONLY (result) = TREE_READONLY (e);
3995 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
3996 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
3998 return result;
4001 /* Stabilize a reference so that we can use it any number of times
4002 without causing its operands to be evaluated more than once.
4003 Returns the stabilized reference. This works by means of save_expr,
4004 so see the caveats in the comments about save_expr.
4006 Also allows conversion expressions whose operands are references.
4007 Any other kind of expression is returned unchanged. */
4009 tree
4010 stabilize_reference (tree ref)
4012 tree result;
4013 enum tree_code code = TREE_CODE (ref);
4015 switch (code)
4017 case VAR_DECL:
4018 case PARM_DECL:
4019 case RESULT_DECL:
4020 /* No action is needed in this case. */
4021 return ref;
4023 CASE_CONVERT:
4024 case FLOAT_EXPR:
4025 case FIX_TRUNC_EXPR:
4026 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
4027 break;
4029 case INDIRECT_REF:
4030 result = build_nt (INDIRECT_REF,
4031 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
4032 break;
4034 case COMPONENT_REF:
4035 result = build_nt (COMPONENT_REF,
4036 stabilize_reference (TREE_OPERAND (ref, 0)),
4037 TREE_OPERAND (ref, 1), NULL_TREE);
4038 break;
4040 case BIT_FIELD_REF:
4041 result = build_nt (BIT_FIELD_REF,
4042 stabilize_reference (TREE_OPERAND (ref, 0)),
4043 TREE_OPERAND (ref, 1), TREE_OPERAND (ref, 2));
4044 break;
4046 case ARRAY_REF:
4047 result = build_nt (ARRAY_REF,
4048 stabilize_reference (TREE_OPERAND (ref, 0)),
4049 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
4050 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
4051 break;
4053 case ARRAY_RANGE_REF:
4054 result = build_nt (ARRAY_RANGE_REF,
4055 stabilize_reference (TREE_OPERAND (ref, 0)),
4056 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
4057 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
4058 break;
4060 case COMPOUND_EXPR:
4061 /* We cannot wrap the first expression in a SAVE_EXPR, as then
4062 it wouldn't be ignored. This matters when dealing with
4063 volatiles. */
4064 return stabilize_reference_1 (ref);
4066 /* If arg isn't a kind of lvalue we recognize, make no change.
4067 Caller should recognize the error for an invalid lvalue. */
4068 default:
4069 return ref;
4071 case ERROR_MARK:
4072 return error_mark_node;
4075 TREE_TYPE (result) = TREE_TYPE (ref);
4076 TREE_READONLY (result) = TREE_READONLY (ref);
4077 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
4078 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
4080 return result;
4083 /* Low-level constructors for expressions. */
4085 /* A helper function for build1 and constant folders. Set TREE_CONSTANT,
4086 and TREE_SIDE_EFFECTS for an ADDR_EXPR. */
4088 void
4089 recompute_tree_invariant_for_addr_expr (tree t)
4091 tree node;
4092 bool tc = true, se = false;
4094 /* We started out assuming this address is both invariant and constant, but
4095 does not have side effects. Now go down any handled components and see if
4096 any of them involve offsets that are either non-constant or non-invariant.
4097 Also check for side-effects.
4099 ??? Note that this code makes no attempt to deal with the case where
4100 taking the address of something causes a copy due to misalignment. */
4102 #define UPDATE_FLAGS(NODE) \
4103 do { tree _node = (NODE); \
4104 if (_node && !TREE_CONSTANT (_node)) tc = false; \
4105 if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
4107 for (node = TREE_OPERAND (t, 0); handled_component_p (node);
4108 node = TREE_OPERAND (node, 0))
4110 /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
4111 array reference (probably made temporarily by the G++ front end),
4112 so ignore all the operands. */
4113 if ((TREE_CODE (node) == ARRAY_REF
4114 || TREE_CODE (node) == ARRAY_RANGE_REF)
4115 && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
4117 UPDATE_FLAGS (TREE_OPERAND (node, 1));
4118 if (TREE_OPERAND (node, 2))
4119 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4120 if (TREE_OPERAND (node, 3))
4121 UPDATE_FLAGS (TREE_OPERAND (node, 3));
4123 /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
4124 FIELD_DECL, apparently. The G++ front end can put something else
4125 there, at least temporarily. */
4126 else if (TREE_CODE (node) == COMPONENT_REF
4127 && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
4129 if (TREE_OPERAND (node, 2))
4130 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4134 node = lang_hooks.expr_to_decl (node, &tc, &se);
4136 /* Now see what's inside. If it's an INDIRECT_REF, copy our properties from
4137 the address, since &(*a)->b is a form of addition. If it's a constant, the
4138 address is constant too. If it's a decl, its address is constant if the
4139 decl is static. Everything else is not constant and, furthermore,
4140 taking the address of a volatile variable is not volatile. */
4141 if (TREE_CODE (node) == INDIRECT_REF
4142 || TREE_CODE (node) == MEM_REF)
4143 UPDATE_FLAGS (TREE_OPERAND (node, 0));
4144 else if (CONSTANT_CLASS_P (node))
4146 else if (DECL_P (node))
4147 tc &= (staticp (node) != NULL_TREE);
4148 else
4150 tc = false;
4151 se |= TREE_SIDE_EFFECTS (node);
4155 TREE_CONSTANT (t) = tc;
4156 TREE_SIDE_EFFECTS (t) = se;
4157 #undef UPDATE_FLAGS
4160 /* Build an expression of code CODE, data type TYPE, and operands as
4161 specified. Expressions and reference nodes can be created this way.
4162 Constants, decls, types and misc nodes cannot be.
4164 We define 5 non-variadic functions, from 0 to 4 arguments. This is
4165 enough for all extant tree codes. */
4167 tree
4168 build0_stat (enum tree_code code, tree tt MEM_STAT_DECL)
4170 tree t;
4172 gcc_assert (TREE_CODE_LENGTH (code) == 0);
4174 t = make_node_stat (code PASS_MEM_STAT);
4175 TREE_TYPE (t) = tt;
4177 return t;
4180 tree
4181 build1_stat (enum tree_code code, tree type, tree node MEM_STAT_DECL)
4183 int length = sizeof (struct tree_exp);
4184 tree t;
4186 record_node_allocation_statistics (code, length);
4188 gcc_assert (TREE_CODE_LENGTH (code) == 1);
4190 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
4192 memset (t, 0, sizeof (struct tree_common));
4194 TREE_SET_CODE (t, code);
4196 TREE_TYPE (t) = type;
4197 SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
4198 TREE_OPERAND (t, 0) = node;
4199 if (node && !TYPE_P (node))
4201 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
4202 TREE_READONLY (t) = TREE_READONLY (node);
4205 if (TREE_CODE_CLASS (code) == tcc_statement)
4206 TREE_SIDE_EFFECTS (t) = 1;
4207 else switch (code)
4209 case VA_ARG_EXPR:
4210 /* All of these have side-effects, no matter what their
4211 operands are. */
4212 TREE_SIDE_EFFECTS (t) = 1;
4213 TREE_READONLY (t) = 0;
4214 break;
4216 case INDIRECT_REF:
4217 /* Whether a dereference is readonly has nothing to do with whether
4218 its operand is readonly. */
4219 TREE_READONLY (t) = 0;
4220 break;
4222 case ADDR_EXPR:
4223 if (node)
4224 recompute_tree_invariant_for_addr_expr (t);
4225 break;
4227 default:
4228 if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
4229 && node && !TYPE_P (node)
4230 && TREE_CONSTANT (node))
4231 TREE_CONSTANT (t) = 1;
4232 if (TREE_CODE_CLASS (code) == tcc_reference
4233 && node && TREE_THIS_VOLATILE (node))
4234 TREE_THIS_VOLATILE (t) = 1;
4235 break;
4238 return t;
4241 #define PROCESS_ARG(N) \
4242 do { \
4243 TREE_OPERAND (t, N) = arg##N; \
4244 if (arg##N &&!TYPE_P (arg##N)) \
4246 if (TREE_SIDE_EFFECTS (arg##N)) \
4247 side_effects = 1; \
4248 if (!TREE_READONLY (arg##N) \
4249 && !CONSTANT_CLASS_P (arg##N)) \
4250 (void) (read_only = 0); \
4251 if (!TREE_CONSTANT (arg##N)) \
4252 (void) (constant = 0); \
4254 } while (0)
4256 tree
4257 build2_stat (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
4259 bool constant, read_only, side_effects;
4260 tree t;
4262 gcc_assert (TREE_CODE_LENGTH (code) == 2);
4264 if ((code == MINUS_EXPR || code == PLUS_EXPR || code == MULT_EXPR)
4265 && arg0 && arg1 && tt && POINTER_TYPE_P (tt)
4266 /* When sizetype precision doesn't match that of pointers
4267 we need to be able to build explicit extensions or truncations
4268 of the offset argument. */
4269 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (tt))
4270 gcc_assert (TREE_CODE (arg0) == INTEGER_CST
4271 && TREE_CODE (arg1) == INTEGER_CST);
4273 if (code == POINTER_PLUS_EXPR && arg0 && arg1 && tt)
4274 gcc_assert (POINTER_TYPE_P (tt) && POINTER_TYPE_P (TREE_TYPE (arg0))
4275 && ptrofftype_p (TREE_TYPE (arg1)));
4277 t = make_node_stat (code PASS_MEM_STAT);
4278 TREE_TYPE (t) = tt;
4280 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
4281 result based on those same flags for the arguments. But if the
4282 arguments aren't really even `tree' expressions, we shouldn't be trying
4283 to do this. */
4285 /* Expressions without side effects may be constant if their
4286 arguments are as well. */
4287 constant = (TREE_CODE_CLASS (code) == tcc_comparison
4288 || TREE_CODE_CLASS (code) == tcc_binary);
4289 read_only = 1;
4290 side_effects = TREE_SIDE_EFFECTS (t);
4292 PROCESS_ARG (0);
4293 PROCESS_ARG (1);
4295 TREE_READONLY (t) = read_only;
4296 TREE_CONSTANT (t) = constant;
4297 TREE_SIDE_EFFECTS (t) = side_effects;
4298 TREE_THIS_VOLATILE (t)
4299 = (TREE_CODE_CLASS (code) == tcc_reference
4300 && arg0 && TREE_THIS_VOLATILE (arg0));
4302 return t;
4306 tree
4307 build3_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4308 tree arg2 MEM_STAT_DECL)
4310 bool constant, read_only, side_effects;
4311 tree t;
4313 gcc_assert (TREE_CODE_LENGTH (code) == 3);
4314 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4316 t = make_node_stat (code PASS_MEM_STAT);
4317 TREE_TYPE (t) = tt;
4319 read_only = 1;
4321 /* As a special exception, if COND_EXPR has NULL branches, we
4322 assume that it is a gimple statement and always consider
4323 it to have side effects. */
4324 if (code == COND_EXPR
4325 && tt == void_type_node
4326 && arg1 == NULL_TREE
4327 && arg2 == NULL_TREE)
4328 side_effects = true;
4329 else
4330 side_effects = TREE_SIDE_EFFECTS (t);
4332 PROCESS_ARG (0);
4333 PROCESS_ARG (1);
4334 PROCESS_ARG (2);
4336 if (code == COND_EXPR)
4337 TREE_READONLY (t) = read_only;
4339 TREE_SIDE_EFFECTS (t) = side_effects;
4340 TREE_THIS_VOLATILE (t)
4341 = (TREE_CODE_CLASS (code) == tcc_reference
4342 && arg0 && TREE_THIS_VOLATILE (arg0));
4344 return t;
4347 tree
4348 build4_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4349 tree arg2, tree arg3 MEM_STAT_DECL)
4351 bool constant, read_only, side_effects;
4352 tree t;
4354 gcc_assert (TREE_CODE_LENGTH (code) == 4);
4356 t = make_node_stat (code PASS_MEM_STAT);
4357 TREE_TYPE (t) = tt;
4359 side_effects = TREE_SIDE_EFFECTS (t);
4361 PROCESS_ARG (0);
4362 PROCESS_ARG (1);
4363 PROCESS_ARG (2);
4364 PROCESS_ARG (3);
4366 TREE_SIDE_EFFECTS (t) = side_effects;
4367 TREE_THIS_VOLATILE (t)
4368 = (TREE_CODE_CLASS (code) == tcc_reference
4369 && arg0 && TREE_THIS_VOLATILE (arg0));
4371 return t;
4374 tree
4375 build5_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4376 tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
4378 bool constant, read_only, side_effects;
4379 tree t;
4381 gcc_assert (TREE_CODE_LENGTH (code) == 5);
4383 t = make_node_stat (code PASS_MEM_STAT);
4384 TREE_TYPE (t) = tt;
4386 side_effects = TREE_SIDE_EFFECTS (t);
4388 PROCESS_ARG (0);
4389 PROCESS_ARG (1);
4390 PROCESS_ARG (2);
4391 PROCESS_ARG (3);
4392 PROCESS_ARG (4);
4394 TREE_SIDE_EFFECTS (t) = side_effects;
4395 TREE_THIS_VOLATILE (t)
4396 = (TREE_CODE_CLASS (code) == tcc_reference
4397 && arg0 && TREE_THIS_VOLATILE (arg0));
4399 return t;
4402 /* Build a simple MEM_REF tree with the sematics of a plain INDIRECT_REF
4403 on the pointer PTR. */
4405 tree
4406 build_simple_mem_ref_loc (location_t loc, tree ptr)
4408 HOST_WIDE_INT offset = 0;
4409 tree ptype = TREE_TYPE (ptr);
4410 tree tem;
4411 /* For convenience allow addresses that collapse to a simple base
4412 and offset. */
4413 if (TREE_CODE (ptr) == ADDR_EXPR
4414 && (handled_component_p (TREE_OPERAND (ptr, 0))
4415 || TREE_CODE (TREE_OPERAND (ptr, 0)) == MEM_REF))
4417 ptr = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &offset);
4418 gcc_assert (ptr);
4419 ptr = build_fold_addr_expr (ptr);
4420 gcc_assert (is_gimple_reg (ptr) || is_gimple_min_invariant (ptr));
4422 tem = build2 (MEM_REF, TREE_TYPE (ptype),
4423 ptr, build_int_cst (ptype, offset));
4424 SET_EXPR_LOCATION (tem, loc);
4425 return tem;
4428 /* Return the constant offset of a MEM_REF or TARGET_MEM_REF tree T. */
4430 offset_int
4431 mem_ref_offset (const_tree t)
4433 return offset_int::from (TREE_OPERAND (t, 1), SIGNED);
4436 /* Return an invariant ADDR_EXPR of type TYPE taking the address of BASE
4437 offsetted by OFFSET units. */
4439 tree
4440 build_invariant_address (tree type, tree base, HOST_WIDE_INT offset)
4442 tree ref = fold_build2 (MEM_REF, TREE_TYPE (type),
4443 build_fold_addr_expr (base),
4444 build_int_cst (ptr_type_node, offset));
4445 tree addr = build1 (ADDR_EXPR, type, ref);
4446 recompute_tree_invariant_for_addr_expr (addr);
4447 return addr;
4450 /* Similar except don't specify the TREE_TYPE
4451 and leave the TREE_SIDE_EFFECTS as 0.
4452 It is permissible for arguments to be null,
4453 or even garbage if their values do not matter. */
4455 tree
4456 build_nt (enum tree_code code, ...)
4458 tree t;
4459 int length;
4460 int i;
4461 va_list p;
4463 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4465 va_start (p, code);
4467 t = make_node (code);
4468 length = TREE_CODE_LENGTH (code);
4470 for (i = 0; i < length; i++)
4471 TREE_OPERAND (t, i) = va_arg (p, tree);
4473 va_end (p);
4474 return t;
4477 /* Similar to build_nt, but for creating a CALL_EXPR object with a
4478 tree vec. */
4480 tree
4481 build_nt_call_vec (tree fn, vec<tree, va_gc> *args)
4483 tree ret, t;
4484 unsigned int ix;
4486 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
4487 CALL_EXPR_FN (ret) = fn;
4488 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
4489 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
4490 CALL_EXPR_ARG (ret, ix) = t;
4491 return ret;
4494 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
4495 We do NOT enter this node in any sort of symbol table.
4497 LOC is the location of the decl.
4499 layout_decl is used to set up the decl's storage layout.
4500 Other slots are initialized to 0 or null pointers. */
4502 tree
4503 build_decl_stat (location_t loc, enum tree_code code, tree name,
4504 tree type MEM_STAT_DECL)
4506 tree t;
4508 t = make_node_stat (code PASS_MEM_STAT);
4509 DECL_SOURCE_LOCATION (t) = loc;
4511 /* if (type == error_mark_node)
4512 type = integer_type_node; */
4513 /* That is not done, deliberately, so that having error_mark_node
4514 as the type can suppress useless errors in the use of this variable. */
4516 DECL_NAME (t) = name;
4517 TREE_TYPE (t) = type;
4519 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
4520 layout_decl (t, 0);
4522 return t;
4525 /* Builds and returns function declaration with NAME and TYPE. */
4527 tree
4528 build_fn_decl (const char *name, tree type)
4530 tree id = get_identifier (name);
4531 tree decl = build_decl (input_location, FUNCTION_DECL, id, type);
4533 DECL_EXTERNAL (decl) = 1;
4534 TREE_PUBLIC (decl) = 1;
4535 DECL_ARTIFICIAL (decl) = 1;
4536 TREE_NOTHROW (decl) = 1;
4538 return decl;
4541 vec<tree, va_gc> *all_translation_units;
4543 /* Builds a new translation-unit decl with name NAME, queues it in the
4544 global list of translation-unit decls and returns it. */
4546 tree
4547 build_translation_unit_decl (tree name)
4549 tree tu = build_decl (UNKNOWN_LOCATION, TRANSLATION_UNIT_DECL,
4550 name, NULL_TREE);
4551 TRANSLATION_UNIT_LANGUAGE (tu) = lang_hooks.name;
4552 vec_safe_push (all_translation_units, tu);
4553 return tu;
4557 /* BLOCK nodes are used to represent the structure of binding contours
4558 and declarations, once those contours have been exited and their contents
4559 compiled. This information is used for outputting debugging info. */
4561 tree
4562 build_block (tree vars, tree subblocks, tree supercontext, tree chain)
4564 tree block = make_node (BLOCK);
4566 BLOCK_VARS (block) = vars;
4567 BLOCK_SUBBLOCKS (block) = subblocks;
4568 BLOCK_SUPERCONTEXT (block) = supercontext;
4569 BLOCK_CHAIN (block) = chain;
4570 return block;
4574 /* Like SET_EXPR_LOCATION, but make sure the tree can have a location.
4576 LOC is the location to use in tree T. */
4578 void
4579 protected_set_expr_location (tree t, location_t loc)
4581 if (CAN_HAVE_LOCATION_P (t))
4582 SET_EXPR_LOCATION (t, loc);
4585 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
4586 is ATTRIBUTE. */
4588 tree
4589 build_decl_attribute_variant (tree ddecl, tree attribute)
4591 DECL_ATTRIBUTES (ddecl) = attribute;
4592 return ddecl;
4595 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4596 is ATTRIBUTE and its qualifiers are QUALS.
4598 Record such modified types already made so we don't make duplicates. */
4600 tree
4601 build_type_attribute_qual_variant (tree ttype, tree attribute, int quals)
4603 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
4605 inchash::hash hstate;
4606 tree ntype;
4607 int i;
4608 tree t;
4609 enum tree_code code = TREE_CODE (ttype);
4611 /* Building a distinct copy of a tagged type is inappropriate; it
4612 causes breakage in code that expects there to be a one-to-one
4613 relationship between a struct and its fields.
4614 build_duplicate_type is another solution (as used in
4615 handle_transparent_union_attribute), but that doesn't play well
4616 with the stronger C++ type identity model. */
4617 if (TREE_CODE (ttype) == RECORD_TYPE
4618 || TREE_CODE (ttype) == UNION_TYPE
4619 || TREE_CODE (ttype) == QUAL_UNION_TYPE
4620 || TREE_CODE (ttype) == ENUMERAL_TYPE)
4622 warning (OPT_Wattributes,
4623 "ignoring attributes applied to %qT after definition",
4624 TYPE_MAIN_VARIANT (ttype));
4625 return build_qualified_type (ttype, quals);
4628 ttype = build_qualified_type (ttype, TYPE_UNQUALIFIED);
4629 ntype = build_distinct_type_copy (ttype);
4631 TYPE_ATTRIBUTES (ntype) = attribute;
4633 hstate.add_int (code);
4634 if (TREE_TYPE (ntype))
4635 hstate.add_object (TYPE_HASH (TREE_TYPE (ntype)));
4636 attribute_hash_list (attribute, hstate);
4638 switch (TREE_CODE (ntype))
4640 case FUNCTION_TYPE:
4641 type_hash_list (TYPE_ARG_TYPES (ntype), hstate);
4642 break;
4643 case ARRAY_TYPE:
4644 if (TYPE_DOMAIN (ntype))
4645 hstate.add_object (TYPE_HASH (TYPE_DOMAIN (ntype)));
4646 break;
4647 case INTEGER_TYPE:
4648 t = TYPE_MAX_VALUE (ntype);
4649 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
4650 hstate.add_object (TREE_INT_CST_ELT (t, i));
4651 break;
4652 case REAL_TYPE:
4653 case FIXED_POINT_TYPE:
4655 unsigned int precision = TYPE_PRECISION (ntype);
4656 hstate.add_object (precision);
4658 break;
4659 default:
4660 break;
4663 ntype = type_hash_canon (hstate.end(), ntype);
4665 /* If the target-dependent attributes make NTYPE different from
4666 its canonical type, we will need to use structural equality
4667 checks for this type. */
4668 if (TYPE_STRUCTURAL_EQUALITY_P (ttype)
4669 || !comp_type_attributes (ntype, ttype))
4670 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
4671 else if (TYPE_CANONICAL (ntype) == ntype)
4672 TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype);
4674 ttype = build_qualified_type (ntype, quals);
4676 else if (TYPE_QUALS (ttype) != quals)
4677 ttype = build_qualified_type (ttype, quals);
4679 return ttype;
4682 /* Check if "omp declare simd" attribute arguments, CLAUSES1 and CLAUSES2, are
4683 the same. */
4685 static bool
4686 omp_declare_simd_clauses_equal (tree clauses1, tree clauses2)
4688 tree cl1, cl2;
4689 for (cl1 = clauses1, cl2 = clauses2;
4690 cl1 && cl2;
4691 cl1 = OMP_CLAUSE_CHAIN (cl1), cl2 = OMP_CLAUSE_CHAIN (cl2))
4693 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_CODE (cl2))
4694 return false;
4695 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_SIMDLEN)
4697 if (simple_cst_equal (OMP_CLAUSE_DECL (cl1),
4698 OMP_CLAUSE_DECL (cl2)) != 1)
4699 return false;
4701 switch (OMP_CLAUSE_CODE (cl1))
4703 case OMP_CLAUSE_ALIGNED:
4704 if (simple_cst_equal (OMP_CLAUSE_ALIGNED_ALIGNMENT (cl1),
4705 OMP_CLAUSE_ALIGNED_ALIGNMENT (cl2)) != 1)
4706 return false;
4707 break;
4708 case OMP_CLAUSE_LINEAR:
4709 if (simple_cst_equal (OMP_CLAUSE_LINEAR_STEP (cl1),
4710 OMP_CLAUSE_LINEAR_STEP (cl2)) != 1)
4711 return false;
4712 break;
4713 case OMP_CLAUSE_SIMDLEN:
4714 if (simple_cst_equal (OMP_CLAUSE_SIMDLEN_EXPR (cl1),
4715 OMP_CLAUSE_SIMDLEN_EXPR (cl2)) != 1)
4716 return false;
4717 default:
4718 break;
4721 return true;
4724 /* Compare two constructor-element-type constants. Return 1 if the lists
4725 are known to be equal; otherwise return 0. */
4727 static bool
4728 simple_cst_list_equal (const_tree l1, const_tree l2)
4730 while (l1 != NULL_TREE && l2 != NULL_TREE)
4732 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
4733 return false;
4735 l1 = TREE_CHAIN (l1);
4736 l2 = TREE_CHAIN (l2);
4739 return l1 == l2;
4742 /* Compare two attributes for their value identity. Return true if the
4743 attribute values are known to be equal; otherwise return false.
4746 static bool
4747 attribute_value_equal (const_tree attr1, const_tree attr2)
4749 if (TREE_VALUE (attr1) == TREE_VALUE (attr2))
4750 return true;
4752 if (TREE_VALUE (attr1) != NULL_TREE
4753 && TREE_CODE (TREE_VALUE (attr1)) == TREE_LIST
4754 && TREE_VALUE (attr2) != NULL
4755 && TREE_CODE (TREE_VALUE (attr2)) == TREE_LIST)
4756 return (simple_cst_list_equal (TREE_VALUE (attr1),
4757 TREE_VALUE (attr2)) == 1);
4759 if ((flag_openmp || flag_openmp_simd)
4760 && TREE_VALUE (attr1) && TREE_VALUE (attr2)
4761 && TREE_CODE (TREE_VALUE (attr1)) == OMP_CLAUSE
4762 && TREE_CODE (TREE_VALUE (attr2)) == OMP_CLAUSE)
4763 return omp_declare_simd_clauses_equal (TREE_VALUE (attr1),
4764 TREE_VALUE (attr2));
4766 return (simple_cst_equal (TREE_VALUE (attr1), TREE_VALUE (attr2)) == 1);
4769 /* Return 0 if the attributes for two types are incompatible, 1 if they
4770 are compatible, and 2 if they are nearly compatible (which causes a
4771 warning to be generated). */
4773 comp_type_attributes (const_tree type1, const_tree type2)
4775 const_tree a1 = TYPE_ATTRIBUTES (type1);
4776 const_tree a2 = TYPE_ATTRIBUTES (type2);
4777 const_tree a;
4779 if (a1 == a2)
4780 return 1;
4781 for (a = a1; a != NULL_TREE; a = TREE_CHAIN (a))
4783 const struct attribute_spec *as;
4784 const_tree attr;
4786 as = lookup_attribute_spec (get_attribute_name (a));
4787 if (!as || as->affects_type_identity == false)
4788 continue;
4790 attr = lookup_attribute (as->name, CONST_CAST_TREE (a2));
4791 if (!attr || !attribute_value_equal (a, attr))
4792 break;
4794 if (!a)
4796 for (a = a2; a != NULL_TREE; a = TREE_CHAIN (a))
4798 const struct attribute_spec *as;
4800 as = lookup_attribute_spec (get_attribute_name (a));
4801 if (!as || as->affects_type_identity == false)
4802 continue;
4804 if (!lookup_attribute (as->name, CONST_CAST_TREE (a1)))
4805 break;
4806 /* We don't need to compare trees again, as we did this
4807 already in first loop. */
4809 /* All types - affecting identity - are equal, so
4810 there is no need to call target hook for comparison. */
4811 if (!a)
4812 return 1;
4814 /* As some type combinations - like default calling-convention - might
4815 be compatible, we have to call the target hook to get the final result. */
4816 return targetm.comp_type_attributes (type1, type2);
4819 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4820 is ATTRIBUTE.
4822 Record such modified types already made so we don't make duplicates. */
4824 tree
4825 build_type_attribute_variant (tree ttype, tree attribute)
4827 return build_type_attribute_qual_variant (ttype, attribute,
4828 TYPE_QUALS (ttype));
4832 /* Reset the expression *EXPR_P, a size or position.
4834 ??? We could reset all non-constant sizes or positions. But it's cheap
4835 enough to not do so and refrain from adding workarounds to dwarf2out.c.
4837 We need to reset self-referential sizes or positions because they cannot
4838 be gimplified and thus can contain a CALL_EXPR after the gimplification
4839 is finished, which will run afoul of LTO streaming. And they need to be
4840 reset to something essentially dummy but not constant, so as to preserve
4841 the properties of the object they are attached to. */
4843 static inline void
4844 free_lang_data_in_one_sizepos (tree *expr_p)
4846 tree expr = *expr_p;
4847 if (CONTAINS_PLACEHOLDER_P (expr))
4848 *expr_p = build0 (PLACEHOLDER_EXPR, TREE_TYPE (expr));
4852 /* Reset all the fields in a binfo node BINFO. We only keep
4853 BINFO_VTABLE, which is used by gimple_fold_obj_type_ref. */
4855 static void
4856 free_lang_data_in_binfo (tree binfo)
4858 unsigned i;
4859 tree t;
4861 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
4863 BINFO_VIRTUALS (binfo) = NULL_TREE;
4864 BINFO_BASE_ACCESSES (binfo) = NULL;
4865 BINFO_INHERITANCE_CHAIN (binfo) = NULL_TREE;
4866 BINFO_SUBVTT_INDEX (binfo) = NULL_TREE;
4868 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (binfo), i, t)
4869 free_lang_data_in_binfo (t);
4873 /* Reset all language specific information still present in TYPE. */
4875 static void
4876 free_lang_data_in_type (tree type)
4878 gcc_assert (TYPE_P (type));
4880 /* Give the FE a chance to remove its own data first. */
4881 lang_hooks.free_lang_data (type);
4883 TREE_LANG_FLAG_0 (type) = 0;
4884 TREE_LANG_FLAG_1 (type) = 0;
4885 TREE_LANG_FLAG_2 (type) = 0;
4886 TREE_LANG_FLAG_3 (type) = 0;
4887 TREE_LANG_FLAG_4 (type) = 0;
4888 TREE_LANG_FLAG_5 (type) = 0;
4889 TREE_LANG_FLAG_6 (type) = 0;
4891 if (TREE_CODE (type) == FUNCTION_TYPE)
4893 /* Remove the const and volatile qualifiers from arguments. The
4894 C++ front end removes them, but the C front end does not,
4895 leading to false ODR violation errors when merging two
4896 instances of the same function signature compiled by
4897 different front ends. */
4898 tree p;
4900 for (p = TYPE_ARG_TYPES (type); p; p = TREE_CHAIN (p))
4902 tree arg_type = TREE_VALUE (p);
4904 if (TYPE_READONLY (arg_type) || TYPE_VOLATILE (arg_type))
4906 int quals = TYPE_QUALS (arg_type)
4907 & ~TYPE_QUAL_CONST
4908 & ~TYPE_QUAL_VOLATILE;
4909 TREE_VALUE (p) = build_qualified_type (arg_type, quals);
4910 free_lang_data_in_type (TREE_VALUE (p));
4915 /* Remove members that are not actually FIELD_DECLs from the field
4916 list of an aggregate. These occur in C++. */
4917 if (RECORD_OR_UNION_TYPE_P (type))
4919 tree prev, member;
4921 /* Note that TYPE_FIELDS can be shared across distinct
4922 TREE_TYPEs. Therefore, if the first field of TYPE_FIELDS is
4923 to be removed, we cannot set its TREE_CHAIN to NULL.
4924 Otherwise, we would not be able to find all the other fields
4925 in the other instances of this TREE_TYPE.
4927 This was causing an ICE in testsuite/g++.dg/lto/20080915.C. */
4928 prev = NULL_TREE;
4929 member = TYPE_FIELDS (type);
4930 while (member)
4932 if (TREE_CODE (member) == FIELD_DECL
4933 || TREE_CODE (member) == TYPE_DECL)
4935 if (prev)
4936 TREE_CHAIN (prev) = member;
4937 else
4938 TYPE_FIELDS (type) = member;
4939 prev = member;
4942 member = TREE_CHAIN (member);
4945 if (prev)
4946 TREE_CHAIN (prev) = NULL_TREE;
4947 else
4948 TYPE_FIELDS (type) = NULL_TREE;
4950 TYPE_METHODS (type) = NULL_TREE;
4951 if (TYPE_BINFO (type))
4952 free_lang_data_in_binfo (TYPE_BINFO (type));
4954 else
4956 /* For non-aggregate types, clear out the language slot (which
4957 overloads TYPE_BINFO). */
4958 TYPE_LANG_SLOT_1 (type) = NULL_TREE;
4960 if (INTEGRAL_TYPE_P (type)
4961 || SCALAR_FLOAT_TYPE_P (type)
4962 || FIXED_POINT_TYPE_P (type))
4964 free_lang_data_in_one_sizepos (&TYPE_MIN_VALUE (type));
4965 free_lang_data_in_one_sizepos (&TYPE_MAX_VALUE (type));
4969 free_lang_data_in_one_sizepos (&TYPE_SIZE (type));
4970 free_lang_data_in_one_sizepos (&TYPE_SIZE_UNIT (type));
4972 if (TYPE_CONTEXT (type)
4973 && TREE_CODE (TYPE_CONTEXT (type)) == BLOCK)
4975 tree ctx = TYPE_CONTEXT (type);
4978 ctx = BLOCK_SUPERCONTEXT (ctx);
4980 while (ctx && TREE_CODE (ctx) == BLOCK);
4981 TYPE_CONTEXT (type) = ctx;
4986 /* Return true if DECL may need an assembler name to be set. */
4988 static inline bool
4989 need_assembler_name_p (tree decl)
4991 /* We use DECL_ASSEMBLER_NAME to hold mangled type names for One Definition Rule
4992 merging. */
4993 if (flag_lto_odr_type_mering
4994 && TREE_CODE (decl) == TYPE_DECL
4995 && DECL_NAME (decl)
4996 && decl == TYPE_NAME (TREE_TYPE (decl))
4997 && !is_lang_specific (TREE_TYPE (decl))
4998 && AGGREGATE_TYPE_P (TREE_TYPE (decl))
4999 && !variably_modified_type_p (TREE_TYPE (decl), NULL_TREE)
5000 && !type_in_anonymous_namespace_p (TREE_TYPE (decl)))
5001 return !DECL_ASSEMBLER_NAME_SET_P (decl);
5002 /* Only FUNCTION_DECLs and VAR_DECLs are considered. */
5003 if (TREE_CODE (decl) != FUNCTION_DECL
5004 && TREE_CODE (decl) != VAR_DECL)
5005 return false;
5007 /* If DECL already has its assembler name set, it does not need a
5008 new one. */
5009 if (!HAS_DECL_ASSEMBLER_NAME_P (decl)
5010 || DECL_ASSEMBLER_NAME_SET_P (decl))
5011 return false;
5013 /* Abstract decls do not need an assembler name. */
5014 if (DECL_ABSTRACT_P (decl))
5015 return false;
5017 /* For VAR_DECLs, only static, public and external symbols need an
5018 assembler name. */
5019 if (TREE_CODE (decl) == VAR_DECL
5020 && !TREE_STATIC (decl)
5021 && !TREE_PUBLIC (decl)
5022 && !DECL_EXTERNAL (decl))
5023 return false;
5025 if (TREE_CODE (decl) == FUNCTION_DECL)
5027 /* Do not set assembler name on builtins. Allow RTL expansion to
5028 decide whether to expand inline or via a regular call. */
5029 if (DECL_BUILT_IN (decl)
5030 && DECL_BUILT_IN_CLASS (decl) != BUILT_IN_FRONTEND)
5031 return false;
5033 /* Functions represented in the callgraph need an assembler name. */
5034 if (cgraph_node::get (decl) != NULL)
5035 return true;
5037 /* Unused and not public functions don't need an assembler name. */
5038 if (!TREE_USED (decl) && !TREE_PUBLIC (decl))
5039 return false;
5042 return true;
5046 /* Reset all language specific information still present in symbol
5047 DECL. */
5049 static void
5050 free_lang_data_in_decl (tree decl)
5052 gcc_assert (DECL_P (decl));
5054 /* Give the FE a chance to remove its own data first. */
5055 lang_hooks.free_lang_data (decl);
5057 TREE_LANG_FLAG_0 (decl) = 0;
5058 TREE_LANG_FLAG_1 (decl) = 0;
5059 TREE_LANG_FLAG_2 (decl) = 0;
5060 TREE_LANG_FLAG_3 (decl) = 0;
5061 TREE_LANG_FLAG_4 (decl) = 0;
5062 TREE_LANG_FLAG_5 (decl) = 0;
5063 TREE_LANG_FLAG_6 (decl) = 0;
5065 free_lang_data_in_one_sizepos (&DECL_SIZE (decl));
5066 free_lang_data_in_one_sizepos (&DECL_SIZE_UNIT (decl));
5067 if (TREE_CODE (decl) == FIELD_DECL)
5069 free_lang_data_in_one_sizepos (&DECL_FIELD_OFFSET (decl));
5070 if (TREE_CODE (DECL_CONTEXT (decl)) == QUAL_UNION_TYPE)
5071 DECL_QUALIFIER (decl) = NULL_TREE;
5074 if (TREE_CODE (decl) == FUNCTION_DECL)
5076 struct cgraph_node *node;
5077 if (!(node = cgraph_node::get (decl))
5078 || (!node->definition && !node->clones))
5080 if (node)
5081 node->release_body ();
5082 else
5084 release_function_body (decl);
5085 DECL_ARGUMENTS (decl) = NULL;
5086 DECL_RESULT (decl) = NULL;
5087 DECL_INITIAL (decl) = error_mark_node;
5090 if (gimple_has_body_p (decl))
5092 tree t;
5094 /* If DECL has a gimple body, then the context for its
5095 arguments must be DECL. Otherwise, it doesn't really
5096 matter, as we will not be emitting any code for DECL. In
5097 general, there may be other instances of DECL created by
5098 the front end and since PARM_DECLs are generally shared,
5099 their DECL_CONTEXT changes as the replicas of DECL are
5100 created. The only time where DECL_CONTEXT is important
5101 is for the FUNCTION_DECLs that have a gimple body (since
5102 the PARM_DECL will be used in the function's body). */
5103 for (t = DECL_ARGUMENTS (decl); t; t = TREE_CHAIN (t))
5104 DECL_CONTEXT (t) = decl;
5107 /* DECL_SAVED_TREE holds the GENERIC representation for DECL.
5108 At this point, it is not needed anymore. */
5109 DECL_SAVED_TREE (decl) = NULL_TREE;
5111 /* Clear the abstract origin if it refers to a method. Otherwise
5112 dwarf2out.c will ICE as we clear TYPE_METHODS and thus the
5113 origin will not be output correctly. */
5114 if (DECL_ABSTRACT_ORIGIN (decl)
5115 && DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))
5116 && RECORD_OR_UNION_TYPE_P
5117 (DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))))
5118 DECL_ABSTRACT_ORIGIN (decl) = NULL_TREE;
5120 /* Sometimes the C++ frontend doesn't manage to transform a temporary
5121 DECL_VINDEX referring to itself into a vtable slot number as it
5122 should. Happens with functions that are copied and then forgotten
5123 about. Just clear it, it won't matter anymore. */
5124 if (DECL_VINDEX (decl) && !tree_fits_shwi_p (DECL_VINDEX (decl)))
5125 DECL_VINDEX (decl) = NULL_TREE;
5127 else if (TREE_CODE (decl) == VAR_DECL)
5129 if ((DECL_EXTERNAL (decl)
5130 && (!TREE_STATIC (decl) || !TREE_READONLY (decl)))
5131 || (decl_function_context (decl) && !TREE_STATIC (decl)))
5132 DECL_INITIAL (decl) = NULL_TREE;
5134 else if (TREE_CODE (decl) == TYPE_DECL
5135 || TREE_CODE (decl) == FIELD_DECL)
5136 DECL_INITIAL (decl) = NULL_TREE;
5137 else if (TREE_CODE (decl) == TRANSLATION_UNIT_DECL
5138 && DECL_INITIAL (decl)
5139 && TREE_CODE (DECL_INITIAL (decl)) == BLOCK)
5141 /* Strip builtins from the translation-unit BLOCK. We still have targets
5142 without builtin_decl_explicit support and also builtins are shared
5143 nodes and thus we can't use TREE_CHAIN in multiple lists. */
5144 tree *nextp = &BLOCK_VARS (DECL_INITIAL (decl));
5145 while (*nextp)
5147 tree var = *nextp;
5148 if (TREE_CODE (var) == FUNCTION_DECL
5149 && DECL_BUILT_IN (var))
5150 *nextp = TREE_CHAIN (var);
5151 else
5152 nextp = &TREE_CHAIN (var);
5158 /* Data used when collecting DECLs and TYPEs for language data removal. */
5160 struct free_lang_data_d
5162 /* Worklist to avoid excessive recursion. */
5163 vec<tree> worklist;
5165 /* Set of traversed objects. Used to avoid duplicate visits. */
5166 hash_set<tree> *pset;
5168 /* Array of symbols to process with free_lang_data_in_decl. */
5169 vec<tree> decls;
5171 /* Array of types to process with free_lang_data_in_type. */
5172 vec<tree> types;
5176 /* Save all language fields needed to generate proper debug information
5177 for DECL. This saves most fields cleared out by free_lang_data_in_decl. */
5179 static void
5180 save_debug_info_for_decl (tree t)
5182 /*struct saved_debug_info_d *sdi;*/
5184 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && DECL_P (t));
5186 /* FIXME. Partial implementation for saving debug info removed. */
5190 /* Save all language fields needed to generate proper debug information
5191 for TYPE. This saves most fields cleared out by free_lang_data_in_type. */
5193 static void
5194 save_debug_info_for_type (tree t)
5196 /*struct saved_debug_info_d *sdi;*/
5198 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && TYPE_P (t));
5200 /* FIXME. Partial implementation for saving debug info removed. */
5204 /* Add type or decl T to one of the list of tree nodes that need their
5205 language data removed. The lists are held inside FLD. */
5207 static void
5208 add_tree_to_fld_list (tree t, struct free_lang_data_d *fld)
5210 if (DECL_P (t))
5212 fld->decls.safe_push (t);
5213 if (debug_info_level > DINFO_LEVEL_TERSE)
5214 save_debug_info_for_decl (t);
5216 else if (TYPE_P (t))
5218 fld->types.safe_push (t);
5219 if (debug_info_level > DINFO_LEVEL_TERSE)
5220 save_debug_info_for_type (t);
5222 else
5223 gcc_unreachable ();
5226 /* Push tree node T into FLD->WORKLIST. */
5228 static inline void
5229 fld_worklist_push (tree t, struct free_lang_data_d *fld)
5231 if (t && !is_lang_specific (t) && !fld->pset->contains (t))
5232 fld->worklist.safe_push ((t));
5236 /* Operand callback helper for free_lang_data_in_node. *TP is the
5237 subtree operand being considered. */
5239 static tree
5240 find_decls_types_r (tree *tp, int *ws, void *data)
5242 tree t = *tp;
5243 struct free_lang_data_d *fld = (struct free_lang_data_d *) data;
5245 if (TREE_CODE (t) == TREE_LIST)
5246 return NULL_TREE;
5248 /* Language specific nodes will be removed, so there is no need
5249 to gather anything under them. */
5250 if (is_lang_specific (t))
5252 *ws = 0;
5253 return NULL_TREE;
5256 if (DECL_P (t))
5258 /* Note that walk_tree does not traverse every possible field in
5259 decls, so we have to do our own traversals here. */
5260 add_tree_to_fld_list (t, fld);
5262 fld_worklist_push (DECL_NAME (t), fld);
5263 fld_worklist_push (DECL_CONTEXT (t), fld);
5264 fld_worklist_push (DECL_SIZE (t), fld);
5265 fld_worklist_push (DECL_SIZE_UNIT (t), fld);
5267 /* We are going to remove everything under DECL_INITIAL for
5268 TYPE_DECLs. No point walking them. */
5269 if (TREE_CODE (t) != TYPE_DECL)
5270 fld_worklist_push (DECL_INITIAL (t), fld);
5272 fld_worklist_push (DECL_ATTRIBUTES (t), fld);
5273 fld_worklist_push (DECL_ABSTRACT_ORIGIN (t), fld);
5275 if (TREE_CODE (t) == FUNCTION_DECL)
5277 fld_worklist_push (DECL_ARGUMENTS (t), fld);
5278 fld_worklist_push (DECL_RESULT (t), fld);
5280 else if (TREE_CODE (t) == TYPE_DECL)
5282 fld_worklist_push (DECL_ORIGINAL_TYPE (t), fld);
5284 else if (TREE_CODE (t) == FIELD_DECL)
5286 fld_worklist_push (DECL_FIELD_OFFSET (t), fld);
5287 fld_worklist_push (DECL_BIT_FIELD_TYPE (t), fld);
5288 fld_worklist_push (DECL_FIELD_BIT_OFFSET (t), fld);
5289 fld_worklist_push (DECL_FCONTEXT (t), fld);
5292 if ((TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
5293 && DECL_HAS_VALUE_EXPR_P (t))
5294 fld_worklist_push (DECL_VALUE_EXPR (t), fld);
5296 if (TREE_CODE (t) != FIELD_DECL
5297 && TREE_CODE (t) != TYPE_DECL)
5298 fld_worklist_push (TREE_CHAIN (t), fld);
5299 *ws = 0;
5301 else if (TYPE_P (t))
5303 /* Note that walk_tree does not traverse every possible field in
5304 types, so we have to do our own traversals here. */
5305 add_tree_to_fld_list (t, fld);
5307 if (!RECORD_OR_UNION_TYPE_P (t))
5308 fld_worklist_push (TYPE_CACHED_VALUES (t), fld);
5309 fld_worklist_push (TYPE_SIZE (t), fld);
5310 fld_worklist_push (TYPE_SIZE_UNIT (t), fld);
5311 fld_worklist_push (TYPE_ATTRIBUTES (t), fld);
5312 fld_worklist_push (TYPE_POINTER_TO (t), fld);
5313 fld_worklist_push (TYPE_REFERENCE_TO (t), fld);
5314 fld_worklist_push (TYPE_NAME (t), fld);
5315 /* Do not walk TYPE_NEXT_PTR_TO or TYPE_NEXT_REF_TO. We do not stream
5316 them and thus do not and want not to reach unused pointer types
5317 this way. */
5318 if (!POINTER_TYPE_P (t))
5319 fld_worklist_push (TYPE_MINVAL (t), fld);
5320 if (!RECORD_OR_UNION_TYPE_P (t))
5321 fld_worklist_push (TYPE_MAXVAL (t), fld);
5322 fld_worklist_push (TYPE_MAIN_VARIANT (t), fld);
5323 /* Do not walk TYPE_NEXT_VARIANT. We do not stream it and thus
5324 do not and want not to reach unused variants this way. */
5325 if (TYPE_CONTEXT (t))
5327 tree ctx = TYPE_CONTEXT (t);
5328 /* We adjust BLOCK TYPE_CONTEXTs to the innermost non-BLOCK one.
5329 So push that instead. */
5330 while (ctx && TREE_CODE (ctx) == BLOCK)
5331 ctx = BLOCK_SUPERCONTEXT (ctx);
5332 fld_worklist_push (ctx, fld);
5334 /* Do not walk TYPE_CANONICAL. We do not stream it and thus do not
5335 and want not to reach unused types this way. */
5337 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t))
5339 unsigned i;
5340 tree tem;
5341 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (TYPE_BINFO (t)), i, tem)
5342 fld_worklist_push (TREE_TYPE (tem), fld);
5343 tem = BINFO_VIRTUALS (TYPE_BINFO (t));
5344 if (tem
5345 /* The Java FE overloads BINFO_VIRTUALS for its own purpose. */
5346 && TREE_CODE (tem) == TREE_LIST)
5349 fld_worklist_push (TREE_VALUE (tem), fld);
5350 tem = TREE_CHAIN (tem);
5352 while (tem);
5354 if (RECORD_OR_UNION_TYPE_P (t))
5356 tree tem;
5357 /* Push all TYPE_FIELDS - there can be interleaving interesting
5358 and non-interesting things. */
5359 tem = TYPE_FIELDS (t);
5360 while (tem)
5362 if (TREE_CODE (tem) == FIELD_DECL
5363 || TREE_CODE (tem) == TYPE_DECL)
5364 fld_worklist_push (tem, fld);
5365 tem = TREE_CHAIN (tem);
5369 fld_worklist_push (TYPE_STUB_DECL (t), fld);
5370 *ws = 0;
5372 else if (TREE_CODE (t) == BLOCK)
5374 tree tem;
5375 for (tem = BLOCK_VARS (t); tem; tem = TREE_CHAIN (tem))
5376 fld_worklist_push (tem, fld);
5377 for (tem = BLOCK_SUBBLOCKS (t); tem; tem = BLOCK_CHAIN (tem))
5378 fld_worklist_push (tem, fld);
5379 fld_worklist_push (BLOCK_ABSTRACT_ORIGIN (t), fld);
5382 if (TREE_CODE (t) != IDENTIFIER_NODE
5383 && CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED))
5384 fld_worklist_push (TREE_TYPE (t), fld);
5386 return NULL_TREE;
5390 /* Find decls and types in T. */
5392 static void
5393 find_decls_types (tree t, struct free_lang_data_d *fld)
5395 while (1)
5397 if (!fld->pset->contains (t))
5398 walk_tree (&t, find_decls_types_r, fld, fld->pset);
5399 if (fld->worklist.is_empty ())
5400 break;
5401 t = fld->worklist.pop ();
5405 /* Translate all the types in LIST with the corresponding runtime
5406 types. */
5408 static tree
5409 get_eh_types_for_runtime (tree list)
5411 tree head, prev;
5413 if (list == NULL_TREE)
5414 return NULL_TREE;
5416 head = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5417 prev = head;
5418 list = TREE_CHAIN (list);
5419 while (list)
5421 tree n = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5422 TREE_CHAIN (prev) = n;
5423 prev = TREE_CHAIN (prev);
5424 list = TREE_CHAIN (list);
5427 return head;
5431 /* Find decls and types referenced in EH region R and store them in
5432 FLD->DECLS and FLD->TYPES. */
5434 static void
5435 find_decls_types_in_eh_region (eh_region r, struct free_lang_data_d *fld)
5437 switch (r->type)
5439 case ERT_CLEANUP:
5440 break;
5442 case ERT_TRY:
5444 eh_catch c;
5446 /* The types referenced in each catch must first be changed to the
5447 EH types used at runtime. This removes references to FE types
5448 in the region. */
5449 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
5451 c->type_list = get_eh_types_for_runtime (c->type_list);
5452 walk_tree (&c->type_list, find_decls_types_r, fld, fld->pset);
5455 break;
5457 case ERT_ALLOWED_EXCEPTIONS:
5458 r->u.allowed.type_list
5459 = get_eh_types_for_runtime (r->u.allowed.type_list);
5460 walk_tree (&r->u.allowed.type_list, find_decls_types_r, fld, fld->pset);
5461 break;
5463 case ERT_MUST_NOT_THROW:
5464 walk_tree (&r->u.must_not_throw.failure_decl,
5465 find_decls_types_r, fld, fld->pset);
5466 break;
5471 /* Find decls and types referenced in cgraph node N and store them in
5472 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5473 look for *every* kind of DECL and TYPE node reachable from N,
5474 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5475 NAMESPACE_DECLs, etc). */
5477 static void
5478 find_decls_types_in_node (struct cgraph_node *n, struct free_lang_data_d *fld)
5480 basic_block bb;
5481 struct function *fn;
5482 unsigned ix;
5483 tree t;
5485 find_decls_types (n->decl, fld);
5487 if (!gimple_has_body_p (n->decl))
5488 return;
5490 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
5492 fn = DECL_STRUCT_FUNCTION (n->decl);
5494 /* Traverse locals. */
5495 FOR_EACH_LOCAL_DECL (fn, ix, t)
5496 find_decls_types (t, fld);
5498 /* Traverse EH regions in FN. */
5500 eh_region r;
5501 FOR_ALL_EH_REGION_FN (r, fn)
5502 find_decls_types_in_eh_region (r, fld);
5505 /* Traverse every statement in FN. */
5506 FOR_EACH_BB_FN (bb, fn)
5508 gimple_stmt_iterator si;
5509 unsigned i;
5511 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
5513 gimple phi = gsi_stmt (si);
5515 for (i = 0; i < gimple_phi_num_args (phi); i++)
5517 tree *arg_p = gimple_phi_arg_def_ptr (phi, i);
5518 find_decls_types (*arg_p, fld);
5522 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
5524 gimple stmt = gsi_stmt (si);
5526 if (is_gimple_call (stmt))
5527 find_decls_types (gimple_call_fntype (stmt), fld);
5529 for (i = 0; i < gimple_num_ops (stmt); i++)
5531 tree arg = gimple_op (stmt, i);
5532 find_decls_types (arg, fld);
5539 /* Find decls and types referenced in varpool node N and store them in
5540 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5541 look for *every* kind of DECL and TYPE node reachable from N,
5542 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5543 NAMESPACE_DECLs, etc). */
5545 static void
5546 find_decls_types_in_var (varpool_node *v, struct free_lang_data_d *fld)
5548 find_decls_types (v->decl, fld);
5551 /* If T needs an assembler name, have one created for it. */
5553 void
5554 assign_assembler_name_if_neeeded (tree t)
5556 if (need_assembler_name_p (t))
5558 /* When setting DECL_ASSEMBLER_NAME, the C++ mangler may emit
5559 diagnostics that use input_location to show locus
5560 information. The problem here is that, at this point,
5561 input_location is generally anchored to the end of the file
5562 (since the parser is long gone), so we don't have a good
5563 position to pin it to.
5565 To alleviate this problem, this uses the location of T's
5566 declaration. Examples of this are
5567 testsuite/g++.dg/template/cond2.C and
5568 testsuite/g++.dg/template/pr35240.C. */
5569 location_t saved_location = input_location;
5570 input_location = DECL_SOURCE_LOCATION (t);
5572 decl_assembler_name (t);
5574 input_location = saved_location;
5579 /* Free language specific information for every operand and expression
5580 in every node of the call graph. This process operates in three stages:
5582 1- Every callgraph node and varpool node is traversed looking for
5583 decls and types embedded in them. This is a more exhaustive
5584 search than that done by find_referenced_vars, because it will
5585 also collect individual fields, decls embedded in types, etc.
5587 2- All the decls found are sent to free_lang_data_in_decl.
5589 3- All the types found are sent to free_lang_data_in_type.
5591 The ordering between decls and types is important because
5592 free_lang_data_in_decl sets assembler names, which includes
5593 mangling. So types cannot be freed up until assembler names have
5594 been set up. */
5596 static void
5597 free_lang_data_in_cgraph (void)
5599 struct cgraph_node *n;
5600 varpool_node *v;
5601 struct free_lang_data_d fld;
5602 tree t;
5603 unsigned i;
5604 alias_pair *p;
5606 /* Initialize sets and arrays to store referenced decls and types. */
5607 fld.pset = new hash_set<tree>;
5608 fld.worklist.create (0);
5609 fld.decls.create (100);
5610 fld.types.create (100);
5612 /* Find decls and types in the body of every function in the callgraph. */
5613 FOR_EACH_FUNCTION (n)
5614 find_decls_types_in_node (n, &fld);
5616 FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
5617 find_decls_types (p->decl, &fld);
5619 /* Find decls and types in every varpool symbol. */
5620 FOR_EACH_VARIABLE (v)
5621 find_decls_types_in_var (v, &fld);
5623 /* Set the assembler name on every decl found. We need to do this
5624 now because free_lang_data_in_decl will invalidate data needed
5625 for mangling. This breaks mangling on interdependent decls. */
5626 FOR_EACH_VEC_ELT (fld.decls, i, t)
5627 assign_assembler_name_if_neeeded (t);
5629 /* Traverse every decl found freeing its language data. */
5630 FOR_EACH_VEC_ELT (fld.decls, i, t)
5631 free_lang_data_in_decl (t);
5633 /* Traverse every type found freeing its language data. */
5634 FOR_EACH_VEC_ELT (fld.types, i, t)
5635 free_lang_data_in_type (t);
5637 delete fld.pset;
5638 fld.worklist.release ();
5639 fld.decls.release ();
5640 fld.types.release ();
5644 /* Free resources that are used by FE but are not needed once they are done. */
5646 static unsigned
5647 free_lang_data (void)
5649 unsigned i;
5651 /* If we are the LTO frontend we have freed lang-specific data already. */
5652 if (in_lto_p
5653 || !flag_generate_lto)
5654 return 0;
5656 /* Allocate and assign alias sets to the standard integer types
5657 while the slots are still in the way the frontends generated them. */
5658 for (i = 0; i < itk_none; ++i)
5659 if (integer_types[i])
5660 TYPE_ALIAS_SET (integer_types[i]) = get_alias_set (integer_types[i]);
5662 /* Traverse the IL resetting language specific information for
5663 operands, expressions, etc. */
5664 free_lang_data_in_cgraph ();
5666 /* Create gimple variants for common types. */
5667 ptrdiff_type_node = integer_type_node;
5668 fileptr_type_node = ptr_type_node;
5670 /* Reset some langhooks. Do not reset types_compatible_p, it may
5671 still be used indirectly via the get_alias_set langhook. */
5672 lang_hooks.dwarf_name = lhd_dwarf_name;
5673 lang_hooks.decl_printable_name = gimple_decl_printable_name;
5674 /* We do not want the default decl_assembler_name implementation,
5675 rather if we have fixed everything we want a wrapper around it
5676 asserting that all non-local symbols already got their assembler
5677 name and only produce assembler names for local symbols. Or rather
5678 make sure we never call decl_assembler_name on local symbols and
5679 devise a separate, middle-end private scheme for it. */
5681 /* Reset diagnostic machinery. */
5682 tree_diagnostics_defaults (global_dc);
5684 return 0;
5688 namespace {
5690 const pass_data pass_data_ipa_free_lang_data =
5692 SIMPLE_IPA_PASS, /* type */
5693 "*free_lang_data", /* name */
5694 OPTGROUP_NONE, /* optinfo_flags */
5695 TV_IPA_FREE_LANG_DATA, /* tv_id */
5696 0, /* properties_required */
5697 0, /* properties_provided */
5698 0, /* properties_destroyed */
5699 0, /* todo_flags_start */
5700 0, /* todo_flags_finish */
5703 class pass_ipa_free_lang_data : public simple_ipa_opt_pass
5705 public:
5706 pass_ipa_free_lang_data (gcc::context *ctxt)
5707 : simple_ipa_opt_pass (pass_data_ipa_free_lang_data, ctxt)
5710 /* opt_pass methods: */
5711 virtual unsigned int execute (function *) { return free_lang_data (); }
5713 }; // class pass_ipa_free_lang_data
5715 } // anon namespace
5717 simple_ipa_opt_pass *
5718 make_pass_ipa_free_lang_data (gcc::context *ctxt)
5720 return new pass_ipa_free_lang_data (ctxt);
5723 /* The backbone of is_attribute_p(). ATTR_LEN is the string length of
5724 ATTR_NAME. Also used internally by remove_attribute(). */
5725 bool
5726 private_is_attribute_p (const char *attr_name, size_t attr_len, const_tree ident)
5728 size_t ident_len = IDENTIFIER_LENGTH (ident);
5730 if (ident_len == attr_len)
5732 if (strcmp (attr_name, IDENTIFIER_POINTER (ident)) == 0)
5733 return true;
5735 else if (ident_len == attr_len + 4)
5737 /* There is the possibility that ATTR is 'text' and IDENT is
5738 '__text__'. */
5739 const char *p = IDENTIFIER_POINTER (ident);
5740 if (p[0] == '_' && p[1] == '_'
5741 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5742 && strncmp (attr_name, p + 2, attr_len) == 0)
5743 return true;
5746 return false;
5749 /* The backbone of lookup_attribute(). ATTR_LEN is the string length
5750 of ATTR_NAME, and LIST is not NULL_TREE. */
5751 tree
5752 private_lookup_attribute (const char *attr_name, size_t attr_len, tree list)
5754 while (list)
5756 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5758 if (ident_len == attr_len)
5760 if (!strcmp (attr_name,
5761 IDENTIFIER_POINTER (get_attribute_name (list))))
5762 break;
5764 /* TODO: If we made sure that attributes were stored in the
5765 canonical form without '__...__' (ie, as in 'text' as opposed
5766 to '__text__') then we could avoid the following case. */
5767 else if (ident_len == attr_len + 4)
5769 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5770 if (p[0] == '_' && p[1] == '_'
5771 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5772 && strncmp (attr_name, p + 2, attr_len) == 0)
5773 break;
5775 list = TREE_CHAIN (list);
5778 return list;
5781 /* Given an attribute name ATTR_NAME and a list of attributes LIST,
5782 return a pointer to the attribute's list first element if the attribute
5783 starts with ATTR_NAME. ATTR_NAME must be in the form 'text' (not
5784 '__text__'). */
5786 tree
5787 private_lookup_attribute_by_prefix (const char *attr_name, size_t attr_len,
5788 tree list)
5790 while (list)
5792 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5794 if (attr_len > ident_len)
5796 list = TREE_CHAIN (list);
5797 continue;
5800 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5802 if (strncmp (attr_name, p, attr_len) == 0)
5803 break;
5805 /* TODO: If we made sure that attributes were stored in the
5806 canonical form without '__...__' (ie, as in 'text' as opposed
5807 to '__text__') then we could avoid the following case. */
5808 if (p[0] == '_' && p[1] == '_' &&
5809 strncmp (attr_name, p + 2, attr_len) == 0)
5810 break;
5812 list = TREE_CHAIN (list);
5815 return list;
5819 /* A variant of lookup_attribute() that can be used with an identifier
5820 as the first argument, and where the identifier can be either
5821 'text' or '__text__'.
5823 Given an attribute ATTR_IDENTIFIER, and a list of attributes LIST,
5824 return a pointer to the attribute's list element if the attribute
5825 is part of the list, or NULL_TREE if not found. If the attribute
5826 appears more than once, this only returns the first occurrence; the
5827 TREE_CHAIN of the return value should be passed back in if further
5828 occurrences are wanted. ATTR_IDENTIFIER must be an identifier but
5829 can be in the form 'text' or '__text__'. */
5830 static tree
5831 lookup_ident_attribute (tree attr_identifier, tree list)
5833 gcc_checking_assert (TREE_CODE (attr_identifier) == IDENTIFIER_NODE);
5835 while (list)
5837 gcc_checking_assert (TREE_CODE (get_attribute_name (list))
5838 == IDENTIFIER_NODE);
5840 /* Identifiers can be compared directly for equality. */
5841 if (attr_identifier == get_attribute_name (list))
5842 break;
5844 /* If they are not equal, they may still be one in the form
5845 'text' while the other one is in the form '__text__'. TODO:
5846 If we were storing attributes in normalized 'text' form, then
5847 this could all go away and we could take full advantage of
5848 the fact that we're comparing identifiers. :-) */
5850 size_t attr_len = IDENTIFIER_LENGTH (attr_identifier);
5851 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5853 if (ident_len == attr_len + 4)
5855 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5856 const char *q = IDENTIFIER_POINTER (attr_identifier);
5857 if (p[0] == '_' && p[1] == '_'
5858 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5859 && strncmp (q, p + 2, attr_len) == 0)
5860 break;
5862 else if (ident_len + 4 == attr_len)
5864 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5865 const char *q = IDENTIFIER_POINTER (attr_identifier);
5866 if (q[0] == '_' && q[1] == '_'
5867 && q[attr_len - 2] == '_' && q[attr_len - 1] == '_'
5868 && strncmp (q + 2, p, ident_len) == 0)
5869 break;
5872 list = TREE_CHAIN (list);
5875 return list;
5878 /* Remove any instances of attribute ATTR_NAME in LIST and return the
5879 modified list. */
5881 tree
5882 remove_attribute (const char *attr_name, tree list)
5884 tree *p;
5885 size_t attr_len = strlen (attr_name);
5887 gcc_checking_assert (attr_name[0] != '_');
5889 for (p = &list; *p; )
5891 tree l = *p;
5892 /* TODO: If we were storing attributes in normalized form, here
5893 we could use a simple strcmp(). */
5894 if (private_is_attribute_p (attr_name, attr_len, get_attribute_name (l)))
5895 *p = TREE_CHAIN (l);
5896 else
5897 p = &TREE_CHAIN (l);
5900 return list;
5903 /* Return an attribute list that is the union of a1 and a2. */
5905 tree
5906 merge_attributes (tree a1, tree a2)
5908 tree attributes;
5910 /* Either one unset? Take the set one. */
5912 if ((attributes = a1) == 0)
5913 attributes = a2;
5915 /* One that completely contains the other? Take it. */
5917 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
5919 if (attribute_list_contained (a2, a1))
5920 attributes = a2;
5921 else
5923 /* Pick the longest list, and hang on the other list. */
5925 if (list_length (a1) < list_length (a2))
5926 attributes = a2, a2 = a1;
5928 for (; a2 != 0; a2 = TREE_CHAIN (a2))
5930 tree a;
5931 for (a = lookup_ident_attribute (get_attribute_name (a2),
5932 attributes);
5933 a != NULL_TREE && !attribute_value_equal (a, a2);
5934 a = lookup_ident_attribute (get_attribute_name (a2),
5935 TREE_CHAIN (a)))
5937 if (a == NULL_TREE)
5939 a1 = copy_node (a2);
5940 TREE_CHAIN (a1) = attributes;
5941 attributes = a1;
5946 return attributes;
5949 /* Given types T1 and T2, merge their attributes and return
5950 the result. */
5952 tree
5953 merge_type_attributes (tree t1, tree t2)
5955 return merge_attributes (TYPE_ATTRIBUTES (t1),
5956 TYPE_ATTRIBUTES (t2));
5959 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
5960 the result. */
5962 tree
5963 merge_decl_attributes (tree olddecl, tree newdecl)
5965 return merge_attributes (DECL_ATTRIBUTES (olddecl),
5966 DECL_ATTRIBUTES (newdecl));
5969 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
5971 /* Specialization of merge_decl_attributes for various Windows targets.
5973 This handles the following situation:
5975 __declspec (dllimport) int foo;
5976 int foo;
5978 The second instance of `foo' nullifies the dllimport. */
5980 tree
5981 merge_dllimport_decl_attributes (tree old, tree new_tree)
5983 tree a;
5984 int delete_dllimport_p = 1;
5986 /* What we need to do here is remove from `old' dllimport if it doesn't
5987 appear in `new'. dllimport behaves like extern: if a declaration is
5988 marked dllimport and a definition appears later, then the object
5989 is not dllimport'd. We also remove a `new' dllimport if the old list
5990 contains dllexport: dllexport always overrides dllimport, regardless
5991 of the order of declaration. */
5992 if (!VAR_OR_FUNCTION_DECL_P (new_tree))
5993 delete_dllimport_p = 0;
5994 else if (DECL_DLLIMPORT_P (new_tree)
5995 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
5997 DECL_DLLIMPORT_P (new_tree) = 0;
5998 warning (OPT_Wattributes, "%q+D already declared with dllexport attribute: "
5999 "dllimport ignored", new_tree);
6001 else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new_tree))
6003 /* Warn about overriding a symbol that has already been used, e.g.:
6004 extern int __attribute__ ((dllimport)) foo;
6005 int* bar () {return &foo;}
6006 int foo;
6008 if (TREE_USED (old))
6010 warning (0, "%q+D redeclared without dllimport attribute "
6011 "after being referenced with dll linkage", new_tree);
6012 /* If we have used a variable's address with dllimport linkage,
6013 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
6014 decl may already have had TREE_CONSTANT computed.
6015 We still remove the attribute so that assembler code refers
6016 to '&foo rather than '_imp__foo'. */
6017 if (TREE_CODE (old) == VAR_DECL && TREE_ADDRESSABLE (old))
6018 DECL_DLLIMPORT_P (new_tree) = 1;
6021 /* Let an inline definition silently override the external reference,
6022 but otherwise warn about attribute inconsistency. */
6023 else if (TREE_CODE (new_tree) == VAR_DECL
6024 || !DECL_DECLARED_INLINE_P (new_tree))
6025 warning (OPT_Wattributes, "%q+D redeclared without dllimport attribute: "
6026 "previous dllimport ignored", new_tree);
6028 else
6029 delete_dllimport_p = 0;
6031 a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new_tree));
6033 if (delete_dllimport_p)
6034 a = remove_attribute ("dllimport", a);
6036 return a;
6039 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
6040 struct attribute_spec.handler. */
6042 tree
6043 handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
6044 bool *no_add_attrs)
6046 tree node = *pnode;
6047 bool is_dllimport;
6049 /* These attributes may apply to structure and union types being created,
6050 but otherwise should pass to the declaration involved. */
6051 if (!DECL_P (node))
6053 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
6054 | (int) ATTR_FLAG_ARRAY_NEXT))
6056 *no_add_attrs = true;
6057 return tree_cons (name, args, NULL_TREE);
6059 if (TREE_CODE (node) == RECORD_TYPE
6060 || TREE_CODE (node) == UNION_TYPE)
6062 node = TYPE_NAME (node);
6063 if (!node)
6064 return NULL_TREE;
6066 else
6068 warning (OPT_Wattributes, "%qE attribute ignored",
6069 name);
6070 *no_add_attrs = true;
6071 return NULL_TREE;
6075 if (TREE_CODE (node) != FUNCTION_DECL
6076 && TREE_CODE (node) != VAR_DECL
6077 && TREE_CODE (node) != TYPE_DECL)
6079 *no_add_attrs = true;
6080 warning (OPT_Wattributes, "%qE attribute ignored",
6081 name);
6082 return NULL_TREE;
6085 if (TREE_CODE (node) == TYPE_DECL
6086 && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE
6087 && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE)
6089 *no_add_attrs = true;
6090 warning (OPT_Wattributes, "%qE attribute ignored",
6091 name);
6092 return NULL_TREE;
6095 is_dllimport = is_attribute_p ("dllimport", name);
6097 /* Report error on dllimport ambiguities seen now before they cause
6098 any damage. */
6099 if (is_dllimport)
6101 /* Honor any target-specific overrides. */
6102 if (!targetm.valid_dllimport_attribute_p (node))
6103 *no_add_attrs = true;
6105 else if (TREE_CODE (node) == FUNCTION_DECL
6106 && DECL_DECLARED_INLINE_P (node))
6108 warning (OPT_Wattributes, "inline function %q+D declared as "
6109 " dllimport: attribute ignored", node);
6110 *no_add_attrs = true;
6112 /* Like MS, treat definition of dllimported variables and
6113 non-inlined functions on declaration as syntax errors. */
6114 else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
6116 error ("function %q+D definition is marked dllimport", node);
6117 *no_add_attrs = true;
6120 else if (TREE_CODE (node) == VAR_DECL)
6122 if (DECL_INITIAL (node))
6124 error ("variable %q+D definition is marked dllimport",
6125 node);
6126 *no_add_attrs = true;
6129 /* `extern' needn't be specified with dllimport.
6130 Specify `extern' now and hope for the best. Sigh. */
6131 DECL_EXTERNAL (node) = 1;
6132 /* Also, implicitly give dllimport'd variables declared within
6133 a function global scope, unless declared static. */
6134 if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
6135 TREE_PUBLIC (node) = 1;
6138 if (*no_add_attrs == false)
6139 DECL_DLLIMPORT_P (node) = 1;
6141 else if (TREE_CODE (node) == FUNCTION_DECL
6142 && DECL_DECLARED_INLINE_P (node)
6143 && flag_keep_inline_dllexport)
6144 /* An exported function, even if inline, must be emitted. */
6145 DECL_EXTERNAL (node) = 0;
6147 /* Report error if symbol is not accessible at global scope. */
6148 if (!TREE_PUBLIC (node)
6149 && (TREE_CODE (node) == VAR_DECL
6150 || TREE_CODE (node) == FUNCTION_DECL))
6152 error ("external linkage required for symbol %q+D because of "
6153 "%qE attribute", node, name);
6154 *no_add_attrs = true;
6157 /* A dllexport'd entity must have default visibility so that other
6158 program units (shared libraries or the main executable) can see
6159 it. A dllimport'd entity must have default visibility so that
6160 the linker knows that undefined references within this program
6161 unit can be resolved by the dynamic linker. */
6162 if (!*no_add_attrs)
6164 if (DECL_VISIBILITY_SPECIFIED (node)
6165 && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT)
6166 error ("%qE implies default visibility, but %qD has already "
6167 "been declared with a different visibility",
6168 name, node);
6169 DECL_VISIBILITY (node) = VISIBILITY_DEFAULT;
6170 DECL_VISIBILITY_SPECIFIED (node) = 1;
6173 return NULL_TREE;
6176 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
6178 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
6179 of the various TYPE_QUAL values. */
6181 static void
6182 set_type_quals (tree type, int type_quals)
6184 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
6185 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
6186 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
6187 TYPE_ATOMIC (type) = (type_quals & TYPE_QUAL_ATOMIC) != 0;
6188 TYPE_ADDR_SPACE (type) = DECODE_QUAL_ADDR_SPACE (type_quals);
6191 /* Returns true iff unqualified CAND and BASE are equivalent. */
6193 bool
6194 check_base_type (const_tree cand, const_tree base)
6196 return (TYPE_NAME (cand) == TYPE_NAME (base)
6197 /* Apparently this is needed for Objective-C. */
6198 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
6199 /* Check alignment. */
6200 && TYPE_ALIGN (cand) == TYPE_ALIGN (base)
6201 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
6202 TYPE_ATTRIBUTES (base)));
6205 /* Returns true iff CAND is equivalent to BASE with TYPE_QUALS. */
6207 bool
6208 check_qualified_type (const_tree cand, const_tree base, int type_quals)
6210 return (TYPE_QUALS (cand) == type_quals
6211 && check_base_type (cand, base));
6214 /* Returns true iff CAND is equivalent to BASE with ALIGN. */
6216 static bool
6217 check_aligned_type (const_tree cand, const_tree base, unsigned int align)
6219 return (TYPE_QUALS (cand) == TYPE_QUALS (base)
6220 && TYPE_NAME (cand) == TYPE_NAME (base)
6221 /* Apparently this is needed for Objective-C. */
6222 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
6223 /* Check alignment. */
6224 && TYPE_ALIGN (cand) == align
6225 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
6226 TYPE_ATTRIBUTES (base)));
6229 /* This function checks to see if TYPE matches the size one of the built-in
6230 atomic types, and returns that core atomic type. */
6232 static tree
6233 find_atomic_core_type (tree type)
6235 tree base_atomic_type;
6237 /* Only handle complete types. */
6238 if (TYPE_SIZE (type) == NULL_TREE)
6239 return NULL_TREE;
6241 HOST_WIDE_INT type_size = tree_to_uhwi (TYPE_SIZE (type));
6242 switch (type_size)
6244 case 8:
6245 base_atomic_type = atomicQI_type_node;
6246 break;
6248 case 16:
6249 base_atomic_type = atomicHI_type_node;
6250 break;
6252 case 32:
6253 base_atomic_type = atomicSI_type_node;
6254 break;
6256 case 64:
6257 base_atomic_type = atomicDI_type_node;
6258 break;
6260 case 128:
6261 base_atomic_type = atomicTI_type_node;
6262 break;
6264 default:
6265 base_atomic_type = NULL_TREE;
6268 return base_atomic_type;
6271 /* Return a version of the TYPE, qualified as indicated by the
6272 TYPE_QUALS, if one exists. If no qualified version exists yet,
6273 return NULL_TREE. */
6275 tree
6276 get_qualified_type (tree type, int type_quals)
6278 tree t;
6280 if (TYPE_QUALS (type) == type_quals)
6281 return type;
6283 /* Search the chain of variants to see if there is already one there just
6284 like the one we need to have. If so, use that existing one. We must
6285 preserve the TYPE_NAME, since there is code that depends on this. */
6286 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6287 if (check_qualified_type (t, type, type_quals))
6288 return t;
6290 return NULL_TREE;
6293 /* Like get_qualified_type, but creates the type if it does not
6294 exist. This function never returns NULL_TREE. */
6296 tree
6297 build_qualified_type (tree type, int type_quals)
6299 tree t;
6301 /* See if we already have the appropriate qualified variant. */
6302 t = get_qualified_type (type, type_quals);
6304 /* If not, build it. */
6305 if (!t)
6307 t = build_variant_type_copy (type);
6308 set_type_quals (t, type_quals);
6310 if (((type_quals & TYPE_QUAL_ATOMIC) == TYPE_QUAL_ATOMIC))
6312 /* See if this object can map to a basic atomic type. */
6313 tree atomic_type = find_atomic_core_type (type);
6314 if (atomic_type)
6316 /* Ensure the alignment of this type is compatible with
6317 the required alignment of the atomic type. */
6318 if (TYPE_ALIGN (atomic_type) > TYPE_ALIGN (t))
6319 TYPE_ALIGN (t) = TYPE_ALIGN (atomic_type);
6323 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6324 /* Propagate structural equality. */
6325 SET_TYPE_STRUCTURAL_EQUALITY (t);
6326 else if (TYPE_CANONICAL (type) != type)
6327 /* Build the underlying canonical type, since it is different
6328 from TYPE. */
6330 tree c = build_qualified_type (TYPE_CANONICAL (type), type_quals);
6331 TYPE_CANONICAL (t) = TYPE_CANONICAL (c);
6333 else
6334 /* T is its own canonical type. */
6335 TYPE_CANONICAL (t) = t;
6339 return t;
6342 /* Create a variant of type T with alignment ALIGN. */
6344 tree
6345 build_aligned_type (tree type, unsigned int align)
6347 tree t;
6349 if (TYPE_PACKED (type)
6350 || TYPE_ALIGN (type) == align)
6351 return type;
6353 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6354 if (check_aligned_type (t, type, align))
6355 return t;
6357 t = build_variant_type_copy (type);
6358 TYPE_ALIGN (t) = align;
6360 return t;
6363 /* Create a new distinct copy of TYPE. The new type is made its own
6364 MAIN_VARIANT. If TYPE requires structural equality checks, the
6365 resulting type requires structural equality checks; otherwise, its
6366 TYPE_CANONICAL points to itself. */
6368 tree
6369 build_distinct_type_copy (tree type)
6371 tree t = copy_node (type);
6373 TYPE_POINTER_TO (t) = 0;
6374 TYPE_REFERENCE_TO (t) = 0;
6376 /* Set the canonical type either to a new equivalence class, or
6377 propagate the need for structural equality checks. */
6378 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6379 SET_TYPE_STRUCTURAL_EQUALITY (t);
6380 else
6381 TYPE_CANONICAL (t) = t;
6383 /* Make it its own variant. */
6384 TYPE_MAIN_VARIANT (t) = t;
6385 TYPE_NEXT_VARIANT (t) = 0;
6387 /* Note that it is now possible for TYPE_MIN_VALUE to be a value
6388 whose TREE_TYPE is not t. This can also happen in the Ada
6389 frontend when using subtypes. */
6391 return t;
6394 /* Create a new variant of TYPE, equivalent but distinct. This is so
6395 the caller can modify it. TYPE_CANONICAL for the return type will
6396 be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
6397 are considered equal by the language itself (or that both types
6398 require structural equality checks). */
6400 tree
6401 build_variant_type_copy (tree type)
6403 tree t, m = TYPE_MAIN_VARIANT (type);
6405 t = build_distinct_type_copy (type);
6407 /* Since we're building a variant, assume that it is a non-semantic
6408 variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
6409 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
6411 /* Add the new type to the chain of variants of TYPE. */
6412 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
6413 TYPE_NEXT_VARIANT (m) = t;
6414 TYPE_MAIN_VARIANT (t) = m;
6416 return t;
6419 /* Return true if the from tree in both tree maps are equal. */
6422 tree_map_base_eq (const void *va, const void *vb)
6424 const struct tree_map_base *const a = (const struct tree_map_base *) va,
6425 *const b = (const struct tree_map_base *) vb;
6426 return (a->from == b->from);
6429 /* Hash a from tree in a tree_base_map. */
6431 unsigned int
6432 tree_map_base_hash (const void *item)
6434 return htab_hash_pointer (((const struct tree_map_base *)item)->from);
6437 /* Return true if this tree map structure is marked for garbage collection
6438 purposes. We simply return true if the from tree is marked, so that this
6439 structure goes away when the from tree goes away. */
6442 tree_map_base_marked_p (const void *p)
6444 return ggc_marked_p (((const struct tree_map_base *) p)->from);
6447 /* Hash a from tree in a tree_map. */
6449 unsigned int
6450 tree_map_hash (const void *item)
6452 return (((const struct tree_map *) item)->hash);
6455 /* Hash a from tree in a tree_decl_map. */
6457 unsigned int
6458 tree_decl_map_hash (const void *item)
6460 return DECL_UID (((const struct tree_decl_map *) item)->base.from);
6463 /* Return the initialization priority for DECL. */
6465 priority_type
6466 decl_init_priority_lookup (tree decl)
6468 symtab_node *snode = symtab_node::get (decl);
6470 if (!snode)
6471 return DEFAULT_INIT_PRIORITY;
6472 return
6473 snode->get_init_priority ();
6476 /* Return the finalization priority for DECL. */
6478 priority_type
6479 decl_fini_priority_lookup (tree decl)
6481 cgraph_node *node = cgraph_node::get (decl);
6483 if (!node)
6484 return DEFAULT_INIT_PRIORITY;
6485 return
6486 node->get_fini_priority ();
6489 /* Set the initialization priority for DECL to PRIORITY. */
6491 void
6492 decl_init_priority_insert (tree decl, priority_type priority)
6494 struct symtab_node *snode;
6496 if (priority == DEFAULT_INIT_PRIORITY)
6498 snode = symtab_node::get (decl);
6499 if (!snode)
6500 return;
6502 else if (TREE_CODE (decl) == VAR_DECL)
6503 snode = varpool_node::get_create (decl);
6504 else
6505 snode = cgraph_node::get_create (decl);
6506 snode->set_init_priority (priority);
6509 /* Set the finalization priority for DECL to PRIORITY. */
6511 void
6512 decl_fini_priority_insert (tree decl, priority_type priority)
6514 struct cgraph_node *node;
6516 if (priority == DEFAULT_INIT_PRIORITY)
6518 node = cgraph_node::get (decl);
6519 if (!node)
6520 return;
6522 else
6523 node = cgraph_node::get_create (decl);
6524 node->set_fini_priority (priority);
6527 /* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
6529 static void
6530 print_debug_expr_statistics (void)
6532 fprintf (stderr, "DECL_DEBUG_EXPR hash: size %ld, %ld elements, %f collisions\n",
6533 (long) htab_size (debug_expr_for_decl),
6534 (long) htab_elements (debug_expr_for_decl),
6535 htab_collisions (debug_expr_for_decl));
6538 /* Print out the statistics for the DECL_VALUE_EXPR hash table. */
6540 static void
6541 print_value_expr_statistics (void)
6543 fprintf (stderr, "DECL_VALUE_EXPR hash: size %ld, %ld elements, %f collisions\n",
6544 (long) htab_size (value_expr_for_decl),
6545 (long) htab_elements (value_expr_for_decl),
6546 htab_collisions (value_expr_for_decl));
6549 /* Lookup a debug expression for FROM, and return it if we find one. */
6551 tree
6552 decl_debug_expr_lookup (tree from)
6554 struct tree_decl_map *h, in;
6555 in.base.from = from;
6557 h = (struct tree_decl_map *)
6558 htab_find_with_hash (debug_expr_for_decl, &in, DECL_UID (from));
6559 if (h)
6560 return h->to;
6561 return NULL_TREE;
6564 /* Insert a mapping FROM->TO in the debug expression hashtable. */
6566 void
6567 decl_debug_expr_insert (tree from, tree to)
6569 struct tree_decl_map *h;
6570 void **loc;
6572 h = ggc_alloc<tree_decl_map> ();
6573 h->base.from = from;
6574 h->to = to;
6575 loc = htab_find_slot_with_hash (debug_expr_for_decl, h, DECL_UID (from),
6576 INSERT);
6577 *(struct tree_decl_map **) loc = h;
6580 /* Lookup a value expression for FROM, and return it if we find one. */
6582 tree
6583 decl_value_expr_lookup (tree from)
6585 struct tree_decl_map *h, in;
6586 in.base.from = from;
6588 h = (struct tree_decl_map *)
6589 htab_find_with_hash (value_expr_for_decl, &in, DECL_UID (from));
6590 if (h)
6591 return h->to;
6592 return NULL_TREE;
6595 /* Insert a mapping FROM->TO in the value expression hashtable. */
6597 void
6598 decl_value_expr_insert (tree from, tree to)
6600 struct tree_decl_map *h;
6601 void **loc;
6603 h = ggc_alloc<tree_decl_map> ();
6604 h->base.from = from;
6605 h->to = to;
6606 loc = htab_find_slot_with_hash (value_expr_for_decl, h, DECL_UID (from),
6607 INSERT);
6608 *(struct tree_decl_map **) loc = h;
6611 /* Lookup a vector of debug arguments for FROM, and return it if we
6612 find one. */
6614 vec<tree, va_gc> **
6615 decl_debug_args_lookup (tree from)
6617 struct tree_vec_map *h, in;
6619 if (!DECL_HAS_DEBUG_ARGS_P (from))
6620 return NULL;
6621 gcc_checking_assert (debug_args_for_decl != NULL);
6622 in.base.from = from;
6623 h = (struct tree_vec_map *)
6624 htab_find_with_hash (debug_args_for_decl, &in, DECL_UID (from));
6625 if (h)
6626 return &h->to;
6627 return NULL;
6630 /* Insert a mapping FROM->empty vector of debug arguments in the value
6631 expression hashtable. */
6633 vec<tree, va_gc> **
6634 decl_debug_args_insert (tree from)
6636 struct tree_vec_map *h;
6637 void **loc;
6639 if (DECL_HAS_DEBUG_ARGS_P (from))
6640 return decl_debug_args_lookup (from);
6641 if (debug_args_for_decl == NULL)
6642 debug_args_for_decl = htab_create_ggc (64, tree_vec_map_hash,
6643 tree_vec_map_eq, 0);
6644 h = ggc_alloc<tree_vec_map> ();
6645 h->base.from = from;
6646 h->to = NULL;
6647 loc = htab_find_slot_with_hash (debug_args_for_decl, h, DECL_UID (from),
6648 INSERT);
6649 *(struct tree_vec_map **) loc = h;
6650 DECL_HAS_DEBUG_ARGS_P (from) = 1;
6651 return &h->to;
6654 /* Hashing of types so that we don't make duplicates.
6655 The entry point is `type_hash_canon'. */
6657 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
6658 with types in the TREE_VALUE slots), by adding the hash codes
6659 of the individual types. */
6661 static void
6662 type_hash_list (const_tree list, inchash::hash &hstate)
6664 const_tree tail;
6666 for (tail = list; tail; tail = TREE_CHAIN (tail))
6667 if (TREE_VALUE (tail) != error_mark_node)
6668 hstate.add_object (TYPE_HASH (TREE_VALUE (tail)));
6671 /* These are the Hashtable callback functions. */
6673 /* Returns true iff the types are equivalent. */
6675 static int
6676 type_hash_eq (const void *va, const void *vb)
6678 const struct type_hash *const a = (const struct type_hash *) va,
6679 *const b = (const struct type_hash *) vb;
6681 /* First test the things that are the same for all types. */
6682 if (a->hash != b->hash
6683 || TREE_CODE (a->type) != TREE_CODE (b->type)
6684 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
6685 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
6686 TYPE_ATTRIBUTES (b->type))
6687 || (TREE_CODE (a->type) != COMPLEX_TYPE
6688 && TYPE_NAME (a->type) != TYPE_NAME (b->type)))
6689 return 0;
6691 /* Be careful about comparing arrays before and after the element type
6692 has been completed; don't compare TYPE_ALIGN unless both types are
6693 complete. */
6694 if (COMPLETE_TYPE_P (a->type) && COMPLETE_TYPE_P (b->type)
6695 && (TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
6696 || TYPE_MODE (a->type) != TYPE_MODE (b->type)))
6697 return 0;
6699 switch (TREE_CODE (a->type))
6701 case VOID_TYPE:
6702 case COMPLEX_TYPE:
6703 case POINTER_TYPE:
6704 case REFERENCE_TYPE:
6705 case NULLPTR_TYPE:
6706 return 1;
6708 case VECTOR_TYPE:
6709 return TYPE_VECTOR_SUBPARTS (a->type) == TYPE_VECTOR_SUBPARTS (b->type);
6711 case ENUMERAL_TYPE:
6712 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
6713 && !(TYPE_VALUES (a->type)
6714 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
6715 && TYPE_VALUES (b->type)
6716 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
6717 && type_list_equal (TYPE_VALUES (a->type),
6718 TYPE_VALUES (b->type))))
6719 return 0;
6721 /* ... fall through ... */
6723 case INTEGER_TYPE:
6724 case REAL_TYPE:
6725 case BOOLEAN_TYPE:
6726 if (TYPE_PRECISION (a->type) != TYPE_PRECISION (b->type))
6727 return false;
6728 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
6729 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
6730 TYPE_MAX_VALUE (b->type)))
6731 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
6732 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
6733 TYPE_MIN_VALUE (b->type))));
6735 case FIXED_POINT_TYPE:
6736 return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
6738 case OFFSET_TYPE:
6739 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
6741 case METHOD_TYPE:
6742 if (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
6743 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6744 || (TYPE_ARG_TYPES (a->type)
6745 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6746 && TYPE_ARG_TYPES (b->type)
6747 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6748 && type_list_equal (TYPE_ARG_TYPES (a->type),
6749 TYPE_ARG_TYPES (b->type)))))
6750 break;
6751 return 0;
6752 case ARRAY_TYPE:
6753 return TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type);
6755 case RECORD_TYPE:
6756 case UNION_TYPE:
6757 case QUAL_UNION_TYPE:
6758 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
6759 || (TYPE_FIELDS (a->type)
6760 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
6761 && TYPE_FIELDS (b->type)
6762 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
6763 && type_list_equal (TYPE_FIELDS (a->type),
6764 TYPE_FIELDS (b->type))));
6766 case FUNCTION_TYPE:
6767 if (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6768 || (TYPE_ARG_TYPES (a->type)
6769 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6770 && TYPE_ARG_TYPES (b->type)
6771 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6772 && type_list_equal (TYPE_ARG_TYPES (a->type),
6773 TYPE_ARG_TYPES (b->type))))
6774 break;
6775 return 0;
6777 default:
6778 return 0;
6781 if (lang_hooks.types.type_hash_eq != NULL)
6782 return lang_hooks.types.type_hash_eq (a->type, b->type);
6784 return 1;
6787 /* Return the cached hash value. */
6789 static hashval_t
6790 type_hash_hash (const void *item)
6792 return ((const struct type_hash *) item)->hash;
6795 /* Given TYPE, and HASHCODE its hash code, return the canonical
6796 object for an identical type if one already exists.
6797 Otherwise, return TYPE, and record it as the canonical object.
6799 To use this function, first create a type of the sort you want.
6800 Then compute its hash code from the fields of the type that
6801 make it different from other similar types.
6802 Then call this function and use the value. */
6804 tree
6805 type_hash_canon (unsigned int hashcode, tree type)
6807 type_hash in;
6808 void **loc;
6810 /* The hash table only contains main variants, so ensure that's what we're
6811 being passed. */
6812 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
6814 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
6815 must call that routine before comparing TYPE_ALIGNs. */
6816 layout_type (type);
6818 in.hash = hashcode;
6819 in.type = type;
6821 loc = htab_find_slot_with_hash (type_hash_table, &in, hashcode, INSERT);
6822 if (*loc)
6824 tree t1 = ((type_hash *) *loc)->type;
6825 gcc_assert (TYPE_MAIN_VARIANT (t1) == t1);
6826 if (GATHER_STATISTICS)
6828 tree_code_counts[(int) TREE_CODE (type)]--;
6829 tree_node_counts[(int) t_kind]--;
6830 tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type_non_common);
6832 return t1;
6834 else
6836 struct type_hash *h;
6838 h = ggc_alloc<type_hash> ();
6839 h->hash = hashcode;
6840 h->type = type;
6841 *loc = (void *)h;
6843 return type;
6847 /* See if the data pointed to by the type hash table is marked. We consider
6848 it marked if the type is marked or if a debug type number or symbol
6849 table entry has been made for the type. */
6851 static int
6852 type_hash_marked_p (const void *p)
6854 const_tree const type = ((const struct type_hash *) p)->type;
6856 return ggc_marked_p (type);
6859 static void
6860 print_type_hash_statistics (void)
6862 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
6863 (long) htab_size (type_hash_table),
6864 (long) htab_elements (type_hash_table),
6865 htab_collisions (type_hash_table));
6868 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
6869 with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
6870 by adding the hash codes of the individual attributes. */
6872 static void
6873 attribute_hash_list (const_tree list, inchash::hash &hstate)
6875 const_tree tail;
6877 for (tail = list; tail; tail = TREE_CHAIN (tail))
6878 /* ??? Do we want to add in TREE_VALUE too? */
6879 hstate.add_object (IDENTIFIER_HASH_VALUE (get_attribute_name (tail)));
6882 /* Given two lists of attributes, return true if list l2 is
6883 equivalent to l1. */
6886 attribute_list_equal (const_tree l1, const_tree l2)
6888 if (l1 == l2)
6889 return 1;
6891 return attribute_list_contained (l1, l2)
6892 && attribute_list_contained (l2, l1);
6895 /* Given two lists of attributes, return true if list L2 is
6896 completely contained within L1. */
6897 /* ??? This would be faster if attribute names were stored in a canonicalized
6898 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
6899 must be used to show these elements are equivalent (which they are). */
6900 /* ??? It's not clear that attributes with arguments will always be handled
6901 correctly. */
6904 attribute_list_contained (const_tree l1, const_tree l2)
6906 const_tree t1, t2;
6908 /* First check the obvious, maybe the lists are identical. */
6909 if (l1 == l2)
6910 return 1;
6912 /* Maybe the lists are similar. */
6913 for (t1 = l1, t2 = l2;
6914 t1 != 0 && t2 != 0
6915 && get_attribute_name (t1) == get_attribute_name (t2)
6916 && TREE_VALUE (t1) == TREE_VALUE (t2);
6917 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6920 /* Maybe the lists are equal. */
6921 if (t1 == 0 && t2 == 0)
6922 return 1;
6924 for (; t2 != 0; t2 = TREE_CHAIN (t2))
6926 const_tree attr;
6927 /* This CONST_CAST is okay because lookup_attribute does not
6928 modify its argument and the return value is assigned to a
6929 const_tree. */
6930 for (attr = lookup_ident_attribute (get_attribute_name (t2),
6931 CONST_CAST_TREE (l1));
6932 attr != NULL_TREE && !attribute_value_equal (t2, attr);
6933 attr = lookup_ident_attribute (get_attribute_name (t2),
6934 TREE_CHAIN (attr)))
6937 if (attr == NULL_TREE)
6938 return 0;
6941 return 1;
6944 /* Given two lists of types
6945 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
6946 return 1 if the lists contain the same types in the same order.
6947 Also, the TREE_PURPOSEs must match. */
6950 type_list_equal (const_tree l1, const_tree l2)
6952 const_tree t1, t2;
6954 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6955 if (TREE_VALUE (t1) != TREE_VALUE (t2)
6956 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
6957 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
6958 && (TREE_TYPE (TREE_PURPOSE (t1))
6959 == TREE_TYPE (TREE_PURPOSE (t2))))))
6960 return 0;
6962 return t1 == t2;
6965 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
6966 given by TYPE. If the argument list accepts variable arguments,
6967 then this function counts only the ordinary arguments. */
6970 type_num_arguments (const_tree type)
6972 int i = 0;
6973 tree t;
6975 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
6976 /* If the function does not take a variable number of arguments,
6977 the last element in the list will have type `void'. */
6978 if (VOID_TYPE_P (TREE_VALUE (t)))
6979 break;
6980 else
6981 ++i;
6983 return i;
6986 /* Nonzero if integer constants T1 and T2
6987 represent the same constant value. */
6990 tree_int_cst_equal (const_tree t1, const_tree t2)
6992 if (t1 == t2)
6993 return 1;
6995 if (t1 == 0 || t2 == 0)
6996 return 0;
6998 if (TREE_CODE (t1) == INTEGER_CST
6999 && TREE_CODE (t2) == INTEGER_CST
7000 && wi::to_widest (t1) == wi::to_widest (t2))
7001 return 1;
7003 return 0;
7006 /* Return true if T is an INTEGER_CST whose numerical value (extended
7007 according to TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. */
7009 bool
7010 tree_fits_shwi_p (const_tree t)
7012 return (t != NULL_TREE
7013 && TREE_CODE (t) == INTEGER_CST
7014 && wi::fits_shwi_p (wi::to_widest (t)));
7017 /* Return true if T is an INTEGER_CST whose numerical value (extended
7018 according to TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. */
7020 bool
7021 tree_fits_uhwi_p (const_tree t)
7023 return (t != NULL_TREE
7024 && TREE_CODE (t) == INTEGER_CST
7025 && wi::fits_uhwi_p (wi::to_widest (t)));
7028 /* T is an INTEGER_CST whose numerical value (extended according to
7029 TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. Return that
7030 HOST_WIDE_INT. */
7032 HOST_WIDE_INT
7033 tree_to_shwi (const_tree t)
7035 gcc_assert (tree_fits_shwi_p (t));
7036 return TREE_INT_CST_LOW (t);
7039 /* T is an INTEGER_CST whose numerical value (extended according to
7040 TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. Return that
7041 HOST_WIDE_INT. */
7043 unsigned HOST_WIDE_INT
7044 tree_to_uhwi (const_tree t)
7046 gcc_assert (tree_fits_uhwi_p (t));
7047 return TREE_INT_CST_LOW (t);
7050 /* Return the most significant (sign) bit of T. */
7053 tree_int_cst_sign_bit (const_tree t)
7055 unsigned bitno = TYPE_PRECISION (TREE_TYPE (t)) - 1;
7057 return wi::extract_uhwi (t, bitno, 1);
7060 /* Return an indication of the sign of the integer constant T.
7061 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
7062 Note that -1 will never be returned if T's type is unsigned. */
7065 tree_int_cst_sgn (const_tree t)
7067 if (wi::eq_p (t, 0))
7068 return 0;
7069 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
7070 return 1;
7071 else if (wi::neg_p (t))
7072 return -1;
7073 else
7074 return 1;
7077 /* Return the minimum number of bits needed to represent VALUE in a
7078 signed or unsigned type, UNSIGNEDP says which. */
7080 unsigned int
7081 tree_int_cst_min_precision (tree value, signop sgn)
7083 /* If the value is negative, compute its negative minus 1. The latter
7084 adjustment is because the absolute value of the largest negative value
7085 is one larger than the largest positive value. This is equivalent to
7086 a bit-wise negation, so use that operation instead. */
7088 if (tree_int_cst_sgn (value) < 0)
7089 value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
7091 /* Return the number of bits needed, taking into account the fact
7092 that we need one more bit for a signed than unsigned type.
7093 If value is 0 or -1, the minimum precision is 1 no matter
7094 whether unsignedp is true or false. */
7096 if (integer_zerop (value))
7097 return 1;
7098 else
7099 return tree_floor_log2 (value) + 1 + (sgn == SIGNED ? 1 : 0) ;
7102 /* Return truthvalue of whether T1 is the same tree structure as T2.
7103 Return 1 if they are the same.
7104 Return 0 if they are understandably different.
7105 Return -1 if either contains tree structure not understood by
7106 this function. */
7109 simple_cst_equal (const_tree t1, const_tree t2)
7111 enum tree_code code1, code2;
7112 int cmp;
7113 int i;
7115 if (t1 == t2)
7116 return 1;
7117 if (t1 == 0 || t2 == 0)
7118 return 0;
7120 code1 = TREE_CODE (t1);
7121 code2 = TREE_CODE (t2);
7123 if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
7125 if (CONVERT_EXPR_CODE_P (code2)
7126 || code2 == NON_LVALUE_EXPR)
7127 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7128 else
7129 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
7132 else if (CONVERT_EXPR_CODE_P (code2)
7133 || code2 == NON_LVALUE_EXPR)
7134 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
7136 if (code1 != code2)
7137 return 0;
7139 switch (code1)
7141 case INTEGER_CST:
7142 return wi::to_widest (t1) == wi::to_widest (t2);
7144 case REAL_CST:
7145 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
7147 case FIXED_CST:
7148 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
7150 case STRING_CST:
7151 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
7152 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
7153 TREE_STRING_LENGTH (t1)));
7155 case CONSTRUCTOR:
7157 unsigned HOST_WIDE_INT idx;
7158 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (t1);
7159 vec<constructor_elt, va_gc> *v2 = CONSTRUCTOR_ELTS (t2);
7161 if (vec_safe_length (v1) != vec_safe_length (v2))
7162 return false;
7164 for (idx = 0; idx < vec_safe_length (v1); ++idx)
7165 /* ??? Should we handle also fields here? */
7166 if (!simple_cst_equal ((*v1)[idx].value, (*v2)[idx].value))
7167 return false;
7168 return true;
7171 case SAVE_EXPR:
7172 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7174 case CALL_EXPR:
7175 cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
7176 if (cmp <= 0)
7177 return cmp;
7178 if (call_expr_nargs (t1) != call_expr_nargs (t2))
7179 return 0;
7181 const_tree arg1, arg2;
7182 const_call_expr_arg_iterator iter1, iter2;
7183 for (arg1 = first_const_call_expr_arg (t1, &iter1),
7184 arg2 = first_const_call_expr_arg (t2, &iter2);
7185 arg1 && arg2;
7186 arg1 = next_const_call_expr_arg (&iter1),
7187 arg2 = next_const_call_expr_arg (&iter2))
7189 cmp = simple_cst_equal (arg1, arg2);
7190 if (cmp <= 0)
7191 return cmp;
7193 return arg1 == arg2;
7196 case TARGET_EXPR:
7197 /* Special case: if either target is an unallocated VAR_DECL,
7198 it means that it's going to be unified with whatever the
7199 TARGET_EXPR is really supposed to initialize, so treat it
7200 as being equivalent to anything. */
7201 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
7202 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
7203 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
7204 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
7205 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
7206 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
7207 cmp = 1;
7208 else
7209 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7211 if (cmp <= 0)
7212 return cmp;
7214 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
7216 case WITH_CLEANUP_EXPR:
7217 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7218 if (cmp <= 0)
7219 return cmp;
7221 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
7223 case COMPONENT_REF:
7224 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
7225 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7227 return 0;
7229 case VAR_DECL:
7230 case PARM_DECL:
7231 case CONST_DECL:
7232 case FUNCTION_DECL:
7233 return 0;
7235 default:
7236 break;
7239 /* This general rule works for most tree codes. All exceptions should be
7240 handled above. If this is a language-specific tree code, we can't
7241 trust what might be in the operand, so say we don't know
7242 the situation. */
7243 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
7244 return -1;
7246 switch (TREE_CODE_CLASS (code1))
7248 case tcc_unary:
7249 case tcc_binary:
7250 case tcc_comparison:
7251 case tcc_expression:
7252 case tcc_reference:
7253 case tcc_statement:
7254 cmp = 1;
7255 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
7257 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
7258 if (cmp <= 0)
7259 return cmp;
7262 return cmp;
7264 default:
7265 return -1;
7269 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
7270 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
7271 than U, respectively. */
7274 compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
7276 if (tree_int_cst_sgn (t) < 0)
7277 return -1;
7278 else if (!tree_fits_uhwi_p (t))
7279 return 1;
7280 else if (TREE_INT_CST_LOW (t) == u)
7281 return 0;
7282 else if (TREE_INT_CST_LOW (t) < u)
7283 return -1;
7284 else
7285 return 1;
7288 /* Return true if SIZE represents a constant size that is in bounds of
7289 what the middle-end and the backend accepts (covering not more than
7290 half of the address-space). */
7292 bool
7293 valid_constant_size_p (const_tree size)
7295 if (! tree_fits_uhwi_p (size)
7296 || TREE_OVERFLOW (size)
7297 || tree_int_cst_sign_bit (size) != 0)
7298 return false;
7299 return true;
7302 /* Return the precision of the type, or for a complex or vector type the
7303 precision of the type of its elements. */
7305 unsigned int
7306 element_precision (const_tree type)
7308 enum tree_code code = TREE_CODE (type);
7309 if (code == COMPLEX_TYPE || code == VECTOR_TYPE)
7310 type = TREE_TYPE (type);
7312 return TYPE_PRECISION (type);
7315 /* Return true if CODE represents an associative tree code. Otherwise
7316 return false. */
7317 bool
7318 associative_tree_code (enum tree_code code)
7320 switch (code)
7322 case BIT_IOR_EXPR:
7323 case BIT_AND_EXPR:
7324 case BIT_XOR_EXPR:
7325 case PLUS_EXPR:
7326 case MULT_EXPR:
7327 case MIN_EXPR:
7328 case MAX_EXPR:
7329 return true;
7331 default:
7332 break;
7334 return false;
7337 /* Return true if CODE represents a commutative tree code. Otherwise
7338 return false. */
7339 bool
7340 commutative_tree_code (enum tree_code code)
7342 switch (code)
7344 case PLUS_EXPR:
7345 case MULT_EXPR:
7346 case MULT_HIGHPART_EXPR:
7347 case MIN_EXPR:
7348 case MAX_EXPR:
7349 case BIT_IOR_EXPR:
7350 case BIT_XOR_EXPR:
7351 case BIT_AND_EXPR:
7352 case NE_EXPR:
7353 case EQ_EXPR:
7354 case UNORDERED_EXPR:
7355 case ORDERED_EXPR:
7356 case UNEQ_EXPR:
7357 case LTGT_EXPR:
7358 case TRUTH_AND_EXPR:
7359 case TRUTH_XOR_EXPR:
7360 case TRUTH_OR_EXPR:
7361 case WIDEN_MULT_EXPR:
7362 case VEC_WIDEN_MULT_HI_EXPR:
7363 case VEC_WIDEN_MULT_LO_EXPR:
7364 case VEC_WIDEN_MULT_EVEN_EXPR:
7365 case VEC_WIDEN_MULT_ODD_EXPR:
7366 return true;
7368 default:
7369 break;
7371 return false;
7374 /* Return true if CODE represents a ternary tree code for which the
7375 first two operands are commutative. Otherwise return false. */
7376 bool
7377 commutative_ternary_tree_code (enum tree_code code)
7379 switch (code)
7381 case WIDEN_MULT_PLUS_EXPR:
7382 case WIDEN_MULT_MINUS_EXPR:
7383 return true;
7385 default:
7386 break;
7388 return false;
7391 namespace inchash
7394 /* Generate a hash value for an expression. This can be used iteratively
7395 by passing a previous result as the HSTATE argument.
7397 This function is intended to produce the same hash for expressions which
7398 would compare equal using operand_equal_p. */
7399 void
7400 add_expr (const_tree t, inchash::hash &hstate)
7402 int i;
7403 enum tree_code code;
7404 enum tree_code_class tclass;
7406 if (t == NULL_TREE)
7408 hstate.merge_hash (0);
7409 return;
7412 code = TREE_CODE (t);
7414 switch (code)
7416 /* Alas, constants aren't shared, so we can't rely on pointer
7417 identity. */
7418 case VOID_CST:
7419 hstate.merge_hash (0);
7420 return;
7421 case INTEGER_CST:
7422 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
7423 hstate.add_wide_int (TREE_INT_CST_ELT (t, i));
7424 return;
7425 case REAL_CST:
7427 unsigned int val2 = real_hash (TREE_REAL_CST_PTR (t));
7428 hstate.merge_hash (val2);
7429 return;
7431 case FIXED_CST:
7433 unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
7434 hstate.merge_hash (val2);
7435 return;
7437 case STRING_CST:
7438 hstate.add ((const void *) TREE_STRING_POINTER (t), TREE_STRING_LENGTH (t));
7439 return;
7440 case COMPLEX_CST:
7441 inchash::add_expr (TREE_REALPART (t), hstate);
7442 inchash::add_expr (TREE_IMAGPART (t), hstate);
7443 return;
7444 case VECTOR_CST:
7446 unsigned i;
7447 for (i = 0; i < VECTOR_CST_NELTS (t); ++i)
7448 inchash::add_expr (VECTOR_CST_ELT (t, i), hstate);
7449 return;
7451 case SSA_NAME:
7452 /* We can just compare by pointer. */
7453 hstate.add_wide_int (SSA_NAME_VERSION (t));
7454 return;
7455 case PLACEHOLDER_EXPR:
7456 /* The node itself doesn't matter. */
7457 return;
7458 case TREE_LIST:
7459 /* A list of expressions, for a CALL_EXPR or as the elements of a
7460 VECTOR_CST. */
7461 for (; t; t = TREE_CHAIN (t))
7462 inchash::add_expr (TREE_VALUE (t), hstate);
7463 return;
7464 case CONSTRUCTOR:
7466 unsigned HOST_WIDE_INT idx;
7467 tree field, value;
7468 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
7470 inchash::add_expr (field, hstate);
7471 inchash::add_expr (value, hstate);
7473 return;
7475 case FUNCTION_DECL:
7476 /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
7477 Otherwise nodes that compare equal according to operand_equal_p might
7478 get different hash codes. However, don't do this for machine specific
7479 or front end builtins, since the function code is overloaded in those
7480 cases. */
7481 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
7482 && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
7484 t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
7485 code = TREE_CODE (t);
7487 /* FALL THROUGH */
7488 default:
7489 tclass = TREE_CODE_CLASS (code);
7491 if (tclass == tcc_declaration)
7493 /* DECL's have a unique ID */
7494 hstate.add_wide_int (DECL_UID (t));
7496 else
7498 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
7500 hstate.add_object (code);
7502 /* Don't hash the type, that can lead to having nodes which
7503 compare equal according to operand_equal_p, but which
7504 have different hash codes. */
7505 if (CONVERT_EXPR_CODE_P (code)
7506 || code == NON_LVALUE_EXPR)
7508 /* Make sure to include signness in the hash computation. */
7509 hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
7510 inchash::add_expr (TREE_OPERAND (t, 0), hstate);
7513 else if (commutative_tree_code (code))
7515 /* It's a commutative expression. We want to hash it the same
7516 however it appears. We do this by first hashing both operands
7517 and then rehashing based on the order of their independent
7518 hashes. */
7519 inchash::hash one, two;
7520 inchash::add_expr (TREE_OPERAND (t, 0), one);
7521 inchash::add_expr (TREE_OPERAND (t, 1), two);
7522 hstate.add_commutative (one, two);
7524 else
7525 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
7526 inchash::add_expr (TREE_OPERAND (t, i), hstate);
7528 return;
7534 /* Constructors for pointer, array and function types.
7535 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
7536 constructed by language-dependent code, not here.) */
7538 /* Construct, lay out and return the type of pointers to TO_TYPE with
7539 mode MODE. If CAN_ALIAS_ALL is TRUE, indicate this type can
7540 reference all of memory. If such a type has already been
7541 constructed, reuse it. */
7543 tree
7544 build_pointer_type_for_mode (tree to_type, enum machine_mode mode,
7545 bool can_alias_all)
7547 tree t;
7549 if (to_type == error_mark_node)
7550 return error_mark_node;
7552 /* If the pointed-to type has the may_alias attribute set, force
7553 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7554 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7555 can_alias_all = true;
7557 /* In some cases, languages will have things that aren't a POINTER_TYPE
7558 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
7559 In that case, return that type without regard to the rest of our
7560 operands.
7562 ??? This is a kludge, but consistent with the way this function has
7563 always operated and there doesn't seem to be a good way to avoid this
7564 at the moment. */
7565 if (TYPE_POINTER_TO (to_type) != 0
7566 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
7567 return TYPE_POINTER_TO (to_type);
7569 /* First, if we already have a type for pointers to TO_TYPE and it's
7570 the proper mode, use it. */
7571 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
7572 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7573 return t;
7575 t = make_node (POINTER_TYPE);
7577 TREE_TYPE (t) = to_type;
7578 SET_TYPE_MODE (t, mode);
7579 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7580 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
7581 TYPE_POINTER_TO (to_type) = t;
7583 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7584 SET_TYPE_STRUCTURAL_EQUALITY (t);
7585 else if (TYPE_CANONICAL (to_type) != to_type)
7586 TYPE_CANONICAL (t)
7587 = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
7588 mode, can_alias_all);
7590 /* Lay out the type. This function has many callers that are concerned
7591 with expression-construction, and this simplifies them all. */
7592 layout_type (t);
7594 return t;
7597 /* By default build pointers in ptr_mode. */
7599 tree
7600 build_pointer_type (tree to_type)
7602 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7603 : TYPE_ADDR_SPACE (to_type);
7604 enum machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7605 return build_pointer_type_for_mode (to_type, pointer_mode, false);
7608 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
7610 tree
7611 build_reference_type_for_mode (tree to_type, enum machine_mode mode,
7612 bool can_alias_all)
7614 tree t;
7616 if (to_type == error_mark_node)
7617 return error_mark_node;
7619 /* If the pointed-to type has the may_alias attribute set, force
7620 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7621 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7622 can_alias_all = true;
7624 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
7625 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
7626 In that case, return that type without regard to the rest of our
7627 operands.
7629 ??? This is a kludge, but consistent with the way this function has
7630 always operated and there doesn't seem to be a good way to avoid this
7631 at the moment. */
7632 if (TYPE_REFERENCE_TO (to_type) != 0
7633 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
7634 return TYPE_REFERENCE_TO (to_type);
7636 /* First, if we already have a type for pointers to TO_TYPE and it's
7637 the proper mode, use it. */
7638 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
7639 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7640 return t;
7642 t = make_node (REFERENCE_TYPE);
7644 TREE_TYPE (t) = to_type;
7645 SET_TYPE_MODE (t, mode);
7646 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7647 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
7648 TYPE_REFERENCE_TO (to_type) = t;
7650 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7651 SET_TYPE_STRUCTURAL_EQUALITY (t);
7652 else if (TYPE_CANONICAL (to_type) != to_type)
7653 TYPE_CANONICAL (t)
7654 = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
7655 mode, can_alias_all);
7657 layout_type (t);
7659 return t;
7663 /* Build the node for the type of references-to-TO_TYPE by default
7664 in ptr_mode. */
7666 tree
7667 build_reference_type (tree to_type)
7669 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7670 : TYPE_ADDR_SPACE (to_type);
7671 enum machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7672 return build_reference_type_for_mode (to_type, pointer_mode, false);
7675 #define MAX_INT_CACHED_PREC \
7676 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
7677 static GTY(()) tree nonstandard_integer_type_cache[2 * MAX_INT_CACHED_PREC + 2];
7679 /* Builds a signed or unsigned integer type of precision PRECISION.
7680 Used for C bitfields whose precision does not match that of
7681 built-in target types. */
7682 tree
7683 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
7684 int unsignedp)
7686 tree itype, ret;
7688 if (unsignedp)
7689 unsignedp = MAX_INT_CACHED_PREC + 1;
7691 if (precision <= MAX_INT_CACHED_PREC)
7693 itype = nonstandard_integer_type_cache[precision + unsignedp];
7694 if (itype)
7695 return itype;
7698 itype = make_node (INTEGER_TYPE);
7699 TYPE_PRECISION (itype) = precision;
7701 if (unsignedp)
7702 fixup_unsigned_type (itype);
7703 else
7704 fixup_signed_type (itype);
7706 ret = itype;
7707 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (itype)))
7708 ret = type_hash_canon (tree_to_uhwi (TYPE_MAX_VALUE (itype)), itype);
7709 if (precision <= MAX_INT_CACHED_PREC)
7710 nonstandard_integer_type_cache[precision + unsignedp] = ret;
7712 return ret;
7715 /* Create a range of some discrete type TYPE (an INTEGER_TYPE, ENUMERAL_TYPE
7716 or BOOLEAN_TYPE) with low bound LOWVAL and high bound HIGHVAL. If SHARED
7717 is true, reuse such a type that has already been constructed. */
7719 static tree
7720 build_range_type_1 (tree type, tree lowval, tree highval, bool shared)
7722 tree itype = make_node (INTEGER_TYPE);
7723 inchash::hash hstate;
7725 TREE_TYPE (itype) = type;
7727 TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
7728 TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
7730 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
7731 SET_TYPE_MODE (itype, TYPE_MODE (type));
7732 TYPE_SIZE (itype) = TYPE_SIZE (type);
7733 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
7734 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
7735 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
7737 if (!shared)
7738 return itype;
7740 if ((TYPE_MIN_VALUE (itype)
7741 && TREE_CODE (TYPE_MIN_VALUE (itype)) != INTEGER_CST)
7742 || (TYPE_MAX_VALUE (itype)
7743 && TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST))
7745 /* Since we cannot reliably merge this type, we need to compare it using
7746 structural equality checks. */
7747 SET_TYPE_STRUCTURAL_EQUALITY (itype);
7748 return itype;
7751 inchash::add_expr (TYPE_MIN_VALUE (itype), hstate);
7752 inchash::add_expr (TYPE_MAX_VALUE (itype), hstate);
7753 hstate.merge_hash (TYPE_HASH (type));
7754 itype = type_hash_canon (hstate.end (), itype);
7756 return itype;
7759 /* Wrapper around build_range_type_1 with SHARED set to true. */
7761 tree
7762 build_range_type (tree type, tree lowval, tree highval)
7764 return build_range_type_1 (type, lowval, highval, true);
7767 /* Wrapper around build_range_type_1 with SHARED set to false. */
7769 tree
7770 build_nonshared_range_type (tree type, tree lowval, tree highval)
7772 return build_range_type_1 (type, lowval, highval, false);
7775 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
7776 MAXVAL should be the maximum value in the domain
7777 (one less than the length of the array).
7779 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
7780 We don't enforce this limit, that is up to caller (e.g. language front end).
7781 The limit exists because the result is a signed type and we don't handle
7782 sizes that use more than one HOST_WIDE_INT. */
7784 tree
7785 build_index_type (tree maxval)
7787 return build_range_type (sizetype, size_zero_node, maxval);
7790 /* Return true if the debug information for TYPE, a subtype, should be emitted
7791 as a subrange type. If so, set LOWVAL to the low bound and HIGHVAL to the
7792 high bound, respectively. Sometimes doing so unnecessarily obfuscates the
7793 debug info and doesn't reflect the source code. */
7795 bool
7796 subrange_type_for_debug_p (const_tree type, tree *lowval, tree *highval)
7798 tree base_type = TREE_TYPE (type), low, high;
7800 /* Subrange types have a base type which is an integral type. */
7801 if (!INTEGRAL_TYPE_P (base_type))
7802 return false;
7804 /* Get the real bounds of the subtype. */
7805 if (lang_hooks.types.get_subrange_bounds)
7806 lang_hooks.types.get_subrange_bounds (type, &low, &high);
7807 else
7809 low = TYPE_MIN_VALUE (type);
7810 high = TYPE_MAX_VALUE (type);
7813 /* If the type and its base type have the same representation and the same
7814 name, then the type is not a subrange but a copy of the base type. */
7815 if ((TREE_CODE (base_type) == INTEGER_TYPE
7816 || TREE_CODE (base_type) == BOOLEAN_TYPE)
7817 && int_size_in_bytes (type) == int_size_in_bytes (base_type)
7818 && tree_int_cst_equal (low, TYPE_MIN_VALUE (base_type))
7819 && tree_int_cst_equal (high, TYPE_MAX_VALUE (base_type))
7820 && TYPE_IDENTIFIER (type) == TYPE_IDENTIFIER (base_type))
7821 return false;
7823 if (lowval)
7824 *lowval = low;
7825 if (highval)
7826 *highval = high;
7827 return true;
7830 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
7831 and number of elements specified by the range of values of INDEX_TYPE.
7832 If SHARED is true, reuse such a type that has already been constructed. */
7834 static tree
7835 build_array_type_1 (tree elt_type, tree index_type, bool shared)
7837 tree t;
7839 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
7841 error ("arrays of functions are not meaningful");
7842 elt_type = integer_type_node;
7845 t = make_node (ARRAY_TYPE);
7846 TREE_TYPE (t) = elt_type;
7847 TYPE_DOMAIN (t) = index_type;
7848 TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (elt_type);
7849 layout_type (t);
7851 /* If the element type is incomplete at this point we get marked for
7852 structural equality. Do not record these types in the canonical
7853 type hashtable. */
7854 if (TYPE_STRUCTURAL_EQUALITY_P (t))
7855 return t;
7857 if (shared)
7859 inchash::hash hstate;
7860 hstate.add_object (TYPE_HASH (elt_type));
7861 if (index_type)
7862 hstate.add_object (TYPE_HASH (index_type));
7863 t = type_hash_canon (hstate.end (), t);
7866 if (TYPE_CANONICAL (t) == t)
7868 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
7869 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
7870 SET_TYPE_STRUCTURAL_EQUALITY (t);
7871 else if (TYPE_CANONICAL (elt_type) != elt_type
7872 || (index_type && TYPE_CANONICAL (index_type) != index_type))
7873 TYPE_CANONICAL (t)
7874 = build_array_type_1 (TYPE_CANONICAL (elt_type),
7875 index_type
7876 ? TYPE_CANONICAL (index_type) : NULL_TREE,
7877 shared);
7880 return t;
7883 /* Wrapper around build_array_type_1 with SHARED set to true. */
7885 tree
7886 build_array_type (tree elt_type, tree index_type)
7888 return build_array_type_1 (elt_type, index_type, true);
7891 /* Wrapper around build_array_type_1 with SHARED set to false. */
7893 tree
7894 build_nonshared_array_type (tree elt_type, tree index_type)
7896 return build_array_type_1 (elt_type, index_type, false);
7899 /* Return a representation of ELT_TYPE[NELTS], using indices of type
7900 sizetype. */
7902 tree
7903 build_array_type_nelts (tree elt_type, unsigned HOST_WIDE_INT nelts)
7905 return build_array_type (elt_type, build_index_type (size_int (nelts - 1)));
7908 /* Recursively examines the array elements of TYPE, until a non-array
7909 element type is found. */
7911 tree
7912 strip_array_types (tree type)
7914 while (TREE_CODE (type) == ARRAY_TYPE)
7915 type = TREE_TYPE (type);
7917 return type;
7920 /* Computes the canonical argument types from the argument type list
7921 ARGTYPES.
7923 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
7924 on entry to this function, or if any of the ARGTYPES are
7925 structural.
7927 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
7928 true on entry to this function, or if any of the ARGTYPES are
7929 non-canonical.
7931 Returns a canonical argument list, which may be ARGTYPES when the
7932 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
7933 true) or would not differ from ARGTYPES. */
7935 static tree
7936 maybe_canonicalize_argtypes (tree argtypes,
7937 bool *any_structural_p,
7938 bool *any_noncanonical_p)
7940 tree arg;
7941 bool any_noncanonical_argtypes_p = false;
7943 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
7945 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
7946 /* Fail gracefully by stating that the type is structural. */
7947 *any_structural_p = true;
7948 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
7949 *any_structural_p = true;
7950 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
7951 || TREE_PURPOSE (arg))
7952 /* If the argument has a default argument, we consider it
7953 non-canonical even though the type itself is canonical.
7954 That way, different variants of function and method types
7955 with default arguments will all point to the variant with
7956 no defaults as their canonical type. */
7957 any_noncanonical_argtypes_p = true;
7960 if (*any_structural_p)
7961 return argtypes;
7963 if (any_noncanonical_argtypes_p)
7965 /* Build the canonical list of argument types. */
7966 tree canon_argtypes = NULL_TREE;
7967 bool is_void = false;
7969 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
7971 if (arg == void_list_node)
7972 is_void = true;
7973 else
7974 canon_argtypes = tree_cons (NULL_TREE,
7975 TYPE_CANONICAL (TREE_VALUE (arg)),
7976 canon_argtypes);
7979 canon_argtypes = nreverse (canon_argtypes);
7980 if (is_void)
7981 canon_argtypes = chainon (canon_argtypes, void_list_node);
7983 /* There is a non-canonical type. */
7984 *any_noncanonical_p = true;
7985 return canon_argtypes;
7988 /* The canonical argument types are the same as ARGTYPES. */
7989 return argtypes;
7992 /* Construct, lay out and return
7993 the type of functions returning type VALUE_TYPE
7994 given arguments of types ARG_TYPES.
7995 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
7996 are data type nodes for the arguments of the function.
7997 If such a type has already been constructed, reuse it. */
7999 tree
8000 build_function_type (tree value_type, tree arg_types)
8002 tree t;
8003 inchash::hash hstate;
8004 bool any_structural_p, any_noncanonical_p;
8005 tree canon_argtypes;
8007 if (TREE_CODE (value_type) == FUNCTION_TYPE)
8009 error ("function return type cannot be function");
8010 value_type = integer_type_node;
8013 /* Make a node of the sort we want. */
8014 t = make_node (FUNCTION_TYPE);
8015 TREE_TYPE (t) = value_type;
8016 TYPE_ARG_TYPES (t) = arg_types;
8018 /* If we already have such a type, use the old one. */
8019 hstate.add_object (TYPE_HASH (value_type));
8020 type_hash_list (arg_types, hstate);
8021 t = type_hash_canon (hstate.end (), t);
8023 /* Set up the canonical type. */
8024 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
8025 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
8026 canon_argtypes = maybe_canonicalize_argtypes (arg_types,
8027 &any_structural_p,
8028 &any_noncanonical_p);
8029 if (any_structural_p)
8030 SET_TYPE_STRUCTURAL_EQUALITY (t);
8031 else if (any_noncanonical_p)
8032 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
8033 canon_argtypes);
8035 if (!COMPLETE_TYPE_P (t))
8036 layout_type (t);
8037 return t;
8040 /* Build a function type. The RETURN_TYPE is the type returned by the
8041 function. If VAARGS is set, no void_type_node is appended to the
8042 the list. ARGP must be always be terminated be a NULL_TREE. */
8044 static tree
8045 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
8047 tree t, args, last;
8049 t = va_arg (argp, tree);
8050 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
8051 args = tree_cons (NULL_TREE, t, args);
8053 if (vaargs)
8055 last = args;
8056 if (args != NULL_TREE)
8057 args = nreverse (args);
8058 gcc_assert (last != void_list_node);
8060 else if (args == NULL_TREE)
8061 args = void_list_node;
8062 else
8064 last = args;
8065 args = nreverse (args);
8066 TREE_CHAIN (last) = void_list_node;
8068 args = build_function_type (return_type, args);
8070 return args;
8073 /* Build a function type. The RETURN_TYPE is the type returned by the
8074 function. If additional arguments are provided, they are
8075 additional argument types. The list of argument types must always
8076 be terminated by NULL_TREE. */
8078 tree
8079 build_function_type_list (tree return_type, ...)
8081 tree args;
8082 va_list p;
8084 va_start (p, return_type);
8085 args = build_function_type_list_1 (false, return_type, p);
8086 va_end (p);
8087 return args;
8090 /* Build a variable argument function type. The RETURN_TYPE is the
8091 type returned by the function. If additional arguments are provided,
8092 they are additional argument types. The list of argument types must
8093 always be terminated by NULL_TREE. */
8095 tree
8096 build_varargs_function_type_list (tree return_type, ...)
8098 tree args;
8099 va_list p;
8101 va_start (p, return_type);
8102 args = build_function_type_list_1 (true, return_type, p);
8103 va_end (p);
8105 return args;
8108 /* Build a function type. RETURN_TYPE is the type returned by the
8109 function; VAARGS indicates whether the function takes varargs. The
8110 function takes N named arguments, the types of which are provided in
8111 ARG_TYPES. */
8113 static tree
8114 build_function_type_array_1 (bool vaargs, tree return_type, int n,
8115 tree *arg_types)
8117 int i;
8118 tree t = vaargs ? NULL_TREE : void_list_node;
8120 for (i = n - 1; i >= 0; i--)
8121 t = tree_cons (NULL_TREE, arg_types[i], t);
8123 return build_function_type (return_type, t);
8126 /* Build a function type. RETURN_TYPE is the type returned by the
8127 function. The function takes N named arguments, the types of which
8128 are provided in ARG_TYPES. */
8130 tree
8131 build_function_type_array (tree return_type, int n, tree *arg_types)
8133 return build_function_type_array_1 (false, return_type, n, arg_types);
8136 /* Build a variable argument function type. RETURN_TYPE is the type
8137 returned by the function. The function takes N named arguments, the
8138 types of which are provided in ARG_TYPES. */
8140 tree
8141 build_varargs_function_type_array (tree return_type, int n, tree *arg_types)
8143 return build_function_type_array_1 (true, return_type, n, arg_types);
8146 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
8147 and ARGTYPES (a TREE_LIST) are the return type and arguments types
8148 for the method. An implicit additional parameter (of type
8149 pointer-to-BASETYPE) is added to the ARGTYPES. */
8151 tree
8152 build_method_type_directly (tree basetype,
8153 tree rettype,
8154 tree argtypes)
8156 tree t;
8157 tree ptype;
8158 inchash::hash hstate;
8159 bool any_structural_p, any_noncanonical_p;
8160 tree canon_argtypes;
8162 /* Make a node of the sort we want. */
8163 t = make_node (METHOD_TYPE);
8165 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8166 TREE_TYPE (t) = rettype;
8167 ptype = build_pointer_type (basetype);
8169 /* The actual arglist for this function includes a "hidden" argument
8170 which is "this". Put it into the list of argument types. */
8171 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
8172 TYPE_ARG_TYPES (t) = argtypes;
8174 /* If we already have such a type, use the old one. */
8175 hstate.add_object (TYPE_HASH (basetype));
8176 hstate.add_object (TYPE_HASH (rettype));
8177 type_hash_list (argtypes, hstate);
8178 t = type_hash_canon (hstate.end (), t);
8180 /* Set up the canonical type. */
8181 any_structural_p
8182 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8183 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
8184 any_noncanonical_p
8185 = (TYPE_CANONICAL (basetype) != basetype
8186 || TYPE_CANONICAL (rettype) != rettype);
8187 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
8188 &any_structural_p,
8189 &any_noncanonical_p);
8190 if (any_structural_p)
8191 SET_TYPE_STRUCTURAL_EQUALITY (t);
8192 else if (any_noncanonical_p)
8193 TYPE_CANONICAL (t)
8194 = build_method_type_directly (TYPE_CANONICAL (basetype),
8195 TYPE_CANONICAL (rettype),
8196 canon_argtypes);
8197 if (!COMPLETE_TYPE_P (t))
8198 layout_type (t);
8200 return t;
8203 /* Construct, lay out and return the type of methods belonging to class
8204 BASETYPE and whose arguments and values are described by TYPE.
8205 If that type exists already, reuse it.
8206 TYPE must be a FUNCTION_TYPE node. */
8208 tree
8209 build_method_type (tree basetype, tree type)
8211 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8213 return build_method_type_directly (basetype,
8214 TREE_TYPE (type),
8215 TYPE_ARG_TYPES (type));
8218 /* Construct, lay out and return the type of offsets to a value
8219 of type TYPE, within an object of type BASETYPE.
8220 If a suitable offset type exists already, reuse it. */
8222 tree
8223 build_offset_type (tree basetype, tree type)
8225 tree t;
8226 inchash::hash hstate;
8228 /* Make a node of the sort we want. */
8229 t = make_node (OFFSET_TYPE);
8231 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8232 TREE_TYPE (t) = type;
8234 /* If we already have such a type, use the old one. */
8235 hstate.add_object (TYPE_HASH (basetype));
8236 hstate.add_object (TYPE_HASH (type));
8237 t = type_hash_canon (hstate.end (), t);
8239 if (!COMPLETE_TYPE_P (t))
8240 layout_type (t);
8242 if (TYPE_CANONICAL (t) == t)
8244 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8245 || TYPE_STRUCTURAL_EQUALITY_P (type))
8246 SET_TYPE_STRUCTURAL_EQUALITY (t);
8247 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
8248 || TYPE_CANONICAL (type) != type)
8249 TYPE_CANONICAL (t)
8250 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
8251 TYPE_CANONICAL (type));
8254 return t;
8257 /* Create a complex type whose components are COMPONENT_TYPE. */
8259 tree
8260 build_complex_type (tree component_type)
8262 tree t;
8263 inchash::hash hstate;
8265 gcc_assert (INTEGRAL_TYPE_P (component_type)
8266 || SCALAR_FLOAT_TYPE_P (component_type)
8267 || FIXED_POINT_TYPE_P (component_type));
8269 /* Make a node of the sort we want. */
8270 t = make_node (COMPLEX_TYPE);
8272 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
8274 /* If we already have such a type, use the old one. */
8275 hstate.add_object (TYPE_HASH (component_type));
8276 t = type_hash_canon (hstate.end (), t);
8278 if (!COMPLETE_TYPE_P (t))
8279 layout_type (t);
8281 if (TYPE_CANONICAL (t) == t)
8283 if (TYPE_STRUCTURAL_EQUALITY_P (component_type))
8284 SET_TYPE_STRUCTURAL_EQUALITY (t);
8285 else if (TYPE_CANONICAL (component_type) != component_type)
8286 TYPE_CANONICAL (t)
8287 = build_complex_type (TYPE_CANONICAL (component_type));
8290 /* We need to create a name, since complex is a fundamental type. */
8291 if (! TYPE_NAME (t))
8293 const char *name;
8294 if (component_type == char_type_node)
8295 name = "complex char";
8296 else if (component_type == signed_char_type_node)
8297 name = "complex signed char";
8298 else if (component_type == unsigned_char_type_node)
8299 name = "complex unsigned char";
8300 else if (component_type == short_integer_type_node)
8301 name = "complex short int";
8302 else if (component_type == short_unsigned_type_node)
8303 name = "complex short unsigned int";
8304 else if (component_type == integer_type_node)
8305 name = "complex int";
8306 else if (component_type == unsigned_type_node)
8307 name = "complex unsigned int";
8308 else if (component_type == long_integer_type_node)
8309 name = "complex long int";
8310 else if (component_type == long_unsigned_type_node)
8311 name = "complex long unsigned int";
8312 else if (component_type == long_long_integer_type_node)
8313 name = "complex long long int";
8314 else if (component_type == long_long_unsigned_type_node)
8315 name = "complex long long unsigned int";
8316 else
8317 name = 0;
8319 if (name != 0)
8320 TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
8321 get_identifier (name), t);
8324 return build_qualified_type (t, TYPE_QUALS (component_type));
8327 /* If TYPE is a real or complex floating-point type and the target
8328 does not directly support arithmetic on TYPE then return the wider
8329 type to be used for arithmetic on TYPE. Otherwise, return
8330 NULL_TREE. */
8332 tree
8333 excess_precision_type (tree type)
8335 if (flag_excess_precision != EXCESS_PRECISION_FAST)
8337 int flt_eval_method = TARGET_FLT_EVAL_METHOD;
8338 switch (TREE_CODE (type))
8340 case REAL_TYPE:
8341 switch (flt_eval_method)
8343 case 1:
8344 if (TYPE_MODE (type) == TYPE_MODE (float_type_node))
8345 return double_type_node;
8346 break;
8347 case 2:
8348 if (TYPE_MODE (type) == TYPE_MODE (float_type_node)
8349 || TYPE_MODE (type) == TYPE_MODE (double_type_node))
8350 return long_double_type_node;
8351 break;
8352 default:
8353 gcc_unreachable ();
8355 break;
8356 case COMPLEX_TYPE:
8357 if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
8358 return NULL_TREE;
8359 switch (flt_eval_method)
8361 case 1:
8362 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node))
8363 return complex_double_type_node;
8364 break;
8365 case 2:
8366 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node)
8367 || (TYPE_MODE (TREE_TYPE (type))
8368 == TYPE_MODE (double_type_node)))
8369 return complex_long_double_type_node;
8370 break;
8371 default:
8372 gcc_unreachable ();
8374 break;
8375 default:
8376 break;
8379 return NULL_TREE;
8382 /* Return OP, stripped of any conversions to wider types as much as is safe.
8383 Converting the value back to OP's type makes a value equivalent to OP.
8385 If FOR_TYPE is nonzero, we return a value which, if converted to
8386 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
8388 OP must have integer, real or enumeral type. Pointers are not allowed!
8390 There are some cases where the obvious value we could return
8391 would regenerate to OP if converted to OP's type,
8392 but would not extend like OP to wider types.
8393 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
8394 For example, if OP is (unsigned short)(signed char)-1,
8395 we avoid returning (signed char)-1 if FOR_TYPE is int,
8396 even though extending that to an unsigned short would regenerate OP,
8397 since the result of extending (signed char)-1 to (int)
8398 is different from (int) OP. */
8400 tree
8401 get_unwidened (tree op, tree for_type)
8403 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
8404 tree type = TREE_TYPE (op);
8405 unsigned final_prec
8406 = TYPE_PRECISION (for_type != 0 ? for_type : type);
8407 int uns
8408 = (for_type != 0 && for_type != type
8409 && final_prec > TYPE_PRECISION (type)
8410 && TYPE_UNSIGNED (type));
8411 tree win = op;
8413 while (CONVERT_EXPR_P (op))
8415 int bitschange;
8417 /* TYPE_PRECISION on vector types has different meaning
8418 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
8419 so avoid them here. */
8420 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
8421 break;
8423 bitschange = TYPE_PRECISION (TREE_TYPE (op))
8424 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
8426 /* Truncations are many-one so cannot be removed.
8427 Unless we are later going to truncate down even farther. */
8428 if (bitschange < 0
8429 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
8430 break;
8432 /* See what's inside this conversion. If we decide to strip it,
8433 we will set WIN. */
8434 op = TREE_OPERAND (op, 0);
8436 /* If we have not stripped any zero-extensions (uns is 0),
8437 we can strip any kind of extension.
8438 If we have previously stripped a zero-extension,
8439 only zero-extensions can safely be stripped.
8440 Any extension can be stripped if the bits it would produce
8441 are all going to be discarded later by truncating to FOR_TYPE. */
8443 if (bitschange > 0)
8445 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
8446 win = op;
8447 /* TYPE_UNSIGNED says whether this is a zero-extension.
8448 Let's avoid computing it if it does not affect WIN
8449 and if UNS will not be needed again. */
8450 if ((uns
8451 || CONVERT_EXPR_P (op))
8452 && TYPE_UNSIGNED (TREE_TYPE (op)))
8454 uns = 1;
8455 win = op;
8460 /* If we finally reach a constant see if it fits in for_type and
8461 in that case convert it. */
8462 if (for_type
8463 && TREE_CODE (win) == INTEGER_CST
8464 && TREE_TYPE (win) != for_type
8465 && int_fits_type_p (win, for_type))
8466 win = fold_convert (for_type, win);
8468 return win;
8471 /* Return OP or a simpler expression for a narrower value
8472 which can be sign-extended or zero-extended to give back OP.
8473 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
8474 or 0 if the value should be sign-extended. */
8476 tree
8477 get_narrower (tree op, int *unsignedp_ptr)
8479 int uns = 0;
8480 int first = 1;
8481 tree win = op;
8482 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
8484 while (TREE_CODE (op) == NOP_EXPR)
8486 int bitschange
8487 = (TYPE_PRECISION (TREE_TYPE (op))
8488 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
8490 /* Truncations are many-one so cannot be removed. */
8491 if (bitschange < 0)
8492 break;
8494 /* See what's inside this conversion. If we decide to strip it,
8495 we will set WIN. */
8497 if (bitschange > 0)
8499 op = TREE_OPERAND (op, 0);
8500 /* An extension: the outermost one can be stripped,
8501 but remember whether it is zero or sign extension. */
8502 if (first)
8503 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8504 /* Otherwise, if a sign extension has been stripped,
8505 only sign extensions can now be stripped;
8506 if a zero extension has been stripped, only zero-extensions. */
8507 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
8508 break;
8509 first = 0;
8511 else /* bitschange == 0 */
8513 /* A change in nominal type can always be stripped, but we must
8514 preserve the unsignedness. */
8515 if (first)
8516 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8517 first = 0;
8518 op = TREE_OPERAND (op, 0);
8519 /* Keep trying to narrow, but don't assign op to win if it
8520 would turn an integral type into something else. */
8521 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
8522 continue;
8525 win = op;
8528 if (TREE_CODE (op) == COMPONENT_REF
8529 /* Since type_for_size always gives an integer type. */
8530 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
8531 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
8532 /* Ensure field is laid out already. */
8533 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
8534 && tree_fits_uhwi_p (DECL_SIZE (TREE_OPERAND (op, 1))))
8536 unsigned HOST_WIDE_INT innerprec
8537 = tree_to_uhwi (DECL_SIZE (TREE_OPERAND (op, 1)));
8538 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
8539 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
8540 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
8542 /* We can get this structure field in a narrower type that fits it,
8543 but the resulting extension to its nominal type (a fullword type)
8544 must satisfy the same conditions as for other extensions.
8546 Do this only for fields that are aligned (not bit-fields),
8547 because when bit-field insns will be used there is no
8548 advantage in doing this. */
8550 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
8551 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
8552 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
8553 && type != 0)
8555 if (first)
8556 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
8557 win = fold_convert (type, op);
8561 *unsignedp_ptr = uns;
8562 return win;
8565 /* Returns true if integer constant C has a value that is permissible
8566 for type TYPE (an INTEGER_TYPE). */
8568 bool
8569 int_fits_type_p (const_tree c, const_tree type)
8571 tree type_low_bound, type_high_bound;
8572 bool ok_for_low_bound, ok_for_high_bound;
8573 signop sgn_c = TYPE_SIGN (TREE_TYPE (c));
8575 retry:
8576 type_low_bound = TYPE_MIN_VALUE (type);
8577 type_high_bound = TYPE_MAX_VALUE (type);
8579 /* If at least one bound of the type is a constant integer, we can check
8580 ourselves and maybe make a decision. If no such decision is possible, but
8581 this type is a subtype, try checking against that. Otherwise, use
8582 fits_to_tree_p, which checks against the precision.
8584 Compute the status for each possibly constant bound, and return if we see
8585 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
8586 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
8587 for "constant known to fit". */
8589 /* Check if c >= type_low_bound. */
8590 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
8592 if (tree_int_cst_lt (c, type_low_bound))
8593 return false;
8594 ok_for_low_bound = true;
8596 else
8597 ok_for_low_bound = false;
8599 /* Check if c <= type_high_bound. */
8600 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
8602 if (tree_int_cst_lt (type_high_bound, c))
8603 return false;
8604 ok_for_high_bound = true;
8606 else
8607 ok_for_high_bound = false;
8609 /* If the constant fits both bounds, the result is known. */
8610 if (ok_for_low_bound && ok_for_high_bound)
8611 return true;
8613 /* Perform some generic filtering which may allow making a decision
8614 even if the bounds are not constant. First, negative integers
8615 never fit in unsigned types, */
8616 if (TYPE_UNSIGNED (type) && sgn_c == SIGNED && wi::neg_p (c))
8617 return false;
8619 /* Second, narrower types always fit in wider ones. */
8620 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
8621 return true;
8623 /* Third, unsigned integers with top bit set never fit signed types. */
8624 if (!TYPE_UNSIGNED (type) && sgn_c == UNSIGNED)
8626 int prec = GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (c))) - 1;
8627 if (prec < TYPE_PRECISION (TREE_TYPE (c)))
8629 /* When a tree_cst is converted to a wide-int, the precision
8630 is taken from the type. However, if the precision of the
8631 mode underneath the type is smaller than that, it is
8632 possible that the value will not fit. The test below
8633 fails if any bit is set between the sign bit of the
8634 underlying mode and the top bit of the type. */
8635 if (wi::ne_p (wi::zext (c, prec - 1), c))
8636 return false;
8638 else if (wi::neg_p (c))
8639 return false;
8642 /* If we haven't been able to decide at this point, there nothing more we
8643 can check ourselves here. Look at the base type if we have one and it
8644 has the same precision. */
8645 if (TREE_CODE (type) == INTEGER_TYPE
8646 && TREE_TYPE (type) != 0
8647 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
8649 type = TREE_TYPE (type);
8650 goto retry;
8653 /* Or to fits_to_tree_p, if nothing else. */
8654 return wi::fits_to_tree_p (c, type);
8657 /* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
8658 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
8659 represented (assuming two's-complement arithmetic) within the bit
8660 precision of the type are returned instead. */
8662 void
8663 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
8665 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
8666 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
8667 wi::to_mpz (TYPE_MIN_VALUE (type), min, TYPE_SIGN (type));
8668 else
8670 if (TYPE_UNSIGNED (type))
8671 mpz_set_ui (min, 0);
8672 else
8674 wide_int mn = wi::min_value (TYPE_PRECISION (type), SIGNED);
8675 wi::to_mpz (mn, min, SIGNED);
8679 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
8680 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
8681 wi::to_mpz (TYPE_MAX_VALUE (type), max, TYPE_SIGN (type));
8682 else
8684 wide_int mn = wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
8685 wi::to_mpz (mn, max, TYPE_SIGN (type));
8689 /* Return true if VAR is an automatic variable defined in function FN. */
8691 bool
8692 auto_var_in_fn_p (const_tree var, const_tree fn)
8694 return (DECL_P (var) && DECL_CONTEXT (var) == fn
8695 && ((((TREE_CODE (var) == VAR_DECL && ! DECL_EXTERNAL (var))
8696 || TREE_CODE (var) == PARM_DECL)
8697 && ! TREE_STATIC (var))
8698 || TREE_CODE (var) == LABEL_DECL
8699 || TREE_CODE (var) == RESULT_DECL));
8702 /* Subprogram of following function. Called by walk_tree.
8704 Return *TP if it is an automatic variable or parameter of the
8705 function passed in as DATA. */
8707 static tree
8708 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
8710 tree fn = (tree) data;
8712 if (TYPE_P (*tp))
8713 *walk_subtrees = 0;
8715 else if (DECL_P (*tp)
8716 && auto_var_in_fn_p (*tp, fn))
8717 return *tp;
8719 return NULL_TREE;
8722 /* Returns true if T is, contains, or refers to a type with variable
8723 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
8724 arguments, but not the return type. If FN is nonzero, only return
8725 true if a modifier of the type or position of FN is a variable or
8726 parameter inside FN.
8728 This concept is more general than that of C99 'variably modified types':
8729 in C99, a struct type is never variably modified because a VLA may not
8730 appear as a structure member. However, in GNU C code like:
8732 struct S { int i[f()]; };
8734 is valid, and other languages may define similar constructs. */
8736 bool
8737 variably_modified_type_p (tree type, tree fn)
8739 tree t;
8741 /* Test if T is either variable (if FN is zero) or an expression containing
8742 a variable in FN. If TYPE isn't gimplified, return true also if
8743 gimplify_one_sizepos would gimplify the expression into a local
8744 variable. */
8745 #define RETURN_TRUE_IF_VAR(T) \
8746 do { tree _t = (T); \
8747 if (_t != NULL_TREE \
8748 && _t != error_mark_node \
8749 && TREE_CODE (_t) != INTEGER_CST \
8750 && TREE_CODE (_t) != PLACEHOLDER_EXPR \
8751 && (!fn \
8752 || (!TYPE_SIZES_GIMPLIFIED (type) \
8753 && !is_gimple_sizepos (_t)) \
8754 || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
8755 return true; } while (0)
8757 if (type == error_mark_node)
8758 return false;
8760 /* If TYPE itself has variable size, it is variably modified. */
8761 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
8762 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
8764 switch (TREE_CODE (type))
8766 case POINTER_TYPE:
8767 case REFERENCE_TYPE:
8768 case VECTOR_TYPE:
8769 if (variably_modified_type_p (TREE_TYPE (type), fn))
8770 return true;
8771 break;
8773 case FUNCTION_TYPE:
8774 case METHOD_TYPE:
8775 /* If TYPE is a function type, it is variably modified if the
8776 return type is variably modified. */
8777 if (variably_modified_type_p (TREE_TYPE (type), fn))
8778 return true;
8779 break;
8781 case INTEGER_TYPE:
8782 case REAL_TYPE:
8783 case FIXED_POINT_TYPE:
8784 case ENUMERAL_TYPE:
8785 case BOOLEAN_TYPE:
8786 /* Scalar types are variably modified if their end points
8787 aren't constant. */
8788 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
8789 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
8790 break;
8792 case RECORD_TYPE:
8793 case UNION_TYPE:
8794 case QUAL_UNION_TYPE:
8795 /* We can't see if any of the fields are variably-modified by the
8796 definition we normally use, since that would produce infinite
8797 recursion via pointers. */
8798 /* This is variably modified if some field's type is. */
8799 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
8800 if (TREE_CODE (t) == FIELD_DECL)
8802 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
8803 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
8804 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
8806 if (TREE_CODE (type) == QUAL_UNION_TYPE)
8807 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
8809 break;
8811 case ARRAY_TYPE:
8812 /* Do not call ourselves to avoid infinite recursion. This is
8813 variably modified if the element type is. */
8814 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
8815 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
8816 break;
8818 default:
8819 break;
8822 /* The current language may have other cases to check, but in general,
8823 all other types are not variably modified. */
8824 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
8826 #undef RETURN_TRUE_IF_VAR
8829 /* Given a DECL or TYPE, return the scope in which it was declared, or
8830 NULL_TREE if there is no containing scope. */
8832 tree
8833 get_containing_scope (const_tree t)
8835 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
8838 /* Return the innermost context enclosing DECL that is
8839 a FUNCTION_DECL, or zero if none. */
8841 tree
8842 decl_function_context (const_tree decl)
8844 tree context;
8846 if (TREE_CODE (decl) == ERROR_MARK)
8847 return 0;
8849 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
8850 where we look up the function at runtime. Such functions always take
8851 a first argument of type 'pointer to real context'.
8853 C++ should really be fixed to use DECL_CONTEXT for the real context,
8854 and use something else for the "virtual context". */
8855 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
8856 context
8857 = TYPE_MAIN_VARIANT
8858 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
8859 else
8860 context = DECL_CONTEXT (decl);
8862 while (context && TREE_CODE (context) != FUNCTION_DECL)
8864 if (TREE_CODE (context) == BLOCK)
8865 context = BLOCK_SUPERCONTEXT (context);
8866 else
8867 context = get_containing_scope (context);
8870 return context;
8873 /* Return the innermost context enclosing DECL that is
8874 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
8875 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
8877 tree
8878 decl_type_context (const_tree decl)
8880 tree context = DECL_CONTEXT (decl);
8882 while (context)
8883 switch (TREE_CODE (context))
8885 case NAMESPACE_DECL:
8886 case TRANSLATION_UNIT_DECL:
8887 return NULL_TREE;
8889 case RECORD_TYPE:
8890 case UNION_TYPE:
8891 case QUAL_UNION_TYPE:
8892 return context;
8894 case TYPE_DECL:
8895 case FUNCTION_DECL:
8896 context = DECL_CONTEXT (context);
8897 break;
8899 case BLOCK:
8900 context = BLOCK_SUPERCONTEXT (context);
8901 break;
8903 default:
8904 gcc_unreachable ();
8907 return NULL_TREE;
8910 /* CALL is a CALL_EXPR. Return the declaration for the function
8911 called, or NULL_TREE if the called function cannot be
8912 determined. */
8914 tree
8915 get_callee_fndecl (const_tree call)
8917 tree addr;
8919 if (call == error_mark_node)
8920 return error_mark_node;
8922 /* It's invalid to call this function with anything but a
8923 CALL_EXPR. */
8924 gcc_assert (TREE_CODE (call) == CALL_EXPR);
8926 /* The first operand to the CALL is the address of the function
8927 called. */
8928 addr = CALL_EXPR_FN (call);
8930 /* If there is no function, return early. */
8931 if (addr == NULL_TREE)
8932 return NULL_TREE;
8934 STRIP_NOPS (addr);
8936 /* If this is a readonly function pointer, extract its initial value. */
8937 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
8938 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
8939 && DECL_INITIAL (addr))
8940 addr = DECL_INITIAL (addr);
8942 /* If the address is just `&f' for some function `f', then we know
8943 that `f' is being called. */
8944 if (TREE_CODE (addr) == ADDR_EXPR
8945 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
8946 return TREE_OPERAND (addr, 0);
8948 /* We couldn't figure out what was being called. */
8949 return NULL_TREE;
8952 /* Print debugging information about tree nodes generated during the compile,
8953 and any language-specific information. */
8955 void
8956 dump_tree_statistics (void)
8958 if (GATHER_STATISTICS)
8960 int i;
8961 int total_nodes, total_bytes;
8962 fprintf (stderr, "Kind Nodes Bytes\n");
8963 fprintf (stderr, "---------------------------------------\n");
8964 total_nodes = total_bytes = 0;
8965 for (i = 0; i < (int) all_kinds; i++)
8967 fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
8968 tree_node_counts[i], tree_node_sizes[i]);
8969 total_nodes += tree_node_counts[i];
8970 total_bytes += tree_node_sizes[i];
8972 fprintf (stderr, "---------------------------------------\n");
8973 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
8974 fprintf (stderr, "---------------------------------------\n");
8975 fprintf (stderr, "Code Nodes\n");
8976 fprintf (stderr, "----------------------------\n");
8977 for (i = 0; i < (int) MAX_TREE_CODES; i++)
8978 fprintf (stderr, "%-20s %7d\n", get_tree_code_name ((enum tree_code) i),
8979 tree_code_counts[i]);
8980 fprintf (stderr, "----------------------------\n");
8981 ssanames_print_statistics ();
8982 phinodes_print_statistics ();
8984 else
8985 fprintf (stderr, "(No per-node statistics)\n");
8987 print_type_hash_statistics ();
8988 print_debug_expr_statistics ();
8989 print_value_expr_statistics ();
8990 lang_hooks.print_statistics ();
8993 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
8995 /* Generate a crc32 of a byte. */
8997 static unsigned
8998 crc32_unsigned_bits (unsigned chksum, unsigned value, unsigned bits)
9000 unsigned ix;
9002 for (ix = bits; ix--; value <<= 1)
9004 unsigned feedback;
9006 feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
9007 chksum <<= 1;
9008 chksum ^= feedback;
9010 return chksum;
9013 /* Generate a crc32 of a 32-bit unsigned. */
9015 unsigned
9016 crc32_unsigned (unsigned chksum, unsigned value)
9018 return crc32_unsigned_bits (chksum, value, 32);
9021 /* Generate a crc32 of a byte. */
9023 unsigned
9024 crc32_byte (unsigned chksum, char byte)
9026 return crc32_unsigned_bits (chksum, (unsigned) byte << 24, 8);
9029 /* Generate a crc32 of a string. */
9031 unsigned
9032 crc32_string (unsigned chksum, const char *string)
9036 chksum = crc32_byte (chksum, *string);
9038 while (*string++);
9039 return chksum;
9042 /* P is a string that will be used in a symbol. Mask out any characters
9043 that are not valid in that context. */
9045 void
9046 clean_symbol_name (char *p)
9048 for (; *p; p++)
9049 if (! (ISALNUM (*p)
9050 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
9051 || *p == '$'
9052 #endif
9053 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
9054 || *p == '.'
9055 #endif
9057 *p = '_';
9060 /* Generate a name for a special-purpose function.
9061 The generated name may need to be unique across the whole link.
9062 Changes to this function may also require corresponding changes to
9063 xstrdup_mask_random.
9064 TYPE is some string to identify the purpose of this function to the
9065 linker or collect2; it must start with an uppercase letter,
9066 one of:
9067 I - for constructors
9068 D - for destructors
9069 N - for C++ anonymous namespaces
9070 F - for DWARF unwind frame information. */
9072 tree
9073 get_file_function_name (const char *type)
9075 char *buf;
9076 const char *p;
9077 char *q;
9079 /* If we already have a name we know to be unique, just use that. */
9080 if (first_global_object_name)
9081 p = q = ASTRDUP (first_global_object_name);
9082 /* If the target is handling the constructors/destructors, they
9083 will be local to this file and the name is only necessary for
9084 debugging purposes.
9085 We also assign sub_I and sub_D sufixes to constructors called from
9086 the global static constructors. These are always local. */
9087 else if (((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
9088 || (strncmp (type, "sub_", 4) == 0
9089 && (type[4] == 'I' || type[4] == 'D')))
9091 const char *file = main_input_filename;
9092 if (! file)
9093 file = LOCATION_FILE (input_location);
9094 /* Just use the file's basename, because the full pathname
9095 might be quite long. */
9096 p = q = ASTRDUP (lbasename (file));
9098 else
9100 /* Otherwise, the name must be unique across the entire link.
9101 We don't have anything that we know to be unique to this translation
9102 unit, so use what we do have and throw in some randomness. */
9103 unsigned len;
9104 const char *name = weak_global_object_name;
9105 const char *file = main_input_filename;
9107 if (! name)
9108 name = "";
9109 if (! file)
9110 file = LOCATION_FILE (input_location);
9112 len = strlen (file);
9113 q = (char *) alloca (9 + 17 + len + 1);
9114 memcpy (q, file, len + 1);
9116 snprintf (q + len, 9 + 17 + 1, "_%08X_" HOST_WIDE_INT_PRINT_HEX,
9117 crc32_string (0, name), get_random_seed (false));
9119 p = q;
9122 clean_symbol_name (q);
9123 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
9124 + strlen (type));
9126 /* Set up the name of the file-level functions we may need.
9127 Use a global object (which is already required to be unique over
9128 the program) rather than the file name (which imposes extra
9129 constraints). */
9130 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
9132 return get_identifier (buf);
9135 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
9137 /* Complain that the tree code of NODE does not match the expected 0
9138 terminated list of trailing codes. The trailing code list can be
9139 empty, for a more vague error message. FILE, LINE, and FUNCTION
9140 are of the caller. */
9142 void
9143 tree_check_failed (const_tree node, const char *file,
9144 int line, const char *function, ...)
9146 va_list args;
9147 const char *buffer;
9148 unsigned length = 0;
9149 enum tree_code code;
9151 va_start (args, function);
9152 while ((code = (enum tree_code) va_arg (args, int)))
9153 length += 4 + strlen (get_tree_code_name (code));
9154 va_end (args);
9155 if (length)
9157 char *tmp;
9158 va_start (args, function);
9159 length += strlen ("expected ");
9160 buffer = tmp = (char *) alloca (length);
9161 length = 0;
9162 while ((code = (enum tree_code) va_arg (args, int)))
9164 const char *prefix = length ? " or " : "expected ";
9166 strcpy (tmp + length, prefix);
9167 length += strlen (prefix);
9168 strcpy (tmp + length, get_tree_code_name (code));
9169 length += strlen (get_tree_code_name (code));
9171 va_end (args);
9173 else
9174 buffer = "unexpected node";
9176 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9177 buffer, get_tree_code_name (TREE_CODE (node)),
9178 function, trim_filename (file), line);
9181 /* Complain that the tree code of NODE does match the expected 0
9182 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
9183 the caller. */
9185 void
9186 tree_not_check_failed (const_tree node, const char *file,
9187 int line, const char *function, ...)
9189 va_list args;
9190 char *buffer;
9191 unsigned length = 0;
9192 enum tree_code code;
9194 va_start (args, function);
9195 while ((code = (enum tree_code) va_arg (args, int)))
9196 length += 4 + strlen (get_tree_code_name (code));
9197 va_end (args);
9198 va_start (args, function);
9199 buffer = (char *) alloca (length);
9200 length = 0;
9201 while ((code = (enum tree_code) va_arg (args, int)))
9203 if (length)
9205 strcpy (buffer + length, " or ");
9206 length += 4;
9208 strcpy (buffer + length, get_tree_code_name (code));
9209 length += strlen (get_tree_code_name (code));
9211 va_end (args);
9213 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
9214 buffer, get_tree_code_name (TREE_CODE (node)),
9215 function, trim_filename (file), line);
9218 /* Similar to tree_check_failed, except that we check for a class of tree
9219 code, given in CL. */
9221 void
9222 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
9223 const char *file, int line, const char *function)
9225 internal_error
9226 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
9227 TREE_CODE_CLASS_STRING (cl),
9228 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9229 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9232 /* Similar to tree_check_failed, except that instead of specifying a
9233 dozen codes, use the knowledge that they're all sequential. */
9235 void
9236 tree_range_check_failed (const_tree node, const char *file, int line,
9237 const char *function, enum tree_code c1,
9238 enum tree_code c2)
9240 char *buffer;
9241 unsigned length = 0;
9242 unsigned int c;
9244 for (c = c1; c <= c2; ++c)
9245 length += 4 + strlen (get_tree_code_name ((enum tree_code) c));
9247 length += strlen ("expected ");
9248 buffer = (char *) alloca (length);
9249 length = 0;
9251 for (c = c1; c <= c2; ++c)
9253 const char *prefix = length ? " or " : "expected ";
9255 strcpy (buffer + length, prefix);
9256 length += strlen (prefix);
9257 strcpy (buffer + length, get_tree_code_name ((enum tree_code) c));
9258 length += strlen (get_tree_code_name ((enum tree_code) c));
9261 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9262 buffer, get_tree_code_name (TREE_CODE (node)),
9263 function, trim_filename (file), line);
9267 /* Similar to tree_check_failed, except that we check that a tree does
9268 not have the specified code, given in CL. */
9270 void
9271 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
9272 const char *file, int line, const char *function)
9274 internal_error
9275 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
9276 TREE_CODE_CLASS_STRING (cl),
9277 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9278 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9282 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
9284 void
9285 omp_clause_check_failed (const_tree node, const char *file, int line,
9286 const char *function, enum omp_clause_code code)
9288 internal_error ("tree check: expected omp_clause %s, have %s in %s, at %s:%d",
9289 omp_clause_code_name[code], get_tree_code_name (TREE_CODE (node)),
9290 function, trim_filename (file), line);
9294 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
9296 void
9297 omp_clause_range_check_failed (const_tree node, const char *file, int line,
9298 const char *function, enum omp_clause_code c1,
9299 enum omp_clause_code c2)
9301 char *buffer;
9302 unsigned length = 0;
9303 unsigned int c;
9305 for (c = c1; c <= c2; ++c)
9306 length += 4 + strlen (omp_clause_code_name[c]);
9308 length += strlen ("expected ");
9309 buffer = (char *) alloca (length);
9310 length = 0;
9312 for (c = c1; c <= c2; ++c)
9314 const char *prefix = length ? " or " : "expected ";
9316 strcpy (buffer + length, prefix);
9317 length += strlen (prefix);
9318 strcpy (buffer + length, omp_clause_code_name[c]);
9319 length += strlen (omp_clause_code_name[c]);
9322 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9323 buffer, omp_clause_code_name[TREE_CODE (node)],
9324 function, trim_filename (file), line);
9328 #undef DEFTREESTRUCT
9329 #define DEFTREESTRUCT(VAL, NAME) NAME,
9331 static const char *ts_enum_names[] = {
9332 #include "treestruct.def"
9334 #undef DEFTREESTRUCT
9336 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
9338 /* Similar to tree_class_check_failed, except that we check for
9339 whether CODE contains the tree structure identified by EN. */
9341 void
9342 tree_contains_struct_check_failed (const_tree node,
9343 const enum tree_node_structure_enum en,
9344 const char *file, int line,
9345 const char *function)
9347 internal_error
9348 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
9349 TS_ENUM_NAME (en),
9350 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9354 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9355 (dynamically sized) vector. */
9357 void
9358 tree_int_cst_elt_check_failed (int idx, int len, const char *file, int line,
9359 const char *function)
9361 internal_error
9362 ("tree check: accessed elt %d of tree_int_cst with %d elts in %s, at %s:%d",
9363 idx + 1, len, function, trim_filename (file), line);
9366 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9367 (dynamically sized) vector. */
9369 void
9370 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
9371 const char *function)
9373 internal_error
9374 ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
9375 idx + 1, len, function, trim_filename (file), line);
9378 /* Similar to above, except that the check is for the bounds of the operand
9379 vector of an expression node EXP. */
9381 void
9382 tree_operand_check_failed (int idx, const_tree exp, const char *file,
9383 int line, const char *function)
9385 enum tree_code code = TREE_CODE (exp);
9386 internal_error
9387 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
9388 idx + 1, get_tree_code_name (code), TREE_OPERAND_LENGTH (exp),
9389 function, trim_filename (file), line);
9392 /* Similar to above, except that the check is for the number of
9393 operands of an OMP_CLAUSE node. */
9395 void
9396 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
9397 int line, const char *function)
9399 internal_error
9400 ("tree check: accessed operand %d of omp_clause %s with %d operands "
9401 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
9402 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
9403 trim_filename (file), line);
9405 #endif /* ENABLE_TREE_CHECKING */
9407 /* Create a new vector type node holding SUBPARTS units of type INNERTYPE,
9408 and mapped to the machine mode MODE. Initialize its fields and build
9409 the information necessary for debugging output. */
9411 static tree
9412 make_vector_type (tree innertype, int nunits, enum machine_mode mode)
9414 tree t;
9415 inchash::hash hstate;
9417 t = make_node (VECTOR_TYPE);
9418 TREE_TYPE (t) = TYPE_MAIN_VARIANT (innertype);
9419 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
9420 SET_TYPE_MODE (t, mode);
9422 if (TYPE_STRUCTURAL_EQUALITY_P (innertype))
9423 SET_TYPE_STRUCTURAL_EQUALITY (t);
9424 else if (TYPE_CANONICAL (innertype) != innertype
9425 || mode != VOIDmode)
9426 TYPE_CANONICAL (t)
9427 = make_vector_type (TYPE_CANONICAL (innertype), nunits, VOIDmode);
9429 layout_type (t);
9431 hstate.add_wide_int (VECTOR_TYPE);
9432 hstate.add_wide_int (nunits);
9433 hstate.add_wide_int (mode);
9434 hstate.add_object (TYPE_HASH (TREE_TYPE (t)));
9435 t = type_hash_canon (hstate.end (), t);
9437 /* We have built a main variant, based on the main variant of the
9438 inner type. Use it to build the variant we return. */
9439 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
9440 && TREE_TYPE (t) != innertype)
9441 return build_type_attribute_qual_variant (t,
9442 TYPE_ATTRIBUTES (innertype),
9443 TYPE_QUALS (innertype));
9445 return t;
9448 static tree
9449 make_or_reuse_type (unsigned size, int unsignedp)
9451 int i;
9453 if (size == INT_TYPE_SIZE)
9454 return unsignedp ? unsigned_type_node : integer_type_node;
9455 if (size == CHAR_TYPE_SIZE)
9456 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
9457 if (size == SHORT_TYPE_SIZE)
9458 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
9459 if (size == LONG_TYPE_SIZE)
9460 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
9461 if (size == LONG_LONG_TYPE_SIZE)
9462 return (unsignedp ? long_long_unsigned_type_node
9463 : long_long_integer_type_node);
9465 for (i = 0; i < NUM_INT_N_ENTS; i ++)
9466 if (size == int_n_data[i].bitsize
9467 && int_n_enabled_p[i])
9468 return (unsignedp ? int_n_trees[i].unsigned_type
9469 : int_n_trees[i].signed_type);
9471 if (unsignedp)
9472 return make_unsigned_type (size);
9473 else
9474 return make_signed_type (size);
9477 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
9479 static tree
9480 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
9482 if (satp)
9484 if (size == SHORT_FRACT_TYPE_SIZE)
9485 return unsignedp ? sat_unsigned_short_fract_type_node
9486 : sat_short_fract_type_node;
9487 if (size == FRACT_TYPE_SIZE)
9488 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
9489 if (size == LONG_FRACT_TYPE_SIZE)
9490 return unsignedp ? sat_unsigned_long_fract_type_node
9491 : sat_long_fract_type_node;
9492 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9493 return unsignedp ? sat_unsigned_long_long_fract_type_node
9494 : sat_long_long_fract_type_node;
9496 else
9498 if (size == SHORT_FRACT_TYPE_SIZE)
9499 return unsignedp ? unsigned_short_fract_type_node
9500 : short_fract_type_node;
9501 if (size == FRACT_TYPE_SIZE)
9502 return unsignedp ? unsigned_fract_type_node : fract_type_node;
9503 if (size == LONG_FRACT_TYPE_SIZE)
9504 return unsignedp ? unsigned_long_fract_type_node
9505 : long_fract_type_node;
9506 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9507 return unsignedp ? unsigned_long_long_fract_type_node
9508 : long_long_fract_type_node;
9511 return make_fract_type (size, unsignedp, satp);
9514 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
9516 static tree
9517 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
9519 if (satp)
9521 if (size == SHORT_ACCUM_TYPE_SIZE)
9522 return unsignedp ? sat_unsigned_short_accum_type_node
9523 : sat_short_accum_type_node;
9524 if (size == ACCUM_TYPE_SIZE)
9525 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
9526 if (size == LONG_ACCUM_TYPE_SIZE)
9527 return unsignedp ? sat_unsigned_long_accum_type_node
9528 : sat_long_accum_type_node;
9529 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9530 return unsignedp ? sat_unsigned_long_long_accum_type_node
9531 : sat_long_long_accum_type_node;
9533 else
9535 if (size == SHORT_ACCUM_TYPE_SIZE)
9536 return unsignedp ? unsigned_short_accum_type_node
9537 : short_accum_type_node;
9538 if (size == ACCUM_TYPE_SIZE)
9539 return unsignedp ? unsigned_accum_type_node : accum_type_node;
9540 if (size == LONG_ACCUM_TYPE_SIZE)
9541 return unsignedp ? unsigned_long_accum_type_node
9542 : long_accum_type_node;
9543 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9544 return unsignedp ? unsigned_long_long_accum_type_node
9545 : long_long_accum_type_node;
9548 return make_accum_type (size, unsignedp, satp);
9552 /* Create an atomic variant node for TYPE. This routine is called
9553 during initialization of data types to create the 5 basic atomic
9554 types. The generic build_variant_type function requires these to
9555 already be set up in order to function properly, so cannot be
9556 called from there. If ALIGN is non-zero, then ensure alignment is
9557 overridden to this value. */
9559 static tree
9560 build_atomic_base (tree type, unsigned int align)
9562 tree t;
9564 /* Make sure its not already registered. */
9565 if ((t = get_qualified_type (type, TYPE_QUAL_ATOMIC)))
9566 return t;
9568 t = build_variant_type_copy (type);
9569 set_type_quals (t, TYPE_QUAL_ATOMIC);
9571 if (align)
9572 TYPE_ALIGN (t) = align;
9574 return t;
9577 /* Create nodes for all integer types (and error_mark_node) using the sizes
9578 of C datatypes. SIGNED_CHAR specifies whether char is signed,
9579 SHORT_DOUBLE specifies whether double should be of the same precision
9580 as float. */
9582 void
9583 build_common_tree_nodes (bool signed_char, bool short_double)
9585 int i;
9587 error_mark_node = make_node (ERROR_MARK);
9588 TREE_TYPE (error_mark_node) = error_mark_node;
9590 initialize_sizetypes ();
9592 /* Define both `signed char' and `unsigned char'. */
9593 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
9594 TYPE_STRING_FLAG (signed_char_type_node) = 1;
9595 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
9596 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
9598 /* Define `char', which is like either `signed char' or `unsigned char'
9599 but not the same as either. */
9600 char_type_node
9601 = (signed_char
9602 ? make_signed_type (CHAR_TYPE_SIZE)
9603 : make_unsigned_type (CHAR_TYPE_SIZE));
9604 TYPE_STRING_FLAG (char_type_node) = 1;
9606 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
9607 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
9608 integer_type_node = make_signed_type (INT_TYPE_SIZE);
9609 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
9610 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
9611 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
9612 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
9613 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
9615 for (i = 0; i < NUM_INT_N_ENTS; i ++)
9617 int_n_trees[i].signed_type = make_signed_type (int_n_data[i].bitsize);
9618 int_n_trees[i].unsigned_type = make_unsigned_type (int_n_data[i].bitsize);
9619 TYPE_SIZE (int_n_trees[i].signed_type) = bitsize_int (int_n_data[i].bitsize);
9620 TYPE_SIZE (int_n_trees[i].unsigned_type) = bitsize_int (int_n_data[i].bitsize);
9622 if (int_n_data[i].bitsize > LONG_LONG_TYPE_SIZE)
9624 integer_types[itk_intN_0 + i * 2] = int_n_trees[i].signed_type;
9625 integer_types[itk_unsigned_intN_0 + i * 2] = int_n_trees[i].unsigned_type;
9629 /* Define a boolean type. This type only represents boolean values but
9630 may be larger than char depending on the value of BOOL_TYPE_SIZE. */
9631 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
9632 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
9633 TYPE_PRECISION (boolean_type_node) = 1;
9634 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
9636 /* Define what type to use for size_t. */
9637 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
9638 size_type_node = unsigned_type_node;
9639 else if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
9640 size_type_node = long_unsigned_type_node;
9641 else if (strcmp (SIZE_TYPE, "long long unsigned int") == 0)
9642 size_type_node = long_long_unsigned_type_node;
9643 else if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
9644 size_type_node = short_unsigned_type_node;
9645 else
9647 int i;
9649 size_type_node = NULL_TREE;
9650 for (i = 0; i < NUM_INT_N_ENTS; i++)
9651 if (int_n_enabled_p[i])
9653 char name[50];
9654 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
9656 if (strcmp (name, SIZE_TYPE) == 0)
9658 size_type_node = int_n_trees[i].unsigned_type;
9661 if (size_type_node == NULL_TREE)
9662 gcc_unreachable ();
9665 /* Fill in the rest of the sized types. Reuse existing type nodes
9666 when possible. */
9667 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
9668 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
9669 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
9670 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
9671 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
9673 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
9674 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
9675 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
9676 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
9677 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
9679 /* Don't call build_qualified type for atomics. That routine does
9680 special processing for atomics, and until they are initialized
9681 it's better not to make that call.
9683 Check to see if there is a target override for atomic types. */
9685 atomicQI_type_node = build_atomic_base (unsigned_intQI_type_node,
9686 targetm.atomic_align_for_mode (QImode));
9687 atomicHI_type_node = build_atomic_base (unsigned_intHI_type_node,
9688 targetm.atomic_align_for_mode (HImode));
9689 atomicSI_type_node = build_atomic_base (unsigned_intSI_type_node,
9690 targetm.atomic_align_for_mode (SImode));
9691 atomicDI_type_node = build_atomic_base (unsigned_intDI_type_node,
9692 targetm.atomic_align_for_mode (DImode));
9693 atomicTI_type_node = build_atomic_base (unsigned_intTI_type_node,
9694 targetm.atomic_align_for_mode (TImode));
9696 access_public_node = get_identifier ("public");
9697 access_protected_node = get_identifier ("protected");
9698 access_private_node = get_identifier ("private");
9700 /* Define these next since types below may used them. */
9701 integer_zero_node = build_int_cst (integer_type_node, 0);
9702 integer_one_node = build_int_cst (integer_type_node, 1);
9703 integer_three_node = build_int_cst (integer_type_node, 3);
9704 integer_minus_one_node = build_int_cst (integer_type_node, -1);
9706 size_zero_node = size_int (0);
9707 size_one_node = size_int (1);
9708 bitsize_zero_node = bitsize_int (0);
9709 bitsize_one_node = bitsize_int (1);
9710 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
9712 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
9713 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
9715 void_type_node = make_node (VOID_TYPE);
9716 layout_type (void_type_node);
9718 /* We are not going to have real types in C with less than byte alignment,
9719 so we might as well not have any types that claim to have it. */
9720 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
9721 TYPE_USER_ALIGN (void_type_node) = 0;
9723 void_node = make_node (VOID_CST);
9724 TREE_TYPE (void_node) = void_type_node;
9726 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
9727 layout_type (TREE_TYPE (null_pointer_node));
9729 ptr_type_node = build_pointer_type (void_type_node);
9730 const_ptr_type_node
9731 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
9732 fileptr_type_node = ptr_type_node;
9734 pointer_sized_int_node = build_nonstandard_integer_type (POINTER_SIZE, 1);
9736 float_type_node = make_node (REAL_TYPE);
9737 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
9738 layout_type (float_type_node);
9740 double_type_node = make_node (REAL_TYPE);
9741 if (short_double)
9742 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
9743 else
9744 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
9745 layout_type (double_type_node);
9747 long_double_type_node = make_node (REAL_TYPE);
9748 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
9749 layout_type (long_double_type_node);
9751 float_ptr_type_node = build_pointer_type (float_type_node);
9752 double_ptr_type_node = build_pointer_type (double_type_node);
9753 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
9754 integer_ptr_type_node = build_pointer_type (integer_type_node);
9756 /* Fixed size integer types. */
9757 uint16_type_node = make_or_reuse_type (16, 1);
9758 uint32_type_node = make_or_reuse_type (32, 1);
9759 uint64_type_node = make_or_reuse_type (64, 1);
9761 /* Decimal float types. */
9762 dfloat32_type_node = make_node (REAL_TYPE);
9763 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
9764 layout_type (dfloat32_type_node);
9765 SET_TYPE_MODE (dfloat32_type_node, SDmode);
9766 dfloat32_ptr_type_node = build_pointer_type (dfloat32_type_node);
9768 dfloat64_type_node = make_node (REAL_TYPE);
9769 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
9770 layout_type (dfloat64_type_node);
9771 SET_TYPE_MODE (dfloat64_type_node, DDmode);
9772 dfloat64_ptr_type_node = build_pointer_type (dfloat64_type_node);
9774 dfloat128_type_node = make_node (REAL_TYPE);
9775 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
9776 layout_type (dfloat128_type_node);
9777 SET_TYPE_MODE (dfloat128_type_node, TDmode);
9778 dfloat128_ptr_type_node = build_pointer_type (dfloat128_type_node);
9780 complex_integer_type_node = build_complex_type (integer_type_node);
9781 complex_float_type_node = build_complex_type (float_type_node);
9782 complex_double_type_node = build_complex_type (double_type_node);
9783 complex_long_double_type_node = build_complex_type (long_double_type_node);
9785 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
9786 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
9787 sat_ ## KIND ## _type_node = \
9788 make_sat_signed_ ## KIND ## _type (SIZE); \
9789 sat_unsigned_ ## KIND ## _type_node = \
9790 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9791 KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9792 unsigned_ ## KIND ## _type_node = \
9793 make_unsigned_ ## KIND ## _type (SIZE);
9795 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
9796 sat_ ## WIDTH ## KIND ## _type_node = \
9797 make_sat_signed_ ## KIND ## _type (SIZE); \
9798 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
9799 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9800 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9801 unsigned_ ## WIDTH ## KIND ## _type_node = \
9802 make_unsigned_ ## KIND ## _type (SIZE);
9804 /* Make fixed-point type nodes based on four different widths. */
9805 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
9806 MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
9807 MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
9808 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
9809 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
9811 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
9812 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
9813 NAME ## _type_node = \
9814 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
9815 u ## NAME ## _type_node = \
9816 make_or_reuse_unsigned_ ## KIND ## _type \
9817 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
9818 sat_ ## NAME ## _type_node = \
9819 make_or_reuse_sat_signed_ ## KIND ## _type \
9820 (GET_MODE_BITSIZE (MODE ## mode)); \
9821 sat_u ## NAME ## _type_node = \
9822 make_or_reuse_sat_unsigned_ ## KIND ## _type \
9823 (GET_MODE_BITSIZE (U ## MODE ## mode));
9825 /* Fixed-point type and mode nodes. */
9826 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
9827 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
9828 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
9829 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
9830 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
9831 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
9832 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
9833 MAKE_FIXED_MODE_NODE (accum, ha, HA)
9834 MAKE_FIXED_MODE_NODE (accum, sa, SA)
9835 MAKE_FIXED_MODE_NODE (accum, da, DA)
9836 MAKE_FIXED_MODE_NODE (accum, ta, TA)
9839 tree t = targetm.build_builtin_va_list ();
9841 /* Many back-ends define record types without setting TYPE_NAME.
9842 If we copied the record type here, we'd keep the original
9843 record type without a name. This breaks name mangling. So,
9844 don't copy record types and let c_common_nodes_and_builtins()
9845 declare the type to be __builtin_va_list. */
9846 if (TREE_CODE (t) != RECORD_TYPE)
9847 t = build_variant_type_copy (t);
9849 va_list_type_node = t;
9853 /* Modify DECL for given flags.
9854 TM_PURE attribute is set only on types, so the function will modify
9855 DECL's type when ECF_TM_PURE is used. */
9857 void
9858 set_call_expr_flags (tree decl, int flags)
9860 if (flags & ECF_NOTHROW)
9861 TREE_NOTHROW (decl) = 1;
9862 if (flags & ECF_CONST)
9863 TREE_READONLY (decl) = 1;
9864 if (flags & ECF_PURE)
9865 DECL_PURE_P (decl) = 1;
9866 if (flags & ECF_LOOPING_CONST_OR_PURE)
9867 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
9868 if (flags & ECF_NOVOPS)
9869 DECL_IS_NOVOPS (decl) = 1;
9870 if (flags & ECF_NORETURN)
9871 TREE_THIS_VOLATILE (decl) = 1;
9872 if (flags & ECF_MALLOC)
9873 DECL_IS_MALLOC (decl) = 1;
9874 if (flags & ECF_RETURNS_TWICE)
9875 DECL_IS_RETURNS_TWICE (decl) = 1;
9876 if (flags & ECF_LEAF)
9877 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("leaf"),
9878 NULL, DECL_ATTRIBUTES (decl));
9879 if ((flags & ECF_TM_PURE) && flag_tm)
9880 apply_tm_attr (decl, get_identifier ("transaction_pure"));
9881 /* Looping const or pure is implied by noreturn.
9882 There is currently no way to declare looping const or looping pure alone. */
9883 gcc_assert (!(flags & ECF_LOOPING_CONST_OR_PURE)
9884 || ((flags & ECF_NORETURN) && (flags & (ECF_CONST | ECF_PURE))));
9888 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
9890 static void
9891 local_define_builtin (const char *name, tree type, enum built_in_function code,
9892 const char *library_name, int ecf_flags)
9894 tree decl;
9896 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
9897 library_name, NULL_TREE);
9898 set_call_expr_flags (decl, ecf_flags);
9900 set_builtin_decl (code, decl, true);
9903 /* Call this function after instantiating all builtins that the language
9904 front end cares about. This will build the rest of the builtins
9905 and internal functions that are relied upon by the tree optimizers and
9906 the middle-end. */
9908 void
9909 build_common_builtin_nodes (void)
9911 tree tmp, ftype;
9912 int ecf_flags;
9914 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE))
9916 ftype = build_function_type (void_type_node, void_list_node);
9917 local_define_builtin ("__builtin_unreachable", ftype, BUILT_IN_UNREACHABLE,
9918 "__builtin_unreachable",
9919 ECF_NOTHROW | ECF_LEAF | ECF_NORETURN
9920 | ECF_CONST | ECF_LEAF);
9923 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY)
9924 || !builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9926 ftype = build_function_type_list (ptr_type_node,
9927 ptr_type_node, const_ptr_type_node,
9928 size_type_node, NULL_TREE);
9930 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY))
9931 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
9932 "memcpy", ECF_NOTHROW | ECF_LEAF);
9933 if (!builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9934 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
9935 "memmove", ECF_NOTHROW | ECF_LEAF);
9938 if (!builtin_decl_explicit_p (BUILT_IN_MEMCMP))
9940 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
9941 const_ptr_type_node, size_type_node,
9942 NULL_TREE);
9943 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
9944 "memcmp", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9947 if (!builtin_decl_explicit_p (BUILT_IN_MEMSET))
9949 ftype = build_function_type_list (ptr_type_node,
9950 ptr_type_node, integer_type_node,
9951 size_type_node, NULL_TREE);
9952 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
9953 "memset", ECF_NOTHROW | ECF_LEAF);
9956 if (!builtin_decl_explicit_p (BUILT_IN_ALLOCA))
9958 ftype = build_function_type_list (ptr_type_node,
9959 size_type_node, NULL_TREE);
9960 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
9961 "alloca", ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
9964 ftype = build_function_type_list (ptr_type_node, size_type_node,
9965 size_type_node, NULL_TREE);
9966 local_define_builtin ("__builtin_alloca_with_align", ftype,
9967 BUILT_IN_ALLOCA_WITH_ALIGN, "alloca",
9968 ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
9970 /* If we're checking the stack, `alloca' can throw. */
9971 if (flag_stack_check)
9973 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA)) = 0;
9974 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN)) = 0;
9977 ftype = build_function_type_list (void_type_node,
9978 ptr_type_node, ptr_type_node,
9979 ptr_type_node, NULL_TREE);
9980 local_define_builtin ("__builtin_init_trampoline", ftype,
9981 BUILT_IN_INIT_TRAMPOLINE,
9982 "__builtin_init_trampoline", ECF_NOTHROW | ECF_LEAF);
9983 local_define_builtin ("__builtin_init_heap_trampoline", ftype,
9984 BUILT_IN_INIT_HEAP_TRAMPOLINE,
9985 "__builtin_init_heap_trampoline",
9986 ECF_NOTHROW | ECF_LEAF);
9988 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
9989 local_define_builtin ("__builtin_adjust_trampoline", ftype,
9990 BUILT_IN_ADJUST_TRAMPOLINE,
9991 "__builtin_adjust_trampoline",
9992 ECF_CONST | ECF_NOTHROW);
9994 ftype = build_function_type_list (void_type_node,
9995 ptr_type_node, ptr_type_node, NULL_TREE);
9996 local_define_builtin ("__builtin_nonlocal_goto", ftype,
9997 BUILT_IN_NONLOCAL_GOTO,
9998 "__builtin_nonlocal_goto",
9999 ECF_NORETURN | ECF_NOTHROW);
10001 ftype = build_function_type_list (void_type_node,
10002 ptr_type_node, ptr_type_node, NULL_TREE);
10003 local_define_builtin ("__builtin_setjmp_setup", ftype,
10004 BUILT_IN_SETJMP_SETUP,
10005 "__builtin_setjmp_setup", ECF_NOTHROW);
10007 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10008 local_define_builtin ("__builtin_setjmp_receiver", ftype,
10009 BUILT_IN_SETJMP_RECEIVER,
10010 "__builtin_setjmp_receiver", ECF_NOTHROW | ECF_LEAF);
10012 ftype = build_function_type_list (ptr_type_node, NULL_TREE);
10013 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
10014 "__builtin_stack_save", ECF_NOTHROW | ECF_LEAF);
10016 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10017 local_define_builtin ("__builtin_stack_restore", ftype,
10018 BUILT_IN_STACK_RESTORE,
10019 "__builtin_stack_restore", ECF_NOTHROW | ECF_LEAF);
10021 /* If there's a possibility that we might use the ARM EABI, build the
10022 alternate __cxa_end_cleanup node used to resume from C++ and Java. */
10023 if (targetm.arm_eabi_unwinder)
10025 ftype = build_function_type_list (void_type_node, NULL_TREE);
10026 local_define_builtin ("__builtin_cxa_end_cleanup", ftype,
10027 BUILT_IN_CXA_END_CLEANUP,
10028 "__cxa_end_cleanup", ECF_NORETURN | ECF_LEAF);
10031 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10032 local_define_builtin ("__builtin_unwind_resume", ftype,
10033 BUILT_IN_UNWIND_RESUME,
10034 ((targetm_common.except_unwind_info (&global_options)
10035 == UI_SJLJ)
10036 ? "_Unwind_SjLj_Resume" : "_Unwind_Resume"),
10037 ECF_NORETURN);
10039 if (builtin_decl_explicit (BUILT_IN_RETURN_ADDRESS) == NULL_TREE)
10041 ftype = build_function_type_list (ptr_type_node, integer_type_node,
10042 NULL_TREE);
10043 local_define_builtin ("__builtin_return_address", ftype,
10044 BUILT_IN_RETURN_ADDRESS,
10045 "__builtin_return_address",
10046 ECF_NOTHROW);
10049 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER)
10050 || !builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
10052 ftype = build_function_type_list (void_type_node, ptr_type_node,
10053 ptr_type_node, NULL_TREE);
10054 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER))
10055 local_define_builtin ("__cyg_profile_func_enter", ftype,
10056 BUILT_IN_PROFILE_FUNC_ENTER,
10057 "__cyg_profile_func_enter", 0);
10058 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
10059 local_define_builtin ("__cyg_profile_func_exit", ftype,
10060 BUILT_IN_PROFILE_FUNC_EXIT,
10061 "__cyg_profile_func_exit", 0);
10064 /* The exception object and filter values from the runtime. The argument
10065 must be zero before exception lowering, i.e. from the front end. After
10066 exception lowering, it will be the region number for the exception
10067 landing pad. These functions are PURE instead of CONST to prevent
10068 them from being hoisted past the exception edge that will initialize
10069 its value in the landing pad. */
10070 ftype = build_function_type_list (ptr_type_node,
10071 integer_type_node, NULL_TREE);
10072 ecf_flags = ECF_PURE | ECF_NOTHROW | ECF_LEAF;
10073 /* Only use TM_PURE if we we have TM language support. */
10074 if (builtin_decl_explicit_p (BUILT_IN_TM_LOAD_1))
10075 ecf_flags |= ECF_TM_PURE;
10076 local_define_builtin ("__builtin_eh_pointer", ftype, BUILT_IN_EH_POINTER,
10077 "__builtin_eh_pointer", ecf_flags);
10079 tmp = lang_hooks.types.type_for_mode (targetm.eh_return_filter_mode (), 0);
10080 ftype = build_function_type_list (tmp, integer_type_node, NULL_TREE);
10081 local_define_builtin ("__builtin_eh_filter", ftype, BUILT_IN_EH_FILTER,
10082 "__builtin_eh_filter", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10084 ftype = build_function_type_list (void_type_node,
10085 integer_type_node, integer_type_node,
10086 NULL_TREE);
10087 local_define_builtin ("__builtin_eh_copy_values", ftype,
10088 BUILT_IN_EH_COPY_VALUES,
10089 "__builtin_eh_copy_values", ECF_NOTHROW);
10091 /* Complex multiplication and division. These are handled as builtins
10092 rather than optabs because emit_library_call_value doesn't support
10093 complex. Further, we can do slightly better with folding these
10094 beasties if the real and complex parts of the arguments are separate. */
10096 int mode;
10098 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
10100 char mode_name_buf[4], *q;
10101 const char *p;
10102 enum built_in_function mcode, dcode;
10103 tree type, inner_type;
10104 const char *prefix = "__";
10106 if (targetm.libfunc_gnu_prefix)
10107 prefix = "__gnu_";
10109 type = lang_hooks.types.type_for_mode ((enum machine_mode) mode, 0);
10110 if (type == NULL)
10111 continue;
10112 inner_type = TREE_TYPE (type);
10114 ftype = build_function_type_list (type, inner_type, inner_type,
10115 inner_type, inner_type, NULL_TREE);
10117 mcode = ((enum built_in_function)
10118 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10119 dcode = ((enum built_in_function)
10120 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10122 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
10123 *q = TOLOWER (*p);
10124 *q = '\0';
10126 built_in_names[mcode] = concat (prefix, "mul", mode_name_buf, "3",
10127 NULL);
10128 local_define_builtin (built_in_names[mcode], ftype, mcode,
10129 built_in_names[mcode],
10130 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
10132 built_in_names[dcode] = concat (prefix, "div", mode_name_buf, "3",
10133 NULL);
10134 local_define_builtin (built_in_names[dcode], ftype, dcode,
10135 built_in_names[dcode],
10136 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
10140 init_internal_fns ();
10143 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
10144 better way.
10146 If we requested a pointer to a vector, build up the pointers that
10147 we stripped off while looking for the inner type. Similarly for
10148 return values from functions.
10150 The argument TYPE is the top of the chain, and BOTTOM is the
10151 new type which we will point to. */
10153 tree
10154 reconstruct_complex_type (tree type, tree bottom)
10156 tree inner, outer;
10158 if (TREE_CODE (type) == POINTER_TYPE)
10160 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10161 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
10162 TYPE_REF_CAN_ALIAS_ALL (type));
10164 else if (TREE_CODE (type) == REFERENCE_TYPE)
10166 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10167 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
10168 TYPE_REF_CAN_ALIAS_ALL (type));
10170 else if (TREE_CODE (type) == ARRAY_TYPE)
10172 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10173 outer = build_array_type (inner, TYPE_DOMAIN (type));
10175 else if (TREE_CODE (type) == FUNCTION_TYPE)
10177 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10178 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
10180 else if (TREE_CODE (type) == METHOD_TYPE)
10182 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10183 /* The build_method_type_directly() routine prepends 'this' to argument list,
10184 so we must compensate by getting rid of it. */
10185 outer
10186 = build_method_type_directly
10187 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
10188 inner,
10189 TREE_CHAIN (TYPE_ARG_TYPES (type)));
10191 else if (TREE_CODE (type) == OFFSET_TYPE)
10193 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10194 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
10196 else
10197 return bottom;
10199 return build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
10200 TYPE_QUALS (type));
10203 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
10204 the inner type. */
10205 tree
10206 build_vector_type_for_mode (tree innertype, enum machine_mode mode)
10208 int nunits;
10210 switch (GET_MODE_CLASS (mode))
10212 case MODE_VECTOR_INT:
10213 case MODE_VECTOR_FLOAT:
10214 case MODE_VECTOR_FRACT:
10215 case MODE_VECTOR_UFRACT:
10216 case MODE_VECTOR_ACCUM:
10217 case MODE_VECTOR_UACCUM:
10218 nunits = GET_MODE_NUNITS (mode);
10219 break;
10221 case MODE_INT:
10222 /* Check that there are no leftover bits. */
10223 gcc_assert (GET_MODE_BITSIZE (mode)
10224 % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
10226 nunits = GET_MODE_BITSIZE (mode)
10227 / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
10228 break;
10230 default:
10231 gcc_unreachable ();
10234 return make_vector_type (innertype, nunits, mode);
10237 /* Similarly, but takes the inner type and number of units, which must be
10238 a power of two. */
10240 tree
10241 build_vector_type (tree innertype, int nunits)
10243 return make_vector_type (innertype, nunits, VOIDmode);
10246 /* Similarly, but builds a variant type with TYPE_VECTOR_OPAQUE set. */
10248 tree
10249 build_opaque_vector_type (tree innertype, int nunits)
10251 tree t = make_vector_type (innertype, nunits, VOIDmode);
10252 tree cand;
10253 /* We always build the non-opaque variant before the opaque one,
10254 so if it already exists, it is TYPE_NEXT_VARIANT of this one. */
10255 cand = TYPE_NEXT_VARIANT (t);
10256 if (cand
10257 && TYPE_VECTOR_OPAQUE (cand)
10258 && check_qualified_type (cand, t, TYPE_QUALS (t)))
10259 return cand;
10260 /* Othewise build a variant type and make sure to queue it after
10261 the non-opaque type. */
10262 cand = build_distinct_type_copy (t);
10263 TYPE_VECTOR_OPAQUE (cand) = true;
10264 TYPE_CANONICAL (cand) = TYPE_CANONICAL (t);
10265 TYPE_NEXT_VARIANT (cand) = TYPE_NEXT_VARIANT (t);
10266 TYPE_NEXT_VARIANT (t) = cand;
10267 TYPE_MAIN_VARIANT (cand) = TYPE_MAIN_VARIANT (t);
10268 return cand;
10272 /* Given an initializer INIT, return TRUE if INIT is zero or some
10273 aggregate of zeros. Otherwise return FALSE. */
10274 bool
10275 initializer_zerop (const_tree init)
10277 tree elt;
10279 STRIP_NOPS (init);
10281 switch (TREE_CODE (init))
10283 case INTEGER_CST:
10284 return integer_zerop (init);
10286 case REAL_CST:
10287 /* ??? Note that this is not correct for C4X float formats. There,
10288 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
10289 negative exponent. */
10290 return real_zerop (init)
10291 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
10293 case FIXED_CST:
10294 return fixed_zerop (init);
10296 case COMPLEX_CST:
10297 return integer_zerop (init)
10298 || (real_zerop (init)
10299 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
10300 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
10302 case VECTOR_CST:
10304 unsigned i;
10305 for (i = 0; i < VECTOR_CST_NELTS (init); ++i)
10306 if (!initializer_zerop (VECTOR_CST_ELT (init, i)))
10307 return false;
10308 return true;
10311 case CONSTRUCTOR:
10313 unsigned HOST_WIDE_INT idx;
10315 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
10316 if (!initializer_zerop (elt))
10317 return false;
10318 return true;
10321 case STRING_CST:
10323 int i;
10325 /* We need to loop through all elements to handle cases like
10326 "\0" and "\0foobar". */
10327 for (i = 0; i < TREE_STRING_LENGTH (init); ++i)
10328 if (TREE_STRING_POINTER (init)[i] != '\0')
10329 return false;
10331 return true;
10334 default:
10335 return false;
10339 /* Check if vector VEC consists of all the equal elements and
10340 that the number of elements corresponds to the type of VEC.
10341 The function returns first element of the vector
10342 or NULL_TREE if the vector is not uniform. */
10343 tree
10344 uniform_vector_p (const_tree vec)
10346 tree first, t;
10347 unsigned i;
10349 if (vec == NULL_TREE)
10350 return NULL_TREE;
10352 gcc_assert (VECTOR_TYPE_P (TREE_TYPE (vec)));
10354 if (TREE_CODE (vec) == VECTOR_CST)
10356 first = VECTOR_CST_ELT (vec, 0);
10357 for (i = 1; i < VECTOR_CST_NELTS (vec); ++i)
10358 if (!operand_equal_p (first, VECTOR_CST_ELT (vec, i), 0))
10359 return NULL_TREE;
10361 return first;
10364 else if (TREE_CODE (vec) == CONSTRUCTOR)
10366 first = error_mark_node;
10368 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (vec), i, t)
10370 if (i == 0)
10372 first = t;
10373 continue;
10375 if (!operand_equal_p (first, t, 0))
10376 return NULL_TREE;
10378 if (i != TYPE_VECTOR_SUBPARTS (TREE_TYPE (vec)))
10379 return NULL_TREE;
10381 return first;
10384 return NULL_TREE;
10387 /* Build an empty statement at location LOC. */
10389 tree
10390 build_empty_stmt (location_t loc)
10392 tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
10393 SET_EXPR_LOCATION (t, loc);
10394 return t;
10398 /* Build an OpenMP clause with code CODE. LOC is the location of the
10399 clause. */
10401 tree
10402 build_omp_clause (location_t loc, enum omp_clause_code code)
10404 tree t;
10405 int size, length;
10407 length = omp_clause_num_ops[code];
10408 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
10410 record_node_allocation_statistics (OMP_CLAUSE, size);
10412 t = (tree) ggc_internal_alloc (size);
10413 memset (t, 0, size);
10414 TREE_SET_CODE (t, OMP_CLAUSE);
10415 OMP_CLAUSE_SET_CODE (t, code);
10416 OMP_CLAUSE_LOCATION (t) = loc;
10418 return t;
10421 /* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
10422 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
10423 Except for the CODE and operand count field, other storage for the
10424 object is initialized to zeros. */
10426 tree
10427 build_vl_exp_stat (enum tree_code code, int len MEM_STAT_DECL)
10429 tree t;
10430 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
10432 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
10433 gcc_assert (len >= 1);
10435 record_node_allocation_statistics (code, length);
10437 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
10439 TREE_SET_CODE (t, code);
10441 /* Can't use TREE_OPERAND to store the length because if checking is
10442 enabled, it will try to check the length before we store it. :-P */
10443 t->exp.operands[0] = build_int_cst (sizetype, len);
10445 return t;
10448 /* Helper function for build_call_* functions; build a CALL_EXPR with
10449 indicated RETURN_TYPE, FN, and NARGS, but do not initialize any of
10450 the argument slots. */
10452 static tree
10453 build_call_1 (tree return_type, tree fn, int nargs)
10455 tree t;
10457 t = build_vl_exp (CALL_EXPR, nargs + 3);
10458 TREE_TYPE (t) = return_type;
10459 CALL_EXPR_FN (t) = fn;
10460 CALL_EXPR_STATIC_CHAIN (t) = NULL;
10462 return t;
10465 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10466 FN and a null static chain slot. NARGS is the number of call arguments
10467 which are specified as "..." arguments. */
10469 tree
10470 build_call_nary (tree return_type, tree fn, int nargs, ...)
10472 tree ret;
10473 va_list args;
10474 va_start (args, nargs);
10475 ret = build_call_valist (return_type, fn, nargs, args);
10476 va_end (args);
10477 return ret;
10480 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10481 FN and a null static chain slot. NARGS is the number of call arguments
10482 which are specified as a va_list ARGS. */
10484 tree
10485 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
10487 tree t;
10488 int i;
10490 t = build_call_1 (return_type, fn, nargs);
10491 for (i = 0; i < nargs; i++)
10492 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
10493 process_call_operands (t);
10494 return t;
10497 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10498 FN and a null static chain slot. NARGS is the number of call arguments
10499 which are specified as a tree array ARGS. */
10501 tree
10502 build_call_array_loc (location_t loc, tree return_type, tree fn,
10503 int nargs, const tree *args)
10505 tree t;
10506 int i;
10508 t = build_call_1 (return_type, fn, nargs);
10509 for (i = 0; i < nargs; i++)
10510 CALL_EXPR_ARG (t, i) = args[i];
10511 process_call_operands (t);
10512 SET_EXPR_LOCATION (t, loc);
10513 return t;
10516 /* Like build_call_array, but takes a vec. */
10518 tree
10519 build_call_vec (tree return_type, tree fn, vec<tree, va_gc> *args)
10521 tree ret, t;
10522 unsigned int ix;
10524 ret = build_call_1 (return_type, fn, vec_safe_length (args));
10525 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
10526 CALL_EXPR_ARG (ret, ix) = t;
10527 process_call_operands (ret);
10528 return ret;
10531 /* Conveniently construct a function call expression. FNDECL names the
10532 function to be called and N arguments are passed in the array
10533 ARGARRAY. */
10535 tree
10536 build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
10538 tree fntype = TREE_TYPE (fndecl);
10539 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
10541 return fold_builtin_call_array (loc, TREE_TYPE (fntype), fn, n, argarray);
10544 /* Conveniently construct a function call expression. FNDECL names the
10545 function to be called and the arguments are passed in the vector
10546 VEC. */
10548 tree
10549 build_call_expr_loc_vec (location_t loc, tree fndecl, vec<tree, va_gc> *vec)
10551 return build_call_expr_loc_array (loc, fndecl, vec_safe_length (vec),
10552 vec_safe_address (vec));
10556 /* Conveniently construct a function call expression. FNDECL names the
10557 function to be called, N is the number of arguments, and the "..."
10558 parameters are the argument expressions. */
10560 tree
10561 build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
10563 va_list ap;
10564 tree *argarray = XALLOCAVEC (tree, n);
10565 int i;
10567 va_start (ap, n);
10568 for (i = 0; i < n; i++)
10569 argarray[i] = va_arg (ap, tree);
10570 va_end (ap);
10571 return build_call_expr_loc_array (loc, fndecl, n, argarray);
10574 /* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
10575 varargs macros aren't supported by all bootstrap compilers. */
10577 tree
10578 build_call_expr (tree fndecl, int n, ...)
10580 va_list ap;
10581 tree *argarray = XALLOCAVEC (tree, n);
10582 int i;
10584 va_start (ap, n);
10585 for (i = 0; i < n; i++)
10586 argarray[i] = va_arg (ap, tree);
10587 va_end (ap);
10588 return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
10591 /* Build internal call expression. This is just like CALL_EXPR, except
10592 its CALL_EXPR_FN is NULL. It will get gimplified later into ordinary
10593 internal function. */
10595 tree
10596 build_call_expr_internal_loc (location_t loc, enum internal_fn ifn,
10597 tree type, int n, ...)
10599 va_list ap;
10600 int i;
10602 tree fn = build_call_1 (type, NULL_TREE, n);
10603 va_start (ap, n);
10604 for (i = 0; i < n; i++)
10605 CALL_EXPR_ARG (fn, i) = va_arg (ap, tree);
10606 va_end (ap);
10607 SET_EXPR_LOCATION (fn, loc);
10608 CALL_EXPR_IFN (fn) = ifn;
10609 return fn;
10612 /* Create a new constant string literal and return a char* pointer to it.
10613 The STRING_CST value is the LEN characters at STR. */
10614 tree
10615 build_string_literal (int len, const char *str)
10617 tree t, elem, index, type;
10619 t = build_string (len, str);
10620 elem = build_type_variant (char_type_node, 1, 0);
10621 index = build_index_type (size_int (len - 1));
10622 type = build_array_type (elem, index);
10623 TREE_TYPE (t) = type;
10624 TREE_CONSTANT (t) = 1;
10625 TREE_READONLY (t) = 1;
10626 TREE_STATIC (t) = 1;
10628 type = build_pointer_type (elem);
10629 t = build1 (ADDR_EXPR, type,
10630 build4 (ARRAY_REF, elem,
10631 t, integer_zero_node, NULL_TREE, NULL_TREE));
10632 return t;
10637 /* Return true if T (assumed to be a DECL) must be assigned a memory
10638 location. */
10640 bool
10641 needs_to_live_in_memory (const_tree t)
10643 return (TREE_ADDRESSABLE (t)
10644 || is_global_var (t)
10645 || (TREE_CODE (t) == RESULT_DECL
10646 && !DECL_BY_REFERENCE (t)
10647 && aggregate_value_p (t, current_function_decl)));
10650 /* Return value of a constant X and sign-extend it. */
10652 HOST_WIDE_INT
10653 int_cst_value (const_tree x)
10655 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10656 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
10658 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10659 gcc_assert (cst_and_fits_in_hwi (x));
10661 if (bits < HOST_BITS_PER_WIDE_INT)
10663 bool negative = ((val >> (bits - 1)) & 1) != 0;
10664 if (negative)
10665 val |= (~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1;
10666 else
10667 val &= ~((~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1);
10670 return val;
10673 /* If TYPE is an integral or pointer type, return an integer type with
10674 the same precision which is unsigned iff UNSIGNEDP is true, or itself
10675 if TYPE is already an integer type of signedness UNSIGNEDP. */
10677 tree
10678 signed_or_unsigned_type_for (int unsignedp, tree type)
10680 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type) == unsignedp)
10681 return type;
10683 if (TREE_CODE (type) == VECTOR_TYPE)
10685 tree inner = TREE_TYPE (type);
10686 tree inner2 = signed_or_unsigned_type_for (unsignedp, inner);
10687 if (!inner2)
10688 return NULL_TREE;
10689 if (inner == inner2)
10690 return type;
10691 return build_vector_type (inner2, TYPE_VECTOR_SUBPARTS (type));
10694 if (!INTEGRAL_TYPE_P (type)
10695 && !POINTER_TYPE_P (type)
10696 && TREE_CODE (type) != OFFSET_TYPE)
10697 return NULL_TREE;
10699 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
10702 /* If TYPE is an integral or pointer type, return an integer type with
10703 the same precision which is unsigned, or itself if TYPE is already an
10704 unsigned integer type. */
10706 tree
10707 unsigned_type_for (tree type)
10709 return signed_or_unsigned_type_for (1, type);
10712 /* If TYPE is an integral or pointer type, return an integer type with
10713 the same precision which is signed, or itself if TYPE is already a
10714 signed integer type. */
10716 tree
10717 signed_type_for (tree type)
10719 return signed_or_unsigned_type_for (0, type);
10722 /* If TYPE is a vector type, return a signed integer vector type with the
10723 same width and number of subparts. Otherwise return boolean_type_node. */
10725 tree
10726 truth_type_for (tree type)
10728 if (TREE_CODE (type) == VECTOR_TYPE)
10730 tree elem = lang_hooks.types.type_for_size
10731 (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))), 0);
10732 return build_opaque_vector_type (elem, TYPE_VECTOR_SUBPARTS (type));
10734 else
10735 return boolean_type_node;
10738 /* Returns the largest value obtainable by casting something in INNER type to
10739 OUTER type. */
10741 tree
10742 upper_bound_in_type (tree outer, tree inner)
10744 unsigned int det = 0;
10745 unsigned oprec = TYPE_PRECISION (outer);
10746 unsigned iprec = TYPE_PRECISION (inner);
10747 unsigned prec;
10749 /* Compute a unique number for every combination. */
10750 det |= (oprec > iprec) ? 4 : 0;
10751 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
10752 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
10754 /* Determine the exponent to use. */
10755 switch (det)
10757 case 0:
10758 case 1:
10759 /* oprec <= iprec, outer: signed, inner: don't care. */
10760 prec = oprec - 1;
10761 break;
10762 case 2:
10763 case 3:
10764 /* oprec <= iprec, outer: unsigned, inner: don't care. */
10765 prec = oprec;
10766 break;
10767 case 4:
10768 /* oprec > iprec, outer: signed, inner: signed. */
10769 prec = iprec - 1;
10770 break;
10771 case 5:
10772 /* oprec > iprec, outer: signed, inner: unsigned. */
10773 prec = iprec;
10774 break;
10775 case 6:
10776 /* oprec > iprec, outer: unsigned, inner: signed. */
10777 prec = oprec;
10778 break;
10779 case 7:
10780 /* oprec > iprec, outer: unsigned, inner: unsigned. */
10781 prec = iprec;
10782 break;
10783 default:
10784 gcc_unreachable ();
10787 return wide_int_to_tree (outer,
10788 wi::mask (prec, false, TYPE_PRECISION (outer)));
10791 /* Returns the smallest value obtainable by casting something in INNER type to
10792 OUTER type. */
10794 tree
10795 lower_bound_in_type (tree outer, tree inner)
10797 unsigned oprec = TYPE_PRECISION (outer);
10798 unsigned iprec = TYPE_PRECISION (inner);
10800 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
10801 and obtain 0. */
10802 if (TYPE_UNSIGNED (outer)
10803 /* If we are widening something of an unsigned type, OUTER type
10804 contains all values of INNER type. In particular, both INNER
10805 and OUTER types have zero in common. */
10806 || (oprec > iprec && TYPE_UNSIGNED (inner)))
10807 return build_int_cst (outer, 0);
10808 else
10810 /* If we are widening a signed type to another signed type, we
10811 want to obtain -2^^(iprec-1). If we are keeping the
10812 precision or narrowing to a signed type, we want to obtain
10813 -2^(oprec-1). */
10814 unsigned prec = oprec > iprec ? iprec : oprec;
10815 return wide_int_to_tree (outer,
10816 wi::mask (prec - 1, true,
10817 TYPE_PRECISION (outer)));
10821 /* Return nonzero if two operands that are suitable for PHI nodes are
10822 necessarily equal. Specifically, both ARG0 and ARG1 must be either
10823 SSA_NAME or invariant. Note that this is strictly an optimization.
10824 That is, callers of this function can directly call operand_equal_p
10825 and get the same result, only slower. */
10828 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
10830 if (arg0 == arg1)
10831 return 1;
10832 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
10833 return 0;
10834 return operand_equal_p (arg0, arg1, 0);
10837 /* Returns number of zeros at the end of binary representation of X. */
10839 tree
10840 num_ending_zeros (const_tree x)
10842 return build_int_cst (TREE_TYPE (x), wi::ctz (x));
10846 #define WALK_SUBTREE(NODE) \
10847 do \
10849 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
10850 if (result) \
10851 return result; \
10853 while (0)
10855 /* This is a subroutine of walk_tree that walks field of TYPE that are to
10856 be walked whenever a type is seen in the tree. Rest of operands and return
10857 value are as for walk_tree. */
10859 static tree
10860 walk_type_fields (tree type, walk_tree_fn func, void *data,
10861 hash_set<tree> *pset, walk_tree_lh lh)
10863 tree result = NULL_TREE;
10865 switch (TREE_CODE (type))
10867 case POINTER_TYPE:
10868 case REFERENCE_TYPE:
10869 case VECTOR_TYPE:
10870 /* We have to worry about mutually recursive pointers. These can't
10871 be written in C. They can in Ada. It's pathological, but
10872 there's an ACATS test (c38102a) that checks it. Deal with this
10873 by checking if we're pointing to another pointer, that one
10874 points to another pointer, that one does too, and we have no htab.
10875 If so, get a hash table. We check three levels deep to avoid
10876 the cost of the hash table if we don't need one. */
10877 if (POINTER_TYPE_P (TREE_TYPE (type))
10878 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
10879 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
10880 && !pset)
10882 result = walk_tree_without_duplicates (&TREE_TYPE (type),
10883 func, data);
10884 if (result)
10885 return result;
10887 break;
10890 /* ... fall through ... */
10892 case COMPLEX_TYPE:
10893 WALK_SUBTREE (TREE_TYPE (type));
10894 break;
10896 case METHOD_TYPE:
10897 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
10899 /* Fall through. */
10901 case FUNCTION_TYPE:
10902 WALK_SUBTREE (TREE_TYPE (type));
10904 tree arg;
10906 /* We never want to walk into default arguments. */
10907 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
10908 WALK_SUBTREE (TREE_VALUE (arg));
10910 break;
10912 case ARRAY_TYPE:
10913 /* Don't follow this nodes's type if a pointer for fear that
10914 we'll have infinite recursion. If we have a PSET, then we
10915 need not fear. */
10916 if (pset
10917 || (!POINTER_TYPE_P (TREE_TYPE (type))
10918 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
10919 WALK_SUBTREE (TREE_TYPE (type));
10920 WALK_SUBTREE (TYPE_DOMAIN (type));
10921 break;
10923 case OFFSET_TYPE:
10924 WALK_SUBTREE (TREE_TYPE (type));
10925 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
10926 break;
10928 default:
10929 break;
10932 return NULL_TREE;
10935 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
10936 called with the DATA and the address of each sub-tree. If FUNC returns a
10937 non-NULL value, the traversal is stopped, and the value returned by FUNC
10938 is returned. If PSET is non-NULL it is used to record the nodes visited,
10939 and to avoid visiting a node more than once. */
10941 tree
10942 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
10943 hash_set<tree> *pset, walk_tree_lh lh)
10945 enum tree_code code;
10946 int walk_subtrees;
10947 tree result;
10949 #define WALK_SUBTREE_TAIL(NODE) \
10950 do \
10952 tp = & (NODE); \
10953 goto tail_recurse; \
10955 while (0)
10957 tail_recurse:
10958 /* Skip empty subtrees. */
10959 if (!*tp)
10960 return NULL_TREE;
10962 /* Don't walk the same tree twice, if the user has requested
10963 that we avoid doing so. */
10964 if (pset && pset->add (*tp))
10965 return NULL_TREE;
10967 /* Call the function. */
10968 walk_subtrees = 1;
10969 result = (*func) (tp, &walk_subtrees, data);
10971 /* If we found something, return it. */
10972 if (result)
10973 return result;
10975 code = TREE_CODE (*tp);
10977 /* Even if we didn't, FUNC may have decided that there was nothing
10978 interesting below this point in the tree. */
10979 if (!walk_subtrees)
10981 /* But we still need to check our siblings. */
10982 if (code == TREE_LIST)
10983 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
10984 else if (code == OMP_CLAUSE)
10985 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10986 else
10987 return NULL_TREE;
10990 if (lh)
10992 result = (*lh) (tp, &walk_subtrees, func, data, pset);
10993 if (result || !walk_subtrees)
10994 return result;
10997 switch (code)
10999 case ERROR_MARK:
11000 case IDENTIFIER_NODE:
11001 case INTEGER_CST:
11002 case REAL_CST:
11003 case FIXED_CST:
11004 case VECTOR_CST:
11005 case STRING_CST:
11006 case BLOCK:
11007 case PLACEHOLDER_EXPR:
11008 case SSA_NAME:
11009 case FIELD_DECL:
11010 case RESULT_DECL:
11011 /* None of these have subtrees other than those already walked
11012 above. */
11013 break;
11015 case TREE_LIST:
11016 WALK_SUBTREE (TREE_VALUE (*tp));
11017 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
11018 break;
11020 case TREE_VEC:
11022 int len = TREE_VEC_LENGTH (*tp);
11024 if (len == 0)
11025 break;
11027 /* Walk all elements but the first. */
11028 while (--len)
11029 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
11031 /* Now walk the first one as a tail call. */
11032 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
11035 case COMPLEX_CST:
11036 WALK_SUBTREE (TREE_REALPART (*tp));
11037 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
11039 case CONSTRUCTOR:
11041 unsigned HOST_WIDE_INT idx;
11042 constructor_elt *ce;
11044 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (*tp), idx, &ce);
11045 idx++)
11046 WALK_SUBTREE (ce->value);
11048 break;
11050 case SAVE_EXPR:
11051 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
11053 case BIND_EXPR:
11055 tree decl;
11056 for (decl = BIND_EXPR_VARS (*tp); decl; decl = DECL_CHAIN (decl))
11058 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
11059 into declarations that are just mentioned, rather than
11060 declared; they don't really belong to this part of the tree.
11061 And, we can see cycles: the initializer for a declaration
11062 can refer to the declaration itself. */
11063 WALK_SUBTREE (DECL_INITIAL (decl));
11064 WALK_SUBTREE (DECL_SIZE (decl));
11065 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
11067 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
11070 case STATEMENT_LIST:
11072 tree_stmt_iterator i;
11073 for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
11074 WALK_SUBTREE (*tsi_stmt_ptr (i));
11076 break;
11078 case OMP_CLAUSE:
11079 switch (OMP_CLAUSE_CODE (*tp))
11081 case OMP_CLAUSE_PRIVATE:
11082 case OMP_CLAUSE_SHARED:
11083 case OMP_CLAUSE_FIRSTPRIVATE:
11084 case OMP_CLAUSE_COPYIN:
11085 case OMP_CLAUSE_COPYPRIVATE:
11086 case OMP_CLAUSE_FINAL:
11087 case OMP_CLAUSE_IF:
11088 case OMP_CLAUSE_NUM_THREADS:
11089 case OMP_CLAUSE_SCHEDULE:
11090 case OMP_CLAUSE_UNIFORM:
11091 case OMP_CLAUSE_DEPEND:
11092 case OMP_CLAUSE_NUM_TEAMS:
11093 case OMP_CLAUSE_THREAD_LIMIT:
11094 case OMP_CLAUSE_DEVICE:
11095 case OMP_CLAUSE_DIST_SCHEDULE:
11096 case OMP_CLAUSE_SAFELEN:
11097 case OMP_CLAUSE_SIMDLEN:
11098 case OMP_CLAUSE__LOOPTEMP_:
11099 case OMP_CLAUSE__SIMDUID_:
11100 case OMP_CLAUSE__CILK_FOR_COUNT_:
11101 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0));
11102 /* FALLTHRU */
11104 case OMP_CLAUSE_NOWAIT:
11105 case OMP_CLAUSE_ORDERED:
11106 case OMP_CLAUSE_DEFAULT:
11107 case OMP_CLAUSE_UNTIED:
11108 case OMP_CLAUSE_MERGEABLE:
11109 case OMP_CLAUSE_PROC_BIND:
11110 case OMP_CLAUSE_INBRANCH:
11111 case OMP_CLAUSE_NOTINBRANCH:
11112 case OMP_CLAUSE_FOR:
11113 case OMP_CLAUSE_PARALLEL:
11114 case OMP_CLAUSE_SECTIONS:
11115 case OMP_CLAUSE_TASKGROUP:
11116 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11118 case OMP_CLAUSE_LASTPRIVATE:
11119 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11120 WALK_SUBTREE (OMP_CLAUSE_LASTPRIVATE_STMT (*tp));
11121 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11123 case OMP_CLAUSE_COLLAPSE:
11125 int i;
11126 for (i = 0; i < 3; i++)
11127 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
11128 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11131 case OMP_CLAUSE_LINEAR:
11132 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11133 WALK_SUBTREE (OMP_CLAUSE_LINEAR_STEP (*tp));
11134 WALK_SUBTREE (OMP_CLAUSE_LINEAR_STMT (*tp));
11135 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11137 case OMP_CLAUSE_ALIGNED:
11138 case OMP_CLAUSE_FROM:
11139 case OMP_CLAUSE_TO:
11140 case OMP_CLAUSE_MAP:
11141 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11142 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 1));
11143 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11145 case OMP_CLAUSE_REDUCTION:
11147 int i;
11148 for (i = 0; i < 4; i++)
11149 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
11150 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11153 default:
11154 gcc_unreachable ();
11156 break;
11158 case TARGET_EXPR:
11160 int i, len;
11162 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
11163 But, we only want to walk once. */
11164 len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
11165 for (i = 0; i < len; ++i)
11166 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11167 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
11170 case DECL_EXPR:
11171 /* If this is a TYPE_DECL, walk into the fields of the type that it's
11172 defining. We only want to walk into these fields of a type in this
11173 case and not in the general case of a mere reference to the type.
11175 The criterion is as follows: if the field can be an expression, it
11176 must be walked only here. This should be in keeping with the fields
11177 that are directly gimplified in gimplify_type_sizes in order for the
11178 mark/copy-if-shared/unmark machinery of the gimplifier to work with
11179 variable-sized types.
11181 Note that DECLs get walked as part of processing the BIND_EXPR. */
11182 if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
11184 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
11185 if (TREE_CODE (*type_p) == ERROR_MARK)
11186 return NULL_TREE;
11188 /* Call the function for the type. See if it returns anything or
11189 doesn't want us to continue. If we are to continue, walk both
11190 the normal fields and those for the declaration case. */
11191 result = (*func) (type_p, &walk_subtrees, data);
11192 if (result || !walk_subtrees)
11193 return result;
11195 /* But do not walk a pointed-to type since it may itself need to
11196 be walked in the declaration case if it isn't anonymous. */
11197 if (!POINTER_TYPE_P (*type_p))
11199 result = walk_type_fields (*type_p, func, data, pset, lh);
11200 if (result)
11201 return result;
11204 /* If this is a record type, also walk the fields. */
11205 if (RECORD_OR_UNION_TYPE_P (*type_p))
11207 tree field;
11209 for (field = TYPE_FIELDS (*type_p); field;
11210 field = DECL_CHAIN (field))
11212 /* We'd like to look at the type of the field, but we can
11213 easily get infinite recursion. So assume it's pointed
11214 to elsewhere in the tree. Also, ignore things that
11215 aren't fields. */
11216 if (TREE_CODE (field) != FIELD_DECL)
11217 continue;
11219 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
11220 WALK_SUBTREE (DECL_SIZE (field));
11221 WALK_SUBTREE (DECL_SIZE_UNIT (field));
11222 if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
11223 WALK_SUBTREE (DECL_QUALIFIER (field));
11227 /* Same for scalar types. */
11228 else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
11229 || TREE_CODE (*type_p) == ENUMERAL_TYPE
11230 || TREE_CODE (*type_p) == INTEGER_TYPE
11231 || TREE_CODE (*type_p) == FIXED_POINT_TYPE
11232 || TREE_CODE (*type_p) == REAL_TYPE)
11234 WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
11235 WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
11238 WALK_SUBTREE (TYPE_SIZE (*type_p));
11239 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
11241 /* FALLTHRU */
11243 default:
11244 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
11246 int i, len;
11248 /* Walk over all the sub-trees of this operand. */
11249 len = TREE_OPERAND_LENGTH (*tp);
11251 /* Go through the subtrees. We need to do this in forward order so
11252 that the scope of a FOR_EXPR is handled properly. */
11253 if (len)
11255 for (i = 0; i < len - 1; ++i)
11256 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11257 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
11260 /* If this is a type, walk the needed fields in the type. */
11261 else if (TYPE_P (*tp))
11262 return walk_type_fields (*tp, func, data, pset, lh);
11263 break;
11266 /* We didn't find what we were looking for. */
11267 return NULL_TREE;
11269 #undef WALK_SUBTREE_TAIL
11271 #undef WALK_SUBTREE
11273 /* Like walk_tree, but does not walk duplicate nodes more than once. */
11275 tree
11276 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
11277 walk_tree_lh lh)
11279 tree result;
11281 hash_set<tree> pset;
11282 result = walk_tree_1 (tp, func, data, &pset, lh);
11283 return result;
11287 tree
11288 tree_block (tree t)
11290 const enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
11292 if (IS_EXPR_CODE_CLASS (c))
11293 return LOCATION_BLOCK (t->exp.locus);
11294 gcc_unreachable ();
11295 return NULL;
11298 void
11299 tree_set_block (tree t, tree b)
11301 const enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
11303 if (IS_EXPR_CODE_CLASS (c))
11305 if (b)
11306 t->exp.locus = COMBINE_LOCATION_DATA (line_table, t->exp.locus, b);
11307 else
11308 t->exp.locus = LOCATION_LOCUS (t->exp.locus);
11310 else
11311 gcc_unreachable ();
11314 /* Create a nameless artificial label and put it in the current
11315 function context. The label has a location of LOC. Returns the
11316 newly created label. */
11318 tree
11319 create_artificial_label (location_t loc)
11321 tree lab = build_decl (loc,
11322 LABEL_DECL, NULL_TREE, void_type_node);
11324 DECL_ARTIFICIAL (lab) = 1;
11325 DECL_IGNORED_P (lab) = 1;
11326 DECL_CONTEXT (lab) = current_function_decl;
11327 return lab;
11330 /* Given a tree, try to return a useful variable name that we can use
11331 to prefix a temporary that is being assigned the value of the tree.
11332 I.E. given <temp> = &A, return A. */
11334 const char *
11335 get_name (tree t)
11337 tree stripped_decl;
11339 stripped_decl = t;
11340 STRIP_NOPS (stripped_decl);
11341 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
11342 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
11343 else if (TREE_CODE (stripped_decl) == SSA_NAME)
11345 tree name = SSA_NAME_IDENTIFIER (stripped_decl);
11346 if (!name)
11347 return NULL;
11348 return IDENTIFIER_POINTER (name);
11350 else
11352 switch (TREE_CODE (stripped_decl))
11354 case ADDR_EXPR:
11355 return get_name (TREE_OPERAND (stripped_decl, 0));
11356 default:
11357 return NULL;
11362 /* Return true if TYPE has a variable argument list. */
11364 bool
11365 stdarg_p (const_tree fntype)
11367 function_args_iterator args_iter;
11368 tree n = NULL_TREE, t;
11370 if (!fntype)
11371 return false;
11373 FOREACH_FUNCTION_ARGS (fntype, t, args_iter)
11375 n = t;
11378 return n != NULL_TREE && n != void_type_node;
11381 /* Return true if TYPE has a prototype. */
11383 bool
11384 prototype_p (tree fntype)
11386 tree t;
11388 gcc_assert (fntype != NULL_TREE);
11390 t = TYPE_ARG_TYPES (fntype);
11391 return (t != NULL_TREE);
11394 /* If BLOCK is inlined from an __attribute__((__artificial__))
11395 routine, return pointer to location from where it has been
11396 called. */
11397 location_t *
11398 block_nonartificial_location (tree block)
11400 location_t *ret = NULL;
11402 while (block && TREE_CODE (block) == BLOCK
11403 && BLOCK_ABSTRACT_ORIGIN (block))
11405 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
11407 while (TREE_CODE (ao) == BLOCK
11408 && BLOCK_ABSTRACT_ORIGIN (ao)
11409 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
11410 ao = BLOCK_ABSTRACT_ORIGIN (ao);
11412 if (TREE_CODE (ao) == FUNCTION_DECL)
11414 /* If AO is an artificial inline, point RET to the
11415 call site locus at which it has been inlined and continue
11416 the loop, in case AO's caller is also an artificial
11417 inline. */
11418 if (DECL_DECLARED_INLINE_P (ao)
11419 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
11420 ret = &BLOCK_SOURCE_LOCATION (block);
11421 else
11422 break;
11424 else if (TREE_CODE (ao) != BLOCK)
11425 break;
11427 block = BLOCK_SUPERCONTEXT (block);
11429 return ret;
11433 /* If EXP is inlined from an __attribute__((__artificial__))
11434 function, return the location of the original call expression. */
11436 location_t
11437 tree_nonartificial_location (tree exp)
11439 location_t *loc = block_nonartificial_location (TREE_BLOCK (exp));
11441 if (loc)
11442 return *loc;
11443 else
11444 return EXPR_LOCATION (exp);
11448 /* These are the hash table functions for the hash table of OPTIMIZATION_NODEq
11449 nodes. */
11451 /* Return the hash code code X, an OPTIMIZATION_NODE or TARGET_OPTION code. */
11453 static hashval_t
11454 cl_option_hash_hash (const void *x)
11456 const_tree const t = (const_tree) x;
11457 const char *p;
11458 size_t i;
11459 size_t len = 0;
11460 hashval_t hash = 0;
11462 if (TREE_CODE (t) == OPTIMIZATION_NODE)
11464 p = (const char *)TREE_OPTIMIZATION (t);
11465 len = sizeof (struct cl_optimization);
11468 else if (TREE_CODE (t) == TARGET_OPTION_NODE)
11470 p = (const char *)TREE_TARGET_OPTION (t);
11471 len = sizeof (struct cl_target_option);
11474 else
11475 gcc_unreachable ();
11477 /* assume most opt flags are just 0/1, some are 2-3, and a few might be
11478 something else. */
11479 for (i = 0; i < len; i++)
11480 if (p[i])
11481 hash = (hash << 4) ^ ((i << 2) | p[i]);
11483 return hash;
11486 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
11487 TARGET_OPTION tree node) is the same as that given by *Y, which is the
11488 same. */
11490 static int
11491 cl_option_hash_eq (const void *x, const void *y)
11493 const_tree const xt = (const_tree) x;
11494 const_tree const yt = (const_tree) y;
11495 const char *xp;
11496 const char *yp;
11497 size_t len;
11499 if (TREE_CODE (xt) != TREE_CODE (yt))
11500 return 0;
11502 if (TREE_CODE (xt) == OPTIMIZATION_NODE)
11504 xp = (const char *)TREE_OPTIMIZATION (xt);
11505 yp = (const char *)TREE_OPTIMIZATION (yt);
11506 len = sizeof (struct cl_optimization);
11509 else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
11511 xp = (const char *)TREE_TARGET_OPTION (xt);
11512 yp = (const char *)TREE_TARGET_OPTION (yt);
11513 len = sizeof (struct cl_target_option);
11516 else
11517 gcc_unreachable ();
11519 return (memcmp (xp, yp, len) == 0);
11522 /* Build an OPTIMIZATION_NODE based on the options in OPTS. */
11524 tree
11525 build_optimization_node (struct gcc_options *opts)
11527 tree t;
11528 void **slot;
11530 /* Use the cache of optimization nodes. */
11532 cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node),
11533 opts);
11535 slot = htab_find_slot (cl_option_hash_table, cl_optimization_node, INSERT);
11536 t = (tree) *slot;
11537 if (!t)
11539 /* Insert this one into the hash table. */
11540 t = cl_optimization_node;
11541 *slot = t;
11543 /* Make a new node for next time round. */
11544 cl_optimization_node = make_node (OPTIMIZATION_NODE);
11547 return t;
11550 /* Build a TARGET_OPTION_NODE based on the options in OPTS. */
11552 tree
11553 build_target_option_node (struct gcc_options *opts)
11555 tree t;
11556 void **slot;
11558 /* Use the cache of optimization nodes. */
11560 cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node),
11561 opts);
11563 slot = htab_find_slot (cl_option_hash_table, cl_target_option_node, INSERT);
11564 t = (tree) *slot;
11565 if (!t)
11567 /* Insert this one into the hash table. */
11568 t = cl_target_option_node;
11569 *slot = t;
11571 /* Make a new node for next time round. */
11572 cl_target_option_node = make_node (TARGET_OPTION_NODE);
11575 return t;
11578 /* Reset TREE_TARGET_GLOBALS cache for TARGET_OPTION_NODE.
11579 Called through htab_traverse. */
11581 static int
11582 prepare_target_option_node_for_pch (void **slot, void *)
11584 tree node = (tree) *slot;
11585 if (TREE_CODE (node) == TARGET_OPTION_NODE)
11586 TREE_TARGET_GLOBALS (node) = NULL;
11587 return 1;
11590 /* Clear TREE_TARGET_GLOBALS of all TARGET_OPTION_NODE trees,
11591 so that they aren't saved during PCH writing. */
11593 void
11594 prepare_target_option_nodes_for_pch (void)
11596 htab_traverse (cl_option_hash_table, prepare_target_option_node_for_pch,
11597 NULL);
11600 /* Determine the "ultimate origin" of a block. The block may be an inlined
11601 instance of an inlined instance of a block which is local to an inline
11602 function, so we have to trace all of the way back through the origin chain
11603 to find out what sort of node actually served as the original seed for the
11604 given block. */
11606 tree
11607 block_ultimate_origin (const_tree block)
11609 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
11611 /* BLOCK_ABSTRACT_ORIGIN can point to itself; ignore that if
11612 we're trying to output the abstract instance of this function. */
11613 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
11614 return NULL_TREE;
11616 if (immediate_origin == NULL_TREE)
11617 return NULL_TREE;
11618 else
11620 tree ret_val;
11621 tree lookahead = immediate_origin;
11625 ret_val = lookahead;
11626 lookahead = (TREE_CODE (ret_val) == BLOCK
11627 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
11629 while (lookahead != NULL && lookahead != ret_val);
11631 /* The block's abstract origin chain may not be the *ultimate* origin of
11632 the block. It could lead to a DECL that has an abstract origin set.
11633 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
11634 will give us if it has one). Note that DECL's abstract origins are
11635 supposed to be the most distant ancestor (or so decl_ultimate_origin
11636 claims), so we don't need to loop following the DECL origins. */
11637 if (DECL_P (ret_val))
11638 return DECL_ORIGIN (ret_val);
11640 return ret_val;
11644 /* Return true iff conversion in EXP generates no instruction. Mark
11645 it inline so that we fully inline into the stripping functions even
11646 though we have two uses of this function. */
11648 static inline bool
11649 tree_nop_conversion (const_tree exp)
11651 tree outer_type, inner_type;
11653 if (!CONVERT_EXPR_P (exp)
11654 && TREE_CODE (exp) != NON_LVALUE_EXPR)
11655 return false;
11656 if (TREE_OPERAND (exp, 0) == error_mark_node)
11657 return false;
11659 outer_type = TREE_TYPE (exp);
11660 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11662 if (!inner_type)
11663 return false;
11665 /* Use precision rather then machine mode when we can, which gives
11666 the correct answer even for submode (bit-field) types. */
11667 if ((INTEGRAL_TYPE_P (outer_type)
11668 || POINTER_TYPE_P (outer_type)
11669 || TREE_CODE (outer_type) == OFFSET_TYPE)
11670 && (INTEGRAL_TYPE_P (inner_type)
11671 || POINTER_TYPE_P (inner_type)
11672 || TREE_CODE (inner_type) == OFFSET_TYPE))
11673 return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
11675 /* Otherwise fall back on comparing machine modes (e.g. for
11676 aggregate types, floats). */
11677 return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
11680 /* Return true iff conversion in EXP generates no instruction. Don't
11681 consider conversions changing the signedness. */
11683 static bool
11684 tree_sign_nop_conversion (const_tree exp)
11686 tree outer_type, inner_type;
11688 if (!tree_nop_conversion (exp))
11689 return false;
11691 outer_type = TREE_TYPE (exp);
11692 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11694 return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
11695 && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
11698 /* Strip conversions from EXP according to tree_nop_conversion and
11699 return the resulting expression. */
11701 tree
11702 tree_strip_nop_conversions (tree exp)
11704 while (tree_nop_conversion (exp))
11705 exp = TREE_OPERAND (exp, 0);
11706 return exp;
11709 /* Strip conversions from EXP according to tree_sign_nop_conversion
11710 and return the resulting expression. */
11712 tree
11713 tree_strip_sign_nop_conversions (tree exp)
11715 while (tree_sign_nop_conversion (exp))
11716 exp = TREE_OPERAND (exp, 0);
11717 return exp;
11720 /* Avoid any floating point extensions from EXP. */
11721 tree
11722 strip_float_extensions (tree exp)
11724 tree sub, expt, subt;
11726 /* For floating point constant look up the narrowest type that can hold
11727 it properly and handle it like (type)(narrowest_type)constant.
11728 This way we can optimize for instance a=a*2.0 where "a" is float
11729 but 2.0 is double constant. */
11730 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
11732 REAL_VALUE_TYPE orig;
11733 tree type = NULL;
11735 orig = TREE_REAL_CST (exp);
11736 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
11737 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
11738 type = float_type_node;
11739 else if (TYPE_PRECISION (TREE_TYPE (exp))
11740 > TYPE_PRECISION (double_type_node)
11741 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
11742 type = double_type_node;
11743 if (type)
11744 return build_real (type, real_value_truncate (TYPE_MODE (type), orig));
11747 if (!CONVERT_EXPR_P (exp))
11748 return exp;
11750 sub = TREE_OPERAND (exp, 0);
11751 subt = TREE_TYPE (sub);
11752 expt = TREE_TYPE (exp);
11754 if (!FLOAT_TYPE_P (subt))
11755 return exp;
11757 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
11758 return exp;
11760 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
11761 return exp;
11763 return strip_float_extensions (sub);
11766 /* Strip out all handled components that produce invariant
11767 offsets. */
11769 const_tree
11770 strip_invariant_refs (const_tree op)
11772 while (handled_component_p (op))
11774 switch (TREE_CODE (op))
11776 case ARRAY_REF:
11777 case ARRAY_RANGE_REF:
11778 if (!is_gimple_constant (TREE_OPERAND (op, 1))
11779 || TREE_OPERAND (op, 2) != NULL_TREE
11780 || TREE_OPERAND (op, 3) != NULL_TREE)
11781 return NULL;
11782 break;
11784 case COMPONENT_REF:
11785 if (TREE_OPERAND (op, 2) != NULL_TREE)
11786 return NULL;
11787 break;
11789 default:;
11791 op = TREE_OPERAND (op, 0);
11794 return op;
11797 static GTY(()) tree gcc_eh_personality_decl;
11799 /* Return the GCC personality function decl. */
11801 tree
11802 lhd_gcc_personality (void)
11804 if (!gcc_eh_personality_decl)
11805 gcc_eh_personality_decl = build_personality_function ("gcc");
11806 return gcc_eh_personality_decl;
11809 /* TARGET is a call target of GIMPLE call statement
11810 (obtained by gimple_call_fn). Return true if it is
11811 OBJ_TYPE_REF representing an virtual call of C++ method.
11812 (As opposed to OBJ_TYPE_REF representing objc calls
11813 through a cast where middle-end devirtualization machinery
11814 can't apply.) */
11816 bool
11817 virtual_method_call_p (tree target)
11819 if (TREE_CODE (target) != OBJ_TYPE_REF)
11820 return false;
11821 target = TREE_TYPE (target);
11822 gcc_checking_assert (TREE_CODE (target) == POINTER_TYPE);
11823 target = TREE_TYPE (target);
11824 if (TREE_CODE (target) == FUNCTION_TYPE)
11825 return false;
11826 gcc_checking_assert (TREE_CODE (target) == METHOD_TYPE);
11827 return true;
11830 /* REF is OBJ_TYPE_REF, return the class the ref corresponds to. */
11832 tree
11833 obj_type_ref_class (tree ref)
11835 gcc_checking_assert (TREE_CODE (ref) == OBJ_TYPE_REF);
11836 ref = TREE_TYPE (ref);
11837 gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
11838 ref = TREE_TYPE (ref);
11839 /* We look for type THIS points to. ObjC also builds
11840 OBJ_TYPE_REF with non-method calls, Their first parameter
11841 ID however also corresponds to class type. */
11842 gcc_checking_assert (TREE_CODE (ref) == METHOD_TYPE
11843 || TREE_CODE (ref) == FUNCTION_TYPE);
11844 ref = TREE_VALUE (TYPE_ARG_TYPES (ref));
11845 gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
11846 return TREE_TYPE (ref);
11849 /* Return true if T is in anonymous namespace. */
11851 bool
11852 type_in_anonymous_namespace_p (const_tree t)
11854 /* TREE_PUBLIC of TYPE_STUB_DECL may not be properly set for
11855 bulitin types; those have CONTEXT NULL. */
11856 if (!TYPE_CONTEXT (t))
11857 return false;
11858 return (TYPE_STUB_DECL (t) && !TREE_PUBLIC (TYPE_STUB_DECL (t)));
11861 /* Try to find a base info of BINFO that would have its field decl at offset
11862 OFFSET within the BINFO type and which is of EXPECTED_TYPE. If it can be
11863 found, return, otherwise return NULL_TREE. */
11865 tree
11866 get_binfo_at_offset (tree binfo, HOST_WIDE_INT offset, tree expected_type)
11868 tree type = BINFO_TYPE (binfo);
11870 while (true)
11872 HOST_WIDE_INT pos, size;
11873 tree fld;
11874 int i;
11876 if (types_same_for_odr (type, expected_type))
11877 return binfo;
11878 if (offset < 0)
11879 return NULL_TREE;
11881 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
11883 if (TREE_CODE (fld) != FIELD_DECL)
11884 continue;
11886 pos = int_bit_position (fld);
11887 size = tree_to_uhwi (DECL_SIZE (fld));
11888 if (pos <= offset && (pos + size) > offset)
11889 break;
11891 if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE)
11892 return NULL_TREE;
11894 if (!DECL_ARTIFICIAL (fld))
11896 binfo = TYPE_BINFO (TREE_TYPE (fld));
11897 if (!binfo)
11898 return NULL_TREE;
11900 /* Offset 0 indicates the primary base, whose vtable contents are
11901 represented in the binfo for the derived class. */
11902 else if (offset != 0)
11904 tree base_binfo, binfo2 = binfo;
11906 /* Find BINFO corresponding to FLD. This is bit harder
11907 by a fact that in virtual inheritance we may need to walk down
11908 the non-virtual inheritance chain. */
11909 while (true)
11911 tree containing_binfo = NULL, found_binfo = NULL;
11912 for (i = 0; BINFO_BASE_ITERATE (binfo2, i, base_binfo); i++)
11913 if (types_same_for_odr (TREE_TYPE (base_binfo), TREE_TYPE (fld)))
11915 found_binfo = base_binfo;
11916 break;
11918 else
11919 if ((tree_to_shwi (BINFO_OFFSET (base_binfo))
11920 - tree_to_shwi (BINFO_OFFSET (binfo)))
11921 * BITS_PER_UNIT < pos
11922 /* Rule out types with no virtual methods or we can get confused
11923 here by zero sized bases. */
11924 && BINFO_VTABLE (TYPE_BINFO (BINFO_TYPE (base_binfo)))
11925 && (!containing_binfo
11926 || (tree_to_shwi (BINFO_OFFSET (containing_binfo))
11927 < tree_to_shwi (BINFO_OFFSET (base_binfo)))))
11928 containing_binfo = base_binfo;
11929 if (found_binfo)
11931 binfo = found_binfo;
11932 break;
11934 if (!containing_binfo)
11935 return NULL_TREE;
11936 binfo2 = containing_binfo;
11940 type = TREE_TYPE (fld);
11941 offset -= pos;
11945 /* Returns true if X is a typedef decl. */
11947 bool
11948 is_typedef_decl (tree x)
11950 return (x && TREE_CODE (x) == TYPE_DECL
11951 && DECL_ORIGINAL_TYPE (x) != NULL_TREE);
11954 /* Returns true iff TYPE is a type variant created for a typedef. */
11956 bool
11957 typedef_variant_p (tree type)
11959 return is_typedef_decl (TYPE_NAME (type));
11962 /* Warn about a use of an identifier which was marked deprecated. */
11963 void
11964 warn_deprecated_use (tree node, tree attr)
11966 const char *msg;
11968 if (node == 0 || !warn_deprecated_decl)
11969 return;
11971 if (!attr)
11973 if (DECL_P (node))
11974 attr = DECL_ATTRIBUTES (node);
11975 else if (TYPE_P (node))
11977 tree decl = TYPE_STUB_DECL (node);
11978 if (decl)
11979 attr = lookup_attribute ("deprecated",
11980 TYPE_ATTRIBUTES (TREE_TYPE (decl)));
11984 if (attr)
11985 attr = lookup_attribute ("deprecated", attr);
11987 if (attr)
11988 msg = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr)));
11989 else
11990 msg = NULL;
11992 if (DECL_P (node))
11994 expanded_location xloc = expand_location (DECL_SOURCE_LOCATION (node));
11995 if (msg)
11996 warning (OPT_Wdeprecated_declarations,
11997 "%qD is deprecated (declared at %r%s:%d%R): %s",
11998 node, "locus", xloc.file, xloc.line, msg);
11999 else
12000 warning (OPT_Wdeprecated_declarations,
12001 "%qD is deprecated (declared at %r%s:%d%R)",
12002 node, "locus", xloc.file, xloc.line);
12004 else if (TYPE_P (node))
12006 tree what = NULL_TREE;
12007 tree decl = TYPE_STUB_DECL (node);
12009 if (TYPE_NAME (node))
12011 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
12012 what = TYPE_NAME (node);
12013 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
12014 && DECL_NAME (TYPE_NAME (node)))
12015 what = DECL_NAME (TYPE_NAME (node));
12018 if (decl)
12020 expanded_location xloc
12021 = expand_location (DECL_SOURCE_LOCATION (decl));
12022 if (what)
12024 if (msg)
12025 warning (OPT_Wdeprecated_declarations,
12026 "%qE is deprecated (declared at %r%s:%d%R): %s",
12027 what, "locus", xloc.file, xloc.line, msg);
12028 else
12029 warning (OPT_Wdeprecated_declarations,
12030 "%qE is deprecated (declared at %r%s:%d%R)",
12031 what, "locus", xloc.file, xloc.line);
12033 else
12035 if (msg)
12036 warning (OPT_Wdeprecated_declarations,
12037 "type is deprecated (declared at %r%s:%d%R): %s",
12038 "locus", xloc.file, xloc.line, msg);
12039 else
12040 warning (OPT_Wdeprecated_declarations,
12041 "type is deprecated (declared at %r%s:%d%R)",
12042 "locus", xloc.file, xloc.line);
12045 else
12047 if (what)
12049 if (msg)
12050 warning (OPT_Wdeprecated_declarations, "%qE is deprecated: %s",
12051 what, msg);
12052 else
12053 warning (OPT_Wdeprecated_declarations, "%qE is deprecated", what);
12055 else
12057 if (msg)
12058 warning (OPT_Wdeprecated_declarations, "type is deprecated: %s",
12059 msg);
12060 else
12061 warning (OPT_Wdeprecated_declarations, "type is deprecated");
12067 /* Return true if REF has a COMPONENT_REF with a bit-field field declaration
12068 somewhere in it. */
12070 bool
12071 contains_bitfld_component_ref_p (const_tree ref)
12073 while (handled_component_p (ref))
12075 if (TREE_CODE (ref) == COMPONENT_REF
12076 && DECL_BIT_FIELD (TREE_OPERAND (ref, 1)))
12077 return true;
12078 ref = TREE_OPERAND (ref, 0);
12081 return false;
12084 /* Try to determine whether a TRY_CATCH expression can fall through.
12085 This is a subroutine of block_may_fallthru. */
12087 static bool
12088 try_catch_may_fallthru (const_tree stmt)
12090 tree_stmt_iterator i;
12092 /* If the TRY block can fall through, the whole TRY_CATCH can
12093 fall through. */
12094 if (block_may_fallthru (TREE_OPERAND (stmt, 0)))
12095 return true;
12097 i = tsi_start (TREE_OPERAND (stmt, 1));
12098 switch (TREE_CODE (tsi_stmt (i)))
12100 case CATCH_EXPR:
12101 /* We expect to see a sequence of CATCH_EXPR trees, each with a
12102 catch expression and a body. The whole TRY_CATCH may fall
12103 through iff any of the catch bodies falls through. */
12104 for (; !tsi_end_p (i); tsi_next (&i))
12106 if (block_may_fallthru (CATCH_BODY (tsi_stmt (i))))
12107 return true;
12109 return false;
12111 case EH_FILTER_EXPR:
12112 /* The exception filter expression only matters if there is an
12113 exception. If the exception does not match EH_FILTER_TYPES,
12114 we will execute EH_FILTER_FAILURE, and we will fall through
12115 if that falls through. If the exception does match
12116 EH_FILTER_TYPES, the stack unwinder will continue up the
12117 stack, so we will not fall through. We don't know whether we
12118 will throw an exception which matches EH_FILTER_TYPES or not,
12119 so we just ignore EH_FILTER_TYPES and assume that we might
12120 throw an exception which doesn't match. */
12121 return block_may_fallthru (EH_FILTER_FAILURE (tsi_stmt (i)));
12123 default:
12124 /* This case represents statements to be executed when an
12125 exception occurs. Those statements are implicitly followed
12126 by a RESX statement to resume execution after the exception.
12127 So in this case the TRY_CATCH never falls through. */
12128 return false;
12132 /* Try to determine if we can fall out of the bottom of BLOCK. This guess
12133 need not be 100% accurate; simply be conservative and return true if we
12134 don't know. This is used only to avoid stupidly generating extra code.
12135 If we're wrong, we'll just delete the extra code later. */
12137 bool
12138 block_may_fallthru (const_tree block)
12140 /* This CONST_CAST is okay because expr_last returns its argument
12141 unmodified and we assign it to a const_tree. */
12142 const_tree stmt = expr_last (CONST_CAST_TREE (block));
12144 switch (stmt ? TREE_CODE (stmt) : ERROR_MARK)
12146 case GOTO_EXPR:
12147 case RETURN_EXPR:
12148 /* Easy cases. If the last statement of the block implies
12149 control transfer, then we can't fall through. */
12150 return false;
12152 case SWITCH_EXPR:
12153 /* If SWITCH_LABELS is set, this is lowered, and represents a
12154 branch to a selected label and hence can not fall through.
12155 Otherwise SWITCH_BODY is set, and the switch can fall
12156 through. */
12157 return SWITCH_LABELS (stmt) == NULL_TREE;
12159 case COND_EXPR:
12160 if (block_may_fallthru (COND_EXPR_THEN (stmt)))
12161 return true;
12162 return block_may_fallthru (COND_EXPR_ELSE (stmt));
12164 case BIND_EXPR:
12165 return block_may_fallthru (BIND_EXPR_BODY (stmt));
12167 case TRY_CATCH_EXPR:
12168 return try_catch_may_fallthru (stmt);
12170 case TRY_FINALLY_EXPR:
12171 /* The finally clause is always executed after the try clause,
12172 so if it does not fall through, then the try-finally will not
12173 fall through. Otherwise, if the try clause does not fall
12174 through, then when the finally clause falls through it will
12175 resume execution wherever the try clause was going. So the
12176 whole try-finally will only fall through if both the try
12177 clause and the finally clause fall through. */
12178 return (block_may_fallthru (TREE_OPERAND (stmt, 0))
12179 && block_may_fallthru (TREE_OPERAND (stmt, 1)));
12181 case MODIFY_EXPR:
12182 if (TREE_CODE (TREE_OPERAND (stmt, 1)) == CALL_EXPR)
12183 stmt = TREE_OPERAND (stmt, 1);
12184 else
12185 return true;
12186 /* FALLTHRU */
12188 case CALL_EXPR:
12189 /* Functions that do not return do not fall through. */
12190 return (call_expr_flags (stmt) & ECF_NORETURN) == 0;
12192 case CLEANUP_POINT_EXPR:
12193 return block_may_fallthru (TREE_OPERAND (stmt, 0));
12195 case TARGET_EXPR:
12196 return block_may_fallthru (TREE_OPERAND (stmt, 1));
12198 case ERROR_MARK:
12199 return true;
12201 default:
12202 return lang_hooks.block_may_fallthru (stmt);
12206 /* True if we are using EH to handle cleanups. */
12207 static bool using_eh_for_cleanups_flag = false;
12209 /* This routine is called from front ends to indicate eh should be used for
12210 cleanups. */
12211 void
12212 using_eh_for_cleanups (void)
12214 using_eh_for_cleanups_flag = true;
12217 /* Query whether EH is used for cleanups. */
12218 bool
12219 using_eh_for_cleanups_p (void)
12221 return using_eh_for_cleanups_flag;
12224 /* Wrapper for tree_code_name to ensure that tree code is valid */
12225 const char *
12226 get_tree_code_name (enum tree_code code)
12228 const char *invalid = "<invalid tree code>";
12230 if (code >= MAX_TREE_CODES)
12231 return invalid;
12233 return tree_code_name[code];
12236 /* Drops the TREE_OVERFLOW flag from T. */
12238 tree
12239 drop_tree_overflow (tree t)
12241 gcc_checking_assert (TREE_OVERFLOW (t));
12243 /* For tree codes with a sharing machinery re-build the result. */
12244 if (TREE_CODE (t) == INTEGER_CST)
12245 return wide_int_to_tree (TREE_TYPE (t), t);
12247 /* Otherwise, as all tcc_constants are possibly shared, copy the node
12248 and drop the flag. */
12249 t = copy_node (t);
12250 TREE_OVERFLOW (t) = 0;
12251 return t;
12254 /* Given a memory reference expression T, return its base address.
12255 The base address of a memory reference expression is the main
12256 object being referenced. For instance, the base address for
12257 'array[i].fld[j]' is 'array'. You can think of this as stripping
12258 away the offset part from a memory address.
12260 This function calls handled_component_p to strip away all the inner
12261 parts of the memory reference until it reaches the base object. */
12263 tree
12264 get_base_address (tree t)
12266 while (handled_component_p (t))
12267 t = TREE_OPERAND (t, 0);
12269 if ((TREE_CODE (t) == MEM_REF
12270 || TREE_CODE (t) == TARGET_MEM_REF)
12271 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR)
12272 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
12274 /* ??? Either the alias oracle or all callers need to properly deal
12275 with WITH_SIZE_EXPRs before we can look through those. */
12276 if (TREE_CODE (t) == WITH_SIZE_EXPR)
12277 return NULL_TREE;
12279 return t;
12282 #include "gt-tree.h"