Merge trunk version 210928 into gupc branch.
[official-gcc.git] / gcc / tree.c
blobaedc469cf62a43334318db246debc18ea819d285
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 "tree-upc.h"
37 #include "stor-layout.h"
38 #include "calls.h"
39 #include "attribs.h"
40 #include "varasm.h"
41 #include "tm_p.h"
42 #include "function.h"
43 #include "obstack.h"
44 #include "toplev.h" /* get_random_seed */
45 #include "hashtab.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 "pointer-set.h"
56 #include "tree-ssa-alias.h"
57 #include "internal-fn.h"
58 #include "gimple-expr.h"
59 #include "is-a.h"
60 #include "gimple.h"
61 #include "gimple-iterator.h"
62 #include "gimplify.h"
63 #include "gimple-ssa.h"
64 #include "cgraph.h"
65 #include "tree-phinodes.h"
66 #include "stringpool.h"
67 #include "tree-ssanames.h"
68 #include "expr.h"
69 #include "tree-dfa.h"
70 #include "params.h"
71 #include "tree-pass.h"
72 #include "langhooks-def.h"
73 #include "diagnostic.h"
74 #include "tree-diagnostic.h"
75 #include "tree-pretty-print.h"
76 #include "except.h"
77 #include "debug.h"
78 #include "intl.h"
79 #include "wide-int.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;
196 /* Hash table and temporary node for larger integer const values. */
197 static GTY (()) tree int_cst_node;
198 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node)))
199 htab_t int_cst_hash_table;
201 /* Hash table for optimization flags and target option flags. Use the same
202 hash table for both sets of options. Nodes for building the current
203 optimization and target option nodes. The assumption is most of the time
204 the options created will already be in the hash table, so we avoid
205 allocating and freeing up a node repeatably. */
206 static GTY (()) tree cl_optimization_node;
207 static GTY (()) tree cl_target_option_node;
208 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node)))
209 htab_t cl_option_hash_table;
211 /* General tree->tree mapping structure for use in hash tables. */
214 static GTY ((if_marked ("tree_decl_map_marked_p"), param_is (struct tree_decl_map)))
215 htab_t debug_expr_for_decl;
217 static GTY ((if_marked ("tree_decl_map_marked_p"), param_is (struct tree_decl_map)))
218 htab_t value_expr_for_decl;
220 static GTY ((if_marked ("tree_vec_map_marked_p"), param_is (struct tree_vec_map)))
221 htab_t debug_args_for_decl;
223 static GTY ((if_marked ("tree_priority_map_marked_p"),
224 param_is (struct tree_priority_map)))
225 htab_t init_priority_for_decl;
227 static void set_type_quals (tree, int, tree);
228 static int type_hash_eq (const void *, const void *);
229 static hashval_t type_hash_hash (const void *);
230 static hashval_t int_cst_hash_hash (const void *);
231 static int int_cst_hash_eq (const void *, const void *);
232 static hashval_t cl_option_hash_hash (const void *);
233 static int cl_option_hash_eq (const void *, const void *);
234 static void print_type_hash_statistics (void);
235 static void print_debug_expr_statistics (void);
236 static void print_value_expr_statistics (void);
237 static int type_hash_marked_p (const void *);
238 static unsigned int type_hash_list (const_tree, hashval_t);
239 static unsigned int attribute_hash_list (const_tree, hashval_t);
240 static bool decls_same_for_odr (tree decl1, tree decl2);
242 tree global_trees[TI_MAX];
243 tree integer_types[itk_none];
245 unsigned char tree_contains_struct[MAX_TREE_CODES][64];
247 /* Number of operands for each OpenMP clause. */
248 unsigned const char omp_clause_num_ops[] =
250 0, /* OMP_CLAUSE_ERROR */
251 1, /* OMP_CLAUSE_PRIVATE */
252 1, /* OMP_CLAUSE_SHARED */
253 1, /* OMP_CLAUSE_FIRSTPRIVATE */
254 2, /* OMP_CLAUSE_LASTPRIVATE */
255 4, /* OMP_CLAUSE_REDUCTION */
256 1, /* OMP_CLAUSE_COPYIN */
257 1, /* OMP_CLAUSE_COPYPRIVATE */
258 3, /* OMP_CLAUSE_LINEAR */
259 2, /* OMP_CLAUSE_ALIGNED */
260 1, /* OMP_CLAUSE_DEPEND */
261 1, /* OMP_CLAUSE_UNIFORM */
262 2, /* OMP_CLAUSE_FROM */
263 2, /* OMP_CLAUSE_TO */
264 2, /* OMP_CLAUSE_MAP */
265 1, /* OMP_CLAUSE__LOOPTEMP_ */
266 1, /* OMP_CLAUSE_IF */
267 1, /* OMP_CLAUSE_NUM_THREADS */
268 1, /* OMP_CLAUSE_SCHEDULE */
269 0, /* OMP_CLAUSE_NOWAIT */
270 0, /* OMP_CLAUSE_ORDERED */
271 0, /* OMP_CLAUSE_DEFAULT */
272 3, /* OMP_CLAUSE_COLLAPSE */
273 0, /* OMP_CLAUSE_UNTIED */
274 1, /* OMP_CLAUSE_FINAL */
275 0, /* OMP_CLAUSE_MERGEABLE */
276 1, /* OMP_CLAUSE_DEVICE */
277 1, /* OMP_CLAUSE_DIST_SCHEDULE */
278 0, /* OMP_CLAUSE_INBRANCH */
279 0, /* OMP_CLAUSE_NOTINBRANCH */
280 1, /* OMP_CLAUSE_NUM_TEAMS */
281 1, /* OMP_CLAUSE_THREAD_LIMIT */
282 0, /* OMP_CLAUSE_PROC_BIND */
283 1, /* OMP_CLAUSE_SAFELEN */
284 1, /* OMP_CLAUSE_SIMDLEN */
285 0, /* OMP_CLAUSE_FOR */
286 0, /* OMP_CLAUSE_PARALLEL */
287 0, /* OMP_CLAUSE_SECTIONS */
288 0, /* OMP_CLAUSE_TASKGROUP */
289 1, /* OMP_CLAUSE__SIMDUID_ */
292 const char * const omp_clause_code_name[] =
294 "error_clause",
295 "private",
296 "shared",
297 "firstprivate",
298 "lastprivate",
299 "reduction",
300 "copyin",
301 "copyprivate",
302 "linear",
303 "aligned",
304 "depend",
305 "uniform",
306 "from",
307 "to",
308 "map",
309 "_looptemp_",
310 "if",
311 "num_threads",
312 "schedule",
313 "nowait",
314 "ordered",
315 "default",
316 "collapse",
317 "untied",
318 "final",
319 "mergeable",
320 "device",
321 "dist_schedule",
322 "inbranch",
323 "notinbranch",
324 "num_teams",
325 "thread_limit",
326 "proc_bind",
327 "safelen",
328 "simdlen",
329 "for",
330 "parallel",
331 "sections",
332 "taskgroup",
333 "_simduid_"
337 /* Return the tree node structure used by tree code CODE. */
339 static inline enum tree_node_structure_enum
340 tree_node_structure_for_code (enum tree_code code)
342 switch (TREE_CODE_CLASS (code))
344 case tcc_declaration:
346 switch (code)
348 case FIELD_DECL:
349 return TS_FIELD_DECL;
350 case PARM_DECL:
351 return TS_PARM_DECL;
352 case VAR_DECL:
353 return TS_VAR_DECL;
354 case LABEL_DECL:
355 return TS_LABEL_DECL;
356 case RESULT_DECL:
357 return TS_RESULT_DECL;
358 case DEBUG_EXPR_DECL:
359 return TS_DECL_WRTL;
360 case CONST_DECL:
361 return TS_CONST_DECL;
362 case TYPE_DECL:
363 return TS_TYPE_DECL;
364 case FUNCTION_DECL:
365 return TS_FUNCTION_DECL;
366 case TRANSLATION_UNIT_DECL:
367 return TS_TRANSLATION_UNIT_DECL;
368 default:
369 return TS_DECL_NON_COMMON;
372 case tcc_type:
373 return TS_TYPE_NON_COMMON;
374 case tcc_reference:
375 case tcc_comparison:
376 case tcc_unary:
377 case tcc_binary:
378 case tcc_expression:
379 case tcc_statement:
380 case tcc_vl_exp:
381 return TS_EXP;
382 default: /* tcc_constant and tcc_exceptional */
383 break;
385 switch (code)
387 /* tcc_constant cases. */
388 case VOID_CST: return TS_TYPED;
389 case INTEGER_CST: return TS_INT_CST;
390 case REAL_CST: return TS_REAL_CST;
391 case FIXED_CST: return TS_FIXED_CST;
392 case COMPLEX_CST: return TS_COMPLEX;
393 case VECTOR_CST: return TS_VECTOR;
394 case STRING_CST: return TS_STRING;
395 /* tcc_exceptional cases. */
396 case ERROR_MARK: return TS_COMMON;
397 case IDENTIFIER_NODE: return TS_IDENTIFIER;
398 case TREE_LIST: return TS_LIST;
399 case TREE_VEC: return TS_VEC;
400 case SSA_NAME: return TS_SSA_NAME;
401 case PLACEHOLDER_EXPR: return TS_COMMON;
402 case STATEMENT_LIST: return TS_STATEMENT_LIST;
403 case BLOCK: return TS_BLOCK;
404 case CONSTRUCTOR: return TS_CONSTRUCTOR;
405 case TREE_BINFO: return TS_BINFO;
406 case OMP_CLAUSE: return TS_OMP_CLAUSE;
407 case OPTIMIZATION_NODE: return TS_OPTIMIZATION;
408 case TARGET_OPTION_NODE: return TS_TARGET_OPTION;
410 default:
411 gcc_unreachable ();
416 /* Initialize tree_contains_struct to describe the hierarchy of tree
417 nodes. */
419 static void
420 initialize_tree_contains_struct (void)
422 unsigned i;
424 for (i = ERROR_MARK; i < LAST_AND_UNUSED_TREE_CODE; i++)
426 enum tree_code code;
427 enum tree_node_structure_enum ts_code;
429 code = (enum tree_code) i;
430 ts_code = tree_node_structure_for_code (code);
432 /* Mark the TS structure itself. */
433 tree_contains_struct[code][ts_code] = 1;
435 /* Mark all the structures that TS is derived from. */
436 switch (ts_code)
438 case TS_TYPED:
439 case TS_BLOCK:
440 MARK_TS_BASE (code);
441 break;
443 case TS_COMMON:
444 case TS_INT_CST:
445 case TS_REAL_CST:
446 case TS_FIXED_CST:
447 case TS_VECTOR:
448 case TS_STRING:
449 case TS_COMPLEX:
450 case TS_SSA_NAME:
451 case TS_CONSTRUCTOR:
452 case TS_EXP:
453 case TS_STATEMENT_LIST:
454 MARK_TS_TYPED (code);
455 break;
457 case TS_IDENTIFIER:
458 case TS_DECL_MINIMAL:
459 case TS_TYPE_COMMON:
460 case TS_LIST:
461 case TS_VEC:
462 case TS_BINFO:
463 case TS_OMP_CLAUSE:
464 case TS_OPTIMIZATION:
465 case TS_TARGET_OPTION:
466 MARK_TS_COMMON (code);
467 break;
469 case TS_TYPE_WITH_LANG_SPECIFIC:
470 MARK_TS_TYPE_COMMON (code);
471 break;
473 case TS_TYPE_NON_COMMON:
474 MARK_TS_TYPE_WITH_LANG_SPECIFIC (code);
475 break;
477 case TS_DECL_COMMON:
478 MARK_TS_DECL_MINIMAL (code);
479 break;
481 case TS_DECL_WRTL:
482 case TS_CONST_DECL:
483 MARK_TS_DECL_COMMON (code);
484 break;
486 case TS_DECL_NON_COMMON:
487 MARK_TS_DECL_WITH_VIS (code);
488 break;
490 case TS_DECL_WITH_VIS:
491 case TS_PARM_DECL:
492 case TS_LABEL_DECL:
493 case TS_RESULT_DECL:
494 MARK_TS_DECL_WRTL (code);
495 break;
497 case TS_FIELD_DECL:
498 MARK_TS_DECL_COMMON (code);
499 break;
501 case TS_VAR_DECL:
502 MARK_TS_DECL_WITH_VIS (code);
503 break;
505 case TS_TYPE_DECL:
506 case TS_FUNCTION_DECL:
507 MARK_TS_DECL_NON_COMMON (code);
508 break;
510 case TS_TRANSLATION_UNIT_DECL:
511 MARK_TS_DECL_COMMON (code);
512 break;
514 default:
515 gcc_unreachable ();
519 /* Basic consistency checks for attributes used in fold. */
520 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_NON_COMMON]);
521 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_NON_COMMON]);
522 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_COMMON]);
523 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_COMMON]);
524 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_COMMON]);
525 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_COMMON]);
526 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_COMMON]);
527 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_COMMON]);
528 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_COMMON]);
529 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_COMMON]);
530 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_COMMON]);
531 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WRTL]);
532 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_WRTL]);
533 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_WRTL]);
534 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WRTL]);
535 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_WRTL]);
536 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_MINIMAL]);
537 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_MINIMAL]);
538 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_MINIMAL]);
539 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_MINIMAL]);
540 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_MINIMAL]);
541 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_MINIMAL]);
542 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_MINIMAL]);
543 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_MINIMAL]);
544 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_MINIMAL]);
545 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WITH_VIS]);
546 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WITH_VIS]);
547 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_WITH_VIS]);
548 gcc_assert (tree_contains_struct[VAR_DECL][TS_VAR_DECL]);
549 gcc_assert (tree_contains_struct[FIELD_DECL][TS_FIELD_DECL]);
550 gcc_assert (tree_contains_struct[PARM_DECL][TS_PARM_DECL]);
551 gcc_assert (tree_contains_struct[LABEL_DECL][TS_LABEL_DECL]);
552 gcc_assert (tree_contains_struct[RESULT_DECL][TS_RESULT_DECL]);
553 gcc_assert (tree_contains_struct[CONST_DECL][TS_CONST_DECL]);
554 gcc_assert (tree_contains_struct[TYPE_DECL][TS_TYPE_DECL]);
555 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_FUNCTION_DECL]);
556 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_MINIMAL]);
557 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_COMMON]);
558 gcc_assert (tree_contains_struct[NAMELIST_DECL][TS_DECL_MINIMAL]);
559 gcc_assert (tree_contains_struct[NAMELIST_DECL][TS_DECL_COMMON]);
563 /* Init tree.c. */
565 void
566 init_ttree (void)
568 /* Initialize the hash table of types. */
569 type_hash_table = htab_create_ggc (TYPE_HASH_INITIAL_SIZE, type_hash_hash,
570 type_hash_eq, 0);
572 /* Initialize hash table used to manage UPC blocking factors. */
573 upc_block_factor_lookup_init ();
575 debug_expr_for_decl = htab_create_ggc (512, tree_decl_map_hash,
576 tree_decl_map_eq, 0);
578 value_expr_for_decl = htab_create_ggc (512, tree_decl_map_hash,
579 tree_decl_map_eq, 0);
580 init_priority_for_decl = htab_create_ggc (512, tree_priority_map_hash,
581 tree_priority_map_eq, 0);
583 int_cst_hash_table = htab_create_ggc (1024, int_cst_hash_hash,
584 int_cst_hash_eq, NULL);
586 int_cst_node = make_int_cst (1, 1);
588 cl_option_hash_table = htab_create_ggc (64, cl_option_hash_hash,
589 cl_option_hash_eq, NULL);
591 cl_optimization_node = make_node (OPTIMIZATION_NODE);
592 cl_target_option_node = make_node (TARGET_OPTION_NODE);
594 /* Initialize the tree_contains_struct array. */
595 initialize_tree_contains_struct ();
596 lang_hooks.init_ts ();
600 /* The name of the object as the assembler will see it (but before any
601 translations made by ASM_OUTPUT_LABELREF). Often this is the same
602 as DECL_NAME. It is an IDENTIFIER_NODE. */
603 tree
604 decl_assembler_name (tree decl)
606 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
607 lang_hooks.set_decl_assembler_name (decl);
608 return DECL_WITH_VIS_CHECK (decl)->decl_with_vis.assembler_name;
611 /* Compute the number of bytes occupied by a tree with code CODE.
612 This function cannot be used for nodes that have variable sizes,
613 including TREE_VEC, INTEGER_CST, STRING_CST, and CALL_EXPR. */
614 size_t
615 tree_code_size (enum tree_code code)
617 switch (TREE_CODE_CLASS (code))
619 case tcc_declaration: /* A decl node */
621 switch (code)
623 case FIELD_DECL:
624 return sizeof (struct tree_field_decl);
625 case PARM_DECL:
626 return sizeof (struct tree_parm_decl);
627 case VAR_DECL:
628 return sizeof (struct tree_var_decl);
629 case LABEL_DECL:
630 return sizeof (struct tree_label_decl);
631 case RESULT_DECL:
632 return sizeof (struct tree_result_decl);
633 case CONST_DECL:
634 return sizeof (struct tree_const_decl);
635 case TYPE_DECL:
636 return sizeof (struct tree_type_decl);
637 case FUNCTION_DECL:
638 return sizeof (struct tree_function_decl);
639 case DEBUG_EXPR_DECL:
640 return sizeof (struct tree_decl_with_rtl);
641 default:
642 return sizeof (struct tree_decl_non_common);
646 case tcc_type: /* a type node */
647 return sizeof (struct tree_type_non_common);
649 case tcc_reference: /* a reference */
650 case tcc_expression: /* an expression */
651 case tcc_statement: /* an expression with side effects */
652 case tcc_comparison: /* a comparison expression */
653 case tcc_unary: /* a unary arithmetic expression */
654 case tcc_binary: /* a binary arithmetic expression */
655 return (sizeof (struct tree_exp)
656 + (TREE_CODE_LENGTH (code) - 1) * sizeof (tree));
658 case tcc_constant: /* a constant */
659 switch (code)
661 case VOID_CST: return sizeof (struct tree_typed);
662 case INTEGER_CST: gcc_unreachable ();
663 case REAL_CST: return sizeof (struct tree_real_cst);
664 case FIXED_CST: return sizeof (struct tree_fixed_cst);
665 case COMPLEX_CST: return sizeof (struct tree_complex);
666 case VECTOR_CST: return sizeof (struct tree_vector);
667 case STRING_CST: gcc_unreachable ();
668 default:
669 return lang_hooks.tree_size (code);
672 case tcc_exceptional: /* something random, like an identifier. */
673 switch (code)
675 case IDENTIFIER_NODE: return lang_hooks.identifier_size;
676 case TREE_LIST: return sizeof (struct tree_list);
678 case ERROR_MARK:
679 case PLACEHOLDER_EXPR: return sizeof (struct tree_common);
681 case TREE_VEC:
682 case OMP_CLAUSE: gcc_unreachable ();
684 case SSA_NAME: return sizeof (struct tree_ssa_name);
686 case STATEMENT_LIST: return sizeof (struct tree_statement_list);
687 case BLOCK: return sizeof (struct tree_block);
688 case CONSTRUCTOR: return sizeof (struct tree_constructor);
689 case OPTIMIZATION_NODE: return sizeof (struct tree_optimization_option);
690 case TARGET_OPTION_NODE: return sizeof (struct tree_target_option);
692 default:
693 return lang_hooks.tree_size (code);
696 default:
697 gcc_unreachable ();
701 /* Compute the number of bytes occupied by NODE. This routine only
702 looks at TREE_CODE, except for those nodes that have variable sizes. */
703 size_t
704 tree_size (const_tree node)
706 const enum tree_code code = TREE_CODE (node);
707 switch (code)
709 case INTEGER_CST:
710 return (sizeof (struct tree_int_cst)
711 + (TREE_INT_CST_EXT_NUNITS (node) - 1) * sizeof (HOST_WIDE_INT));
713 case TREE_BINFO:
714 return (offsetof (struct tree_binfo, base_binfos)
715 + vec<tree, va_gc>
716 ::embedded_size (BINFO_N_BASE_BINFOS (node)));
718 case TREE_VEC:
719 return (sizeof (struct tree_vec)
720 + (TREE_VEC_LENGTH (node) - 1) * sizeof (tree));
722 case VECTOR_CST:
723 return (sizeof (struct tree_vector)
724 + (TYPE_VECTOR_SUBPARTS (TREE_TYPE (node)) - 1) * sizeof (tree));
726 case STRING_CST:
727 return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
729 case OMP_CLAUSE:
730 return (sizeof (struct tree_omp_clause)
731 + (omp_clause_num_ops[OMP_CLAUSE_CODE (node)] - 1)
732 * sizeof (tree));
734 default:
735 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
736 return (sizeof (struct tree_exp)
737 + (VL_EXP_OPERAND_LENGTH (node) - 1) * sizeof (tree));
738 else
739 return tree_code_size (code);
743 /* Record interesting allocation statistics for a tree node with CODE
744 and LENGTH. */
746 static void
747 record_node_allocation_statistics (enum tree_code code ATTRIBUTE_UNUSED,
748 size_t length ATTRIBUTE_UNUSED)
750 enum tree_code_class type = TREE_CODE_CLASS (code);
751 tree_node_kind kind;
753 if (!GATHER_STATISTICS)
754 return;
756 switch (type)
758 case tcc_declaration: /* A decl node */
759 kind = d_kind;
760 break;
762 case tcc_type: /* a type node */
763 kind = t_kind;
764 break;
766 case tcc_statement: /* an expression with side effects */
767 kind = s_kind;
768 break;
770 case tcc_reference: /* a reference */
771 kind = r_kind;
772 break;
774 case tcc_expression: /* an expression */
775 case tcc_comparison: /* a comparison expression */
776 case tcc_unary: /* a unary arithmetic expression */
777 case tcc_binary: /* a binary arithmetic expression */
778 kind = e_kind;
779 break;
781 case tcc_constant: /* a constant */
782 kind = c_kind;
783 break;
785 case tcc_exceptional: /* something random, like an identifier. */
786 switch (code)
788 case IDENTIFIER_NODE:
789 kind = id_kind;
790 break;
792 case TREE_VEC:
793 kind = vec_kind;
794 break;
796 case TREE_BINFO:
797 kind = binfo_kind;
798 break;
800 case SSA_NAME:
801 kind = ssa_name_kind;
802 break;
804 case BLOCK:
805 kind = b_kind;
806 break;
808 case CONSTRUCTOR:
809 kind = constr_kind;
810 break;
812 case OMP_CLAUSE:
813 kind = omp_clause_kind;
814 break;
816 default:
817 kind = x_kind;
818 break;
820 break;
822 case tcc_vl_exp:
823 kind = e_kind;
824 break;
826 default:
827 gcc_unreachable ();
830 tree_code_counts[(int) code]++;
831 tree_node_counts[(int) kind]++;
832 tree_node_sizes[(int) kind] += length;
835 /* Allocate and return a new UID from the DECL_UID namespace. */
838 allocate_decl_uid (void)
840 return next_decl_uid++;
843 /* Return a newly allocated node of code CODE. For decl and type
844 nodes, some other fields are initialized. The rest of the node is
845 initialized to zero. This function cannot be used for TREE_VEC,
846 INTEGER_CST or OMP_CLAUSE nodes, which is enforced by asserts in
847 tree_code_size.
849 Achoo! I got a code in the node. */
851 tree
852 make_node_stat (enum tree_code code MEM_STAT_DECL)
854 tree t;
855 enum tree_code_class type = TREE_CODE_CLASS (code);
856 size_t length = tree_code_size (code);
858 record_node_allocation_statistics (code, length);
860 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
861 TREE_SET_CODE (t, code);
863 switch (type)
865 case tcc_statement:
866 TREE_SIDE_EFFECTS (t) = 1;
867 break;
869 case tcc_declaration:
870 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
872 if (code == FUNCTION_DECL)
874 DECL_ALIGN (t) = FUNCTION_BOUNDARY;
875 DECL_MODE (t) = FUNCTION_MODE;
877 else
878 DECL_ALIGN (t) = 1;
880 DECL_SOURCE_LOCATION (t) = input_location;
881 if (TREE_CODE (t) == DEBUG_EXPR_DECL)
882 DECL_UID (t) = --next_debug_decl_uid;
883 else
885 DECL_UID (t) = allocate_decl_uid ();
886 SET_DECL_PT_UID (t, -1);
888 if (TREE_CODE (t) == LABEL_DECL)
889 LABEL_DECL_UID (t) = -1;
891 break;
893 case tcc_type:
894 TYPE_UID (t) = next_type_uid++;
895 TYPE_ALIGN (t) = BITS_PER_UNIT;
896 TYPE_USER_ALIGN (t) = 0;
897 TYPE_MAIN_VARIANT (t) = t;
898 TYPE_CANONICAL (t) = t;
900 /* Default to no attributes for type, but let target change that. */
901 TYPE_ATTRIBUTES (t) = NULL_TREE;
902 targetm.set_default_type_attributes (t);
904 /* We have not yet computed the alias set for this type. */
905 TYPE_ALIAS_SET (t) = -1;
906 break;
908 case tcc_constant:
909 TREE_CONSTANT (t) = 1;
910 break;
912 case tcc_expression:
913 switch (code)
915 case INIT_EXPR:
916 case MODIFY_EXPR:
917 case VA_ARG_EXPR:
918 case PREDECREMENT_EXPR:
919 case PREINCREMENT_EXPR:
920 case POSTDECREMENT_EXPR:
921 case POSTINCREMENT_EXPR:
922 /* All of these have side-effects, no matter what their
923 operands are. */
924 TREE_SIDE_EFFECTS (t) = 1;
925 break;
927 default:
928 break;
930 break;
932 default:
933 /* Other classes need no special treatment. */
934 break;
937 return t;
940 /* Return a new node with the same contents as NODE except that its
941 TREE_CHAIN, if it has one, is zero and it has a fresh uid. */
943 tree
944 copy_node_stat (tree node MEM_STAT_DECL)
946 tree t;
947 enum tree_code code = TREE_CODE (node);
948 size_t length;
950 gcc_assert (code != STATEMENT_LIST);
952 length = tree_size (node);
953 record_node_allocation_statistics (code, length);
954 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
955 memcpy (t, node, length);
957 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
958 TREE_CHAIN (t) = 0;
959 TREE_ASM_WRITTEN (t) = 0;
960 TREE_VISITED (t) = 0;
962 if (TREE_CODE_CLASS (code) == tcc_declaration)
964 if (code == DEBUG_EXPR_DECL)
965 DECL_UID (t) = --next_debug_decl_uid;
966 else
968 DECL_UID (t) = allocate_decl_uid ();
969 if (DECL_PT_UID_SET_P (node))
970 SET_DECL_PT_UID (t, DECL_PT_UID (node));
972 if ((TREE_CODE (node) == PARM_DECL || TREE_CODE (node) == VAR_DECL)
973 && DECL_HAS_VALUE_EXPR_P (node))
975 SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (node));
976 DECL_HAS_VALUE_EXPR_P (t) = 1;
978 /* DECL_DEBUG_EXPR is copied explicitely by callers. */
979 if (TREE_CODE (node) == VAR_DECL)
981 DECL_HAS_DEBUG_EXPR_P (t) = 0;
982 t->decl_with_vis.symtab_node = NULL;
984 if (TREE_CODE (node) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (node))
986 SET_DECL_INIT_PRIORITY (t, DECL_INIT_PRIORITY (node));
987 DECL_HAS_INIT_PRIORITY_P (t) = 1;
989 if (TREE_CODE (node) == FUNCTION_DECL)
991 DECL_STRUCT_FUNCTION (t) = NULL;
992 t->decl_with_vis.symtab_node = NULL;
995 else if (TREE_CODE_CLASS (code) == tcc_type)
997 TYPE_UID (t) = next_type_uid++;
998 /* The following is so that the debug code for
999 the copy is different from the original type.
1000 The two statements usually duplicate each other
1001 (because they clear fields of the same union),
1002 but the optimizer should catch that. */
1003 TYPE_SYMTAB_POINTER (t) = 0;
1004 TYPE_SYMTAB_ADDRESS (t) = 0;
1006 /* Do not copy the values cache. */
1007 if (TYPE_CACHED_VALUES_P (t))
1009 TYPE_CACHED_VALUES_P (t) = 0;
1010 TYPE_CACHED_VALUES (t) = NULL_TREE;
1013 if (TYPE_HAS_UPC_BLOCK_FACTOR (node))
1014 SET_TYPE_UPC_BLOCK_FACTOR (t, TYPE_UPC_BLOCK_FACTOR (node));
1017 return t;
1020 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
1021 For example, this can copy a list made of TREE_LIST nodes. */
1023 tree
1024 copy_list (tree list)
1026 tree head;
1027 tree prev, next;
1029 if (list == 0)
1030 return 0;
1032 head = prev = copy_node (list);
1033 next = TREE_CHAIN (list);
1034 while (next)
1036 TREE_CHAIN (prev) = copy_node (next);
1037 prev = TREE_CHAIN (prev);
1038 next = TREE_CHAIN (next);
1040 return head;
1044 /* Return the value that TREE_INT_CST_EXT_NUNITS should have for an
1045 INTEGER_CST with value CST and type TYPE. */
1047 static unsigned int
1048 get_int_cst_ext_nunits (tree type, const wide_int &cst)
1050 gcc_checking_assert (cst.get_precision () == TYPE_PRECISION (type));
1051 /* We need an extra zero HWI if CST is an unsigned integer with its
1052 upper bit set, and if CST occupies a whole number of HWIs. */
1053 if (TYPE_UNSIGNED (type)
1054 && wi::neg_p (cst)
1055 && (cst.get_precision () % HOST_BITS_PER_WIDE_INT) == 0)
1056 return cst.get_precision () / HOST_BITS_PER_WIDE_INT + 1;
1057 return cst.get_len ();
1060 /* Return a new INTEGER_CST with value CST and type TYPE. */
1062 static tree
1063 build_new_int_cst (tree type, const wide_int &cst)
1065 unsigned int len = cst.get_len ();
1066 unsigned int ext_len = get_int_cst_ext_nunits (type, cst);
1067 tree nt = make_int_cst (len, ext_len);
1069 if (len < ext_len)
1071 --ext_len;
1072 TREE_INT_CST_ELT (nt, ext_len) = 0;
1073 for (unsigned int i = len; i < ext_len; ++i)
1074 TREE_INT_CST_ELT (nt, i) = -1;
1076 else if (TYPE_UNSIGNED (type)
1077 && cst.get_precision () < len * HOST_BITS_PER_WIDE_INT)
1079 len--;
1080 TREE_INT_CST_ELT (nt, len)
1081 = zext_hwi (cst.elt (len),
1082 cst.get_precision () % HOST_BITS_PER_WIDE_INT);
1085 for (unsigned int i = 0; i < len; i++)
1086 TREE_INT_CST_ELT (nt, i) = cst.elt (i);
1087 TREE_TYPE (nt) = type;
1088 return nt;
1091 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1093 tree
1094 build_int_cst (tree type, HOST_WIDE_INT low)
1096 /* Support legacy code. */
1097 if (!type)
1098 type = integer_type_node;
1100 return wide_int_to_tree (type, wi::shwi (low, TYPE_PRECISION (type)));
1103 tree
1104 build_int_cstu (tree type, unsigned HOST_WIDE_INT cst)
1106 return wide_int_to_tree (type, wi::uhwi (cst, TYPE_PRECISION (type)));
1109 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1111 tree
1112 build_int_cst_type (tree type, HOST_WIDE_INT low)
1114 gcc_assert (type);
1115 return wide_int_to_tree (type, wi::shwi (low, TYPE_PRECISION (type)));
1118 /* Constructs tree in type TYPE from with value given by CST. Signedness
1119 of CST is assumed to be the same as the signedness of TYPE. */
1121 tree
1122 double_int_to_tree (tree type, double_int cst)
1124 return wide_int_to_tree (type, widest_int::from (cst, TYPE_SIGN (type)));
1127 /* We force the wide_int CST to the range of the type TYPE by sign or
1128 zero extending it. OVERFLOWABLE indicates if we are interested in
1129 overflow of the value, when >0 we are only interested in signed
1130 overflow, for <0 we are interested in any overflow. OVERFLOWED
1131 indicates whether overflow has already occurred. CONST_OVERFLOWED
1132 indicates whether constant overflow has already occurred. We force
1133 T's value to be within range of T's type (by setting to 0 or 1 all
1134 the bits outside the type's range). We set TREE_OVERFLOWED if,
1135 OVERFLOWED is nonzero,
1136 or OVERFLOWABLE is >0 and signed overflow occurs
1137 or OVERFLOWABLE is <0 and any overflow occurs
1138 We return a new tree node for the extended wide_int. The node
1139 is shared if no overflow flags are set. */
1142 tree
1143 force_fit_type (tree type, const wide_int_ref &cst,
1144 int overflowable, bool overflowed)
1146 signop sign = TYPE_SIGN (type);
1148 /* If we need to set overflow flags, return a new unshared node. */
1149 if (overflowed || !wi::fits_to_tree_p (cst, type))
1151 if (overflowed
1152 || overflowable < 0
1153 || (overflowable > 0 && sign == SIGNED))
1155 wide_int tmp = wide_int::from (cst, TYPE_PRECISION (type), sign);
1156 tree t = build_new_int_cst (type, tmp);
1157 TREE_OVERFLOW (t) = 1;
1158 return t;
1162 /* Else build a shared node. */
1163 return wide_int_to_tree (type, cst);
1166 /* These are the hash table functions for the hash table of INTEGER_CST
1167 nodes of a sizetype. */
1169 /* Return the hash code code X, an INTEGER_CST. */
1171 static hashval_t
1172 int_cst_hash_hash (const void *x)
1174 const_tree const t = (const_tree) x;
1175 hashval_t code = htab_hash_pointer (TREE_TYPE (t));
1176 int i;
1178 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
1179 code ^= TREE_INT_CST_ELT (t, i);
1181 return code;
1184 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
1185 is the same as that given by *Y, which is the same. */
1187 static int
1188 int_cst_hash_eq (const void *x, const void *y)
1190 const_tree const xt = (const_tree) x;
1191 const_tree const yt = (const_tree) y;
1193 if (TREE_TYPE (xt) != TREE_TYPE (yt)
1194 || TREE_INT_CST_NUNITS (xt) != TREE_INT_CST_NUNITS (yt)
1195 || TREE_INT_CST_EXT_NUNITS (xt) != TREE_INT_CST_EXT_NUNITS (yt))
1196 return false;
1198 for (int i = 0; i < TREE_INT_CST_NUNITS (xt); i++)
1199 if (TREE_INT_CST_ELT (xt, i) != TREE_INT_CST_ELT (yt, i))
1200 return false;
1202 return true;
1205 /* Create an INT_CST node of TYPE and value CST.
1206 The returned node is always shared. For small integers we use a
1207 per-type vector cache, for larger ones we use a single hash table.
1208 The value is extended from its precision according to the sign of
1209 the type to be a multiple of HOST_BITS_PER_WIDE_INT. This defines
1210 the upper bits and ensures that hashing and value equality based
1211 upon the underlying HOST_WIDE_INTs works without masking. */
1213 tree
1214 wide_int_to_tree (tree type, const wide_int_ref &pcst)
1216 tree t;
1217 int ix = -1;
1218 int limit = 0;
1220 gcc_assert (type);
1221 unsigned int prec = TYPE_PRECISION (type);
1222 signop sgn = TYPE_SIGN (type);
1224 /* Verify that everything is canonical. */
1225 int l = pcst.get_len ();
1226 if (l > 1)
1228 if (pcst.elt (l - 1) == 0)
1229 gcc_checking_assert (pcst.elt (l - 2) < 0);
1230 if (pcst.elt (l - 1) == (HOST_WIDE_INT) -1)
1231 gcc_checking_assert (pcst.elt (l - 2) >= 0);
1234 wide_int cst = wide_int::from (pcst, prec, sgn);
1235 unsigned int ext_len = get_int_cst_ext_nunits (type, cst);
1237 if (ext_len == 1)
1239 /* We just need to store a single HOST_WIDE_INT. */
1240 HOST_WIDE_INT hwi;
1241 if (TYPE_UNSIGNED (type))
1242 hwi = cst.to_uhwi ();
1243 else
1244 hwi = cst.to_shwi ();
1246 switch (TREE_CODE (type))
1248 case NULLPTR_TYPE:
1249 gcc_assert (hwi == 0);
1250 /* Fallthru. */
1252 case POINTER_TYPE:
1253 case REFERENCE_TYPE:
1254 /* Cache NULL pointer. */
1255 if (hwi == 0)
1257 limit = 1;
1258 ix = 0;
1260 break;
1262 case BOOLEAN_TYPE:
1263 /* Cache false or true. */
1264 limit = 2;
1265 if (hwi < 2)
1266 ix = hwi;
1267 break;
1269 case INTEGER_TYPE:
1270 case OFFSET_TYPE:
1271 if (TYPE_SIGN (type) == UNSIGNED)
1273 /* Cache [0, N). */
1274 limit = INTEGER_SHARE_LIMIT;
1275 if (IN_RANGE (hwi, 0, INTEGER_SHARE_LIMIT - 1))
1276 ix = hwi;
1278 else
1280 /* Cache [-1, N). */
1281 limit = INTEGER_SHARE_LIMIT + 1;
1282 if (IN_RANGE (hwi, -1, INTEGER_SHARE_LIMIT - 1))
1283 ix = hwi + 1;
1285 break;
1287 case ENUMERAL_TYPE:
1288 break;
1290 default:
1291 gcc_unreachable ();
1294 if (ix >= 0)
1296 /* Look for it in the type's vector of small shared ints. */
1297 if (!TYPE_CACHED_VALUES_P (type))
1299 TYPE_CACHED_VALUES_P (type) = 1;
1300 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1303 t = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix);
1304 if (t)
1305 /* Make sure no one is clobbering the shared constant. */
1306 gcc_checking_assert (TREE_TYPE (t) == type
1307 && TREE_INT_CST_NUNITS (t) == 1
1308 && TREE_INT_CST_OFFSET_NUNITS (t) == 1
1309 && TREE_INT_CST_EXT_NUNITS (t) == 1
1310 && TREE_INT_CST_ELT (t, 0) == hwi);
1311 else
1313 /* Create a new shared int. */
1314 t = build_new_int_cst (type, cst);
1315 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1318 else
1320 /* Use the cache of larger shared ints, using int_cst_node as
1321 a temporary. */
1322 void **slot;
1324 TREE_INT_CST_ELT (int_cst_node, 0) = hwi;
1325 TREE_TYPE (int_cst_node) = type;
1327 slot = htab_find_slot (int_cst_hash_table, int_cst_node, INSERT);
1328 t = (tree) *slot;
1329 if (!t)
1331 /* Insert this one into the hash table. */
1332 t = int_cst_node;
1333 *slot = t;
1334 /* Make a new node for next time round. */
1335 int_cst_node = make_int_cst (1, 1);
1339 else
1341 /* The value either hashes properly or we drop it on the floor
1342 for the gc to take care of. There will not be enough of them
1343 to worry about. */
1344 void **slot;
1346 tree nt = build_new_int_cst (type, cst);
1347 slot = htab_find_slot (int_cst_hash_table, nt, INSERT);
1348 t = (tree) *slot;
1349 if (!t)
1351 /* Insert this one into the hash table. */
1352 t = nt;
1353 *slot = t;
1357 return t;
1360 void
1361 cache_integer_cst (tree t)
1363 tree type = TREE_TYPE (t);
1364 int ix = -1;
1365 int limit = 0;
1366 int prec = TYPE_PRECISION (type);
1368 gcc_assert (!TREE_OVERFLOW (t));
1370 switch (TREE_CODE (type))
1372 case NULLPTR_TYPE:
1373 gcc_assert (integer_zerop (t));
1374 /* Fallthru. */
1376 case POINTER_TYPE:
1377 case REFERENCE_TYPE:
1378 /* Cache NULL pointer. */
1379 if (integer_zerop (t))
1381 limit = 1;
1382 ix = 0;
1384 break;
1386 case BOOLEAN_TYPE:
1387 /* Cache false or true. */
1388 limit = 2;
1389 if (wi::ltu_p (t, 2))
1390 ix = TREE_INT_CST_ELT (t, 0);
1391 break;
1393 case INTEGER_TYPE:
1394 case OFFSET_TYPE:
1395 if (TYPE_UNSIGNED (type))
1397 /* Cache 0..N */
1398 limit = INTEGER_SHARE_LIMIT;
1400 /* This is a little hokie, but if the prec is smaller than
1401 what is necessary to hold INTEGER_SHARE_LIMIT, then the
1402 obvious test will not get the correct answer. */
1403 if (prec < HOST_BITS_PER_WIDE_INT)
1405 if (tree_to_uhwi (t) < (unsigned HOST_WIDE_INT) INTEGER_SHARE_LIMIT)
1406 ix = tree_to_uhwi (t);
1408 else if (wi::ltu_p (t, INTEGER_SHARE_LIMIT))
1409 ix = tree_to_uhwi (t);
1411 else
1413 /* Cache -1..N */
1414 limit = INTEGER_SHARE_LIMIT + 1;
1416 if (integer_minus_onep (t))
1417 ix = 0;
1418 else if (!wi::neg_p (t))
1420 if (prec < HOST_BITS_PER_WIDE_INT)
1422 if (tree_to_shwi (t) < INTEGER_SHARE_LIMIT)
1423 ix = tree_to_shwi (t) + 1;
1425 else if (wi::ltu_p (t, INTEGER_SHARE_LIMIT))
1426 ix = tree_to_shwi (t) + 1;
1429 break;
1431 case ENUMERAL_TYPE:
1432 break;
1434 default:
1435 gcc_unreachable ();
1438 if (ix >= 0)
1440 /* Look for it in the type's vector of small shared ints. */
1441 if (!TYPE_CACHED_VALUES_P (type))
1443 TYPE_CACHED_VALUES_P (type) = 1;
1444 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1447 gcc_assert (TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) == NULL_TREE);
1448 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1450 else
1452 /* Use the cache of larger shared ints. */
1453 void **slot;
1455 slot = htab_find_slot (int_cst_hash_table, t, INSERT);
1456 /* If there is already an entry for the number verify it's the
1457 same. */
1458 if (*slot)
1459 gcc_assert (wi::eq_p (tree (*slot), t));
1460 else
1461 /* Otherwise insert this one into the hash table. */
1462 *slot = t;
1467 /* Builds an integer constant in TYPE such that lowest BITS bits are ones
1468 and the rest are zeros. */
1470 tree
1471 build_low_bits_mask (tree type, unsigned bits)
1473 gcc_assert (bits <= TYPE_PRECISION (type));
1475 return wide_int_to_tree (type, wi::mask (bits, false,
1476 TYPE_PRECISION (type)));
1479 /* Checks that X is integer constant that can be expressed in (unsigned)
1480 HOST_WIDE_INT without loss of precision. */
1482 bool
1483 cst_and_fits_in_hwi (const_tree x)
1485 if (TREE_CODE (x) != INTEGER_CST)
1486 return false;
1488 if (TYPE_PRECISION (TREE_TYPE (x)) > HOST_BITS_PER_WIDE_INT)
1489 return false;
1491 return TREE_INT_CST_NUNITS (x) == 1;
1494 /* Build a newly constructed TREE_VEC node of length LEN. */
1496 tree
1497 make_vector_stat (unsigned len MEM_STAT_DECL)
1499 tree t;
1500 unsigned length = (len - 1) * sizeof (tree) + sizeof (struct tree_vector);
1502 record_node_allocation_statistics (VECTOR_CST, length);
1504 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1506 TREE_SET_CODE (t, VECTOR_CST);
1507 TREE_CONSTANT (t) = 1;
1509 return t;
1512 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1513 are in a list pointed to by VALS. */
1515 tree
1516 build_vector_stat (tree type, tree *vals MEM_STAT_DECL)
1518 int over = 0;
1519 unsigned cnt = 0;
1520 tree v = make_vector (TYPE_VECTOR_SUBPARTS (type));
1521 TREE_TYPE (v) = type;
1523 /* Iterate through elements and check for overflow. */
1524 for (cnt = 0; cnt < TYPE_VECTOR_SUBPARTS (type); ++cnt)
1526 tree value = vals[cnt];
1528 VECTOR_CST_ELT (v, cnt) = value;
1530 /* Don't crash if we get an address constant. */
1531 if (!CONSTANT_CLASS_P (value))
1532 continue;
1534 over |= TREE_OVERFLOW (value);
1537 TREE_OVERFLOW (v) = over;
1538 return v;
1541 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1542 are extracted from V, a vector of CONSTRUCTOR_ELT. */
1544 tree
1545 build_vector_from_ctor (tree type, vec<constructor_elt, va_gc> *v)
1547 tree *vec = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
1548 unsigned HOST_WIDE_INT idx;
1549 tree value;
1551 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1552 vec[idx] = value;
1553 for (; idx < TYPE_VECTOR_SUBPARTS (type); ++idx)
1554 vec[idx] = build_zero_cst (TREE_TYPE (type));
1556 return build_vector (type, vec);
1559 /* Build a vector of type VECTYPE where all the elements are SCs. */
1560 tree
1561 build_vector_from_val (tree vectype, tree sc)
1563 int i, nunits = TYPE_VECTOR_SUBPARTS (vectype);
1565 if (sc == error_mark_node)
1566 return sc;
1568 /* Verify that the vector type is suitable for SC. Note that there
1569 is some inconsistency in the type-system with respect to restrict
1570 qualifications of pointers. Vector types always have a main-variant
1571 element type and the qualification is applied to the vector-type.
1572 So TREE_TYPE (vector-type) does not return a properly qualified
1573 vector element-type. */
1574 gcc_checking_assert (types_compatible_p (TYPE_MAIN_VARIANT (TREE_TYPE (sc)),
1575 TREE_TYPE (vectype)));
1577 if (CONSTANT_CLASS_P (sc))
1579 tree *v = XALLOCAVEC (tree, nunits);
1580 for (i = 0; i < nunits; ++i)
1581 v[i] = sc;
1582 return build_vector (vectype, v);
1584 else
1586 vec<constructor_elt, va_gc> *v;
1587 vec_alloc (v, nunits);
1588 for (i = 0; i < nunits; ++i)
1589 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, sc);
1590 return build_constructor (vectype, v);
1594 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1595 are in the vec pointed to by VALS. */
1596 tree
1597 build_constructor (tree type, vec<constructor_elt, va_gc> *vals)
1599 tree c = make_node (CONSTRUCTOR);
1600 unsigned int i;
1601 constructor_elt *elt;
1602 bool constant_p = true;
1603 bool side_effects_p = false;
1605 TREE_TYPE (c) = type;
1606 CONSTRUCTOR_ELTS (c) = vals;
1608 FOR_EACH_VEC_SAFE_ELT (vals, i, elt)
1610 /* Mostly ctors will have elts that don't have side-effects, so
1611 the usual case is to scan all the elements. Hence a single
1612 loop for both const and side effects, rather than one loop
1613 each (with early outs). */
1614 if (!TREE_CONSTANT (elt->value))
1615 constant_p = false;
1616 if (TREE_SIDE_EFFECTS (elt->value))
1617 side_effects_p = true;
1620 TREE_SIDE_EFFECTS (c) = side_effects_p;
1621 TREE_CONSTANT (c) = constant_p;
1623 return c;
1626 /* Build a CONSTRUCTOR node made of a single initializer, with the specified
1627 INDEX and VALUE. */
1628 tree
1629 build_constructor_single (tree type, tree index, tree value)
1631 vec<constructor_elt, va_gc> *v;
1632 constructor_elt elt = {index, value};
1634 vec_alloc (v, 1);
1635 v->quick_push (elt);
1637 return build_constructor (type, v);
1641 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1642 are in a list pointed to by VALS. */
1643 tree
1644 build_constructor_from_list (tree type, tree vals)
1646 tree t;
1647 vec<constructor_elt, va_gc> *v = NULL;
1649 if (vals)
1651 vec_alloc (v, list_length (vals));
1652 for (t = vals; t; t = TREE_CHAIN (t))
1653 CONSTRUCTOR_APPEND_ELT (v, TREE_PURPOSE (t), TREE_VALUE (t));
1656 return build_constructor (type, v);
1659 /* Return a new CONSTRUCTOR node whose type is TYPE. NELTS is the number
1660 of elements, provided as index/value pairs. */
1662 tree
1663 build_constructor_va (tree type, int nelts, ...)
1665 vec<constructor_elt, va_gc> *v = NULL;
1666 va_list p;
1668 va_start (p, nelts);
1669 vec_alloc (v, nelts);
1670 while (nelts--)
1672 tree index = va_arg (p, tree);
1673 tree value = va_arg (p, tree);
1674 CONSTRUCTOR_APPEND_ELT (v, index, value);
1676 va_end (p);
1677 return build_constructor (type, v);
1680 /* Return a new FIXED_CST node whose type is TYPE and value is F. */
1682 tree
1683 build_fixed (tree type, FIXED_VALUE_TYPE f)
1685 tree v;
1686 FIXED_VALUE_TYPE *fp;
1688 v = make_node (FIXED_CST);
1689 fp = ggc_alloc<fixed_value> ();
1690 memcpy (fp, &f, sizeof (FIXED_VALUE_TYPE));
1692 TREE_TYPE (v) = type;
1693 TREE_FIXED_CST_PTR (v) = fp;
1694 return v;
1697 /* Return a new REAL_CST node whose type is TYPE and value is D. */
1699 tree
1700 build_real (tree type, REAL_VALUE_TYPE d)
1702 tree v;
1703 REAL_VALUE_TYPE *dp;
1704 int overflow = 0;
1706 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
1707 Consider doing it via real_convert now. */
1709 v = make_node (REAL_CST);
1710 dp = ggc_alloc<real_value> ();
1711 memcpy (dp, &d, sizeof (REAL_VALUE_TYPE));
1713 TREE_TYPE (v) = type;
1714 TREE_REAL_CST_PTR (v) = dp;
1715 TREE_OVERFLOW (v) = overflow;
1716 return v;
1719 /* Return a new REAL_CST node whose type is TYPE
1720 and whose value is the integer value of the INTEGER_CST node I. */
1722 REAL_VALUE_TYPE
1723 real_value_from_int_cst (const_tree type, const_tree i)
1725 REAL_VALUE_TYPE d;
1727 /* Clear all bits of the real value type so that we can later do
1728 bitwise comparisons to see if two values are the same. */
1729 memset (&d, 0, sizeof d);
1731 real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode, i,
1732 TYPE_SIGN (TREE_TYPE (i)));
1733 return d;
1736 /* Given a tree representing an integer constant I, return a tree
1737 representing the same value as a floating-point constant of type TYPE. */
1739 tree
1740 build_real_from_int_cst (tree type, const_tree i)
1742 tree v;
1743 int overflow = TREE_OVERFLOW (i);
1745 v = build_real (type, real_value_from_int_cst (type, i));
1747 TREE_OVERFLOW (v) |= overflow;
1748 return v;
1751 /* Return a newly constructed STRING_CST node whose value is
1752 the LEN characters at STR.
1753 Note that for a C string literal, LEN should include the trailing NUL.
1754 The TREE_TYPE is not initialized. */
1756 tree
1757 build_string (int len, const char *str)
1759 tree s;
1760 size_t length;
1762 /* Do not waste bytes provided by padding of struct tree_string. */
1763 length = len + offsetof (struct tree_string, str) + 1;
1765 record_node_allocation_statistics (STRING_CST, length);
1767 s = (tree) ggc_internal_alloc (length);
1769 memset (s, 0, sizeof (struct tree_typed));
1770 TREE_SET_CODE (s, STRING_CST);
1771 TREE_CONSTANT (s) = 1;
1772 TREE_STRING_LENGTH (s) = len;
1773 memcpy (s->string.str, str, len);
1774 s->string.str[len] = '\0';
1776 return s;
1779 /* Return a newly constructed COMPLEX_CST node whose value is
1780 specified by the real and imaginary parts REAL and IMAG.
1781 Both REAL and IMAG should be constant nodes. TYPE, if specified,
1782 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
1784 tree
1785 build_complex (tree type, tree real, tree imag)
1787 tree t = make_node (COMPLEX_CST);
1789 TREE_REALPART (t) = real;
1790 TREE_IMAGPART (t) = imag;
1791 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
1792 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
1793 return t;
1796 /* Return a constant of arithmetic type TYPE which is the
1797 multiplicative identity of the set TYPE. */
1799 tree
1800 build_one_cst (tree type)
1802 switch (TREE_CODE (type))
1804 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1805 case POINTER_TYPE: case REFERENCE_TYPE:
1806 case OFFSET_TYPE:
1807 return build_int_cst (type, 1);
1809 case REAL_TYPE:
1810 return build_real (type, dconst1);
1812 case FIXED_POINT_TYPE:
1813 /* We can only generate 1 for accum types. */
1814 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
1815 return build_fixed (type, FCONST1 (TYPE_MODE (type)));
1817 case VECTOR_TYPE:
1819 tree scalar = build_one_cst (TREE_TYPE (type));
1821 return build_vector_from_val (type, scalar);
1824 case COMPLEX_TYPE:
1825 return build_complex (type,
1826 build_one_cst (TREE_TYPE (type)),
1827 build_zero_cst (TREE_TYPE (type)));
1829 default:
1830 gcc_unreachable ();
1834 /* Return an integer of type TYPE containing all 1's in as much precision as
1835 it contains, or a complex or vector whose subparts are such integers. */
1837 tree
1838 build_all_ones_cst (tree type)
1840 if (TREE_CODE (type) == COMPLEX_TYPE)
1842 tree scalar = build_all_ones_cst (TREE_TYPE (type));
1843 return build_complex (type, scalar, scalar);
1845 else
1846 return build_minus_one_cst (type);
1849 /* Return a constant of arithmetic type TYPE which is the
1850 opposite of the multiplicative identity of the set TYPE. */
1852 tree
1853 build_minus_one_cst (tree type)
1855 switch (TREE_CODE (type))
1857 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1858 case POINTER_TYPE: case REFERENCE_TYPE:
1859 case OFFSET_TYPE:
1860 return build_int_cst (type, -1);
1862 case REAL_TYPE:
1863 return build_real (type, dconstm1);
1865 case FIXED_POINT_TYPE:
1866 /* We can only generate 1 for accum types. */
1867 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
1868 return build_fixed (type, fixed_from_double_int (double_int_minus_one,
1869 TYPE_MODE (type)));
1871 case VECTOR_TYPE:
1873 tree scalar = build_minus_one_cst (TREE_TYPE (type));
1875 return build_vector_from_val (type, scalar);
1878 case COMPLEX_TYPE:
1879 return build_complex (type,
1880 build_minus_one_cst (TREE_TYPE (type)),
1881 build_zero_cst (TREE_TYPE (type)));
1883 default:
1884 gcc_unreachable ();
1888 /* Build 0 constant of type TYPE. This is used by constructor folding
1889 and thus the constant should be represented in memory by
1890 zero(es). */
1892 tree
1893 build_zero_cst (tree type)
1895 switch (TREE_CODE (type))
1897 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1898 case POINTER_TYPE: case REFERENCE_TYPE:
1899 case OFFSET_TYPE: case NULLPTR_TYPE:
1900 return build_int_cst (type, 0);
1902 case REAL_TYPE:
1903 return build_real (type, dconst0);
1905 case FIXED_POINT_TYPE:
1906 return build_fixed (type, FCONST0 (TYPE_MODE (type)));
1908 case VECTOR_TYPE:
1910 tree scalar = build_zero_cst (TREE_TYPE (type));
1912 return build_vector_from_val (type, scalar);
1915 case COMPLEX_TYPE:
1917 tree zero = build_zero_cst (TREE_TYPE (type));
1919 return build_complex (type, zero, zero);
1922 default:
1923 if (!AGGREGATE_TYPE_P (type))
1924 return fold_convert (type, integer_zero_node);
1925 return build_constructor (type, NULL);
1930 /* Build a BINFO with LEN language slots. */
1932 tree
1933 make_tree_binfo_stat (unsigned base_binfos MEM_STAT_DECL)
1935 tree t;
1936 size_t length = (offsetof (struct tree_binfo, base_binfos)
1937 + vec<tree, va_gc>::embedded_size (base_binfos));
1939 record_node_allocation_statistics (TREE_BINFO, length);
1941 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
1943 memset (t, 0, offsetof (struct tree_binfo, base_binfos));
1945 TREE_SET_CODE (t, TREE_BINFO);
1947 BINFO_BASE_BINFOS (t)->embedded_init (base_binfos);
1949 return t;
1952 /* Create a CASE_LABEL_EXPR tree node and return it. */
1954 tree
1955 build_case_label (tree low_value, tree high_value, tree label_decl)
1957 tree t = make_node (CASE_LABEL_EXPR);
1959 TREE_TYPE (t) = void_type_node;
1960 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (label_decl));
1962 CASE_LOW (t) = low_value;
1963 CASE_HIGH (t) = high_value;
1964 CASE_LABEL (t) = label_decl;
1965 CASE_CHAIN (t) = NULL_TREE;
1967 return t;
1970 /* Build a newly constructed INTEGER_CST node. LEN and EXT_LEN are the
1971 values of TREE_INT_CST_NUNITS and TREE_INT_CST_EXT_NUNITS respectively.
1972 The latter determines the length of the HOST_WIDE_INT vector. */
1974 tree
1975 make_int_cst_stat (int len, int ext_len MEM_STAT_DECL)
1977 tree t;
1978 int length = ((ext_len - 1) * sizeof (HOST_WIDE_INT)
1979 + sizeof (struct tree_int_cst));
1981 gcc_assert (len);
1982 record_node_allocation_statistics (INTEGER_CST, length);
1984 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1986 TREE_SET_CODE (t, INTEGER_CST);
1987 TREE_INT_CST_NUNITS (t) = len;
1988 TREE_INT_CST_EXT_NUNITS (t) = ext_len;
1989 /* to_offset can only be applied to trees that are offset_int-sized
1990 or smaller. EXT_LEN is correct if it fits, otherwise the constant
1991 must be exactly the precision of offset_int and so LEN is correct. */
1992 if (ext_len <= OFFSET_INT_ELTS)
1993 TREE_INT_CST_OFFSET_NUNITS (t) = ext_len;
1994 else
1995 TREE_INT_CST_OFFSET_NUNITS (t) = len;
1997 TREE_CONSTANT (t) = 1;
1999 return t;
2002 /* Build a newly constructed TREE_VEC node of length LEN. */
2004 tree
2005 make_tree_vec_stat (int len MEM_STAT_DECL)
2007 tree t;
2008 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
2010 record_node_allocation_statistics (TREE_VEC, length);
2012 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2014 TREE_SET_CODE (t, TREE_VEC);
2015 TREE_VEC_LENGTH (t) = len;
2017 return t;
2020 /* Grow a TREE_VEC node to new length LEN. */
2022 tree
2023 grow_tree_vec_stat (tree v, int len MEM_STAT_DECL)
2025 gcc_assert (TREE_CODE (v) == TREE_VEC);
2027 int oldlen = TREE_VEC_LENGTH (v);
2028 gcc_assert (len > oldlen);
2030 int oldlength = (oldlen - 1) * sizeof (tree) + sizeof (struct tree_vec);
2031 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
2033 record_node_allocation_statistics (TREE_VEC, length - oldlength);
2035 v = (tree) ggc_realloc (v, length PASS_MEM_STAT);
2037 TREE_VEC_LENGTH (v) = len;
2039 return v;
2042 /* Return 1 if EXPR is the integer constant zero or a complex constant
2043 of zero. */
2046 integer_zerop (const_tree expr)
2048 STRIP_NOPS (expr);
2050 switch (TREE_CODE (expr))
2052 case INTEGER_CST:
2053 return wi::eq_p (expr, 0);
2054 case COMPLEX_CST:
2055 return (integer_zerop (TREE_REALPART (expr))
2056 && integer_zerop (TREE_IMAGPART (expr)));
2057 case VECTOR_CST:
2059 unsigned i;
2060 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2061 if (!integer_zerop (VECTOR_CST_ELT (expr, i)))
2062 return false;
2063 return true;
2065 default:
2066 return false;
2070 /* Return 1 if EXPR is the integer constant one or the corresponding
2071 complex constant. */
2074 integer_onep (const_tree expr)
2076 STRIP_NOPS (expr);
2078 switch (TREE_CODE (expr))
2080 case INTEGER_CST:
2081 return wi::eq_p (wi::to_widest (expr), 1);
2082 case COMPLEX_CST:
2083 return (integer_onep (TREE_REALPART (expr))
2084 && integer_zerop (TREE_IMAGPART (expr)));
2085 case VECTOR_CST:
2087 unsigned i;
2088 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2089 if (!integer_onep (VECTOR_CST_ELT (expr, i)))
2090 return false;
2091 return true;
2093 default:
2094 return false;
2098 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
2099 it contains, or a complex or vector whose subparts are such integers. */
2102 integer_all_onesp (const_tree expr)
2104 STRIP_NOPS (expr);
2106 if (TREE_CODE (expr) == COMPLEX_CST
2107 && integer_all_onesp (TREE_REALPART (expr))
2108 && integer_all_onesp (TREE_IMAGPART (expr)))
2109 return 1;
2111 else if (TREE_CODE (expr) == VECTOR_CST)
2113 unsigned i;
2114 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2115 if (!integer_all_onesp (VECTOR_CST_ELT (expr, i)))
2116 return 0;
2117 return 1;
2120 else if (TREE_CODE (expr) != INTEGER_CST)
2121 return 0;
2123 return wi::max_value (TYPE_PRECISION (TREE_TYPE (expr)), UNSIGNED) == expr;
2126 /* Return 1 if EXPR is the integer constant minus one. */
2129 integer_minus_onep (const_tree expr)
2131 STRIP_NOPS (expr);
2133 if (TREE_CODE (expr) == COMPLEX_CST)
2134 return (integer_all_onesp (TREE_REALPART (expr))
2135 && integer_zerop (TREE_IMAGPART (expr)));
2136 else
2137 return integer_all_onesp (expr);
2140 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
2141 one bit on). */
2144 integer_pow2p (const_tree expr)
2146 STRIP_NOPS (expr);
2148 if (TREE_CODE (expr) == COMPLEX_CST
2149 && integer_pow2p (TREE_REALPART (expr))
2150 && integer_zerop (TREE_IMAGPART (expr)))
2151 return 1;
2153 if (TREE_CODE (expr) != INTEGER_CST)
2154 return 0;
2156 return wi::popcount (expr) == 1;
2159 /* Return 1 if EXPR is an integer constant other than zero or a
2160 complex constant other than zero. */
2163 integer_nonzerop (const_tree expr)
2165 STRIP_NOPS (expr);
2167 return ((TREE_CODE (expr) == INTEGER_CST
2168 && !wi::eq_p (expr, 0))
2169 || (TREE_CODE (expr) == COMPLEX_CST
2170 && (integer_nonzerop (TREE_REALPART (expr))
2171 || integer_nonzerop (TREE_IMAGPART (expr)))));
2174 /* Return 1 if EXPR is the fixed-point constant zero. */
2177 fixed_zerop (const_tree expr)
2179 return (TREE_CODE (expr) == FIXED_CST
2180 && TREE_FIXED_CST (expr).data.is_zero ());
2183 /* Return the power of two represented by a tree node known to be a
2184 power of two. */
2187 tree_log2 (const_tree expr)
2189 STRIP_NOPS (expr);
2191 if (TREE_CODE (expr) == COMPLEX_CST)
2192 return tree_log2 (TREE_REALPART (expr));
2194 return wi::exact_log2 (expr);
2197 /* Similar, but return the largest integer Y such that 2 ** Y is less
2198 than or equal to EXPR. */
2201 tree_floor_log2 (const_tree expr)
2203 STRIP_NOPS (expr);
2205 if (TREE_CODE (expr) == COMPLEX_CST)
2206 return tree_log2 (TREE_REALPART (expr));
2208 return wi::floor_log2 (expr);
2211 /* Return number of known trailing zero bits in EXPR, or, if the value of
2212 EXPR is known to be zero, the precision of it's type. */
2214 unsigned int
2215 tree_ctz (const_tree expr)
2217 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
2218 && !POINTER_TYPE_P (TREE_TYPE (expr)))
2219 return 0;
2221 unsigned int ret1, ret2, prec = TYPE_PRECISION (TREE_TYPE (expr));
2222 switch (TREE_CODE (expr))
2224 case INTEGER_CST:
2225 ret1 = wi::ctz (expr);
2226 return MIN (ret1, prec);
2227 case SSA_NAME:
2228 ret1 = wi::ctz (get_nonzero_bits (expr));
2229 return MIN (ret1, prec);
2230 case PLUS_EXPR:
2231 case MINUS_EXPR:
2232 case BIT_IOR_EXPR:
2233 case BIT_XOR_EXPR:
2234 case MIN_EXPR:
2235 case MAX_EXPR:
2236 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2237 if (ret1 == 0)
2238 return ret1;
2239 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2240 return MIN (ret1, ret2);
2241 case POINTER_PLUS_EXPR:
2242 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2243 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2244 /* Second operand is sizetype, which could be in theory
2245 wider than pointer's precision. Make sure we never
2246 return more than prec. */
2247 ret2 = MIN (ret2, prec);
2248 return MIN (ret1, ret2);
2249 case BIT_AND_EXPR:
2250 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2251 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2252 return MAX (ret1, ret2);
2253 case MULT_EXPR:
2254 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2255 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2256 return MIN (ret1 + ret2, prec);
2257 case LSHIFT_EXPR:
2258 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2259 if (tree_fits_uhwi_p (TREE_OPERAND (expr, 1))
2260 && (tree_to_uhwi (TREE_OPERAND (expr, 1)) < prec))
2262 ret2 = tree_to_uhwi (TREE_OPERAND (expr, 1));
2263 return MIN (ret1 + ret2, prec);
2265 return ret1;
2266 case RSHIFT_EXPR:
2267 if (tree_fits_uhwi_p (TREE_OPERAND (expr, 1))
2268 && (tree_to_uhwi (TREE_OPERAND (expr, 1)) < prec))
2270 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2271 ret2 = tree_to_uhwi (TREE_OPERAND (expr, 1));
2272 if (ret1 > ret2)
2273 return ret1 - ret2;
2275 return 0;
2276 case TRUNC_DIV_EXPR:
2277 case CEIL_DIV_EXPR:
2278 case FLOOR_DIV_EXPR:
2279 case ROUND_DIV_EXPR:
2280 case EXACT_DIV_EXPR:
2281 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
2282 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) == 1)
2284 int l = tree_log2 (TREE_OPERAND (expr, 1));
2285 if (l >= 0)
2287 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2288 ret2 = l;
2289 if (ret1 > ret2)
2290 return ret1 - ret2;
2293 return 0;
2294 CASE_CONVERT:
2295 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2296 if (ret1 && ret1 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2297 ret1 = prec;
2298 return MIN (ret1, prec);
2299 case SAVE_EXPR:
2300 return tree_ctz (TREE_OPERAND (expr, 0));
2301 case COND_EXPR:
2302 ret1 = tree_ctz (TREE_OPERAND (expr, 1));
2303 if (ret1 == 0)
2304 return 0;
2305 ret2 = tree_ctz (TREE_OPERAND (expr, 2));
2306 return MIN (ret1, ret2);
2307 case COMPOUND_EXPR:
2308 return tree_ctz (TREE_OPERAND (expr, 1));
2309 case ADDR_EXPR:
2310 ret1 = get_pointer_alignment (CONST_CAST_TREE (expr));
2311 if (ret1 > BITS_PER_UNIT)
2313 ret1 = ctz_hwi (ret1 / BITS_PER_UNIT);
2314 return MIN (ret1, prec);
2316 return 0;
2317 default:
2318 return 0;
2322 /* Return 1 if EXPR is the real constant zero. Trailing zeroes matter for
2323 decimal float constants, so don't return 1 for them. */
2326 real_zerop (const_tree expr)
2328 STRIP_NOPS (expr);
2330 switch (TREE_CODE (expr))
2332 case REAL_CST:
2333 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0)
2334 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2335 case COMPLEX_CST:
2336 return real_zerop (TREE_REALPART (expr))
2337 && real_zerop (TREE_IMAGPART (expr));
2338 case VECTOR_CST:
2340 unsigned i;
2341 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2342 if (!real_zerop (VECTOR_CST_ELT (expr, i)))
2343 return false;
2344 return true;
2346 default:
2347 return false;
2351 /* Return 1 if EXPR is the real constant one in real or complex form.
2352 Trailing zeroes matter for decimal float constants, so don't return
2353 1 for them. */
2356 real_onep (const_tree expr)
2358 STRIP_NOPS (expr);
2360 switch (TREE_CODE (expr))
2362 case REAL_CST:
2363 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1)
2364 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2365 case COMPLEX_CST:
2366 return real_onep (TREE_REALPART (expr))
2367 && real_zerop (TREE_IMAGPART (expr));
2368 case VECTOR_CST:
2370 unsigned i;
2371 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2372 if (!real_onep (VECTOR_CST_ELT (expr, i)))
2373 return false;
2374 return true;
2376 default:
2377 return false;
2381 /* Return 1 if EXPR is the real constant minus one. Trailing zeroes
2382 matter for decimal float constants, so don't return 1 for them. */
2385 real_minus_onep (const_tree expr)
2387 STRIP_NOPS (expr);
2389 switch (TREE_CODE (expr))
2391 case REAL_CST:
2392 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconstm1)
2393 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2394 case COMPLEX_CST:
2395 return real_minus_onep (TREE_REALPART (expr))
2396 && real_zerop (TREE_IMAGPART (expr));
2397 case VECTOR_CST:
2399 unsigned i;
2400 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2401 if (!real_minus_onep (VECTOR_CST_ELT (expr, i)))
2402 return false;
2403 return true;
2405 default:
2406 return false;
2410 /* Nonzero if EXP is a constant or a cast of a constant. */
2413 really_constant_p (const_tree exp)
2415 /* This is not quite the same as STRIP_NOPS. It does more. */
2416 while (CONVERT_EXPR_P (exp)
2417 || TREE_CODE (exp) == NON_LVALUE_EXPR)
2418 exp = TREE_OPERAND (exp, 0);
2419 return TREE_CONSTANT (exp);
2422 /* Return first list element whose TREE_VALUE is ELEM.
2423 Return 0 if ELEM is not in LIST. */
2425 tree
2426 value_member (tree elem, tree list)
2428 while (list)
2430 if (elem == TREE_VALUE (list))
2431 return list;
2432 list = TREE_CHAIN (list);
2434 return NULL_TREE;
2437 /* Return first list element whose TREE_PURPOSE is ELEM.
2438 Return 0 if ELEM is not in LIST. */
2440 tree
2441 purpose_member (const_tree elem, tree list)
2443 while (list)
2445 if (elem == TREE_PURPOSE (list))
2446 return list;
2447 list = TREE_CHAIN (list);
2449 return NULL_TREE;
2452 /* Return true if ELEM is in V. */
2454 bool
2455 vec_member (const_tree elem, vec<tree, va_gc> *v)
2457 unsigned ix;
2458 tree t;
2459 FOR_EACH_VEC_SAFE_ELT (v, ix, t)
2460 if (elem == t)
2461 return true;
2462 return false;
2465 /* Returns element number IDX (zero-origin) of chain CHAIN, or
2466 NULL_TREE. */
2468 tree
2469 chain_index (int idx, tree chain)
2471 for (; chain && idx > 0; --idx)
2472 chain = TREE_CHAIN (chain);
2473 return chain;
2476 /* Return nonzero if ELEM is part of the chain CHAIN. */
2479 chain_member (const_tree elem, const_tree chain)
2481 while (chain)
2483 if (elem == chain)
2484 return 1;
2485 chain = DECL_CHAIN (chain);
2488 return 0;
2491 /* Return the length of a chain of nodes chained through TREE_CHAIN.
2492 We expect a null pointer to mark the end of the chain.
2493 This is the Lisp primitive `length'. */
2496 list_length (const_tree t)
2498 const_tree p = t;
2499 #ifdef ENABLE_TREE_CHECKING
2500 const_tree q = t;
2501 #endif
2502 int len = 0;
2504 while (p)
2506 p = TREE_CHAIN (p);
2507 #ifdef ENABLE_TREE_CHECKING
2508 if (len % 2)
2509 q = TREE_CHAIN (q);
2510 gcc_assert (p != q);
2511 #endif
2512 len++;
2515 return len;
2518 /* Returns the first FIELD_DECL in the TYPE_FIELDS of the RECORD_TYPE or
2519 UNION_TYPE TYPE, or NULL_TREE if none. */
2521 tree
2522 first_field (const_tree type)
2524 tree t = TYPE_FIELDS (type);
2525 while (t && TREE_CODE (t) != FIELD_DECL)
2526 t = TREE_CHAIN (t);
2527 return t;
2530 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
2531 by modifying the last node in chain 1 to point to chain 2.
2532 This is the Lisp primitive `nconc'. */
2534 tree
2535 chainon (tree op1, tree op2)
2537 tree t1;
2539 if (!op1)
2540 return op2;
2541 if (!op2)
2542 return op1;
2544 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
2545 continue;
2546 TREE_CHAIN (t1) = op2;
2548 #ifdef ENABLE_TREE_CHECKING
2550 tree t2;
2551 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
2552 gcc_assert (t2 != t1);
2554 #endif
2556 return op1;
2559 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
2561 tree
2562 tree_last (tree chain)
2564 tree next;
2565 if (chain)
2566 while ((next = TREE_CHAIN (chain)))
2567 chain = next;
2568 return chain;
2571 /* Reverse the order of elements in the chain T,
2572 and return the new head of the chain (old last element). */
2574 tree
2575 nreverse (tree t)
2577 tree prev = 0, decl, next;
2578 for (decl = t; decl; decl = next)
2580 /* We shouldn't be using this function to reverse BLOCK chains; we
2581 have blocks_nreverse for that. */
2582 gcc_checking_assert (TREE_CODE (decl) != BLOCK);
2583 next = TREE_CHAIN (decl);
2584 TREE_CHAIN (decl) = prev;
2585 prev = decl;
2587 return prev;
2590 /* Return a newly created TREE_LIST node whose
2591 purpose and value fields are PARM and VALUE. */
2593 tree
2594 build_tree_list_stat (tree parm, tree value MEM_STAT_DECL)
2596 tree t = make_node_stat (TREE_LIST PASS_MEM_STAT);
2597 TREE_PURPOSE (t) = parm;
2598 TREE_VALUE (t) = value;
2599 return t;
2602 /* Build a chain of TREE_LIST nodes from a vector. */
2604 tree
2605 build_tree_list_vec_stat (const vec<tree, va_gc> *vec MEM_STAT_DECL)
2607 tree ret = NULL_TREE;
2608 tree *pp = &ret;
2609 unsigned int i;
2610 tree t;
2611 FOR_EACH_VEC_SAFE_ELT (vec, i, t)
2613 *pp = build_tree_list_stat (NULL, t PASS_MEM_STAT);
2614 pp = &TREE_CHAIN (*pp);
2616 return ret;
2619 /* Return a newly created TREE_LIST node whose
2620 purpose and value fields are PURPOSE and VALUE
2621 and whose TREE_CHAIN is CHAIN. */
2623 tree
2624 tree_cons_stat (tree purpose, tree value, tree chain MEM_STAT_DECL)
2626 tree node;
2628 node = ggc_alloc_tree_node_stat (sizeof (struct tree_list) PASS_MEM_STAT);
2629 memset (node, 0, sizeof (struct tree_common));
2631 record_node_allocation_statistics (TREE_LIST, sizeof (struct tree_list));
2633 TREE_SET_CODE (node, TREE_LIST);
2634 TREE_CHAIN (node) = chain;
2635 TREE_PURPOSE (node) = purpose;
2636 TREE_VALUE (node) = value;
2637 return node;
2640 /* Return the values of the elements of a CONSTRUCTOR as a vector of
2641 trees. */
2643 vec<tree, va_gc> *
2644 ctor_to_vec (tree ctor)
2646 vec<tree, va_gc> *vec;
2647 vec_alloc (vec, CONSTRUCTOR_NELTS (ctor));
2648 unsigned int ix;
2649 tree val;
2651 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), ix, val)
2652 vec->quick_push (val);
2654 return vec;
2657 /* Return the size nominally occupied by an object of type TYPE
2658 when it resides in memory. The value is measured in units of bytes,
2659 and its data type is that normally used for type sizes
2660 (which is the first type created by make_signed_type or
2661 make_unsigned_type). */
2663 tree
2664 size_in_bytes (const_tree type)
2666 tree t;
2668 if (type == error_mark_node)
2669 return integer_zero_node;
2671 type = TYPE_MAIN_VARIANT (type);
2672 t = TYPE_SIZE_UNIT (type);
2674 if (t == 0)
2676 lang_hooks.types.incomplete_type_error (NULL_TREE, type);
2677 return size_zero_node;
2680 return t;
2683 /* Returns a tree for the size of EXP in bytes. */
2685 tree
2686 tree_expr_size (const_tree exp)
2688 if (DECL_P (exp)
2689 && DECL_SIZE_UNIT (exp) != 0)
2690 return DECL_SIZE_UNIT (exp);
2691 else
2692 return size_in_bytes (TREE_TYPE (exp));
2695 /* Return the size of TYPE (in bytes) as a wide integer
2696 or return -1 if the size can vary or is larger than an integer. */
2698 HOST_WIDE_INT
2699 int_size_in_bytes (const_tree type)
2701 tree t;
2703 if (type == error_mark_node)
2704 return 0;
2706 type = TYPE_MAIN_VARIANT (type);
2707 t = TYPE_SIZE_UNIT (type);
2709 if (t && tree_fits_uhwi_p (t))
2710 return TREE_INT_CST_LOW (t);
2711 else
2712 return -1;
2715 /* Return the maximum size of TYPE (in bytes) as a wide integer
2716 or return -1 if the size can vary or is larger than an integer. */
2718 HOST_WIDE_INT
2719 max_int_size_in_bytes (const_tree type)
2721 HOST_WIDE_INT size = -1;
2722 tree size_tree;
2724 /* If this is an array type, check for a possible MAX_SIZE attached. */
2726 if (TREE_CODE (type) == ARRAY_TYPE)
2728 size_tree = TYPE_ARRAY_MAX_SIZE (type);
2730 if (size_tree && tree_fits_uhwi_p (size_tree))
2731 size = tree_to_uhwi (size_tree);
2734 /* If we still haven't been able to get a size, see if the language
2735 can compute a maximum size. */
2737 if (size == -1)
2739 size_tree = lang_hooks.types.max_size (type);
2741 if (size_tree && tree_fits_uhwi_p (size_tree))
2742 size = tree_to_uhwi (size_tree);
2745 return size;
2748 /* Return the bit position of FIELD, in bits from the start of the record.
2749 This is a tree of type bitsizetype. */
2751 tree
2752 bit_position (const_tree field)
2754 return bit_from_pos (DECL_FIELD_OFFSET (field),
2755 DECL_FIELD_BIT_OFFSET (field));
2758 /* Likewise, but return as an integer. It must be representable in
2759 that way (since it could be a signed value, we don't have the
2760 option of returning -1 like int_size_in_byte can. */
2762 HOST_WIDE_INT
2763 int_bit_position (const_tree field)
2765 return tree_to_shwi (bit_position (field));
2768 /* Return the byte position of FIELD, in bytes from the start of the record.
2769 This is a tree of type sizetype. */
2771 tree
2772 byte_position (const_tree field)
2774 return byte_from_pos (DECL_FIELD_OFFSET (field),
2775 DECL_FIELD_BIT_OFFSET (field));
2778 /* Likewise, but return as an integer. It must be representable in
2779 that way (since it could be a signed value, we don't have the
2780 option of returning -1 like int_size_in_byte can. */
2782 HOST_WIDE_INT
2783 int_byte_position (const_tree field)
2785 return tree_to_shwi (byte_position (field));
2788 /* Return the strictest alignment, in bits, that T is known to have. */
2790 unsigned int
2791 expr_align (const_tree t)
2793 unsigned int align0, align1;
2795 switch (TREE_CODE (t))
2797 CASE_CONVERT: case NON_LVALUE_EXPR:
2798 /* If we have conversions, we know that the alignment of the
2799 object must meet each of the alignments of the types. */
2800 align0 = expr_align (TREE_OPERAND (t, 0));
2801 align1 = TYPE_ALIGN (TREE_TYPE (t));
2802 return MAX (align0, align1);
2804 case SAVE_EXPR: case COMPOUND_EXPR: case MODIFY_EXPR:
2805 case INIT_EXPR: case TARGET_EXPR: case WITH_CLEANUP_EXPR:
2806 case CLEANUP_POINT_EXPR:
2807 /* These don't change the alignment of an object. */
2808 return expr_align (TREE_OPERAND (t, 0));
2810 case COND_EXPR:
2811 /* The best we can do is say that the alignment is the least aligned
2812 of the two arms. */
2813 align0 = expr_align (TREE_OPERAND (t, 1));
2814 align1 = expr_align (TREE_OPERAND (t, 2));
2815 return MIN (align0, align1);
2817 /* FIXME: LABEL_DECL and CONST_DECL never have DECL_ALIGN set
2818 meaningfully, it's always 1. */
2819 case LABEL_DECL: case CONST_DECL:
2820 case VAR_DECL: case PARM_DECL: case RESULT_DECL:
2821 case FUNCTION_DECL:
2822 gcc_assert (DECL_ALIGN (t) != 0);
2823 return DECL_ALIGN (t);
2825 default:
2826 break;
2829 /* Otherwise take the alignment from that of the type. */
2830 return TYPE_ALIGN (TREE_TYPE (t));
2833 /* Return, as a tree node, the number of elements for TYPE (which is an
2834 ARRAY_TYPE) minus one. This counts only elements of the top array. */
2836 tree
2837 array_type_nelts (const_tree type)
2839 tree index_type, min, max;
2841 /* If they did it with unspecified bounds, then we should have already
2842 given an error about it before we got here. */
2843 if (! TYPE_DOMAIN (type))
2844 return error_mark_node;
2846 index_type = TYPE_DOMAIN (type);
2847 min = TYPE_MIN_VALUE (index_type);
2848 max = TYPE_MAX_VALUE (index_type);
2850 /* TYPE_MAX_VALUE may not be set if the array has unknown length. */
2851 if (!max)
2852 return error_mark_node;
2854 return (integer_zerop (min)
2855 ? max
2856 : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
2859 /* If arg is static -- a reference to an object in static storage -- then
2860 return the object. This is not the same as the C meaning of `static'.
2861 If arg isn't static, return NULL. */
2863 tree
2864 staticp (tree arg)
2866 switch (TREE_CODE (arg))
2868 case FUNCTION_DECL:
2869 /* Nested functions are static, even though taking their address will
2870 involve a trampoline as we unnest the nested function and create
2871 the trampoline on the tree level. */
2872 return arg;
2874 case VAR_DECL:
2875 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2876 && ! DECL_THREAD_LOCAL_P (arg)
2877 && ! DECL_DLLIMPORT_P (arg)
2878 ? arg : NULL);
2880 case CONST_DECL:
2881 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2882 ? arg : NULL);
2884 case CONSTRUCTOR:
2885 return TREE_STATIC (arg) ? arg : NULL;
2887 case LABEL_DECL:
2888 case STRING_CST:
2889 return arg;
2891 case COMPONENT_REF:
2892 /* If the thing being referenced is not a field, then it is
2893 something language specific. */
2894 gcc_assert (TREE_CODE (TREE_OPERAND (arg, 1)) == FIELD_DECL);
2896 /* If we are referencing a bitfield, we can't evaluate an
2897 ADDR_EXPR at compile time and so it isn't a constant. */
2898 if (DECL_BIT_FIELD (TREE_OPERAND (arg, 1)))
2899 return NULL;
2901 return staticp (TREE_OPERAND (arg, 0));
2903 case BIT_FIELD_REF:
2904 return NULL;
2906 case INDIRECT_REF:
2907 return TREE_CONSTANT (TREE_OPERAND (arg, 0)) ? arg : NULL;
2909 case ARRAY_REF:
2910 case ARRAY_RANGE_REF:
2911 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
2912 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
2913 return staticp (TREE_OPERAND (arg, 0));
2914 else
2915 return NULL;
2917 case COMPOUND_LITERAL_EXPR:
2918 return TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (arg)) ? arg : NULL;
2920 default:
2921 return NULL;
2928 /* Return whether OP is a DECL whose address is function-invariant. */
2930 bool
2931 decl_address_invariant_p (const_tree op)
2933 /* The conditions below are slightly less strict than the one in
2934 staticp. */
2936 switch (TREE_CODE (op))
2938 case PARM_DECL:
2939 case RESULT_DECL:
2940 case LABEL_DECL:
2941 case FUNCTION_DECL:
2942 return true;
2944 case VAR_DECL:
2945 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
2946 || DECL_THREAD_LOCAL_P (op)
2947 || DECL_CONTEXT (op) == current_function_decl
2948 || decl_function_context (op) == current_function_decl)
2949 return true;
2950 break;
2952 case CONST_DECL:
2953 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
2954 || decl_function_context (op) == current_function_decl)
2955 return true;
2956 break;
2958 default:
2959 break;
2962 return false;
2965 /* Return whether OP is a DECL whose address is interprocedural-invariant. */
2967 bool
2968 decl_address_ip_invariant_p (const_tree op)
2970 /* The conditions below are slightly less strict than the one in
2971 staticp. */
2973 switch (TREE_CODE (op))
2975 case LABEL_DECL:
2976 case FUNCTION_DECL:
2977 case STRING_CST:
2978 return true;
2980 case VAR_DECL:
2981 if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
2982 && !DECL_DLLIMPORT_P (op))
2983 || DECL_THREAD_LOCAL_P (op))
2984 return true;
2985 break;
2987 case CONST_DECL:
2988 if ((TREE_STATIC (op) || DECL_EXTERNAL (op)))
2989 return true;
2990 break;
2992 default:
2993 break;
2996 return false;
3000 /* Return true if T is function-invariant (internal function, does
3001 not handle arithmetic; that's handled in skip_simple_arithmetic and
3002 tree_invariant_p). */
3004 static bool tree_invariant_p (tree t);
3006 static bool
3007 tree_invariant_p_1 (tree t)
3009 tree op;
3011 if (TREE_CONSTANT (t)
3012 || (TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t)))
3013 return true;
3015 switch (TREE_CODE (t))
3017 case SAVE_EXPR:
3018 return true;
3020 case ADDR_EXPR:
3021 op = TREE_OPERAND (t, 0);
3022 while (handled_component_p (op))
3024 switch (TREE_CODE (op))
3026 case ARRAY_REF:
3027 case ARRAY_RANGE_REF:
3028 if (!tree_invariant_p (TREE_OPERAND (op, 1))
3029 || TREE_OPERAND (op, 2) != NULL_TREE
3030 || TREE_OPERAND (op, 3) != NULL_TREE)
3031 return false;
3032 break;
3034 case COMPONENT_REF:
3035 if (TREE_OPERAND (op, 2) != NULL_TREE)
3036 return false;
3037 break;
3039 default:;
3041 op = TREE_OPERAND (op, 0);
3044 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
3046 default:
3047 break;
3050 return false;
3053 /* Return true if T is function-invariant. */
3055 static bool
3056 tree_invariant_p (tree t)
3058 tree inner = skip_simple_arithmetic (t);
3059 return tree_invariant_p_1 (inner);
3062 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
3063 Do this to any expression which may be used in more than one place,
3064 but must be evaluated only once.
3066 Normally, expand_expr would reevaluate the expression each time.
3067 Calling save_expr produces something that is evaluated and recorded
3068 the first time expand_expr is called on it. Subsequent calls to
3069 expand_expr just reuse the recorded value.
3071 The call to expand_expr that generates code that actually computes
3072 the value is the first call *at compile time*. Subsequent calls
3073 *at compile time* generate code to use the saved value.
3074 This produces correct result provided that *at run time* control
3075 always flows through the insns made by the first expand_expr
3076 before reaching the other places where the save_expr was evaluated.
3077 You, the caller of save_expr, must make sure this is so.
3079 Constants, and certain read-only nodes, are returned with no
3080 SAVE_EXPR because that is safe. Expressions containing placeholders
3081 are not touched; see tree.def for an explanation of what these
3082 are used for. */
3084 tree
3085 save_expr (tree expr)
3087 tree t = fold (expr);
3088 tree inner;
3090 /* If the tree evaluates to a constant, then we don't want to hide that
3091 fact (i.e. this allows further folding, and direct checks for constants).
3092 However, a read-only object that has side effects cannot be bypassed.
3093 Since it is no problem to reevaluate literals, we just return the
3094 literal node. */
3095 inner = skip_simple_arithmetic (t);
3096 if (TREE_CODE (inner) == ERROR_MARK)
3097 return inner;
3099 if (tree_invariant_p_1 (inner))
3100 return t;
3102 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
3103 it means that the size or offset of some field of an object depends on
3104 the value within another field.
3106 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
3107 and some variable since it would then need to be both evaluated once and
3108 evaluated more than once. Front-ends must assure this case cannot
3109 happen by surrounding any such subexpressions in their own SAVE_EXPR
3110 and forcing evaluation at the proper time. */
3111 if (contains_placeholder_p (inner))
3112 return t;
3114 t = build1 (SAVE_EXPR, TREE_TYPE (expr), t);
3115 SET_EXPR_LOCATION (t, EXPR_LOCATION (expr));
3117 /* This expression might be placed ahead of a jump to ensure that the
3118 value was computed on both sides of the jump. So make sure it isn't
3119 eliminated as dead. */
3120 TREE_SIDE_EFFECTS (t) = 1;
3121 return t;
3124 /* Look inside EXPR into any simple arithmetic operations. Return the
3125 outermost non-arithmetic or non-invariant node. */
3127 tree
3128 skip_simple_arithmetic (tree expr)
3130 /* We don't care about whether this can be used as an lvalue in this
3131 context. */
3132 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3133 expr = TREE_OPERAND (expr, 0);
3135 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
3136 a constant, it will be more efficient to not make another SAVE_EXPR since
3137 it will allow better simplification and GCSE will be able to merge the
3138 computations if they actually occur. */
3139 while (true)
3141 if (UNARY_CLASS_P (expr))
3142 expr = TREE_OPERAND (expr, 0);
3143 else if (BINARY_CLASS_P (expr))
3145 if (tree_invariant_p (TREE_OPERAND (expr, 1)))
3146 expr = TREE_OPERAND (expr, 0);
3147 else if (tree_invariant_p (TREE_OPERAND (expr, 0)))
3148 expr = TREE_OPERAND (expr, 1);
3149 else
3150 break;
3152 else
3153 break;
3156 return expr;
3159 /* Look inside EXPR into simple arithmetic operations involving constants.
3160 Return the outermost non-arithmetic or non-constant node. */
3162 tree
3163 skip_simple_constant_arithmetic (tree expr)
3165 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3166 expr = TREE_OPERAND (expr, 0);
3168 while (true)
3170 if (UNARY_CLASS_P (expr))
3171 expr = TREE_OPERAND (expr, 0);
3172 else if (BINARY_CLASS_P (expr))
3174 if (TREE_CONSTANT (TREE_OPERAND (expr, 1)))
3175 expr = TREE_OPERAND (expr, 0);
3176 else if (TREE_CONSTANT (TREE_OPERAND (expr, 0)))
3177 expr = TREE_OPERAND (expr, 1);
3178 else
3179 break;
3181 else
3182 break;
3185 return expr;
3188 /* Return which tree structure is used by T. */
3190 enum tree_node_structure_enum
3191 tree_node_structure (const_tree t)
3193 const enum tree_code code = TREE_CODE (t);
3194 return tree_node_structure_for_code (code);
3197 /* Set various status flags when building a CALL_EXPR object T. */
3199 static void
3200 process_call_operands (tree t)
3202 bool side_effects = TREE_SIDE_EFFECTS (t);
3203 bool read_only = false;
3204 int i = call_expr_flags (t);
3206 /* Calls have side-effects, except those to const or pure functions. */
3207 if ((i & ECF_LOOPING_CONST_OR_PURE) || !(i & (ECF_CONST | ECF_PURE)))
3208 side_effects = true;
3209 /* Propagate TREE_READONLY of arguments for const functions. */
3210 if (i & ECF_CONST)
3211 read_only = true;
3213 if (!side_effects || read_only)
3214 for (i = 1; i < TREE_OPERAND_LENGTH (t); i++)
3216 tree op = TREE_OPERAND (t, i);
3217 if (op && TREE_SIDE_EFFECTS (op))
3218 side_effects = true;
3219 if (op && !TREE_READONLY (op) && !CONSTANT_CLASS_P (op))
3220 read_only = false;
3223 TREE_SIDE_EFFECTS (t) = side_effects;
3224 TREE_READONLY (t) = read_only;
3227 /* Return true if EXP contains a PLACEHOLDER_EXPR, i.e. if it represents a
3228 size or offset that depends on a field within a record. */
3230 bool
3231 contains_placeholder_p (const_tree exp)
3233 enum tree_code code;
3235 if (!exp)
3236 return 0;
3238 code = TREE_CODE (exp);
3239 if (code == PLACEHOLDER_EXPR)
3240 return 1;
3242 switch (TREE_CODE_CLASS (code))
3244 case tcc_reference:
3245 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
3246 position computations since they will be converted into a
3247 WITH_RECORD_EXPR involving the reference, which will assume
3248 here will be valid. */
3249 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3251 case tcc_exceptional:
3252 if (code == TREE_LIST)
3253 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
3254 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
3255 break;
3257 case tcc_unary:
3258 case tcc_binary:
3259 case tcc_comparison:
3260 case tcc_expression:
3261 switch (code)
3263 case COMPOUND_EXPR:
3264 /* Ignoring the first operand isn't quite right, but works best. */
3265 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
3267 case COND_EXPR:
3268 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3269 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
3270 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
3272 case SAVE_EXPR:
3273 /* The save_expr function never wraps anything containing
3274 a PLACEHOLDER_EXPR. */
3275 return 0;
3277 default:
3278 break;
3281 switch (TREE_CODE_LENGTH (code))
3283 case 1:
3284 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3285 case 2:
3286 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3287 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
3288 default:
3289 return 0;
3292 case tcc_vl_exp:
3293 switch (code)
3295 case CALL_EXPR:
3297 const_tree arg;
3298 const_call_expr_arg_iterator iter;
3299 FOR_EACH_CONST_CALL_EXPR_ARG (arg, iter, exp)
3300 if (CONTAINS_PLACEHOLDER_P (arg))
3301 return 1;
3302 return 0;
3304 default:
3305 return 0;
3308 default:
3309 return 0;
3311 return 0;
3314 /* Return true if any part of the structure of TYPE involves a PLACEHOLDER_EXPR
3315 directly. This includes size, bounds, qualifiers (for QUAL_UNION_TYPE) and
3316 field positions. */
3318 static bool
3319 type_contains_placeholder_1 (const_tree type)
3321 /* If the size contains a placeholder or the parent type (component type in
3322 the case of arrays) type involves a placeholder, this type does. */
3323 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
3324 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
3325 || (!POINTER_TYPE_P (type)
3326 && TREE_TYPE (type)
3327 && type_contains_placeholder_p (TREE_TYPE (type))))
3328 return true;
3330 /* Now do type-specific checks. Note that the last part of the check above
3331 greatly limits what we have to do below. */
3332 switch (TREE_CODE (type))
3334 case VOID_TYPE:
3335 case COMPLEX_TYPE:
3336 case ENUMERAL_TYPE:
3337 case BOOLEAN_TYPE:
3338 case POINTER_TYPE:
3339 case OFFSET_TYPE:
3340 case REFERENCE_TYPE:
3341 case METHOD_TYPE:
3342 case FUNCTION_TYPE:
3343 case VECTOR_TYPE:
3344 case NULLPTR_TYPE:
3345 return false;
3347 case INTEGER_TYPE:
3348 case REAL_TYPE:
3349 case FIXED_POINT_TYPE:
3350 /* Here we just check the bounds. */
3351 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
3352 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
3354 case ARRAY_TYPE:
3355 /* We have already checked the component type above, so just check the
3356 domain type. */
3357 return type_contains_placeholder_p (TYPE_DOMAIN (type));
3359 case RECORD_TYPE:
3360 case UNION_TYPE:
3361 case QUAL_UNION_TYPE:
3363 tree field;
3365 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3366 if (TREE_CODE (field) == FIELD_DECL
3367 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
3368 || (TREE_CODE (type) == QUAL_UNION_TYPE
3369 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
3370 || type_contains_placeholder_p (TREE_TYPE (field))))
3371 return true;
3373 return false;
3376 default:
3377 gcc_unreachable ();
3381 /* Wrapper around above function used to cache its result. */
3383 bool
3384 type_contains_placeholder_p (tree type)
3386 bool result;
3388 /* If the contains_placeholder_bits field has been initialized,
3389 then we know the answer. */
3390 if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
3391 return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
3393 /* Indicate that we've seen this type node, and the answer is false.
3394 This is what we want to return if we run into recursion via fields. */
3395 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
3397 /* Compute the real value. */
3398 result = type_contains_placeholder_1 (type);
3400 /* Store the real value. */
3401 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
3403 return result;
3406 /* Push tree EXP onto vector QUEUE if it is not already present. */
3408 static void
3409 push_without_duplicates (tree exp, vec<tree> *queue)
3411 unsigned int i;
3412 tree iter;
3414 FOR_EACH_VEC_ELT (*queue, i, iter)
3415 if (simple_cst_equal (iter, exp) == 1)
3416 break;
3418 if (!iter)
3419 queue->safe_push (exp);
3422 /* Given a tree EXP, find all occurrences of references to fields
3423 in a PLACEHOLDER_EXPR and place them in vector REFS without
3424 duplicates. Also record VAR_DECLs and CONST_DECLs. Note that
3425 we assume here that EXP contains only arithmetic expressions
3426 or CALL_EXPRs with PLACEHOLDER_EXPRs occurring only in their
3427 argument list. */
3429 void
3430 find_placeholder_in_expr (tree exp, vec<tree> *refs)
3432 enum tree_code code = TREE_CODE (exp);
3433 tree inner;
3434 int i;
3436 /* We handle TREE_LIST and COMPONENT_REF separately. */
3437 if (code == TREE_LIST)
3439 FIND_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), refs);
3440 FIND_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), refs);
3442 else if (code == COMPONENT_REF)
3444 for (inner = TREE_OPERAND (exp, 0);
3445 REFERENCE_CLASS_P (inner);
3446 inner = TREE_OPERAND (inner, 0))
3449 if (TREE_CODE (inner) == PLACEHOLDER_EXPR)
3450 push_without_duplicates (exp, refs);
3451 else
3452 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), refs);
3454 else
3455 switch (TREE_CODE_CLASS (code))
3457 case tcc_constant:
3458 break;
3460 case tcc_declaration:
3461 /* Variables allocated to static storage can stay. */
3462 if (!TREE_STATIC (exp))
3463 push_without_duplicates (exp, refs);
3464 break;
3466 case tcc_expression:
3467 /* This is the pattern built in ada/make_aligning_type. */
3468 if (code == ADDR_EXPR
3469 && TREE_CODE (TREE_OPERAND (exp, 0)) == PLACEHOLDER_EXPR)
3471 push_without_duplicates (exp, refs);
3472 break;
3475 /* Fall through... */
3477 case tcc_exceptional:
3478 case tcc_unary:
3479 case tcc_binary:
3480 case tcc_comparison:
3481 case tcc_reference:
3482 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
3483 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3484 break;
3486 case tcc_vl_exp:
3487 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3488 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3489 break;
3491 default:
3492 gcc_unreachable ();
3496 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
3497 return a tree with all occurrences of references to F in a
3498 PLACEHOLDER_EXPR replaced by R. Also handle VAR_DECLs and
3499 CONST_DECLs. Note that we assume here that EXP contains only
3500 arithmetic expressions or CALL_EXPRs with PLACEHOLDER_EXPRs
3501 occurring only in their argument list. */
3503 tree
3504 substitute_in_expr (tree exp, tree f, tree r)
3506 enum tree_code code = TREE_CODE (exp);
3507 tree op0, op1, op2, op3;
3508 tree new_tree;
3510 /* We handle TREE_LIST and COMPONENT_REF separately. */
3511 if (code == TREE_LIST)
3513 op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
3514 op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
3515 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3516 return exp;
3518 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3520 else if (code == COMPONENT_REF)
3522 tree inner;
3524 /* If this expression is getting a value from a PLACEHOLDER_EXPR
3525 and it is the right field, replace it with R. */
3526 for (inner = TREE_OPERAND (exp, 0);
3527 REFERENCE_CLASS_P (inner);
3528 inner = TREE_OPERAND (inner, 0))
3531 /* The field. */
3532 op1 = TREE_OPERAND (exp, 1);
3534 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f)
3535 return r;
3537 /* If this expression hasn't been completed let, leave it alone. */
3538 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && !TREE_TYPE (inner))
3539 return exp;
3541 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3542 if (op0 == TREE_OPERAND (exp, 0))
3543 return exp;
3545 new_tree
3546 = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE);
3548 else
3549 switch (TREE_CODE_CLASS (code))
3551 case tcc_constant:
3552 return exp;
3554 case tcc_declaration:
3555 if (exp == f)
3556 return r;
3557 else
3558 return exp;
3560 case tcc_expression:
3561 if (exp == f)
3562 return r;
3564 /* Fall through... */
3566 case tcc_exceptional:
3567 case tcc_unary:
3568 case tcc_binary:
3569 case tcc_comparison:
3570 case tcc_reference:
3571 switch (TREE_CODE_LENGTH (code))
3573 case 0:
3574 return exp;
3576 case 1:
3577 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3578 if (op0 == TREE_OPERAND (exp, 0))
3579 return exp;
3581 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3582 break;
3584 case 2:
3585 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3586 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3588 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3589 return exp;
3591 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3592 break;
3594 case 3:
3595 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3596 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3597 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3599 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3600 && op2 == TREE_OPERAND (exp, 2))
3601 return exp;
3603 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3604 break;
3606 case 4:
3607 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3608 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3609 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3610 op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
3612 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3613 && op2 == TREE_OPERAND (exp, 2)
3614 && op3 == TREE_OPERAND (exp, 3))
3615 return exp;
3617 new_tree
3618 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3619 break;
3621 default:
3622 gcc_unreachable ();
3624 break;
3626 case tcc_vl_exp:
3628 int i;
3630 new_tree = NULL_TREE;
3632 /* If we are trying to replace F with a constant, inline back
3633 functions which do nothing else than computing a value from
3634 the arguments they are passed. This makes it possible to
3635 fold partially or entirely the replacement expression. */
3636 if (CONSTANT_CLASS_P (r) && code == CALL_EXPR)
3638 tree t = maybe_inline_call_in_expr (exp);
3639 if (t)
3640 return SUBSTITUTE_IN_EXPR (t, f, r);
3643 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3645 tree op = TREE_OPERAND (exp, i);
3646 tree new_op = SUBSTITUTE_IN_EXPR (op, f, r);
3647 if (new_op != op)
3649 if (!new_tree)
3650 new_tree = copy_node (exp);
3651 TREE_OPERAND (new_tree, i) = new_op;
3655 if (new_tree)
3657 new_tree = fold (new_tree);
3658 if (TREE_CODE (new_tree) == CALL_EXPR)
3659 process_call_operands (new_tree);
3661 else
3662 return exp;
3664 break;
3666 default:
3667 gcc_unreachable ();
3670 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3672 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3673 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3675 return new_tree;
3678 /* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
3679 for it within OBJ, a tree that is an object or a chain of references. */
3681 tree
3682 substitute_placeholder_in_expr (tree exp, tree obj)
3684 enum tree_code code = TREE_CODE (exp);
3685 tree op0, op1, op2, op3;
3686 tree new_tree;
3688 /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
3689 in the chain of OBJ. */
3690 if (code == PLACEHOLDER_EXPR)
3692 tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
3693 tree elt;
3695 for (elt = obj; elt != 0;
3696 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3697 || TREE_CODE (elt) == COND_EXPR)
3698 ? TREE_OPERAND (elt, 1)
3699 : (REFERENCE_CLASS_P (elt)
3700 || UNARY_CLASS_P (elt)
3701 || BINARY_CLASS_P (elt)
3702 || VL_EXP_CLASS_P (elt)
3703 || EXPRESSION_CLASS_P (elt))
3704 ? TREE_OPERAND (elt, 0) : 0))
3705 if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
3706 return elt;
3708 for (elt = obj; elt != 0;
3709 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3710 || TREE_CODE (elt) == COND_EXPR)
3711 ? TREE_OPERAND (elt, 1)
3712 : (REFERENCE_CLASS_P (elt)
3713 || UNARY_CLASS_P (elt)
3714 || BINARY_CLASS_P (elt)
3715 || VL_EXP_CLASS_P (elt)
3716 || EXPRESSION_CLASS_P (elt))
3717 ? TREE_OPERAND (elt, 0) : 0))
3718 if (POINTER_TYPE_P (TREE_TYPE (elt))
3719 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
3720 == need_type))
3721 return fold_build1 (INDIRECT_REF, need_type, elt);
3723 /* If we didn't find it, return the original PLACEHOLDER_EXPR. If it
3724 survives until RTL generation, there will be an error. */
3725 return exp;
3728 /* TREE_LIST is special because we need to look at TREE_VALUE
3729 and TREE_CHAIN, not TREE_OPERANDS. */
3730 else if (code == TREE_LIST)
3732 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
3733 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
3734 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3735 return exp;
3737 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3739 else
3740 switch (TREE_CODE_CLASS (code))
3742 case tcc_constant:
3743 case tcc_declaration:
3744 return exp;
3746 case tcc_exceptional:
3747 case tcc_unary:
3748 case tcc_binary:
3749 case tcc_comparison:
3750 case tcc_expression:
3751 case tcc_reference:
3752 case tcc_statement:
3753 switch (TREE_CODE_LENGTH (code))
3755 case 0:
3756 return exp;
3758 case 1:
3759 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3760 if (op0 == TREE_OPERAND (exp, 0))
3761 return exp;
3763 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3764 break;
3766 case 2:
3767 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3768 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3770 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3771 return exp;
3773 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3774 break;
3776 case 3:
3777 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3778 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3779 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3781 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3782 && op2 == TREE_OPERAND (exp, 2))
3783 return exp;
3785 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3786 break;
3788 case 4:
3789 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3790 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3791 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3792 op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
3794 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3795 && op2 == TREE_OPERAND (exp, 2)
3796 && op3 == TREE_OPERAND (exp, 3))
3797 return exp;
3799 new_tree
3800 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3801 break;
3803 default:
3804 gcc_unreachable ();
3806 break;
3808 case tcc_vl_exp:
3810 int i;
3812 new_tree = NULL_TREE;
3814 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3816 tree op = TREE_OPERAND (exp, i);
3817 tree new_op = SUBSTITUTE_PLACEHOLDER_IN_EXPR (op, obj);
3818 if (new_op != op)
3820 if (!new_tree)
3821 new_tree = copy_node (exp);
3822 TREE_OPERAND (new_tree, i) = new_op;
3826 if (new_tree)
3828 new_tree = fold (new_tree);
3829 if (TREE_CODE (new_tree) == CALL_EXPR)
3830 process_call_operands (new_tree);
3832 else
3833 return exp;
3835 break;
3837 default:
3838 gcc_unreachable ();
3841 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3843 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3844 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3846 return new_tree;
3850 /* Subroutine of stabilize_reference; this is called for subtrees of
3851 references. Any expression with side-effects must be put in a SAVE_EXPR
3852 to ensure that it is only evaluated once.
3854 We don't put SAVE_EXPR nodes around everything, because assigning very
3855 simple expressions to temporaries causes us to miss good opportunities
3856 for optimizations. Among other things, the opportunity to fold in the
3857 addition of a constant into an addressing mode often gets lost, e.g.
3858 "y[i+1] += x;". In general, we take the approach that we should not make
3859 an assignment unless we are forced into it - i.e., that any non-side effect
3860 operator should be allowed, and that cse should take care of coalescing
3861 multiple utterances of the same expression should that prove fruitful. */
3863 static tree
3864 stabilize_reference_1 (tree e)
3866 tree result;
3867 enum tree_code code = TREE_CODE (e);
3869 /* We cannot ignore const expressions because it might be a reference
3870 to a const array but whose index contains side-effects. But we can
3871 ignore things that are actual constant or that already have been
3872 handled by this function. */
3874 if (tree_invariant_p (e))
3875 return e;
3877 switch (TREE_CODE_CLASS (code))
3879 case tcc_exceptional:
3880 case tcc_type:
3881 case tcc_declaration:
3882 case tcc_comparison:
3883 case tcc_statement:
3884 case tcc_expression:
3885 case tcc_reference:
3886 case tcc_vl_exp:
3887 /* If the expression has side-effects, then encase it in a SAVE_EXPR
3888 so that it will only be evaluated once. */
3889 /* The reference (r) and comparison (<) classes could be handled as
3890 below, but it is generally faster to only evaluate them once. */
3891 if (TREE_SIDE_EFFECTS (e))
3892 return save_expr (e);
3893 return e;
3895 case tcc_constant:
3896 /* Constants need no processing. In fact, we should never reach
3897 here. */
3898 return e;
3900 case tcc_binary:
3901 /* Division is slow and tends to be compiled with jumps,
3902 especially the division by powers of 2 that is often
3903 found inside of an array reference. So do it just once. */
3904 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
3905 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
3906 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
3907 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
3908 return save_expr (e);
3909 /* Recursively stabilize each operand. */
3910 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
3911 stabilize_reference_1 (TREE_OPERAND (e, 1)));
3912 break;
3914 case tcc_unary:
3915 /* Recursively stabilize each operand. */
3916 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
3917 break;
3919 default:
3920 gcc_unreachable ();
3923 TREE_TYPE (result) = TREE_TYPE (e);
3924 TREE_READONLY (result) = TREE_READONLY (e);
3925 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
3926 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
3927 TREE_SHARED (result) = TREE_SHARED (e);
3928 TREE_STRICT (result) = TREE_STRICT (e);
3929 TREE_RELAXED (result) = TREE_RELAXED (e);
3931 return result;
3934 /* Stabilize a reference so that we can use it any number of times
3935 without causing its operands to be evaluated more than once.
3936 Returns the stabilized reference. This works by means of save_expr,
3937 so see the caveats in the comments about save_expr.
3939 Also allows conversion expressions whose operands are references.
3940 Any other kind of expression is returned unchanged. */
3942 tree
3943 stabilize_reference (tree ref)
3945 tree result;
3946 enum tree_code code = TREE_CODE (ref);
3948 switch (code)
3950 case VAR_DECL:
3951 case PARM_DECL:
3952 case RESULT_DECL:
3953 /* No action is needed in this case. */
3954 return ref;
3956 CASE_CONVERT:
3957 case FLOAT_EXPR:
3958 case FIX_TRUNC_EXPR:
3959 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
3960 break;
3962 case INDIRECT_REF:
3963 result = build_nt (INDIRECT_REF,
3964 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
3965 break;
3967 case COMPONENT_REF:
3968 result = build_nt (COMPONENT_REF,
3969 stabilize_reference (TREE_OPERAND (ref, 0)),
3970 TREE_OPERAND (ref, 1), NULL_TREE);
3971 break;
3973 case BIT_FIELD_REF:
3974 result = build_nt (BIT_FIELD_REF,
3975 stabilize_reference (TREE_OPERAND (ref, 0)),
3976 TREE_OPERAND (ref, 1), TREE_OPERAND (ref, 2));
3977 break;
3979 case ARRAY_REF:
3980 result = build_nt (ARRAY_REF,
3981 stabilize_reference (TREE_OPERAND (ref, 0)),
3982 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
3983 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
3984 break;
3986 case ARRAY_RANGE_REF:
3987 result = build_nt (ARRAY_RANGE_REF,
3988 stabilize_reference (TREE_OPERAND (ref, 0)),
3989 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
3990 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
3991 break;
3993 case COMPOUND_EXPR:
3994 /* We cannot wrap the first expression in a SAVE_EXPR, as then
3995 it wouldn't be ignored. This matters when dealing with
3996 volatiles. */
3997 return stabilize_reference_1 (ref);
3999 /* If arg isn't a kind of lvalue we recognize, make no change.
4000 Caller should recognize the error for an invalid lvalue. */
4001 default:
4002 return ref;
4004 case ERROR_MARK:
4005 return error_mark_node;
4008 TREE_TYPE (result) = TREE_TYPE (ref);
4009 TREE_READONLY (result) = TREE_READONLY (ref);
4010 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
4011 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
4012 TREE_SHARED (result) = TREE_SHARED (ref);
4013 TREE_STRICT (result) = TREE_STRICT (ref);
4014 TREE_RELAXED (result) = TREE_RELAXED (ref);
4016 return result;
4020 /* Low-level constructors for expressions. */
4022 /* A helper function for build1 and constant folders. Set TREE_CONSTANT,
4023 and TREE_SIDE_EFFECTS for an ADDR_EXPR. */
4025 void
4026 recompute_tree_invariant_for_addr_expr (tree t)
4028 tree node;
4029 bool tc = true, se = false;
4031 /* We started out assuming this address is both invariant and constant, but
4032 does not have side effects. Now go down any handled components and see if
4033 any of them involve offsets that are either non-constant or non-invariant.
4034 Also check for side-effects.
4036 ??? Note that this code makes no attempt to deal with the case where
4037 taking the address of something causes a copy due to misalignment. */
4039 #define UPDATE_FLAGS(NODE) \
4040 do { tree _node = (NODE); \
4041 if (_node && !TREE_CONSTANT (_node)) tc = false; \
4042 if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
4044 for (node = TREE_OPERAND (t, 0); handled_component_p (node);
4045 node = TREE_OPERAND (node, 0))
4047 /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
4048 array reference (probably made temporarily by the G++ front end),
4049 so ignore all the operands. */
4050 if ((TREE_CODE (node) == ARRAY_REF
4051 || TREE_CODE (node) == ARRAY_RANGE_REF)
4052 && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
4054 UPDATE_FLAGS (TREE_OPERAND (node, 1));
4055 if (TREE_OPERAND (node, 2))
4056 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4057 if (TREE_OPERAND (node, 3))
4058 UPDATE_FLAGS (TREE_OPERAND (node, 3));
4060 /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
4061 FIELD_DECL, apparently. The G++ front end can put something else
4062 there, at least temporarily. */
4063 else if (TREE_CODE (node) == COMPONENT_REF
4064 && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
4066 if (TREE_OPERAND (node, 2))
4067 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4071 node = lang_hooks.expr_to_decl (node, &tc, &se);
4073 /* Now see what's inside. If it's an INDIRECT_REF, copy our properties from
4074 the address, since &(*a)->b is a form of addition. If it's a constant, the
4075 address is constant too. If it's a decl, its address is constant if the
4076 decl is static. Everything else is not constant and, furthermore,
4077 taking the address of a volatile variable is not volatile. */
4078 if (TREE_CODE (node) == INDIRECT_REF
4079 || TREE_CODE (node) == MEM_REF)
4080 UPDATE_FLAGS (TREE_OPERAND (node, 0));
4081 else if (CONSTANT_CLASS_P (node))
4083 else if (DECL_P (node))
4084 tc &= (staticp (node) != NULL_TREE);
4085 else
4087 tc = false;
4088 se |= TREE_SIDE_EFFECTS (node);
4092 TREE_CONSTANT (t) = tc;
4093 TREE_SIDE_EFFECTS (t) = se;
4094 #undef UPDATE_FLAGS
4097 /* Build an expression of code CODE, data type TYPE, and operands as
4098 specified. Expressions and reference nodes can be created this way.
4099 Constants, decls, types and misc nodes cannot be.
4101 We define 5 non-variadic functions, from 0 to 4 arguments. This is
4102 enough for all extant tree codes. */
4104 tree
4105 build0_stat (enum tree_code code, tree tt MEM_STAT_DECL)
4107 tree t;
4109 gcc_assert (TREE_CODE_LENGTH (code) == 0);
4111 t = make_node_stat (code PASS_MEM_STAT);
4112 TREE_TYPE (t) = tt;
4114 return t;
4117 tree
4118 build1_stat (enum tree_code code, tree type, tree node MEM_STAT_DECL)
4120 int length = sizeof (struct tree_exp);
4121 tree t;
4123 record_node_allocation_statistics (code, length);
4125 gcc_assert (TREE_CODE_LENGTH (code) == 1);
4127 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
4129 memset (t, 0, sizeof (struct tree_common));
4131 TREE_SET_CODE (t, code);
4133 TREE_TYPE (t) = type;
4134 SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
4135 TREE_OPERAND (t, 0) = node;
4136 if (node && !TYPE_P (node))
4138 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
4139 TREE_READONLY (t) = TREE_READONLY (node);
4142 if (TREE_CODE_CLASS (code) == tcc_statement)
4143 TREE_SIDE_EFFECTS (t) = 1;
4144 else switch (code)
4146 case VA_ARG_EXPR:
4147 /* All of these have side-effects, no matter what their
4148 operands are. */
4149 TREE_SIDE_EFFECTS (t) = 1;
4150 TREE_READONLY (t) = 0;
4151 break;
4153 case INDIRECT_REF:
4154 /* Whether a dereference is readonly has nothing to do with whether
4155 its operand is readonly. */
4156 TREE_READONLY (t) = 0;
4157 TREE_SHARED (t) = upc_shared_type_p (type);
4158 break;
4160 case ADDR_EXPR:
4161 if (node)
4162 recompute_tree_invariant_for_addr_expr (t);
4163 break;
4165 default:
4166 if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
4167 && node && !TYPE_P (node)
4168 && TREE_CONSTANT (node))
4169 TREE_CONSTANT (t) = 1;
4170 if (TREE_CODE_CLASS (code) == tcc_reference
4171 && node && TREE_THIS_VOLATILE (node))
4172 TREE_THIS_VOLATILE (t) = 1;
4173 /* Drop the UPC "shared" type qualifier for
4174 expressions involving UPC shared objects. */
4175 if (TREE_CODE_CLASS (code) == tcc_unary
4176 && node && !TYPE_P (node)
4177 && upc_shared_type_p (type))
4178 TREE_TYPE (t) = build_upc_unshared_type (type);
4179 break;
4182 return t;
4185 #define PROCESS_ARG(N) \
4186 do { \
4187 TREE_OPERAND (t, N) = arg##N; \
4188 if (arg##N &&!TYPE_P (arg##N)) \
4190 if (TREE_SIDE_EFFECTS (arg##N)) \
4191 side_effects = 1; \
4192 if (!TREE_READONLY (arg##N) \
4193 && !CONSTANT_CLASS_P (arg##N)) \
4194 (void) (read_only = 0); \
4195 if (!TREE_CONSTANT (arg##N)) \
4196 (void) (constant = 0); \
4198 } while (0)
4200 tree
4201 build2_stat (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
4203 bool constant, read_only, side_effects;
4204 tree t;
4206 gcc_assert (TREE_CODE_LENGTH (code) == 2);
4208 if ((code == MINUS_EXPR || code == PLUS_EXPR || code == MULT_EXPR)
4209 && arg0 && arg1 && tt && POINTER_TYPE_P (tt)
4210 /* When sizetype precision doesn't match that of pointers
4211 we need to be able to build explicit extensions or truncations
4212 of the offset argument. */
4213 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (tt))
4214 gcc_assert (TREE_CODE (arg0) == INTEGER_CST
4215 && TREE_CODE (arg1) == INTEGER_CST);
4217 if (code == POINTER_PLUS_EXPR && arg0 && arg1 && tt)
4218 gcc_assert (POINTER_TYPE_P (tt) && POINTER_TYPE_P (TREE_TYPE (arg0))
4219 && ptrofftype_p (TREE_TYPE (arg1)));
4221 t = make_node_stat (code PASS_MEM_STAT);
4223 /* Remove UPC shared type qualifiers from the result type. */
4224 if (upc_shared_type_p (tt))
4225 tt = build_upc_unshared_type (tt);
4226 TREE_TYPE (t) = tt;
4228 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
4229 result based on those same flags for the arguments. But if the
4230 arguments aren't really even `tree' expressions, we shouldn't be trying
4231 to do this. */
4233 /* Expressions without side effects may be constant if their
4234 arguments are as well. */
4235 constant = (TREE_CODE_CLASS (code) == tcc_comparison
4236 || TREE_CODE_CLASS (code) == tcc_binary);
4237 read_only = 1;
4238 side_effects = TREE_SIDE_EFFECTS (t);
4240 PROCESS_ARG (0);
4241 PROCESS_ARG (1);
4243 TREE_READONLY (t) = read_only;
4244 TREE_CONSTANT (t) = constant;
4245 TREE_SIDE_EFFECTS (t) = side_effects;
4246 TREE_THIS_VOLATILE (t)
4247 = (TREE_CODE_CLASS (code) == tcc_reference
4248 && arg0 && TREE_THIS_VOLATILE (arg0));
4250 return t;
4254 tree
4255 build3_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4256 tree arg2 MEM_STAT_DECL)
4258 bool constant, read_only, side_effects;
4259 tree t;
4261 gcc_assert (TREE_CODE_LENGTH (code) == 3);
4262 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4264 t = make_node_stat (code PASS_MEM_STAT);
4265 TREE_TYPE (t) = tt;
4267 read_only = 1;
4269 /* As a special exception, if COND_EXPR has NULL branches, we
4270 assume that it is a gimple statement and always consider
4271 it to have side effects. */
4272 if (code == COND_EXPR
4273 && tt == void_type_node
4274 && arg1 == NULL_TREE
4275 && arg2 == NULL_TREE)
4276 side_effects = true;
4277 else
4278 side_effects = TREE_SIDE_EFFECTS (t);
4280 PROCESS_ARG (0);
4281 PROCESS_ARG (1);
4282 PROCESS_ARG (2);
4284 if (code == COND_EXPR)
4285 TREE_READONLY (t) = read_only;
4287 TREE_SIDE_EFFECTS (t) = side_effects;
4288 TREE_THIS_VOLATILE (t)
4289 = (TREE_CODE_CLASS (code) == tcc_reference
4290 && arg0 && TREE_THIS_VOLATILE (arg0));
4291 TREE_SHARED (t)
4292 = (TREE_CODE_CLASS (code) == tcc_reference
4293 && arg0 && TREE_SHARED (arg0));
4294 if (TREE_SHARED (t))
4296 TREE_STRICT (t) = TREE_STRICT (arg0);
4297 TREE_RELAXED (t) = TREE_RELAXED (arg0);
4300 return t;
4303 tree
4304 build4_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4305 tree arg2, tree arg3 MEM_STAT_DECL)
4307 bool constant, read_only, side_effects;
4308 tree t;
4310 gcc_assert (TREE_CODE_LENGTH (code) == 4);
4312 t = make_node_stat (code PASS_MEM_STAT);
4313 TREE_TYPE (t) = tt;
4315 side_effects = TREE_SIDE_EFFECTS (t);
4317 PROCESS_ARG (0);
4318 PROCESS_ARG (1);
4319 PROCESS_ARG (2);
4320 PROCESS_ARG (3);
4322 TREE_SIDE_EFFECTS (t) = side_effects;
4323 TREE_THIS_VOLATILE (t)
4324 = (TREE_CODE_CLASS (code) == tcc_reference
4325 && arg0 && TREE_THIS_VOLATILE (arg0));
4327 return t;
4330 tree
4331 build5_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4332 tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
4334 bool constant, read_only, side_effects;
4335 tree t;
4337 gcc_assert (TREE_CODE_LENGTH (code) == 5);
4339 t = make_node_stat (code PASS_MEM_STAT);
4340 TREE_TYPE (t) = tt;
4342 side_effects = TREE_SIDE_EFFECTS (t);
4344 PROCESS_ARG (0);
4345 PROCESS_ARG (1);
4346 PROCESS_ARG (2);
4347 PROCESS_ARG (3);
4348 PROCESS_ARG (4);
4350 TREE_SIDE_EFFECTS (t) = side_effects;
4351 TREE_THIS_VOLATILE (t)
4352 = (TREE_CODE_CLASS (code) == tcc_reference
4353 && arg0 && TREE_THIS_VOLATILE (arg0));
4355 return t;
4358 /* Build a simple MEM_REF tree with the sematics of a plain INDIRECT_REF
4359 on the pointer PTR. */
4361 tree
4362 build_simple_mem_ref_loc (location_t loc, tree ptr)
4364 HOST_WIDE_INT offset = 0;
4365 tree ptype = TREE_TYPE (ptr);
4366 tree tem;
4367 /* For convenience allow addresses that collapse to a simple base
4368 and offset. */
4369 if (TREE_CODE (ptr) == ADDR_EXPR
4370 && (handled_component_p (TREE_OPERAND (ptr, 0))
4371 || TREE_CODE (TREE_OPERAND (ptr, 0)) == MEM_REF))
4373 ptr = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &offset);
4374 gcc_assert (ptr);
4375 ptr = build_fold_addr_expr (ptr);
4376 gcc_assert (is_gimple_reg (ptr) || is_gimple_min_invariant (ptr));
4378 tem = build2 (MEM_REF, TREE_TYPE (ptype),
4379 ptr, build_int_cst (ptype, offset));
4380 SET_EXPR_LOCATION (tem, loc);
4381 return tem;
4384 /* Return the constant offset of a MEM_REF or TARGET_MEM_REF tree T. */
4386 offset_int
4387 mem_ref_offset (const_tree t)
4389 return offset_int::from (TREE_OPERAND (t, 1), SIGNED);
4392 /* Return an invariant ADDR_EXPR of type TYPE taking the address of BASE
4393 offsetted by OFFSET units. */
4395 tree
4396 build_invariant_address (tree type, tree base, HOST_WIDE_INT offset)
4398 tree ref = fold_build2 (MEM_REF, TREE_TYPE (type),
4399 build_fold_addr_expr (base),
4400 build_int_cst (ptr_type_node, offset));
4401 tree addr = build1 (ADDR_EXPR, type, ref);
4402 recompute_tree_invariant_for_addr_expr (addr);
4403 return addr;
4406 /* Similar except don't specify the TREE_TYPE
4407 and leave the TREE_SIDE_EFFECTS as 0.
4408 It is permissible for arguments to be null,
4409 or even garbage if their values do not matter. */
4411 tree
4412 build_nt (enum tree_code code, ...)
4414 tree t;
4415 int length;
4416 int i;
4417 va_list p;
4419 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4421 va_start (p, code);
4423 t = make_node (code);
4424 length = TREE_CODE_LENGTH (code);
4426 for (i = 0; i < length; i++)
4427 TREE_OPERAND (t, i) = va_arg (p, tree);
4429 va_end (p);
4430 return t;
4433 /* Similar to build_nt, but for creating a CALL_EXPR object with a
4434 tree vec. */
4436 tree
4437 build_nt_call_vec (tree fn, vec<tree, va_gc> *args)
4439 tree ret, t;
4440 unsigned int ix;
4442 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
4443 CALL_EXPR_FN (ret) = fn;
4444 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
4445 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
4446 CALL_EXPR_ARG (ret, ix) = t;
4447 return ret;
4450 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
4451 We do NOT enter this node in any sort of symbol table.
4453 LOC is the location of the decl.
4455 layout_decl is used to set up the decl's storage layout.
4456 Other slots are initialized to 0 or null pointers. */
4458 tree
4459 build_decl_stat (location_t loc, enum tree_code code, tree name,
4460 tree type MEM_STAT_DECL)
4462 tree t;
4464 t = make_node_stat (code PASS_MEM_STAT);
4465 DECL_SOURCE_LOCATION (t) = loc;
4467 /* if (type == error_mark_node)
4468 type = integer_type_node; */
4469 /* That is not done, deliberately, so that having error_mark_node
4470 as the type can suppress useless errors in the use of this variable. */
4472 DECL_NAME (t) = name;
4473 TREE_TYPE (t) = type;
4475 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
4476 layout_decl (t, 0);
4478 return t;
4481 /* Builds and returns function declaration with NAME and TYPE. */
4483 tree
4484 build_fn_decl (const char *name, tree type)
4486 tree id = get_identifier (name);
4487 tree decl = build_decl (input_location, FUNCTION_DECL, id, type);
4489 DECL_EXTERNAL (decl) = 1;
4490 TREE_PUBLIC (decl) = 1;
4491 DECL_ARTIFICIAL (decl) = 1;
4492 TREE_NOTHROW (decl) = 1;
4494 return decl;
4497 vec<tree, va_gc> *all_translation_units;
4499 /* Builds a new translation-unit decl with name NAME, queues it in the
4500 global list of translation-unit decls and returns it. */
4502 tree
4503 build_translation_unit_decl (tree name)
4505 tree tu = build_decl (UNKNOWN_LOCATION, TRANSLATION_UNIT_DECL,
4506 name, NULL_TREE);
4507 TRANSLATION_UNIT_LANGUAGE (tu) = lang_hooks.name;
4508 vec_safe_push (all_translation_units, tu);
4509 return tu;
4513 /* BLOCK nodes are used to represent the structure of binding contours
4514 and declarations, once those contours have been exited and their contents
4515 compiled. This information is used for outputting debugging info. */
4517 tree
4518 build_block (tree vars, tree subblocks, tree supercontext, tree chain)
4520 tree block = make_node (BLOCK);
4522 BLOCK_VARS (block) = vars;
4523 BLOCK_SUBBLOCKS (block) = subblocks;
4524 BLOCK_SUPERCONTEXT (block) = supercontext;
4525 BLOCK_CHAIN (block) = chain;
4526 return block;
4530 /* Like SET_EXPR_LOCATION, but make sure the tree can have a location.
4532 LOC is the location to use in tree T. */
4534 void
4535 protected_set_expr_location (tree t, location_t loc)
4537 if (t && CAN_HAVE_LOCATION_P (t))
4538 SET_EXPR_LOCATION (t, loc);
4541 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
4542 is ATTRIBUTE. */
4544 tree
4545 build_decl_attribute_variant (tree ddecl, tree attribute)
4547 DECL_ATTRIBUTES (ddecl) = attribute;
4548 return ddecl;
4551 /* Borrowed from hashtab.c iterative_hash implementation. */
4552 #define mix(a,b,c) \
4554 a -= b; a -= c; a ^= (c>>13); \
4555 b -= c; b -= a; b ^= (a<< 8); \
4556 c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \
4557 a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \
4558 b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \
4559 c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \
4560 a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \
4561 b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \
4562 c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \
4566 /* Produce good hash value combining VAL and VAL2. */
4567 hashval_t
4568 iterative_hash_hashval_t (hashval_t val, hashval_t val2)
4570 /* the golden ratio; an arbitrary value. */
4571 hashval_t a = 0x9e3779b9;
4573 mix (a, val, val2);
4574 return val2;
4577 /* Produce good hash value combining VAL and VAL2. */
4578 hashval_t
4579 iterative_hash_host_wide_int (HOST_WIDE_INT val, hashval_t val2)
4581 if (sizeof (HOST_WIDE_INT) == sizeof (hashval_t))
4582 return iterative_hash_hashval_t (val, val2);
4583 else
4585 hashval_t a = (hashval_t) val;
4586 /* Avoid warnings about shifting of more than the width of the type on
4587 hosts that won't execute this path. */
4588 int zero = 0;
4589 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 8 + zero));
4590 mix (a, b, val2);
4591 if (sizeof (HOST_WIDE_INT) > 2 * sizeof (hashval_t))
4593 hashval_t a = (hashval_t) (val >> (sizeof (hashval_t) * 16 + zero));
4594 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 24 + zero));
4595 mix (a, b, val2);
4597 return val2;
4601 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4602 is ATTRIBUTE and its qualifiers are QUALS.
4604 Record such modified types already made so we don't make duplicates. */
4606 tree
4607 build_type_attribute_qual_variant (tree ttype, tree attribute, int quals)
4609 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
4611 hashval_t hashcode = 0;
4612 tree ntype;
4613 int i;
4614 tree t;
4615 enum tree_code code = TREE_CODE (ttype);
4617 /* Building a distinct copy of a tagged type is inappropriate; it
4618 causes breakage in code that expects there to be a one-to-one
4619 relationship between a struct and its fields.
4620 build_duplicate_type is another solution (as used in
4621 handle_transparent_union_attribute), but that doesn't play well
4622 with the stronger C++ type identity model. */
4623 if (TREE_CODE (ttype) == RECORD_TYPE
4624 || TREE_CODE (ttype) == UNION_TYPE
4625 || TREE_CODE (ttype) == QUAL_UNION_TYPE
4626 || TREE_CODE (ttype) == ENUMERAL_TYPE)
4628 warning (OPT_Wattributes,
4629 "ignoring attributes applied to %qT after definition",
4630 TYPE_MAIN_VARIANT (ttype));
4631 return build_qualified_type (ttype, quals);
4634 ttype = build_qualified_type (ttype, TYPE_UNQUALIFIED);
4635 ntype = build_distinct_type_copy (ttype);
4637 TYPE_ATTRIBUTES (ntype) = attribute;
4639 hashcode = iterative_hash_object (code, hashcode);
4640 if (TREE_TYPE (ntype))
4641 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (ntype)),
4642 hashcode);
4643 hashcode = attribute_hash_list (attribute, hashcode);
4645 switch (TREE_CODE (ntype))
4647 case FUNCTION_TYPE:
4648 hashcode = type_hash_list (TYPE_ARG_TYPES (ntype), hashcode);
4649 break;
4650 case ARRAY_TYPE:
4651 if (TYPE_DOMAIN (ntype))
4652 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (ntype)),
4653 hashcode);
4654 break;
4655 case INTEGER_TYPE:
4656 t = TYPE_MAX_VALUE (ntype);
4657 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
4658 hashcode = iterative_hash_object (TREE_INT_CST_ELT (t, i), hashcode);
4659 break;
4660 case REAL_TYPE:
4661 case FIXED_POINT_TYPE:
4663 unsigned int precision = TYPE_PRECISION (ntype);
4664 hashcode = iterative_hash_object (precision, hashcode);
4666 break;
4667 default:
4668 break;
4671 ntype = type_hash_canon (hashcode, ntype);
4673 /* If the target-dependent attributes make NTYPE different from
4674 its canonical type, we will need to use structural equality
4675 checks for this type. */
4676 if (TYPE_STRUCTURAL_EQUALITY_P (ttype)
4677 || !comp_type_attributes (ntype, ttype))
4678 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
4679 else if (TYPE_CANONICAL (ntype) == ntype)
4680 TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype);
4682 ttype = build_qualified_type (ntype, quals);
4684 else if (TYPE_QUALS (ttype) != quals)
4685 ttype = build_qualified_type (ttype, quals);
4687 return ttype;
4690 /* Check if "omp declare simd" attribute arguments, CLAUSES1 and CLAUSES2, are
4691 the same. */
4693 static bool
4694 omp_declare_simd_clauses_equal (tree clauses1, tree clauses2)
4696 tree cl1, cl2;
4697 for (cl1 = clauses1, cl2 = clauses2;
4698 cl1 && cl2;
4699 cl1 = OMP_CLAUSE_CHAIN (cl1), cl2 = OMP_CLAUSE_CHAIN (cl2))
4701 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_CODE (cl2))
4702 return false;
4703 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_SIMDLEN)
4705 if (simple_cst_equal (OMP_CLAUSE_DECL (cl1),
4706 OMP_CLAUSE_DECL (cl2)) != 1)
4707 return false;
4709 switch (OMP_CLAUSE_CODE (cl1))
4711 case OMP_CLAUSE_ALIGNED:
4712 if (simple_cst_equal (OMP_CLAUSE_ALIGNED_ALIGNMENT (cl1),
4713 OMP_CLAUSE_ALIGNED_ALIGNMENT (cl2)) != 1)
4714 return false;
4715 break;
4716 case OMP_CLAUSE_LINEAR:
4717 if (simple_cst_equal (OMP_CLAUSE_LINEAR_STEP (cl1),
4718 OMP_CLAUSE_LINEAR_STEP (cl2)) != 1)
4719 return false;
4720 break;
4721 case OMP_CLAUSE_SIMDLEN:
4722 if (simple_cst_equal (OMP_CLAUSE_SIMDLEN_EXPR (cl1),
4723 OMP_CLAUSE_SIMDLEN_EXPR (cl2)) != 1)
4724 return false;
4725 default:
4726 break;
4729 return true;
4732 /* Compare two constructor-element-type constants. Return 1 if the lists
4733 are known to be equal; otherwise return 0. */
4735 static bool
4736 simple_cst_list_equal (const_tree l1, const_tree l2)
4738 while (l1 != NULL_TREE && l2 != NULL_TREE)
4740 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
4741 return false;
4743 l1 = TREE_CHAIN (l1);
4744 l2 = TREE_CHAIN (l2);
4747 return l1 == l2;
4750 /* Compare two attributes for their value identity. Return true if the
4751 attribute values are known to be equal; otherwise return false.
4754 static bool
4755 attribute_value_equal (const_tree attr1, const_tree attr2)
4757 if (TREE_VALUE (attr1) == TREE_VALUE (attr2))
4758 return true;
4760 if (TREE_VALUE (attr1) != NULL_TREE
4761 && TREE_CODE (TREE_VALUE (attr1)) == TREE_LIST
4762 && TREE_VALUE (attr2) != NULL
4763 && TREE_CODE (TREE_VALUE (attr2)) == TREE_LIST)
4764 return (simple_cst_list_equal (TREE_VALUE (attr1),
4765 TREE_VALUE (attr2)) == 1);
4767 if ((flag_openmp || flag_openmp_simd)
4768 && TREE_VALUE (attr1) && TREE_VALUE (attr2)
4769 && TREE_CODE (TREE_VALUE (attr1)) == OMP_CLAUSE
4770 && TREE_CODE (TREE_VALUE (attr2)) == OMP_CLAUSE)
4771 return omp_declare_simd_clauses_equal (TREE_VALUE (attr1),
4772 TREE_VALUE (attr2));
4774 return (simple_cst_equal (TREE_VALUE (attr1), TREE_VALUE (attr2)) == 1);
4777 /* Return 0 if the attributes for two types are incompatible, 1 if they
4778 are compatible, and 2 if they are nearly compatible (which causes a
4779 warning to be generated). */
4781 comp_type_attributes (const_tree type1, const_tree type2)
4783 const_tree a1 = TYPE_ATTRIBUTES (type1);
4784 const_tree a2 = TYPE_ATTRIBUTES (type2);
4785 const_tree a;
4787 if (a1 == a2)
4788 return 1;
4789 for (a = a1; a != NULL_TREE; a = TREE_CHAIN (a))
4791 const struct attribute_spec *as;
4792 const_tree attr;
4794 as = lookup_attribute_spec (get_attribute_name (a));
4795 if (!as || as->affects_type_identity == false)
4796 continue;
4798 attr = lookup_attribute (as->name, CONST_CAST_TREE (a2));
4799 if (!attr || !attribute_value_equal (a, attr))
4800 break;
4802 if (!a)
4804 for (a = a2; a != NULL_TREE; a = TREE_CHAIN (a))
4806 const struct attribute_spec *as;
4808 as = lookup_attribute_spec (get_attribute_name (a));
4809 if (!as || as->affects_type_identity == false)
4810 continue;
4812 if (!lookup_attribute (as->name, CONST_CAST_TREE (a1)))
4813 break;
4814 /* We don't need to compare trees again, as we did this
4815 already in first loop. */
4817 /* All types - affecting identity - are equal, so
4818 there is no need to call target hook for comparison. */
4819 if (!a)
4820 return 1;
4822 /* As some type combinations - like default calling-convention - might
4823 be compatible, we have to call the target hook to get the final result. */
4824 return targetm.comp_type_attributes (type1, type2);
4827 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4828 is ATTRIBUTE.
4830 Record such modified types already made so we don't make duplicates. */
4832 tree
4833 build_type_attribute_variant (tree ttype, tree attribute)
4835 return build_type_attribute_qual_variant (ttype, attribute,
4836 TYPE_QUALS (ttype));
4840 /* Reset the expression *EXPR_P, a size or position.
4842 ??? We could reset all non-constant sizes or positions. But it's cheap
4843 enough to not do so and refrain from adding workarounds to dwarf2out.c.
4845 We need to reset self-referential sizes or positions because they cannot
4846 be gimplified and thus can contain a CALL_EXPR after the gimplification
4847 is finished, which will run afoul of LTO streaming. And they need to be
4848 reset to something essentially dummy but not constant, so as to preserve
4849 the properties of the object they are attached to. */
4851 static inline void
4852 free_lang_data_in_one_sizepos (tree *expr_p)
4854 tree expr = *expr_p;
4855 if (CONTAINS_PLACEHOLDER_P (expr))
4856 *expr_p = build0 (PLACEHOLDER_EXPR, TREE_TYPE (expr));
4860 /* Reset all the fields in a binfo node BINFO. We only keep
4861 BINFO_VTABLE, which is used by gimple_fold_obj_type_ref. */
4863 static void
4864 free_lang_data_in_binfo (tree binfo)
4866 unsigned i;
4867 tree t;
4869 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
4871 BINFO_VIRTUALS (binfo) = NULL_TREE;
4872 BINFO_BASE_ACCESSES (binfo) = NULL;
4873 BINFO_INHERITANCE_CHAIN (binfo) = NULL_TREE;
4874 BINFO_SUBVTT_INDEX (binfo) = NULL_TREE;
4876 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (binfo), i, t)
4877 free_lang_data_in_binfo (t);
4881 /* Reset all language specific information still present in TYPE. */
4883 static void
4884 free_lang_data_in_type (tree type)
4886 gcc_assert (TYPE_P (type));
4888 /* Give the FE a chance to remove its own data first. */
4889 lang_hooks.free_lang_data (type);
4891 TREE_LANG_FLAG_0 (type) = 0;
4892 TREE_LANG_FLAG_1 (type) = 0;
4893 TREE_LANG_FLAG_2 (type) = 0;
4894 TREE_LANG_FLAG_3 (type) = 0;
4895 TREE_LANG_FLAG_4 (type) = 0;
4896 TREE_LANG_FLAG_5 (type) = 0;
4897 TREE_LANG_FLAG_6 (type) = 0;
4899 if (TREE_CODE (type) == FUNCTION_TYPE)
4901 /* Remove the const and volatile qualifiers from arguments. The
4902 C++ front end removes them, but the C front end does not,
4903 leading to false ODR violation errors when merging two
4904 instances of the same function signature compiled by
4905 different front ends. */
4906 tree p;
4908 for (p = TYPE_ARG_TYPES (type); p; p = TREE_CHAIN (p))
4910 tree arg_type = TREE_VALUE (p);
4912 if (TYPE_READONLY (arg_type) || TYPE_VOLATILE (arg_type))
4914 int quals = TYPE_QUALS (arg_type)
4915 & ~TYPE_QUAL_CONST
4916 & ~TYPE_QUAL_VOLATILE;
4917 TREE_VALUE (p) = build_qualified_type (arg_type, quals);
4918 free_lang_data_in_type (TREE_VALUE (p));
4923 /* Remove members that are not actually FIELD_DECLs from the field
4924 list of an aggregate. These occur in C++. */
4925 if (RECORD_OR_UNION_TYPE_P (type))
4927 tree prev, member;
4929 /* Note that TYPE_FIELDS can be shared across distinct
4930 TREE_TYPEs. Therefore, if the first field of TYPE_FIELDS is
4931 to be removed, we cannot set its TREE_CHAIN to NULL.
4932 Otherwise, we would not be able to find all the other fields
4933 in the other instances of this TREE_TYPE.
4935 This was causing an ICE in testsuite/g++.dg/lto/20080915.C. */
4936 prev = NULL_TREE;
4937 member = TYPE_FIELDS (type);
4938 while (member)
4940 if (TREE_CODE (member) == FIELD_DECL
4941 || TREE_CODE (member) == TYPE_DECL)
4943 if (prev)
4944 TREE_CHAIN (prev) = member;
4945 else
4946 TYPE_FIELDS (type) = member;
4947 prev = member;
4950 member = TREE_CHAIN (member);
4953 if (prev)
4954 TREE_CHAIN (prev) = NULL_TREE;
4955 else
4956 TYPE_FIELDS (type) = NULL_TREE;
4958 TYPE_METHODS (type) = NULL_TREE;
4959 if (TYPE_BINFO (type))
4960 free_lang_data_in_binfo (TYPE_BINFO (type));
4962 else
4964 /* For non-aggregate types, clear out the language slot (which
4965 overloads TYPE_BINFO). */
4966 TYPE_LANG_SLOT_1 (type) = NULL_TREE;
4968 if (INTEGRAL_TYPE_P (type)
4969 || SCALAR_FLOAT_TYPE_P (type)
4970 || FIXED_POINT_TYPE_P (type))
4972 free_lang_data_in_one_sizepos (&TYPE_MIN_VALUE (type));
4973 free_lang_data_in_one_sizepos (&TYPE_MAX_VALUE (type));
4977 free_lang_data_in_one_sizepos (&TYPE_SIZE (type));
4978 free_lang_data_in_one_sizepos (&TYPE_SIZE_UNIT (type));
4980 if (TYPE_CONTEXT (type)
4981 && TREE_CODE (TYPE_CONTEXT (type)) == BLOCK)
4983 tree ctx = TYPE_CONTEXT (type);
4986 ctx = BLOCK_SUPERCONTEXT (ctx);
4988 while (ctx && TREE_CODE (ctx) == BLOCK);
4989 TYPE_CONTEXT (type) = ctx;
4994 /* Return true if DECL may need an assembler name to be set. */
4996 static inline bool
4997 need_assembler_name_p (tree decl)
4999 /* Only FUNCTION_DECLs and VAR_DECLs are considered. */
5000 if (TREE_CODE (decl) != FUNCTION_DECL
5001 && TREE_CODE (decl) != VAR_DECL)
5002 return false;
5004 /* If DECL already has its assembler name set, it does not need a
5005 new one. */
5006 if (!HAS_DECL_ASSEMBLER_NAME_P (decl)
5007 || DECL_ASSEMBLER_NAME_SET_P (decl))
5008 return false;
5010 /* Abstract decls do not need an assembler name. */
5011 if (DECL_ABSTRACT (decl))
5012 return false;
5014 /* For VAR_DECLs, only static, public and external symbols need an
5015 assembler name. */
5016 if (TREE_CODE (decl) == VAR_DECL
5017 && !TREE_STATIC (decl)
5018 && !TREE_PUBLIC (decl)
5019 && !DECL_EXTERNAL (decl))
5020 return false;
5022 if (TREE_CODE (decl) == FUNCTION_DECL)
5024 /* Do not set assembler name on builtins. Allow RTL expansion to
5025 decide whether to expand inline or via a regular call. */
5026 if (DECL_BUILT_IN (decl)
5027 && DECL_BUILT_IN_CLASS (decl) != BUILT_IN_FRONTEND)
5028 return false;
5030 /* Functions represented in the callgraph need an assembler name. */
5031 if (cgraph_get_node (decl) != NULL)
5032 return true;
5034 /* Unused and not public functions don't need an assembler name. */
5035 if (!TREE_USED (decl) && !TREE_PUBLIC (decl))
5036 return false;
5039 return true;
5043 /* Reset all language specific information still present in symbol
5044 DECL. */
5046 static void
5047 free_lang_data_in_decl (tree decl)
5049 gcc_assert (DECL_P (decl));
5051 /* Give the FE a chance to remove its own data first. */
5052 lang_hooks.free_lang_data (decl);
5054 TREE_LANG_FLAG_0 (decl) = 0;
5055 TREE_LANG_FLAG_1 (decl) = 0;
5056 TREE_LANG_FLAG_2 (decl) = 0;
5057 TREE_LANG_FLAG_3 (decl) = 0;
5058 TREE_LANG_FLAG_4 (decl) = 0;
5059 TREE_LANG_FLAG_5 (decl) = 0;
5060 TREE_LANG_FLAG_6 (decl) = 0;
5062 free_lang_data_in_one_sizepos (&DECL_SIZE (decl));
5063 free_lang_data_in_one_sizepos (&DECL_SIZE_UNIT (decl));
5064 if (TREE_CODE (decl) == FIELD_DECL)
5066 free_lang_data_in_one_sizepos (&DECL_FIELD_OFFSET (decl));
5067 if (TREE_CODE (DECL_CONTEXT (decl)) == QUAL_UNION_TYPE)
5068 DECL_QUALIFIER (decl) = NULL_TREE;
5071 if (TREE_CODE (decl) == FUNCTION_DECL)
5073 struct cgraph_node *node;
5074 if (!(node = cgraph_get_node (decl))
5075 || (!node->definition && !node->clones))
5077 if (node)
5078 cgraph_release_function_body (node);
5079 else
5081 release_function_body (decl);
5082 DECL_ARGUMENTS (decl) = NULL;
5083 DECL_RESULT (decl) = NULL;
5084 DECL_INITIAL (decl) = error_mark_node;
5087 if (gimple_has_body_p (decl))
5089 tree t;
5091 /* If DECL has a gimple body, then the context for its
5092 arguments must be DECL. Otherwise, it doesn't really
5093 matter, as we will not be emitting any code for DECL. In
5094 general, there may be other instances of DECL created by
5095 the front end and since PARM_DECLs are generally shared,
5096 their DECL_CONTEXT changes as the replicas of DECL are
5097 created. The only time where DECL_CONTEXT is important
5098 is for the FUNCTION_DECLs that have a gimple body (since
5099 the PARM_DECL will be used in the function's body). */
5100 for (t = DECL_ARGUMENTS (decl); t; t = TREE_CHAIN (t))
5101 DECL_CONTEXT (t) = decl;
5104 /* DECL_SAVED_TREE holds the GENERIC representation for DECL.
5105 At this point, it is not needed anymore. */
5106 DECL_SAVED_TREE (decl) = NULL_TREE;
5108 /* Clear the abstract origin if it refers to a method. Otherwise
5109 dwarf2out.c will ICE as we clear TYPE_METHODS and thus the
5110 origin will not be output correctly. */
5111 if (DECL_ABSTRACT_ORIGIN (decl)
5112 && DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))
5113 && RECORD_OR_UNION_TYPE_P
5114 (DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))))
5115 DECL_ABSTRACT_ORIGIN (decl) = NULL_TREE;
5117 /* Sometimes the C++ frontend doesn't manage to transform a temporary
5118 DECL_VINDEX referring to itself into a vtable slot number as it
5119 should. Happens with functions that are copied and then forgotten
5120 about. Just clear it, it won't matter anymore. */
5121 if (DECL_VINDEX (decl) && !tree_fits_shwi_p (DECL_VINDEX (decl)))
5122 DECL_VINDEX (decl) = NULL_TREE;
5124 else if (TREE_CODE (decl) == VAR_DECL)
5126 if ((DECL_EXTERNAL (decl)
5127 && (!TREE_STATIC (decl) || !TREE_READONLY (decl)))
5128 || (decl_function_context (decl) && !TREE_STATIC (decl)))
5129 DECL_INITIAL (decl) = NULL_TREE;
5131 else if (TREE_CODE (decl) == TYPE_DECL
5132 || TREE_CODE (decl) == FIELD_DECL)
5133 DECL_INITIAL (decl) = NULL_TREE;
5134 else if (TREE_CODE (decl) == TRANSLATION_UNIT_DECL
5135 && DECL_INITIAL (decl)
5136 && TREE_CODE (DECL_INITIAL (decl)) == BLOCK)
5138 /* Strip builtins from the translation-unit BLOCK. We still have targets
5139 without builtin_decl_explicit support and also builtins are shared
5140 nodes and thus we can't use TREE_CHAIN in multiple lists. */
5141 tree *nextp = &BLOCK_VARS (DECL_INITIAL (decl));
5142 while (*nextp)
5144 tree var = *nextp;
5145 if (TREE_CODE (var) == FUNCTION_DECL
5146 && DECL_BUILT_IN (var))
5147 *nextp = TREE_CHAIN (var);
5148 else
5149 nextp = &TREE_CHAIN (var);
5155 /* Data used when collecting DECLs and TYPEs for language data removal. */
5157 struct free_lang_data_d
5159 /* Worklist to avoid excessive recursion. */
5160 vec<tree> worklist;
5162 /* Set of traversed objects. Used to avoid duplicate visits. */
5163 struct pointer_set_t *pset;
5165 /* Array of symbols to process with free_lang_data_in_decl. */
5166 vec<tree> decls;
5168 /* Array of types to process with free_lang_data_in_type. */
5169 vec<tree> types;
5173 /* Save all language fields needed to generate proper debug information
5174 for DECL. This saves most fields cleared out by free_lang_data_in_decl. */
5176 static void
5177 save_debug_info_for_decl (tree t)
5179 /*struct saved_debug_info_d *sdi;*/
5181 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && DECL_P (t));
5183 /* FIXME. Partial implementation for saving debug info removed. */
5187 /* Save all language fields needed to generate proper debug information
5188 for TYPE. This saves most fields cleared out by free_lang_data_in_type. */
5190 static void
5191 save_debug_info_for_type (tree t)
5193 /*struct saved_debug_info_d *sdi;*/
5195 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && TYPE_P (t));
5197 /* FIXME. Partial implementation for saving debug info removed. */
5201 /* Add type or decl T to one of the list of tree nodes that need their
5202 language data removed. The lists are held inside FLD. */
5204 static void
5205 add_tree_to_fld_list (tree t, struct free_lang_data_d *fld)
5207 if (DECL_P (t))
5209 fld->decls.safe_push (t);
5210 if (debug_info_level > DINFO_LEVEL_TERSE)
5211 save_debug_info_for_decl (t);
5213 else if (TYPE_P (t))
5215 fld->types.safe_push (t);
5216 if (debug_info_level > DINFO_LEVEL_TERSE)
5217 save_debug_info_for_type (t);
5219 else
5220 gcc_unreachable ();
5223 /* Push tree node T into FLD->WORKLIST. */
5225 static inline void
5226 fld_worklist_push (tree t, struct free_lang_data_d *fld)
5228 if (t && !is_lang_specific (t) && !pointer_set_contains (fld->pset, t))
5229 fld->worklist.safe_push ((t));
5233 /* Operand callback helper for free_lang_data_in_node. *TP is the
5234 subtree operand being considered. */
5236 static tree
5237 find_decls_types_r (tree *tp, int *ws, void *data)
5239 tree t = *tp;
5240 struct free_lang_data_d *fld = (struct free_lang_data_d *) data;
5242 if (TREE_CODE (t) == TREE_LIST)
5243 return NULL_TREE;
5245 /* Language specific nodes will be removed, so there is no need
5246 to gather anything under them. */
5247 if (is_lang_specific (t))
5249 *ws = 0;
5250 return NULL_TREE;
5253 if (DECL_P (t))
5255 /* Note that walk_tree does not traverse every possible field in
5256 decls, so we have to do our own traversals here. */
5257 add_tree_to_fld_list (t, fld);
5259 fld_worklist_push (DECL_NAME (t), fld);
5260 fld_worklist_push (DECL_CONTEXT (t), fld);
5261 fld_worklist_push (DECL_SIZE (t), fld);
5262 fld_worklist_push (DECL_SIZE_UNIT (t), fld);
5264 /* We are going to remove everything under DECL_INITIAL for
5265 TYPE_DECLs. No point walking them. */
5266 if (TREE_CODE (t) != TYPE_DECL)
5267 fld_worklist_push (DECL_INITIAL (t), fld);
5269 fld_worklist_push (DECL_ATTRIBUTES (t), fld);
5270 fld_worklist_push (DECL_ABSTRACT_ORIGIN (t), fld);
5272 if (TREE_CODE (t) == FUNCTION_DECL)
5274 fld_worklist_push (DECL_ARGUMENTS (t), fld);
5275 fld_worklist_push (DECL_RESULT (t), fld);
5277 else if (TREE_CODE (t) == TYPE_DECL)
5279 fld_worklist_push (DECL_ARGUMENT_FLD (t), fld);
5280 fld_worklist_push (DECL_VINDEX (t), fld);
5281 fld_worklist_push (DECL_ORIGINAL_TYPE (t), fld);
5283 else if (TREE_CODE (t) == FIELD_DECL)
5285 fld_worklist_push (DECL_FIELD_OFFSET (t), fld);
5286 fld_worklist_push (DECL_BIT_FIELD_TYPE (t), fld);
5287 fld_worklist_push (DECL_FIELD_BIT_OFFSET (t), fld);
5288 fld_worklist_push (DECL_FCONTEXT (t), fld);
5290 else if (TREE_CODE (t) == VAR_DECL)
5292 fld_worklist_push (DECL_SECTION_NAME (t), fld);
5295 if ((TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
5296 && DECL_HAS_VALUE_EXPR_P (t))
5297 fld_worklist_push (DECL_VALUE_EXPR (t), fld);
5299 if (TREE_CODE (t) != FIELD_DECL
5300 && TREE_CODE (t) != TYPE_DECL)
5301 fld_worklist_push (TREE_CHAIN (t), fld);
5302 *ws = 0;
5304 else if (TYPE_P (t))
5306 /* Note that walk_tree does not traverse every possible field in
5307 types, so we have to do our own traversals here. */
5308 add_tree_to_fld_list (t, fld);
5310 if (!RECORD_OR_UNION_TYPE_P (t))
5311 fld_worklist_push (TYPE_CACHED_VALUES (t), fld);
5312 fld_worklist_push (TYPE_SIZE (t), fld);
5313 fld_worklist_push (TYPE_SIZE_UNIT (t), fld);
5314 fld_worklist_push (TYPE_ATTRIBUTES (t), fld);
5315 fld_worklist_push (TYPE_POINTER_TO (t), fld);
5316 fld_worklist_push (TYPE_REFERENCE_TO (t), fld);
5317 fld_worklist_push (TYPE_NAME (t), fld);
5318 /* Do not walk TYPE_NEXT_PTR_TO or TYPE_NEXT_REF_TO. We do not stream
5319 them and thus do not and want not to reach unused pointer types
5320 this way. */
5321 if (!POINTER_TYPE_P (t))
5322 fld_worklist_push (TYPE_MINVAL (t), fld);
5323 if (!RECORD_OR_UNION_TYPE_P (t))
5324 fld_worklist_push (TYPE_MAXVAL (t), fld);
5325 fld_worklist_push (TYPE_MAIN_VARIANT (t), fld);
5326 /* Do not walk TYPE_NEXT_VARIANT. We do not stream it and thus
5327 do not and want not to reach unused variants this way. */
5328 if (TYPE_CONTEXT (t))
5330 tree ctx = TYPE_CONTEXT (t);
5331 /* We adjust BLOCK TYPE_CONTEXTs to the innermost non-BLOCK one.
5332 So push that instead. */
5333 while (ctx && TREE_CODE (ctx) == BLOCK)
5334 ctx = BLOCK_SUPERCONTEXT (ctx);
5335 fld_worklist_push (ctx, fld);
5337 /* Do not walk TYPE_CANONICAL. We do not stream it and thus do not
5338 and want not to reach unused types this way. */
5340 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t))
5342 unsigned i;
5343 tree tem;
5344 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (TYPE_BINFO (t)), i, tem)
5345 fld_worklist_push (TREE_TYPE (tem), fld);
5346 tem = BINFO_VIRTUALS (TYPE_BINFO (t));
5347 if (tem
5348 /* The Java FE overloads BINFO_VIRTUALS for its own purpose. */
5349 && TREE_CODE (tem) == TREE_LIST)
5352 fld_worklist_push (TREE_VALUE (tem), fld);
5353 tem = TREE_CHAIN (tem);
5355 while (tem);
5357 if (RECORD_OR_UNION_TYPE_P (t))
5359 tree tem;
5360 /* Push all TYPE_FIELDS - there can be interleaving interesting
5361 and non-interesting things. */
5362 tem = TYPE_FIELDS (t);
5363 while (tem)
5365 if (TREE_CODE (tem) == FIELD_DECL
5366 || TREE_CODE (tem) == TYPE_DECL)
5367 fld_worklist_push (tem, fld);
5368 tem = TREE_CHAIN (tem);
5372 fld_worklist_push (TYPE_STUB_DECL (t), fld);
5373 *ws = 0;
5375 else if (TREE_CODE (t) == BLOCK)
5377 tree tem;
5378 for (tem = BLOCK_VARS (t); tem; tem = TREE_CHAIN (tem))
5379 fld_worklist_push (tem, fld);
5380 for (tem = BLOCK_SUBBLOCKS (t); tem; tem = BLOCK_CHAIN (tem))
5381 fld_worklist_push (tem, fld);
5382 fld_worklist_push (BLOCK_ABSTRACT_ORIGIN (t), fld);
5385 if (TREE_CODE (t) != IDENTIFIER_NODE
5386 && CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED))
5387 fld_worklist_push (TREE_TYPE (t), fld);
5389 return NULL_TREE;
5393 /* Find decls and types in T. */
5395 static void
5396 find_decls_types (tree t, struct free_lang_data_d *fld)
5398 while (1)
5400 if (!pointer_set_contains (fld->pset, t))
5401 walk_tree (&t, find_decls_types_r, fld, fld->pset);
5402 if (fld->worklist.is_empty ())
5403 break;
5404 t = fld->worklist.pop ();
5408 /* Translate all the types in LIST with the corresponding runtime
5409 types. */
5411 static tree
5412 get_eh_types_for_runtime (tree list)
5414 tree head, prev;
5416 if (list == NULL_TREE)
5417 return NULL_TREE;
5419 head = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5420 prev = head;
5421 list = TREE_CHAIN (list);
5422 while (list)
5424 tree n = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5425 TREE_CHAIN (prev) = n;
5426 prev = TREE_CHAIN (prev);
5427 list = TREE_CHAIN (list);
5430 return head;
5434 /* Find decls and types referenced in EH region R and store them in
5435 FLD->DECLS and FLD->TYPES. */
5437 static void
5438 find_decls_types_in_eh_region (eh_region r, struct free_lang_data_d *fld)
5440 switch (r->type)
5442 case ERT_CLEANUP:
5443 break;
5445 case ERT_TRY:
5447 eh_catch c;
5449 /* The types referenced in each catch must first be changed to the
5450 EH types used at runtime. This removes references to FE types
5451 in the region. */
5452 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
5454 c->type_list = get_eh_types_for_runtime (c->type_list);
5455 walk_tree (&c->type_list, find_decls_types_r, fld, fld->pset);
5458 break;
5460 case ERT_ALLOWED_EXCEPTIONS:
5461 r->u.allowed.type_list
5462 = get_eh_types_for_runtime (r->u.allowed.type_list);
5463 walk_tree (&r->u.allowed.type_list, find_decls_types_r, fld, fld->pset);
5464 break;
5466 case ERT_MUST_NOT_THROW:
5467 walk_tree (&r->u.must_not_throw.failure_decl,
5468 find_decls_types_r, fld, fld->pset);
5469 break;
5474 /* Find decls and types referenced in cgraph node N and store them in
5475 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5476 look for *every* kind of DECL and TYPE node reachable from N,
5477 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5478 NAMESPACE_DECLs, etc). */
5480 static void
5481 find_decls_types_in_node (struct cgraph_node *n, struct free_lang_data_d *fld)
5483 basic_block bb;
5484 struct function *fn;
5485 unsigned ix;
5486 tree t;
5488 find_decls_types (n->decl, fld);
5490 if (!gimple_has_body_p (n->decl))
5491 return;
5493 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
5495 fn = DECL_STRUCT_FUNCTION (n->decl);
5497 /* Traverse locals. */
5498 FOR_EACH_LOCAL_DECL (fn, ix, t)
5499 find_decls_types (t, fld);
5501 /* Traverse EH regions in FN. */
5503 eh_region r;
5504 FOR_ALL_EH_REGION_FN (r, fn)
5505 find_decls_types_in_eh_region (r, fld);
5508 /* Traverse every statement in FN. */
5509 FOR_EACH_BB_FN (bb, fn)
5511 gimple_stmt_iterator si;
5512 unsigned i;
5514 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
5516 gimple phi = gsi_stmt (si);
5518 for (i = 0; i < gimple_phi_num_args (phi); i++)
5520 tree *arg_p = gimple_phi_arg_def_ptr (phi, i);
5521 find_decls_types (*arg_p, fld);
5525 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
5527 gimple stmt = gsi_stmt (si);
5529 if (is_gimple_call (stmt))
5530 find_decls_types (gimple_call_fntype (stmt), fld);
5532 for (i = 0; i < gimple_num_ops (stmt); i++)
5534 tree arg = gimple_op (stmt, i);
5535 find_decls_types (arg, fld);
5542 /* Find decls and types referenced in varpool node N and store them in
5543 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5544 look for *every* kind of DECL and TYPE node reachable from N,
5545 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5546 NAMESPACE_DECLs, etc). */
5548 static void
5549 find_decls_types_in_var (varpool_node *v, struct free_lang_data_d *fld)
5551 find_decls_types (v->decl, fld);
5554 /* If T needs an assembler name, have one created for it. */
5556 void
5557 assign_assembler_name_if_neeeded (tree t)
5559 if (need_assembler_name_p (t))
5561 /* When setting DECL_ASSEMBLER_NAME, the C++ mangler may emit
5562 diagnostics that use input_location to show locus
5563 information. The problem here is that, at this point,
5564 input_location is generally anchored to the end of the file
5565 (since the parser is long gone), so we don't have a good
5566 position to pin it to.
5568 To alleviate this problem, this uses the location of T's
5569 declaration. Examples of this are
5570 testsuite/g++.dg/template/cond2.C and
5571 testsuite/g++.dg/template/pr35240.C. */
5572 location_t saved_location = input_location;
5573 input_location = DECL_SOURCE_LOCATION (t);
5575 decl_assembler_name (t);
5577 input_location = saved_location;
5582 /* Free language specific information for every operand and expression
5583 in every node of the call graph. This process operates in three stages:
5585 1- Every callgraph node and varpool node is traversed looking for
5586 decls and types embedded in them. This is a more exhaustive
5587 search than that done by find_referenced_vars, because it will
5588 also collect individual fields, decls embedded in types, etc.
5590 2- All the decls found are sent to free_lang_data_in_decl.
5592 3- All the types found are sent to free_lang_data_in_type.
5594 The ordering between decls and types is important because
5595 free_lang_data_in_decl sets assembler names, which includes
5596 mangling. So types cannot be freed up until assembler names have
5597 been set up. */
5599 static void
5600 free_lang_data_in_cgraph (void)
5602 struct cgraph_node *n;
5603 varpool_node *v;
5604 struct free_lang_data_d fld;
5605 tree t;
5606 unsigned i;
5607 alias_pair *p;
5609 /* Initialize sets and arrays to store referenced decls and types. */
5610 fld.pset = pointer_set_create ();
5611 fld.worklist.create (0);
5612 fld.decls.create (100);
5613 fld.types.create (100);
5615 /* Find decls and types in the body of every function in the callgraph. */
5616 FOR_EACH_FUNCTION (n)
5617 find_decls_types_in_node (n, &fld);
5619 FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
5620 find_decls_types (p->decl, &fld);
5622 /* Find decls and types in every varpool symbol. */
5623 FOR_EACH_VARIABLE (v)
5624 find_decls_types_in_var (v, &fld);
5626 /* Set the assembler name on every decl found. We need to do this
5627 now because free_lang_data_in_decl will invalidate data needed
5628 for mangling. This breaks mangling on interdependent decls. */
5629 FOR_EACH_VEC_ELT (fld.decls, i, t)
5630 assign_assembler_name_if_neeeded (t);
5632 /* Traverse every decl found freeing its language data. */
5633 FOR_EACH_VEC_ELT (fld.decls, i, t)
5634 free_lang_data_in_decl (t);
5636 /* Traverse every type found freeing its language data. */
5637 FOR_EACH_VEC_ELT (fld.types, i, t)
5638 free_lang_data_in_type (t);
5640 pointer_set_destroy (fld.pset);
5641 fld.worklist.release ();
5642 fld.decls.release ();
5643 fld.types.release ();
5647 /* Free resources that are used by FE but are not needed once they are done. */
5649 static unsigned
5650 free_lang_data (void)
5652 unsigned i;
5654 /* If we are the LTO frontend we have freed lang-specific data already. */
5655 if (in_lto_p
5656 || !flag_generate_lto)
5657 return 0;
5659 /* Allocate and assign alias sets to the standard integer types
5660 while the slots are still in the way the frontends generated them. */
5661 for (i = 0; i < itk_none; ++i)
5662 if (integer_types[i])
5663 TYPE_ALIAS_SET (integer_types[i]) = get_alias_set (integer_types[i]);
5665 /* Traverse the IL resetting language specific information for
5666 operands, expressions, etc. */
5667 free_lang_data_in_cgraph ();
5669 /* Create gimple variants for common types. */
5670 ptrdiff_type_node = integer_type_node;
5671 fileptr_type_node = ptr_type_node;
5673 /* Reset some langhooks. Do not reset types_compatible_p, it may
5674 still be used indirectly via the get_alias_set langhook. */
5675 lang_hooks.dwarf_name = lhd_dwarf_name;
5676 lang_hooks.decl_printable_name = gimple_decl_printable_name;
5677 /* We do not want the default decl_assembler_name implementation,
5678 rather if we have fixed everything we want a wrapper around it
5679 asserting that all non-local symbols already got their assembler
5680 name and only produce assembler names for local symbols. Or rather
5681 make sure we never call decl_assembler_name on local symbols and
5682 devise a separate, middle-end private scheme for it. */
5684 /* Reset diagnostic machinery. */
5685 tree_diagnostics_defaults (global_dc);
5687 return 0;
5691 namespace {
5693 const pass_data pass_data_ipa_free_lang_data =
5695 SIMPLE_IPA_PASS, /* type */
5696 "*free_lang_data", /* name */
5697 OPTGROUP_NONE, /* optinfo_flags */
5698 true, /* has_execute */
5699 TV_IPA_FREE_LANG_DATA, /* tv_id */
5700 0, /* properties_required */
5701 0, /* properties_provided */
5702 0, /* properties_destroyed */
5703 0, /* todo_flags_start */
5704 0, /* todo_flags_finish */
5707 class pass_ipa_free_lang_data : public simple_ipa_opt_pass
5709 public:
5710 pass_ipa_free_lang_data (gcc::context *ctxt)
5711 : simple_ipa_opt_pass (pass_data_ipa_free_lang_data, ctxt)
5714 /* opt_pass methods: */
5715 virtual unsigned int execute (function *) { return free_lang_data (); }
5717 }; // class pass_ipa_free_lang_data
5719 } // anon namespace
5721 simple_ipa_opt_pass *
5722 make_pass_ipa_free_lang_data (gcc::context *ctxt)
5724 return new pass_ipa_free_lang_data (ctxt);
5727 /* The backbone of is_attribute_p(). ATTR_LEN is the string length of
5728 ATTR_NAME. Also used internally by remove_attribute(). */
5729 bool
5730 private_is_attribute_p (const char *attr_name, size_t attr_len, const_tree ident)
5732 size_t ident_len = IDENTIFIER_LENGTH (ident);
5734 if (ident_len == attr_len)
5736 if (strcmp (attr_name, IDENTIFIER_POINTER (ident)) == 0)
5737 return true;
5739 else if (ident_len == attr_len + 4)
5741 /* There is the possibility that ATTR is 'text' and IDENT is
5742 '__text__'. */
5743 const char *p = IDENTIFIER_POINTER (ident);
5744 if (p[0] == '_' && p[1] == '_'
5745 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5746 && strncmp (attr_name, p + 2, attr_len) == 0)
5747 return true;
5750 return false;
5753 /* The backbone of lookup_attribute(). ATTR_LEN is the string length
5754 of ATTR_NAME, and LIST is not NULL_TREE. */
5755 tree
5756 private_lookup_attribute (const char *attr_name, size_t attr_len, tree list)
5758 while (list)
5760 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5762 if (ident_len == attr_len)
5764 if (!strcmp (attr_name,
5765 IDENTIFIER_POINTER (get_attribute_name (list))))
5766 break;
5768 /* TODO: If we made sure that attributes were stored in the
5769 canonical form without '__...__' (ie, as in 'text' as opposed
5770 to '__text__') then we could avoid the following case. */
5771 else if (ident_len == attr_len + 4)
5773 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5774 if (p[0] == '_' && p[1] == '_'
5775 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5776 && strncmp (attr_name, p + 2, attr_len) == 0)
5777 break;
5779 list = TREE_CHAIN (list);
5782 return list;
5785 /* A variant of lookup_attribute() that can be used with an identifier
5786 as the first argument, and where the identifier can be either
5787 'text' or '__text__'.
5789 Given an attribute ATTR_IDENTIFIER, and a list of attributes LIST,
5790 return a pointer to the attribute's list element if the attribute
5791 is part of the list, or NULL_TREE if not found. If the attribute
5792 appears more than once, this only returns the first occurrence; the
5793 TREE_CHAIN of the return value should be passed back in if further
5794 occurrences are wanted. ATTR_IDENTIFIER must be an identifier but
5795 can be in the form 'text' or '__text__'. */
5796 static tree
5797 lookup_ident_attribute (tree attr_identifier, tree list)
5799 gcc_checking_assert (TREE_CODE (attr_identifier) == IDENTIFIER_NODE);
5801 while (list)
5803 gcc_checking_assert (TREE_CODE (get_attribute_name (list))
5804 == IDENTIFIER_NODE);
5806 /* Identifiers can be compared directly for equality. */
5807 if (attr_identifier == get_attribute_name (list))
5808 break;
5810 /* If they are not equal, they may still be one in the form
5811 'text' while the other one is in the form '__text__'. TODO:
5812 If we were storing attributes in normalized 'text' form, then
5813 this could all go away and we could take full advantage of
5814 the fact that we're comparing identifiers. :-) */
5816 size_t attr_len = IDENTIFIER_LENGTH (attr_identifier);
5817 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5819 if (ident_len == attr_len + 4)
5821 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5822 const char *q = IDENTIFIER_POINTER (attr_identifier);
5823 if (p[0] == '_' && p[1] == '_'
5824 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5825 && strncmp (q, p + 2, attr_len) == 0)
5826 break;
5828 else if (ident_len + 4 == attr_len)
5830 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5831 const char *q = IDENTIFIER_POINTER (attr_identifier);
5832 if (q[0] == '_' && q[1] == '_'
5833 && q[attr_len - 2] == '_' && q[attr_len - 1] == '_'
5834 && strncmp (q + 2, p, ident_len) == 0)
5835 break;
5838 list = TREE_CHAIN (list);
5841 return list;
5844 /* Remove any instances of attribute ATTR_NAME in LIST and return the
5845 modified list. */
5847 tree
5848 remove_attribute (const char *attr_name, tree list)
5850 tree *p;
5851 size_t attr_len = strlen (attr_name);
5853 gcc_checking_assert (attr_name[0] != '_');
5855 for (p = &list; *p; )
5857 tree l = *p;
5858 /* TODO: If we were storing attributes in normalized form, here
5859 we could use a simple strcmp(). */
5860 if (private_is_attribute_p (attr_name, attr_len, get_attribute_name (l)))
5861 *p = TREE_CHAIN (l);
5862 else
5863 p = &TREE_CHAIN (l);
5866 return list;
5869 /* Return an attribute list that is the union of a1 and a2. */
5871 tree
5872 merge_attributes (tree a1, tree a2)
5874 tree attributes;
5876 /* Either one unset? Take the set one. */
5878 if ((attributes = a1) == 0)
5879 attributes = a2;
5881 /* One that completely contains the other? Take it. */
5883 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
5885 if (attribute_list_contained (a2, a1))
5886 attributes = a2;
5887 else
5889 /* Pick the longest list, and hang on the other list. */
5891 if (list_length (a1) < list_length (a2))
5892 attributes = a2, a2 = a1;
5894 for (; a2 != 0; a2 = TREE_CHAIN (a2))
5896 tree a;
5897 for (a = lookup_ident_attribute (get_attribute_name (a2),
5898 attributes);
5899 a != NULL_TREE && !attribute_value_equal (a, a2);
5900 a = lookup_ident_attribute (get_attribute_name (a2),
5901 TREE_CHAIN (a)))
5903 if (a == NULL_TREE)
5905 a1 = copy_node (a2);
5906 TREE_CHAIN (a1) = attributes;
5907 attributes = a1;
5912 return attributes;
5915 /* Given types T1 and T2, merge their attributes and return
5916 the result. */
5918 tree
5919 merge_type_attributes (tree t1, tree t2)
5921 return merge_attributes (TYPE_ATTRIBUTES (t1),
5922 TYPE_ATTRIBUTES (t2));
5925 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
5926 the result. */
5928 tree
5929 merge_decl_attributes (tree olddecl, tree newdecl)
5931 return merge_attributes (DECL_ATTRIBUTES (olddecl),
5932 DECL_ATTRIBUTES (newdecl));
5935 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
5937 /* Specialization of merge_decl_attributes for various Windows targets.
5939 This handles the following situation:
5941 __declspec (dllimport) int foo;
5942 int foo;
5944 The second instance of `foo' nullifies the dllimport. */
5946 tree
5947 merge_dllimport_decl_attributes (tree old, tree new_tree)
5949 tree a;
5950 int delete_dllimport_p = 1;
5952 /* What we need to do here is remove from `old' dllimport if it doesn't
5953 appear in `new'. dllimport behaves like extern: if a declaration is
5954 marked dllimport and a definition appears later, then the object
5955 is not dllimport'd. We also remove a `new' dllimport if the old list
5956 contains dllexport: dllexport always overrides dllimport, regardless
5957 of the order of declaration. */
5958 if (!VAR_OR_FUNCTION_DECL_P (new_tree))
5959 delete_dllimport_p = 0;
5960 else if (DECL_DLLIMPORT_P (new_tree)
5961 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
5963 DECL_DLLIMPORT_P (new_tree) = 0;
5964 warning (OPT_Wattributes, "%q+D already declared with dllexport attribute: "
5965 "dllimport ignored", new_tree);
5967 else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new_tree))
5969 /* Warn about overriding a symbol that has already been used, e.g.:
5970 extern int __attribute__ ((dllimport)) foo;
5971 int* bar () {return &foo;}
5972 int foo;
5974 if (TREE_USED (old))
5976 warning (0, "%q+D redeclared without dllimport attribute "
5977 "after being referenced with dll linkage", new_tree);
5978 /* If we have used a variable's address with dllimport linkage,
5979 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
5980 decl may already have had TREE_CONSTANT computed.
5981 We still remove the attribute so that assembler code refers
5982 to '&foo rather than '_imp__foo'. */
5983 if (TREE_CODE (old) == VAR_DECL && TREE_ADDRESSABLE (old))
5984 DECL_DLLIMPORT_P (new_tree) = 1;
5987 /* Let an inline definition silently override the external reference,
5988 but otherwise warn about attribute inconsistency. */
5989 else if (TREE_CODE (new_tree) == VAR_DECL
5990 || !DECL_DECLARED_INLINE_P (new_tree))
5991 warning (OPT_Wattributes, "%q+D redeclared without dllimport attribute: "
5992 "previous dllimport ignored", new_tree);
5994 else
5995 delete_dllimport_p = 0;
5997 a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new_tree));
5999 if (delete_dllimport_p)
6000 a = remove_attribute ("dllimport", a);
6002 return a;
6005 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
6006 struct attribute_spec.handler. */
6008 tree
6009 handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
6010 bool *no_add_attrs)
6012 tree node = *pnode;
6013 bool is_dllimport;
6015 /* These attributes may apply to structure and union types being created,
6016 but otherwise should pass to the declaration involved. */
6017 if (!DECL_P (node))
6019 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
6020 | (int) ATTR_FLAG_ARRAY_NEXT))
6022 *no_add_attrs = true;
6023 return tree_cons (name, args, NULL_TREE);
6025 if (TREE_CODE (node) == RECORD_TYPE
6026 || TREE_CODE (node) == UNION_TYPE)
6028 node = TYPE_NAME (node);
6029 if (!node)
6030 return NULL_TREE;
6032 else
6034 warning (OPT_Wattributes, "%qE attribute ignored",
6035 name);
6036 *no_add_attrs = true;
6037 return NULL_TREE;
6041 if (TREE_CODE (node) != FUNCTION_DECL
6042 && TREE_CODE (node) != VAR_DECL
6043 && TREE_CODE (node) != TYPE_DECL)
6045 *no_add_attrs = true;
6046 warning (OPT_Wattributes, "%qE attribute ignored",
6047 name);
6048 return NULL_TREE;
6051 if (TREE_CODE (node) == TYPE_DECL
6052 && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE
6053 && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE)
6055 *no_add_attrs = true;
6056 warning (OPT_Wattributes, "%qE attribute ignored",
6057 name);
6058 return NULL_TREE;
6061 is_dllimport = is_attribute_p ("dllimport", name);
6063 /* Report error on dllimport ambiguities seen now before they cause
6064 any damage. */
6065 if (is_dllimport)
6067 /* Honor any target-specific overrides. */
6068 if (!targetm.valid_dllimport_attribute_p (node))
6069 *no_add_attrs = true;
6071 else if (TREE_CODE (node) == FUNCTION_DECL
6072 && DECL_DECLARED_INLINE_P (node))
6074 warning (OPT_Wattributes, "inline function %q+D declared as "
6075 " dllimport: attribute ignored", node);
6076 *no_add_attrs = true;
6078 /* Like MS, treat definition of dllimported variables and
6079 non-inlined functions on declaration as syntax errors. */
6080 else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
6082 error ("function %q+D definition is marked dllimport", node);
6083 *no_add_attrs = true;
6086 else if (TREE_CODE (node) == VAR_DECL)
6088 if (DECL_INITIAL (node))
6090 error ("variable %q+D definition is marked dllimport",
6091 node);
6092 *no_add_attrs = true;
6095 /* `extern' needn't be specified with dllimport.
6096 Specify `extern' now and hope for the best. Sigh. */
6097 DECL_EXTERNAL (node) = 1;
6098 /* Also, implicitly give dllimport'd variables declared within
6099 a function global scope, unless declared static. */
6100 if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
6101 TREE_PUBLIC (node) = 1;
6104 if (*no_add_attrs == false)
6105 DECL_DLLIMPORT_P (node) = 1;
6107 else if (TREE_CODE (node) == FUNCTION_DECL
6108 && DECL_DECLARED_INLINE_P (node)
6109 && flag_keep_inline_dllexport)
6110 /* An exported function, even if inline, must be emitted. */
6111 DECL_EXTERNAL (node) = 0;
6113 /* Report error if symbol is not accessible at global scope. */
6114 if (!TREE_PUBLIC (node)
6115 && (TREE_CODE (node) == VAR_DECL
6116 || TREE_CODE (node) == FUNCTION_DECL))
6118 error ("external linkage required for symbol %q+D because of "
6119 "%qE attribute", node, name);
6120 *no_add_attrs = true;
6123 /* A dllexport'd entity must have default visibility so that other
6124 program units (shared libraries or the main executable) can see
6125 it. A dllimport'd entity must have default visibility so that
6126 the linker knows that undefined references within this program
6127 unit can be resolved by the dynamic linker. */
6128 if (!*no_add_attrs)
6130 if (DECL_VISIBILITY_SPECIFIED (node)
6131 && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT)
6132 error ("%qE implies default visibility, but %qD has already "
6133 "been declared with a different visibility",
6134 name, node);
6135 DECL_VISIBILITY (node) = VISIBILITY_DEFAULT;
6136 DECL_VISIBILITY_SPECIFIED (node) = 1;
6139 return NULL_TREE;
6142 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
6144 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
6145 of the various TYPE_QUAL values. Also, set the UPC layout qualifier,
6146 which is either null or a reference to an integral constant. */
6148 static void
6149 set_type_quals (tree type, int type_quals, tree layout_qualifier)
6151 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
6152 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
6153 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
6154 TYPE_ATOMIC (type) = (type_quals & TYPE_QUAL_ATOMIC) != 0;
6155 TYPE_ADDR_SPACE (type) = DECODE_QUAL_ADDR_SPACE (type_quals);
6156 TYPE_UPC_SHARED (type) = (type_quals & TYPE_QUAL_UPC_SHARED) != 0;
6157 TYPE_UPC_STRICT (type) = (type_quals & TYPE_QUAL_UPC_STRICT) != 0;
6158 TYPE_UPC_RELAXED (type) = (type_quals & TYPE_QUAL_UPC_RELAXED) != 0;
6159 if (TYPE_UPC_SHARED (type))
6160 SET_TYPE_UPC_BLOCK_FACTOR (type, layout_qualifier);
6163 /* Returns true iff CAND is equivalent to BASE with
6164 TYPE_QUALS and LAYOUT_QUALIFIER. */
6166 bool
6167 check_qualified_type (const_tree cand, const_tree base,
6168 int type_quals, tree layout_qualifier)
6170 return (TYPE_QUALS (cand) == type_quals
6171 && TYPE_UPC_BLOCK_FACTOR (cand) == layout_qualifier
6172 && TYPE_NAME (cand) == TYPE_NAME (base)
6173 /* Apparently this is needed for Objective-C. */
6174 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
6175 /* Check alignment. */
6176 && TYPE_ALIGN (cand) == TYPE_ALIGN (base)
6177 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
6178 TYPE_ATTRIBUTES (base)));
6181 /* Returns true iff CAND is equivalent to BASE with ALIGN. */
6183 static bool
6184 check_aligned_type (const_tree cand, const_tree base, unsigned int align)
6186 return (TYPE_QUALS (cand) == TYPE_QUALS (base)
6187 && TYPE_UPC_BLOCK_FACTOR (cand) == TYPE_UPC_BLOCK_FACTOR (base)
6188 && TYPE_NAME (cand) == TYPE_NAME (base)
6189 /* Apparently this is needed for Objective-C. */
6190 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
6191 /* Check alignment. */
6192 && TYPE_ALIGN (cand) == align
6193 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
6194 TYPE_ATTRIBUTES (base)));
6197 /* This function checks to see if TYPE matches the size one of the built-in
6198 atomic types, and returns that core atomic type. */
6200 static tree
6201 find_atomic_core_type (tree type)
6203 tree base_atomic_type;
6205 /* Only handle complete types. */
6206 if (TYPE_SIZE (type) == NULL_TREE)
6207 return NULL_TREE;
6209 HOST_WIDE_INT type_size = tree_to_uhwi (TYPE_SIZE (type));
6210 switch (type_size)
6212 case 8:
6213 base_atomic_type = atomicQI_type_node;
6214 break;
6216 case 16:
6217 base_atomic_type = atomicHI_type_node;
6218 break;
6220 case 32:
6221 base_atomic_type = atomicSI_type_node;
6222 break;
6224 case 64:
6225 base_atomic_type = atomicDI_type_node;
6226 break;
6228 case 128:
6229 base_atomic_type = atomicTI_type_node;
6230 break;
6232 default:
6233 base_atomic_type = NULL_TREE;
6236 return base_atomic_type;
6239 /* Return a version of the TYPE, qualified as indicated by the
6240 TYPE_QUALS, if one exists. If no qualified version exists yet,
6241 return NULL_TREE. */
6243 tree
6244 get_qualified_type_1 (tree type, int type_quals, tree layout_qualifier)
6246 tree t;
6248 if (TYPE_QUALS (type) == type_quals)
6249 return type;
6251 /* Search the chain of variants to see if there is already one there just
6252 like the one we need to have. If so, use that existing one. We must
6253 preserve the TYPE_NAME, since there is code that depends on this. */
6254 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6255 if (check_qualified_type (t, type, type_quals, layout_qualifier))
6256 return t;
6258 return NULL_TREE;
6261 /* Like get_qualified_type_1, but creates the type if it does not
6262 exist. This function never returns NULL_TREE. */
6264 tree
6265 build_qualified_type_1 (tree type, int type_quals, tree layout_qualifier)
6267 tree t;
6269 /* See if we already have the appropriate qualified variant. */
6270 t = get_qualified_type_1 (type, type_quals, layout_qualifier);
6272 /* If not, build it. */
6273 if (!t)
6275 t = build_variant_type_copy (type);
6276 set_type_quals (t, type_quals, layout_qualifier);
6278 if (((type_quals & TYPE_QUAL_ATOMIC) == TYPE_QUAL_ATOMIC))
6280 /* See if this object can map to a basic atomic type. */
6281 tree atomic_type = find_atomic_core_type (type);
6282 if (atomic_type)
6284 /* Ensure the alignment of this type is compatible with
6285 the required alignment of the atomic type. */
6286 if (TYPE_ALIGN (atomic_type) > TYPE_ALIGN (t))
6287 TYPE_ALIGN (t) = TYPE_ALIGN (atomic_type);
6291 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6292 /* Propagate structural equality. */
6293 SET_TYPE_STRUCTURAL_EQUALITY (t);
6294 else if (TYPE_CANONICAL (type) != type)
6295 /* Build the underlying canonical type, since it is different
6296 from TYPE. */
6297 TYPE_CANONICAL (t) = build_qualified_type (TYPE_CANONICAL (type),
6298 type_quals);
6299 else
6300 /* T is its own canonical type. */
6301 TYPE_CANONICAL (t) = t;
6305 return t;
6308 /* Create a variant of type T with alignment ALIGN. */
6310 tree
6311 build_aligned_type (tree type, unsigned int align)
6313 tree t;
6315 if (TYPE_PACKED (type)
6316 || TYPE_ALIGN (type) == align)
6317 return type;
6319 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6320 if (check_aligned_type (t, type, align))
6321 return t;
6323 t = build_variant_type_copy (type);
6324 TYPE_ALIGN (t) = align;
6326 return t;
6329 /* Create a new distinct copy of TYPE. The new type is made its own
6330 MAIN_VARIANT. If TYPE requires structural equality checks, the
6331 resulting type requires structural equality checks; otherwise, its
6332 TYPE_CANONICAL points to itself. */
6334 tree
6335 build_distinct_type_copy (tree type)
6337 tree t = copy_node (type);
6339 TYPE_POINTER_TO (t) = 0;
6340 TYPE_REFERENCE_TO (t) = 0;
6342 /* Set the canonical type either to a new equivalence class, or
6343 propagate the need for structural equality checks. */
6344 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6345 SET_TYPE_STRUCTURAL_EQUALITY (t);
6346 else
6347 TYPE_CANONICAL (t) = t;
6349 /* Make it its own variant. */
6350 TYPE_MAIN_VARIANT (t) = t;
6351 TYPE_NEXT_VARIANT (t) = 0;
6353 /* Note that it is now possible for TYPE_MIN_VALUE to be a value
6354 whose TREE_TYPE is not t. This can also happen in the Ada
6355 frontend when using subtypes. */
6357 return t;
6360 /* Create a new variant of TYPE, equivalent but distinct. This is so
6361 the caller can modify it. TYPE_CANONICAL for the return type will
6362 be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
6363 are considered equal by the language itself (or that both types
6364 require structural equality checks). */
6366 tree
6367 build_variant_type_copy (tree type)
6369 tree t, m = TYPE_MAIN_VARIANT (type);
6371 t = build_distinct_type_copy (type);
6373 /* Since we're building a variant, assume that it is a non-semantic
6374 variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
6375 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
6377 /* Add the new type to the chain of variants of TYPE. */
6378 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
6379 TYPE_NEXT_VARIANT (m) = t;
6380 TYPE_MAIN_VARIANT (t) = m;
6382 return t;
6385 /* Return true if the from tree in both tree maps are equal. */
6388 tree_map_base_eq (const void *va, const void *vb)
6390 const struct tree_map_base *const a = (const struct tree_map_base *) va,
6391 *const b = (const struct tree_map_base *) vb;
6392 return (a->from == b->from);
6395 /* Hash a from tree in a tree_base_map. */
6397 unsigned int
6398 tree_map_base_hash (const void *item)
6400 return htab_hash_pointer (((const struct tree_map_base *)item)->from);
6403 /* Return true if this tree map structure is marked for garbage collection
6404 purposes. We simply return true if the from tree is marked, so that this
6405 structure goes away when the from tree goes away. */
6408 tree_map_base_marked_p (const void *p)
6410 return ggc_marked_p (((const struct tree_map_base *) p)->from);
6413 /* Hash a from tree in a tree_map. */
6415 unsigned int
6416 tree_map_hash (const void *item)
6418 return (((const struct tree_map *) item)->hash);
6421 /* Hash a from tree in a tree_decl_map. */
6423 unsigned int
6424 tree_decl_map_hash (const void *item)
6426 return DECL_UID (((const struct tree_decl_map *) item)->base.from);
6429 /* Return the initialization priority for DECL. */
6431 priority_type
6432 decl_init_priority_lookup (tree decl)
6434 struct tree_priority_map *h;
6435 struct tree_map_base in;
6437 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
6438 in.from = decl;
6439 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
6440 return h ? h->init : DEFAULT_INIT_PRIORITY;
6443 /* Return the finalization priority for DECL. */
6445 priority_type
6446 decl_fini_priority_lookup (tree decl)
6448 struct tree_priority_map *h;
6449 struct tree_map_base in;
6451 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
6452 in.from = decl;
6453 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
6454 return h ? h->fini : DEFAULT_INIT_PRIORITY;
6457 /* Return the initialization and finalization priority information for
6458 DECL. If there is no previous priority information, a freshly
6459 allocated structure is returned. */
6461 static struct tree_priority_map *
6462 decl_priority_info (tree decl)
6464 struct tree_priority_map in;
6465 struct tree_priority_map *h;
6466 void **loc;
6468 in.base.from = decl;
6469 loc = htab_find_slot (init_priority_for_decl, &in, INSERT);
6470 h = (struct tree_priority_map *) *loc;
6471 if (!h)
6473 h = ggc_cleared_alloc<tree_priority_map> ();
6474 *loc = h;
6475 h->base.from = decl;
6476 h->init = DEFAULT_INIT_PRIORITY;
6477 h->fini = DEFAULT_INIT_PRIORITY;
6480 return h;
6483 /* Set the initialization priority for DECL to PRIORITY. */
6485 void
6486 decl_init_priority_insert (tree decl, priority_type priority)
6488 struct tree_priority_map *h;
6490 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
6491 if (priority == DEFAULT_INIT_PRIORITY)
6492 return;
6493 h = decl_priority_info (decl);
6494 h->init = priority;
6497 /* Set the finalization priority for DECL to PRIORITY. */
6499 void
6500 decl_fini_priority_insert (tree decl, priority_type priority)
6502 struct tree_priority_map *h;
6504 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
6505 if (priority == DEFAULT_INIT_PRIORITY)
6506 return;
6507 h = decl_priority_info (decl);
6508 h->fini = priority;
6511 /* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
6513 static void
6514 print_debug_expr_statistics (void)
6516 fprintf (stderr, "DECL_DEBUG_EXPR hash: size %ld, %ld elements, %f collisions\n",
6517 (long) htab_size (debug_expr_for_decl),
6518 (long) htab_elements (debug_expr_for_decl),
6519 htab_collisions (debug_expr_for_decl));
6522 /* Print out the statistics for the DECL_VALUE_EXPR hash table. */
6524 static void
6525 print_value_expr_statistics (void)
6527 fprintf (stderr, "DECL_VALUE_EXPR hash: size %ld, %ld elements, %f collisions\n",
6528 (long) htab_size (value_expr_for_decl),
6529 (long) htab_elements (value_expr_for_decl),
6530 htab_collisions (value_expr_for_decl));
6533 /* Lookup a debug expression for FROM, and return it if we find one. */
6535 tree
6536 decl_debug_expr_lookup (tree from)
6538 struct tree_decl_map *h, in;
6539 in.base.from = from;
6541 h = (struct tree_decl_map *)
6542 htab_find_with_hash (debug_expr_for_decl, &in, DECL_UID (from));
6543 if (h)
6544 return h->to;
6545 return NULL_TREE;
6548 /* Insert a mapping FROM->TO in the debug expression hashtable. */
6550 void
6551 decl_debug_expr_insert (tree from, tree to)
6553 struct tree_decl_map *h;
6554 void **loc;
6556 h = ggc_alloc<tree_decl_map> ();
6557 h->base.from = from;
6558 h->to = to;
6559 loc = htab_find_slot_with_hash (debug_expr_for_decl, h, DECL_UID (from),
6560 INSERT);
6561 *(struct tree_decl_map **) loc = h;
6564 /* Lookup a value expression for FROM, and return it if we find one. */
6566 tree
6567 decl_value_expr_lookup (tree from)
6569 struct tree_decl_map *h, in;
6570 in.base.from = from;
6572 h = (struct tree_decl_map *)
6573 htab_find_with_hash (value_expr_for_decl, &in, DECL_UID (from));
6574 if (h)
6575 return h->to;
6576 return NULL_TREE;
6579 /* Insert a mapping FROM->TO in the value expression hashtable. */
6581 void
6582 decl_value_expr_insert (tree from, tree to)
6584 struct tree_decl_map *h;
6585 void **loc;
6587 h = ggc_alloc<tree_decl_map> ();
6588 h->base.from = from;
6589 h->to = to;
6590 loc = htab_find_slot_with_hash (value_expr_for_decl, h, DECL_UID (from),
6591 INSERT);
6592 *(struct tree_decl_map **) loc = h;
6595 /* Lookup a vector of debug arguments for FROM, and return it if we
6596 find one. */
6598 vec<tree, va_gc> **
6599 decl_debug_args_lookup (tree from)
6601 struct tree_vec_map *h, in;
6603 if (!DECL_HAS_DEBUG_ARGS_P (from))
6604 return NULL;
6605 gcc_checking_assert (debug_args_for_decl != NULL);
6606 in.base.from = from;
6607 h = (struct tree_vec_map *)
6608 htab_find_with_hash (debug_args_for_decl, &in, DECL_UID (from));
6609 if (h)
6610 return &h->to;
6611 return NULL;
6614 /* Insert a mapping FROM->empty vector of debug arguments in the value
6615 expression hashtable. */
6617 vec<tree, va_gc> **
6618 decl_debug_args_insert (tree from)
6620 struct tree_vec_map *h;
6621 void **loc;
6623 if (DECL_HAS_DEBUG_ARGS_P (from))
6624 return decl_debug_args_lookup (from);
6625 if (debug_args_for_decl == NULL)
6626 debug_args_for_decl = htab_create_ggc (64, tree_vec_map_hash,
6627 tree_vec_map_eq, 0);
6628 h = ggc_alloc<tree_vec_map> ();
6629 h->base.from = from;
6630 h->to = NULL;
6631 loc = htab_find_slot_with_hash (debug_args_for_decl, h, DECL_UID (from),
6632 INSERT);
6633 *(struct tree_vec_map **) loc = h;
6634 DECL_HAS_DEBUG_ARGS_P (from) = 1;
6635 return &h->to;
6638 /* Hashing of types so that we don't make duplicates.
6639 The entry point is `type_hash_canon'. */
6641 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
6642 with types in the TREE_VALUE slots), by adding the hash codes
6643 of the individual types. */
6645 static unsigned int
6646 type_hash_list (const_tree list, hashval_t hashcode)
6648 const_tree tail;
6650 for (tail = list; tail; tail = TREE_CHAIN (tail))
6651 if (TREE_VALUE (tail) != error_mark_node)
6652 hashcode = iterative_hash_object (TYPE_HASH (TREE_VALUE (tail)),
6653 hashcode);
6655 return hashcode;
6658 /* These are the Hashtable callback functions. */
6660 /* Returns true iff the types are equivalent. */
6662 static int
6663 type_hash_eq (const void *va, const void *vb)
6665 const struct type_hash *const a = (const struct type_hash *) va,
6666 *const b = (const struct type_hash *) vb;
6668 /* First test the things that are the same for all types. */
6669 if (a->hash != b->hash
6670 || TREE_CODE (a->type) != TREE_CODE (b->type)
6671 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
6672 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
6673 TYPE_ATTRIBUTES (b->type))
6674 || (TREE_CODE (a->type) != COMPLEX_TYPE
6675 && TYPE_NAME (a->type) != TYPE_NAME (b->type)))
6676 return 0;
6678 /* Be careful about comparing arrays before and after the element type
6679 has been completed; don't compare TYPE_ALIGN unless both types are
6680 complete. */
6681 if (COMPLETE_TYPE_P (a->type) && COMPLETE_TYPE_P (b->type)
6682 && (TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
6683 || TYPE_MODE (a->type) != TYPE_MODE (b->type)))
6684 return 0;
6686 switch (TREE_CODE (a->type))
6688 case VOID_TYPE:
6689 case COMPLEX_TYPE:
6690 case POINTER_TYPE:
6691 case REFERENCE_TYPE:
6692 case NULLPTR_TYPE:
6693 return 1;
6695 case VECTOR_TYPE:
6696 return TYPE_VECTOR_SUBPARTS (a->type) == TYPE_VECTOR_SUBPARTS (b->type);
6698 case ENUMERAL_TYPE:
6699 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
6700 && !(TYPE_VALUES (a->type)
6701 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
6702 && TYPE_VALUES (b->type)
6703 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
6704 && type_list_equal (TYPE_VALUES (a->type),
6705 TYPE_VALUES (b->type))))
6706 return 0;
6708 /* ... fall through ... */
6710 case INTEGER_TYPE:
6711 case REAL_TYPE:
6712 case BOOLEAN_TYPE:
6713 if (TYPE_PRECISION (a->type) != TYPE_PRECISION (b->type))
6714 return false;
6715 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
6716 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
6717 TYPE_MAX_VALUE (b->type)))
6718 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
6719 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
6720 TYPE_MIN_VALUE (b->type))));
6722 case FIXED_POINT_TYPE:
6723 return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
6725 case OFFSET_TYPE:
6726 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
6728 case METHOD_TYPE:
6729 if (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
6730 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6731 || (TYPE_ARG_TYPES (a->type)
6732 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6733 && TYPE_ARG_TYPES (b->type)
6734 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6735 && type_list_equal (TYPE_ARG_TYPES (a->type),
6736 TYPE_ARG_TYPES (b->type)))))
6737 break;
6738 return 0;
6739 case ARRAY_TYPE:
6740 return TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type);
6742 case RECORD_TYPE:
6743 case UNION_TYPE:
6744 case QUAL_UNION_TYPE:
6745 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
6746 || (TYPE_FIELDS (a->type)
6747 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
6748 && TYPE_FIELDS (b->type)
6749 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
6750 && type_list_equal (TYPE_FIELDS (a->type),
6751 TYPE_FIELDS (b->type))));
6753 case FUNCTION_TYPE:
6754 if (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6755 || (TYPE_ARG_TYPES (a->type)
6756 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6757 && TYPE_ARG_TYPES (b->type)
6758 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6759 && type_list_equal (TYPE_ARG_TYPES (a->type),
6760 TYPE_ARG_TYPES (b->type))))
6761 break;
6762 return 0;
6764 default:
6765 return 0;
6768 if (lang_hooks.types.type_hash_eq != NULL)
6769 return lang_hooks.types.type_hash_eq (a->type, b->type);
6771 return 1;
6774 /* Return the cached hash value. */
6776 static hashval_t
6777 type_hash_hash (const void *item)
6779 return ((const struct type_hash *) item)->hash;
6782 /* Look in the type hash table for a type isomorphic to TYPE.
6783 If one is found, return it. Otherwise return 0. */
6785 static tree
6786 type_hash_lookup (hashval_t hashcode, tree type)
6788 struct type_hash *h, in;
6790 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
6791 must call that routine before comparing TYPE_ALIGNs. */
6792 layout_type (type);
6794 in.hash = hashcode;
6795 in.type = type;
6797 h = (struct type_hash *) htab_find_with_hash (type_hash_table, &in,
6798 hashcode);
6799 if (h)
6800 return h->type;
6801 return NULL_TREE;
6804 /* Add an entry to the type-hash-table
6805 for a type TYPE whose hash code is HASHCODE. */
6807 static void
6808 type_hash_add (hashval_t hashcode, tree type)
6810 struct type_hash *h;
6811 void **loc;
6813 h = ggc_alloc<type_hash> ();
6814 h->hash = hashcode;
6815 h->type = type;
6816 loc = htab_find_slot_with_hash (type_hash_table, h, hashcode, INSERT);
6817 *loc = (void *)h;
6820 /* Given TYPE, and HASHCODE its hash code, return the canonical
6821 object for an identical type if one already exists.
6822 Otherwise, return TYPE, and record it as the canonical object.
6824 To use this function, first create a type of the sort you want.
6825 Then compute its hash code from the fields of the type that
6826 make it different from other similar types.
6827 Then call this function and use the value. */
6829 tree
6830 type_hash_canon (unsigned int hashcode, tree type)
6832 tree t1;
6834 /* The hash table only contains main variants, so ensure that's what we're
6835 being passed. */
6836 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
6838 /* See if the type is in the hash table already. If so, return it.
6839 Otherwise, add the type. */
6840 t1 = type_hash_lookup (hashcode, type);
6841 if (t1 != 0)
6843 if (GATHER_STATISTICS)
6845 tree_code_counts[(int) TREE_CODE (type)]--;
6846 tree_node_counts[(int) t_kind]--;
6847 tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type_non_common);
6849 return t1;
6851 else
6853 type_hash_add (hashcode, type);
6854 return type;
6858 /* See if the data pointed to by the type hash table is marked. We consider
6859 it marked if the type is marked or if a debug type number or symbol
6860 table entry has been made for the type. */
6862 static int
6863 type_hash_marked_p (const void *p)
6865 const_tree const type = ((const struct type_hash *) p)->type;
6867 return ggc_marked_p (type);
6870 static void
6871 print_type_hash_statistics (void)
6873 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
6874 (long) htab_size (type_hash_table),
6875 (long) htab_elements (type_hash_table),
6876 htab_collisions (type_hash_table));
6879 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
6880 with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
6881 by adding the hash codes of the individual attributes. */
6883 static unsigned int
6884 attribute_hash_list (const_tree list, hashval_t hashcode)
6886 const_tree tail;
6888 for (tail = list; tail; tail = TREE_CHAIN (tail))
6889 /* ??? Do we want to add in TREE_VALUE too? */
6890 hashcode = iterative_hash_object
6891 (IDENTIFIER_HASH_VALUE (get_attribute_name (tail)), hashcode);
6892 return hashcode;
6895 /* Given two lists of attributes, return true if list l2 is
6896 equivalent to l1. */
6899 attribute_list_equal (const_tree l1, const_tree l2)
6901 if (l1 == l2)
6902 return 1;
6904 return attribute_list_contained (l1, l2)
6905 && attribute_list_contained (l2, l1);
6908 /* Given two lists of attributes, return true if list L2 is
6909 completely contained within L1. */
6910 /* ??? This would be faster if attribute names were stored in a canonicalized
6911 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
6912 must be used to show these elements are equivalent (which they are). */
6913 /* ??? It's not clear that attributes with arguments will always be handled
6914 correctly. */
6917 attribute_list_contained (const_tree l1, const_tree l2)
6919 const_tree t1, t2;
6921 /* First check the obvious, maybe the lists are identical. */
6922 if (l1 == l2)
6923 return 1;
6925 /* Maybe the lists are similar. */
6926 for (t1 = l1, t2 = l2;
6927 t1 != 0 && t2 != 0
6928 && get_attribute_name (t1) == get_attribute_name (t2)
6929 && TREE_VALUE (t1) == TREE_VALUE (t2);
6930 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6933 /* Maybe the lists are equal. */
6934 if (t1 == 0 && t2 == 0)
6935 return 1;
6937 for (; t2 != 0; t2 = TREE_CHAIN (t2))
6939 const_tree attr;
6940 /* This CONST_CAST is okay because lookup_attribute does not
6941 modify its argument and the return value is assigned to a
6942 const_tree. */
6943 for (attr = lookup_ident_attribute (get_attribute_name (t2),
6944 CONST_CAST_TREE (l1));
6945 attr != NULL_TREE && !attribute_value_equal (t2, attr);
6946 attr = lookup_ident_attribute (get_attribute_name (t2),
6947 TREE_CHAIN (attr)))
6950 if (attr == NULL_TREE)
6951 return 0;
6954 return 1;
6957 /* Given two lists of types
6958 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
6959 return 1 if the lists contain the same types in the same order.
6960 Also, the TREE_PURPOSEs must match. */
6963 type_list_equal (const_tree l1, const_tree l2)
6965 const_tree t1, t2;
6967 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6968 if (TREE_VALUE (t1) != TREE_VALUE (t2)
6969 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
6970 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
6971 && (TREE_TYPE (TREE_PURPOSE (t1))
6972 == TREE_TYPE (TREE_PURPOSE (t2))))))
6973 return 0;
6975 return t1 == t2;
6978 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
6979 given by TYPE. If the argument list accepts variable arguments,
6980 then this function counts only the ordinary arguments. */
6983 type_num_arguments (const_tree type)
6985 int i = 0;
6986 tree t;
6988 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
6989 /* If the function does not take a variable number of arguments,
6990 the last element in the list will have type `void'. */
6991 if (VOID_TYPE_P (TREE_VALUE (t)))
6992 break;
6993 else
6994 ++i;
6996 return i;
6999 /* Nonzero if integer constants T1 and T2
7000 represent the same constant value. */
7003 tree_int_cst_equal (const_tree t1, const_tree t2)
7005 if (t1 == t2)
7006 return 1;
7008 if (t1 == 0 || t2 == 0)
7009 return 0;
7011 if (TREE_CODE (t1) == INTEGER_CST
7012 && TREE_CODE (t2) == INTEGER_CST
7013 && wi::to_widest (t1) == wi::to_widest (t2))
7014 return 1;
7016 return 0;
7019 /* Return true if T is an INTEGER_CST whose numerical value (extended
7020 according to TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. */
7022 bool
7023 tree_fits_shwi_p (const_tree t)
7025 return (t != NULL_TREE
7026 && TREE_CODE (t) == INTEGER_CST
7027 && wi::fits_shwi_p (wi::to_widest (t)));
7030 /* Return true if T is an INTEGER_CST whose numerical value (extended
7031 according to TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. */
7033 bool
7034 tree_fits_uhwi_p (const_tree t)
7036 return (t != NULL_TREE
7037 && TREE_CODE (t) == INTEGER_CST
7038 && wi::fits_uhwi_p (wi::to_widest (t)));
7041 /* T is an INTEGER_CST whose numerical value (extended according to
7042 TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. Return that
7043 HOST_WIDE_INT. */
7045 HOST_WIDE_INT
7046 tree_to_shwi (const_tree t)
7048 gcc_assert (tree_fits_shwi_p (t));
7049 return TREE_INT_CST_LOW (t);
7052 /* T is an INTEGER_CST whose numerical value (extended according to
7053 TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. Return that
7054 HOST_WIDE_INT. */
7056 unsigned HOST_WIDE_INT
7057 tree_to_uhwi (const_tree t)
7059 gcc_assert (tree_fits_uhwi_p (t));
7060 return TREE_INT_CST_LOW (t);
7063 /* Return the most significant (sign) bit of T. */
7066 tree_int_cst_sign_bit (const_tree t)
7068 unsigned bitno = TYPE_PRECISION (TREE_TYPE (t)) - 1;
7070 return wi::extract_uhwi (t, bitno, 1);
7073 /* Return an indication of the sign of the integer constant T.
7074 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
7075 Note that -1 will never be returned if T's type is unsigned. */
7078 tree_int_cst_sgn (const_tree t)
7080 if (wi::eq_p (t, 0))
7081 return 0;
7082 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
7083 return 1;
7084 else if (wi::neg_p (t))
7085 return -1;
7086 else
7087 return 1;
7090 /* Return the minimum number of bits needed to represent VALUE in a
7091 signed or unsigned type, UNSIGNEDP says which. */
7093 unsigned int
7094 tree_int_cst_min_precision (tree value, signop sgn)
7096 /* If the value is negative, compute its negative minus 1. The latter
7097 adjustment is because the absolute value of the largest negative value
7098 is one larger than the largest positive value. This is equivalent to
7099 a bit-wise negation, so use that operation instead. */
7101 if (tree_int_cst_sgn (value) < 0)
7102 value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
7104 /* Return the number of bits needed, taking into account the fact
7105 that we need one more bit for a signed than unsigned type.
7106 If value is 0 or -1, the minimum precision is 1 no matter
7107 whether unsignedp is true or false. */
7109 if (integer_zerop (value))
7110 return 1;
7111 else
7112 return tree_floor_log2 (value) + 1 + (sgn == SIGNED ? 1 : 0) ;
7115 /* Return truthvalue of whether T1 is the same tree structure as T2.
7116 Return 1 if they are the same.
7117 Return 0 if they are understandably different.
7118 Return -1 if either contains tree structure not understood by
7119 this function. */
7122 simple_cst_equal (const_tree t1, const_tree t2)
7124 enum tree_code code1, code2;
7125 int cmp;
7126 int i;
7128 if (t1 == t2)
7129 return 1;
7130 if (t1 == 0 || t2 == 0)
7131 return 0;
7133 code1 = TREE_CODE (t1);
7134 code2 = TREE_CODE (t2);
7136 if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
7138 if (CONVERT_EXPR_CODE_P (code2)
7139 || code2 == NON_LVALUE_EXPR)
7140 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7141 else
7142 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
7145 else if (CONVERT_EXPR_CODE_P (code2)
7146 || code2 == NON_LVALUE_EXPR)
7147 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
7149 if (code1 != code2)
7150 return 0;
7152 switch (code1)
7154 case INTEGER_CST:
7155 return wi::to_widest (t1) == wi::to_widest (t2);
7157 case REAL_CST:
7158 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
7160 case FIXED_CST:
7161 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
7163 case STRING_CST:
7164 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
7165 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
7166 TREE_STRING_LENGTH (t1)));
7168 case CONSTRUCTOR:
7170 unsigned HOST_WIDE_INT idx;
7171 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (t1);
7172 vec<constructor_elt, va_gc> *v2 = CONSTRUCTOR_ELTS (t2);
7174 if (vec_safe_length (v1) != vec_safe_length (v2))
7175 return false;
7177 for (idx = 0; idx < vec_safe_length (v1); ++idx)
7178 /* ??? Should we handle also fields here? */
7179 if (!simple_cst_equal ((*v1)[idx].value, (*v2)[idx].value))
7180 return false;
7181 return true;
7184 case SAVE_EXPR:
7185 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7187 case CALL_EXPR:
7188 cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
7189 if (cmp <= 0)
7190 return cmp;
7191 if (call_expr_nargs (t1) != call_expr_nargs (t2))
7192 return 0;
7194 const_tree arg1, arg2;
7195 const_call_expr_arg_iterator iter1, iter2;
7196 for (arg1 = first_const_call_expr_arg (t1, &iter1),
7197 arg2 = first_const_call_expr_arg (t2, &iter2);
7198 arg1 && arg2;
7199 arg1 = next_const_call_expr_arg (&iter1),
7200 arg2 = next_const_call_expr_arg (&iter2))
7202 cmp = simple_cst_equal (arg1, arg2);
7203 if (cmp <= 0)
7204 return cmp;
7206 return arg1 == arg2;
7209 case TARGET_EXPR:
7210 /* Special case: if either target is an unallocated VAR_DECL,
7211 it means that it's going to be unified with whatever the
7212 TARGET_EXPR is really supposed to initialize, so treat it
7213 as being equivalent to anything. */
7214 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
7215 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
7216 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
7217 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
7218 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
7219 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
7220 cmp = 1;
7221 else
7222 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7224 if (cmp <= 0)
7225 return cmp;
7227 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
7229 case WITH_CLEANUP_EXPR:
7230 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7231 if (cmp <= 0)
7232 return cmp;
7234 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
7236 case COMPONENT_REF:
7237 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
7238 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7240 return 0;
7242 case VAR_DECL:
7243 case PARM_DECL:
7244 case CONST_DECL:
7245 case FUNCTION_DECL:
7246 return 0;
7248 default:
7249 break;
7252 /* This general rule works for most tree codes. All exceptions should be
7253 handled above. If this is a language-specific tree code, we can't
7254 trust what might be in the operand, so say we don't know
7255 the situation. */
7256 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
7257 return -1;
7259 switch (TREE_CODE_CLASS (code1))
7261 case tcc_unary:
7262 case tcc_binary:
7263 case tcc_comparison:
7264 case tcc_expression:
7265 case tcc_reference:
7266 case tcc_statement:
7267 cmp = 1;
7268 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
7270 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
7271 if (cmp <= 0)
7272 return cmp;
7275 return cmp;
7277 default:
7278 return -1;
7282 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
7283 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
7284 than U, respectively. */
7287 compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
7289 if (tree_int_cst_sgn (t) < 0)
7290 return -1;
7291 else if (!tree_fits_uhwi_p (t))
7292 return 1;
7293 else if (TREE_INT_CST_LOW (t) == u)
7294 return 0;
7295 else if (TREE_INT_CST_LOW (t) < u)
7296 return -1;
7297 else
7298 return 1;
7301 /* Return true if SIZE represents a constant size that is in bounds of
7302 what the middle-end and the backend accepts (covering not more than
7303 half of the address-space). */
7305 bool
7306 valid_constant_size_p (const_tree size)
7308 if (! tree_fits_uhwi_p (size)
7309 || TREE_OVERFLOW (size)
7310 || tree_int_cst_sign_bit (size) != 0)
7311 return false;
7312 return true;
7315 /* Return the precision of the type, or for a complex or vector type the
7316 precision of the type of its elements. */
7318 unsigned int
7319 element_precision (const_tree type)
7321 enum tree_code code = TREE_CODE (type);
7322 if (code == COMPLEX_TYPE || code == VECTOR_TYPE)
7323 type = TREE_TYPE (type);
7325 return TYPE_PRECISION (type);
7328 /* Return true if CODE represents an associative tree code. Otherwise
7329 return false. */
7330 bool
7331 associative_tree_code (enum tree_code code)
7333 switch (code)
7335 case BIT_IOR_EXPR:
7336 case BIT_AND_EXPR:
7337 case BIT_XOR_EXPR:
7338 case PLUS_EXPR:
7339 case MULT_EXPR:
7340 case MIN_EXPR:
7341 case MAX_EXPR:
7342 return true;
7344 default:
7345 break;
7347 return false;
7350 /* Return true if CODE represents a commutative tree code. Otherwise
7351 return false. */
7352 bool
7353 commutative_tree_code (enum tree_code code)
7355 switch (code)
7357 case PLUS_EXPR:
7358 case MULT_EXPR:
7359 case MULT_HIGHPART_EXPR:
7360 case MIN_EXPR:
7361 case MAX_EXPR:
7362 case BIT_IOR_EXPR:
7363 case BIT_XOR_EXPR:
7364 case BIT_AND_EXPR:
7365 case NE_EXPR:
7366 case EQ_EXPR:
7367 case UNORDERED_EXPR:
7368 case ORDERED_EXPR:
7369 case UNEQ_EXPR:
7370 case LTGT_EXPR:
7371 case TRUTH_AND_EXPR:
7372 case TRUTH_XOR_EXPR:
7373 case TRUTH_OR_EXPR:
7374 case WIDEN_MULT_EXPR:
7375 case VEC_WIDEN_MULT_HI_EXPR:
7376 case VEC_WIDEN_MULT_LO_EXPR:
7377 case VEC_WIDEN_MULT_EVEN_EXPR:
7378 case VEC_WIDEN_MULT_ODD_EXPR:
7379 return true;
7381 default:
7382 break;
7384 return false;
7387 /* Return true if CODE represents a ternary tree code for which the
7388 first two operands are commutative. Otherwise return false. */
7389 bool
7390 commutative_ternary_tree_code (enum tree_code code)
7392 switch (code)
7394 case WIDEN_MULT_PLUS_EXPR:
7395 case WIDEN_MULT_MINUS_EXPR:
7396 return true;
7398 default:
7399 break;
7401 return false;
7404 /* Generate a hash value for an expression. This can be used iteratively
7405 by passing a previous result as the VAL argument.
7407 This function is intended to produce the same hash for expressions which
7408 would compare equal using operand_equal_p. */
7410 hashval_t
7411 iterative_hash_expr (const_tree t, hashval_t val)
7413 int i;
7414 enum tree_code code;
7415 enum tree_code_class tclass;
7417 if (t == NULL_TREE)
7418 return iterative_hash_hashval_t (0, val);
7420 code = TREE_CODE (t);
7422 switch (code)
7424 /* Alas, constants aren't shared, so we can't rely on pointer
7425 identity. */
7426 case VOID_CST:
7427 return iterative_hash_hashval_t (0, val);
7428 case INTEGER_CST:
7429 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
7430 val = iterative_hash_host_wide_int (TREE_INT_CST_ELT (t, i), val);
7431 return val;
7432 case REAL_CST:
7434 unsigned int val2 = real_hash (TREE_REAL_CST_PTR (t));
7436 return iterative_hash_hashval_t (val2, val);
7438 case FIXED_CST:
7440 unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
7442 return iterative_hash_hashval_t (val2, val);
7444 case STRING_CST:
7445 return iterative_hash (TREE_STRING_POINTER (t),
7446 TREE_STRING_LENGTH (t), val);
7447 case COMPLEX_CST:
7448 val = iterative_hash_expr (TREE_REALPART (t), val);
7449 return iterative_hash_expr (TREE_IMAGPART (t), val);
7450 case VECTOR_CST:
7452 unsigned i;
7453 for (i = 0; i < VECTOR_CST_NELTS (t); ++i)
7454 val = iterative_hash_expr (VECTOR_CST_ELT (t, i), val);
7455 return val;
7457 case SSA_NAME:
7458 /* We can just compare by pointer. */
7459 return iterative_hash_host_wide_int (SSA_NAME_VERSION (t), val);
7460 case PLACEHOLDER_EXPR:
7461 /* The node itself doesn't matter. */
7462 return val;
7463 case TREE_LIST:
7464 /* A list of expressions, for a CALL_EXPR or as the elements of a
7465 VECTOR_CST. */
7466 for (; t; t = TREE_CHAIN (t))
7467 val = iterative_hash_expr (TREE_VALUE (t), val);
7468 return val;
7469 case CONSTRUCTOR:
7471 unsigned HOST_WIDE_INT idx;
7472 tree field, value;
7473 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
7475 val = iterative_hash_expr (field, val);
7476 val = iterative_hash_expr (value, val);
7478 return val;
7480 case FUNCTION_DECL:
7481 /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
7482 Otherwise nodes that compare equal according to operand_equal_p might
7483 get different hash codes. However, don't do this for machine specific
7484 or front end builtins, since the function code is overloaded in those
7485 cases. */
7486 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
7487 && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
7489 t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
7490 code = TREE_CODE (t);
7492 /* FALL THROUGH */
7493 default:
7494 tclass = TREE_CODE_CLASS (code);
7496 if (tclass == tcc_declaration)
7498 /* DECL's have a unique ID */
7499 val = iterative_hash_host_wide_int (DECL_UID (t), val);
7501 else
7503 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
7505 val = iterative_hash_object (code, val);
7507 /* Don't hash the type, that can lead to having nodes which
7508 compare equal according to operand_equal_p, but which
7509 have different hash codes. */
7510 if (CONVERT_EXPR_CODE_P (code)
7511 || code == NON_LVALUE_EXPR)
7513 /* Make sure to include signness in the hash computation. */
7514 val += TYPE_UNSIGNED (TREE_TYPE (t));
7515 val = iterative_hash_expr (TREE_OPERAND (t, 0), val);
7518 else if (commutative_tree_code (code))
7520 /* It's a commutative expression. We want to hash it the same
7521 however it appears. We do this by first hashing both operands
7522 and then rehashing based on the order of their independent
7523 hashes. */
7524 hashval_t one = iterative_hash_expr (TREE_OPERAND (t, 0), 0);
7525 hashval_t two = iterative_hash_expr (TREE_OPERAND (t, 1), 0);
7526 hashval_t t;
7528 if (one > two)
7529 t = one, one = two, two = t;
7531 val = iterative_hash_hashval_t (one, val);
7532 val = iterative_hash_hashval_t (two, val);
7534 else
7535 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
7536 val = iterative_hash_expr (TREE_OPERAND (t, i), val);
7538 return val;
7542 /* Constructors for pointer, array and function types.
7543 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
7544 constructed by language-dependent code, not here.) */
7546 /* Construct, lay out and return the type of pointers to TO_TYPE with
7547 mode MODE. If CAN_ALIAS_ALL is TRUE, indicate this type can
7548 reference all of memory. If such a type has already been
7549 constructed, reuse it. */
7551 tree
7552 build_pointer_type_for_mode (tree to_type, enum machine_mode mode,
7553 bool can_alias_all)
7555 tree t;
7557 if (to_type == error_mark_node)
7558 return error_mark_node;
7560 /* If the pointed-to type has the may_alias attribute set, force
7561 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7562 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7563 can_alias_all = true;
7565 /* In some cases, languages will have things that aren't a POINTER_TYPE
7566 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
7567 In that case, return that type without regard to the rest of our
7568 operands.
7570 ??? This is a kludge, but consistent with the way this function has
7571 always operated and there doesn't seem to be a good way to avoid this
7572 at the moment. */
7573 if (TYPE_POINTER_TO (to_type) != 0
7574 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
7575 return TYPE_POINTER_TO (to_type);
7577 /* First, if we already have a type for pointers to TO_TYPE and it's
7578 the proper mode, use it. */
7579 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
7580 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7581 return t;
7583 t = make_node (POINTER_TYPE);
7585 TREE_TYPE (t) = to_type;
7586 SET_TYPE_MODE (t, mode);
7587 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7588 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
7589 TYPE_POINTER_TO (to_type) = t;
7591 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7592 SET_TYPE_STRUCTURAL_EQUALITY (t);
7593 else if (TYPE_CANONICAL (to_type) != to_type)
7594 TYPE_CANONICAL (t)
7595 = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
7596 mode, can_alias_all);
7598 /* Lay out the type. This function has many callers that are concerned
7599 with expression-construction, and this simplifies them all. */
7600 layout_type (t);
7602 return t;
7605 /* By default build pointers in ptr_mode. */
7607 tree
7608 build_pointer_type (tree to_type)
7610 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7611 : TYPE_ADDR_SPACE (to_type);
7612 enum machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7613 if (upc_shared_type_p (to_type))
7615 tree upc_pts_type;
7616 pointer_mode = TYPE_MODE (upc_pts_rep_type_node);
7617 upc_pts_type = build_pointer_type_for_mode (to_type, pointer_mode,
7618 false);
7619 TYPE_USER_ALIGN (upc_pts_type) = TYPE_USER_ALIGN (upc_pts_rep_type_node);
7620 TYPE_ALIGN (upc_pts_type) = TYPE_ALIGN (upc_pts_rep_type_node);
7621 return upc_pts_type;
7623 return build_pointer_type_for_mode (to_type, pointer_mode, false);
7626 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
7628 tree
7629 build_reference_type_for_mode (tree to_type, enum machine_mode mode,
7630 bool can_alias_all)
7632 tree t;
7634 if (to_type == error_mark_node)
7635 return error_mark_node;
7637 /* If the pointed-to type has the may_alias attribute set, force
7638 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7639 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7640 can_alias_all = true;
7642 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
7643 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
7644 In that case, return that type without regard to the rest of our
7645 operands.
7647 ??? This is a kludge, but consistent with the way this function has
7648 always operated and there doesn't seem to be a good way to avoid this
7649 at the moment. */
7650 if (TYPE_REFERENCE_TO (to_type) != 0
7651 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
7652 return TYPE_REFERENCE_TO (to_type);
7654 /* First, if we already have a type for pointers to TO_TYPE and it's
7655 the proper mode, use it. */
7656 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
7657 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7658 return t;
7660 t = make_node (REFERENCE_TYPE);
7662 TREE_TYPE (t) = to_type;
7663 SET_TYPE_MODE (t, mode);
7664 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7665 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
7666 TYPE_REFERENCE_TO (to_type) = t;
7668 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7669 SET_TYPE_STRUCTURAL_EQUALITY (t);
7670 else if (TYPE_CANONICAL (to_type) != to_type)
7671 TYPE_CANONICAL (t)
7672 = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
7673 mode, can_alias_all);
7675 layout_type (t);
7677 return t;
7681 /* Build the node for the type of references-to-TO_TYPE by default
7682 in ptr_mode. */
7684 tree
7685 build_reference_type (tree to_type)
7687 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7688 : TYPE_ADDR_SPACE (to_type);
7689 enum machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7690 return build_reference_type_for_mode (to_type, pointer_mode, false);
7693 #define MAX_INT_CACHED_PREC \
7694 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
7695 static GTY(()) tree nonstandard_integer_type_cache[2 * MAX_INT_CACHED_PREC + 2];
7697 /* Builds a signed or unsigned integer type of precision PRECISION.
7698 Used for C bitfields whose precision does not match that of
7699 built-in target types. */
7700 tree
7701 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
7702 int unsignedp)
7704 tree itype, ret;
7706 if (unsignedp)
7707 unsignedp = MAX_INT_CACHED_PREC + 1;
7709 if (precision <= MAX_INT_CACHED_PREC)
7711 itype = nonstandard_integer_type_cache[precision + unsignedp];
7712 if (itype)
7713 return itype;
7716 itype = make_node (INTEGER_TYPE);
7717 TYPE_PRECISION (itype) = precision;
7719 if (unsignedp)
7720 fixup_unsigned_type (itype);
7721 else
7722 fixup_signed_type (itype);
7724 ret = itype;
7725 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (itype)))
7726 ret = type_hash_canon (tree_to_uhwi (TYPE_MAX_VALUE (itype)), itype);
7727 if (precision <= MAX_INT_CACHED_PREC)
7728 nonstandard_integer_type_cache[precision + unsignedp] = ret;
7730 return ret;
7733 /* Create a range of some discrete type TYPE (an INTEGER_TYPE, ENUMERAL_TYPE
7734 or BOOLEAN_TYPE) with low bound LOWVAL and high bound HIGHVAL. If SHARED
7735 is true, reuse such a type that has already been constructed. */
7737 static tree
7738 build_range_type_1 (tree type, tree lowval, tree highval, bool shared)
7740 tree itype = make_node (INTEGER_TYPE);
7741 hashval_t hashcode = 0;
7743 TREE_TYPE (itype) = type;
7745 TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
7746 TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
7748 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
7749 SET_TYPE_MODE (itype, TYPE_MODE (type));
7750 TYPE_SIZE (itype) = TYPE_SIZE (type);
7751 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
7752 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
7753 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
7755 if (!shared)
7756 return itype;
7758 if ((TYPE_MIN_VALUE (itype)
7759 && TREE_CODE (TYPE_MIN_VALUE (itype)) != INTEGER_CST)
7760 || (TYPE_MAX_VALUE (itype)
7761 && TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST))
7763 /* Since we cannot reliably merge this type, we need to compare it using
7764 structural equality checks. */
7765 SET_TYPE_STRUCTURAL_EQUALITY (itype);
7766 return itype;
7769 hashcode = iterative_hash_expr (TYPE_MIN_VALUE (itype), hashcode);
7770 hashcode = iterative_hash_expr (TYPE_MAX_VALUE (itype), hashcode);
7771 hashcode = iterative_hash_hashval_t (TYPE_HASH (type), hashcode);
7772 itype = type_hash_canon (hashcode, itype);
7774 return itype;
7777 /* Wrapper around build_range_type_1 with SHARED set to true. */
7779 tree
7780 build_range_type (tree type, tree lowval, tree highval)
7782 return build_range_type_1 (type, lowval, highval, true);
7785 /* Wrapper around build_range_type_1 with SHARED set to false. */
7787 tree
7788 build_nonshared_range_type (tree type, tree lowval, tree highval)
7790 return build_range_type_1 (type, lowval, highval, false);
7793 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
7794 MAXVAL should be the maximum value in the domain
7795 (one less than the length of the array).
7797 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
7798 We don't enforce this limit, that is up to caller (e.g. language front end).
7799 The limit exists because the result is a signed type and we don't handle
7800 sizes that use more than one HOST_WIDE_INT. */
7802 tree
7803 build_index_type (tree maxval)
7805 return build_range_type (sizetype, size_zero_node, maxval);
7808 /* Return true if the debug information for TYPE, a subtype, should be emitted
7809 as a subrange type. If so, set LOWVAL to the low bound and HIGHVAL to the
7810 high bound, respectively. Sometimes doing so unnecessarily obfuscates the
7811 debug info and doesn't reflect the source code. */
7813 bool
7814 subrange_type_for_debug_p (const_tree type, tree *lowval, tree *highval)
7816 tree base_type = TREE_TYPE (type), low, high;
7818 /* Subrange types have a base type which is an integral type. */
7819 if (!INTEGRAL_TYPE_P (base_type))
7820 return false;
7822 /* Get the real bounds of the subtype. */
7823 if (lang_hooks.types.get_subrange_bounds)
7824 lang_hooks.types.get_subrange_bounds (type, &low, &high);
7825 else
7827 low = TYPE_MIN_VALUE (type);
7828 high = TYPE_MAX_VALUE (type);
7831 /* If the type and its base type have the same representation and the same
7832 name, then the type is not a subrange but a copy of the base type. */
7833 if ((TREE_CODE (base_type) == INTEGER_TYPE
7834 || TREE_CODE (base_type) == BOOLEAN_TYPE)
7835 && int_size_in_bytes (type) == int_size_in_bytes (base_type)
7836 && tree_int_cst_equal (low, TYPE_MIN_VALUE (base_type))
7837 && tree_int_cst_equal (high, TYPE_MAX_VALUE (base_type))
7838 && TYPE_IDENTIFIER (type) == TYPE_IDENTIFIER (base_type))
7839 return false;
7841 if (lowval)
7842 *lowval = low;
7843 if (highval)
7844 *highval = high;
7845 return true;
7848 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
7849 and number of elements specified by the range of values of INDEX_TYPE.
7850 If SHARED is true, reuse such a type that has already been constructed. */
7852 static tree
7853 build_array_type_1 (tree elt_type, tree index_type, bool shared)
7855 tree t;
7857 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
7859 error ("arrays of functions are not meaningful");
7860 elt_type = integer_type_node;
7863 t = make_node (ARRAY_TYPE);
7864 TREE_TYPE (t) = elt_type;
7865 TYPE_DOMAIN (t) = index_type;
7866 TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (elt_type);
7867 layout_type (t);
7869 /* If the element type is incomplete at this point we get marked for
7870 structural equality. Do not record these types in the canonical
7871 type hashtable. */
7872 if (TYPE_STRUCTURAL_EQUALITY_P (t))
7873 return t;
7875 if (shared)
7877 hashval_t hashcode = iterative_hash_object (TYPE_HASH (elt_type), 0);
7878 if (index_type)
7879 hashcode = iterative_hash_object (TYPE_HASH (index_type), hashcode);
7880 t = type_hash_canon (hashcode, t);
7883 if (TYPE_CANONICAL (t) == t)
7885 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
7886 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
7887 SET_TYPE_STRUCTURAL_EQUALITY (t);
7888 else if (TYPE_CANONICAL (elt_type) != elt_type
7889 || (index_type && TYPE_CANONICAL (index_type) != index_type))
7890 TYPE_CANONICAL (t)
7891 = build_array_type_1 (TYPE_CANONICAL (elt_type),
7892 index_type
7893 ? TYPE_CANONICAL (index_type) : NULL_TREE,
7894 shared);
7897 return t;
7900 /* Wrapper around build_array_type_1 with SHARED set to true. */
7902 tree
7903 build_array_type (tree elt_type, tree index_type)
7905 return build_array_type_1 (elt_type, index_type, true);
7908 /* Wrapper around build_array_type_1 with SHARED set to false. */
7910 tree
7911 build_nonshared_array_type (tree elt_type, tree index_type)
7913 return build_array_type_1 (elt_type, index_type, false);
7916 /* Return a representation of ELT_TYPE[NELTS], using indices of type
7917 sizetype. */
7919 tree
7920 build_array_type_nelts (tree elt_type, unsigned HOST_WIDE_INT nelts)
7922 return build_array_type (elt_type, build_index_type (size_int (nelts - 1)));
7925 /* Recursively examines the array elements of TYPE, until a non-array
7926 element type is found. */
7928 tree
7929 strip_array_types (tree type)
7931 while (TREE_CODE (type) == ARRAY_TYPE)
7932 type = TREE_TYPE (type);
7934 return type;
7937 /* Computes the canonical argument types from the argument type list
7938 ARGTYPES.
7940 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
7941 on entry to this function, or if any of the ARGTYPES are
7942 structural.
7944 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
7945 true on entry to this function, or if any of the ARGTYPES are
7946 non-canonical.
7948 Returns a canonical argument list, which may be ARGTYPES when the
7949 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
7950 true) or would not differ from ARGTYPES. */
7952 static tree
7953 maybe_canonicalize_argtypes (tree argtypes,
7954 bool *any_structural_p,
7955 bool *any_noncanonical_p)
7957 tree arg;
7958 bool any_noncanonical_argtypes_p = false;
7960 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
7962 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
7963 /* Fail gracefully by stating that the type is structural. */
7964 *any_structural_p = true;
7965 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
7966 *any_structural_p = true;
7967 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
7968 || TREE_PURPOSE (arg))
7969 /* If the argument has a default argument, we consider it
7970 non-canonical even though the type itself is canonical.
7971 That way, different variants of function and method types
7972 with default arguments will all point to the variant with
7973 no defaults as their canonical type. */
7974 any_noncanonical_argtypes_p = true;
7977 if (*any_structural_p)
7978 return argtypes;
7980 if (any_noncanonical_argtypes_p)
7982 /* Build the canonical list of argument types. */
7983 tree canon_argtypes = NULL_TREE;
7984 bool is_void = false;
7986 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
7988 if (arg == void_list_node)
7989 is_void = true;
7990 else
7991 canon_argtypes = tree_cons (NULL_TREE,
7992 TYPE_CANONICAL (TREE_VALUE (arg)),
7993 canon_argtypes);
7996 canon_argtypes = nreverse (canon_argtypes);
7997 if (is_void)
7998 canon_argtypes = chainon (canon_argtypes, void_list_node);
8000 /* There is a non-canonical type. */
8001 *any_noncanonical_p = true;
8002 return canon_argtypes;
8005 /* The canonical argument types are the same as ARGTYPES. */
8006 return argtypes;
8009 /* Construct, lay out and return
8010 the type of functions returning type VALUE_TYPE
8011 given arguments of types ARG_TYPES.
8012 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
8013 are data type nodes for the arguments of the function.
8014 If such a type has already been constructed, reuse it. */
8016 tree
8017 build_function_type (tree value_type, tree arg_types)
8019 tree t;
8020 hashval_t hashcode = 0;
8021 bool any_structural_p, any_noncanonical_p;
8022 tree canon_argtypes;
8024 if (TREE_CODE (value_type) == FUNCTION_TYPE)
8026 error ("function return type cannot be function");
8027 value_type = integer_type_node;
8030 /* Make a node of the sort we want. */
8031 t = make_node (FUNCTION_TYPE);
8032 TREE_TYPE (t) = value_type;
8033 TYPE_ARG_TYPES (t) = arg_types;
8035 /* If we already have such a type, use the old one. */
8036 hashcode = iterative_hash_object (TYPE_HASH (value_type), hashcode);
8037 hashcode = type_hash_list (arg_types, hashcode);
8038 t = type_hash_canon (hashcode, t);
8040 /* Set up the canonical type. */
8041 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
8042 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
8043 canon_argtypes = maybe_canonicalize_argtypes (arg_types,
8044 &any_structural_p,
8045 &any_noncanonical_p);
8046 if (any_structural_p)
8047 SET_TYPE_STRUCTURAL_EQUALITY (t);
8048 else if (any_noncanonical_p)
8049 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
8050 canon_argtypes);
8052 if (!COMPLETE_TYPE_P (t))
8053 layout_type (t);
8054 return t;
8057 /* Build a function type. The RETURN_TYPE is the type returned by the
8058 function. If VAARGS is set, no void_type_node is appended to the
8059 the list. ARGP must be always be terminated be a NULL_TREE. */
8061 static tree
8062 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
8064 tree t, args, last;
8066 t = va_arg (argp, tree);
8067 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
8068 args = tree_cons (NULL_TREE, t, args);
8070 if (vaargs)
8072 last = args;
8073 if (args != NULL_TREE)
8074 args = nreverse (args);
8075 gcc_assert (last != void_list_node);
8077 else if (args == NULL_TREE)
8078 args = void_list_node;
8079 else
8081 last = args;
8082 args = nreverse (args);
8083 TREE_CHAIN (last) = void_list_node;
8085 args = build_function_type (return_type, args);
8087 return args;
8090 /* Build a function type. The RETURN_TYPE is the type returned by the
8091 function. If additional arguments are provided, they are
8092 additional argument types. The list of argument types must always
8093 be terminated by NULL_TREE. */
8095 tree
8096 build_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 (false, return_type, p);
8103 va_end (p);
8104 return args;
8107 /* Build a variable argument function type. The RETURN_TYPE is the
8108 type returned by the function. If additional arguments are provided,
8109 they are additional argument types. The list of argument types must
8110 always be terminated by NULL_TREE. */
8112 tree
8113 build_varargs_function_type_list (tree return_type, ...)
8115 tree args;
8116 va_list p;
8118 va_start (p, return_type);
8119 args = build_function_type_list_1 (true, return_type, p);
8120 va_end (p);
8122 return args;
8125 /* Build a function type. RETURN_TYPE is the type returned by the
8126 function; VAARGS indicates whether the function takes varargs. The
8127 function takes N named arguments, the types of which are provided in
8128 ARG_TYPES. */
8130 static tree
8131 build_function_type_array_1 (bool vaargs, tree return_type, int n,
8132 tree *arg_types)
8134 int i;
8135 tree t = vaargs ? NULL_TREE : void_list_node;
8137 for (i = n - 1; i >= 0; i--)
8138 t = tree_cons (NULL_TREE, arg_types[i], t);
8140 return build_function_type (return_type, t);
8143 /* Build a function type. RETURN_TYPE is the type returned by the
8144 function. The function takes N named arguments, the types of which
8145 are provided in ARG_TYPES. */
8147 tree
8148 build_function_type_array (tree return_type, int n, tree *arg_types)
8150 return build_function_type_array_1 (false, return_type, n, arg_types);
8153 /* Build a variable argument function type. RETURN_TYPE is the type
8154 returned by the function. The function takes N named arguments, the
8155 types of which are provided in ARG_TYPES. */
8157 tree
8158 build_varargs_function_type_array (tree return_type, int n, tree *arg_types)
8160 return build_function_type_array_1 (true, return_type, n, arg_types);
8163 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
8164 and ARGTYPES (a TREE_LIST) are the return type and arguments types
8165 for the method. An implicit additional parameter (of type
8166 pointer-to-BASETYPE) is added to the ARGTYPES. */
8168 tree
8169 build_method_type_directly (tree basetype,
8170 tree rettype,
8171 tree argtypes)
8173 tree t;
8174 tree ptype;
8175 int hashcode = 0;
8176 bool any_structural_p, any_noncanonical_p;
8177 tree canon_argtypes;
8179 /* Make a node of the sort we want. */
8180 t = make_node (METHOD_TYPE);
8182 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8183 TREE_TYPE (t) = rettype;
8184 ptype = build_pointer_type (basetype);
8186 /* The actual arglist for this function includes a "hidden" argument
8187 which is "this". Put it into the list of argument types. */
8188 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
8189 TYPE_ARG_TYPES (t) = argtypes;
8191 /* If we already have such a type, use the old one. */
8192 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
8193 hashcode = iterative_hash_object (TYPE_HASH (rettype), hashcode);
8194 hashcode = type_hash_list (argtypes, hashcode);
8195 t = type_hash_canon (hashcode, t);
8197 /* Set up the canonical type. */
8198 any_structural_p
8199 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8200 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
8201 any_noncanonical_p
8202 = (TYPE_CANONICAL (basetype) != basetype
8203 || TYPE_CANONICAL (rettype) != rettype);
8204 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
8205 &any_structural_p,
8206 &any_noncanonical_p);
8207 if (any_structural_p)
8208 SET_TYPE_STRUCTURAL_EQUALITY (t);
8209 else if (any_noncanonical_p)
8210 TYPE_CANONICAL (t)
8211 = build_method_type_directly (TYPE_CANONICAL (basetype),
8212 TYPE_CANONICAL (rettype),
8213 canon_argtypes);
8214 if (!COMPLETE_TYPE_P (t))
8215 layout_type (t);
8217 return t;
8220 /* Construct, lay out and return the type of methods belonging to class
8221 BASETYPE and whose arguments and values are described by TYPE.
8222 If that type exists already, reuse it.
8223 TYPE must be a FUNCTION_TYPE node. */
8225 tree
8226 build_method_type (tree basetype, tree type)
8228 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8230 return build_method_type_directly (basetype,
8231 TREE_TYPE (type),
8232 TYPE_ARG_TYPES (type));
8235 /* Construct, lay out and return the type of offsets to a value
8236 of type TYPE, within an object of type BASETYPE.
8237 If a suitable offset type exists already, reuse it. */
8239 tree
8240 build_offset_type (tree basetype, tree type)
8242 tree t;
8243 hashval_t hashcode = 0;
8245 /* Make a node of the sort we want. */
8246 t = make_node (OFFSET_TYPE);
8248 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8249 TREE_TYPE (t) = type;
8251 /* If we already have such a type, use the old one. */
8252 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
8253 hashcode = iterative_hash_object (TYPE_HASH (type), hashcode);
8254 t = type_hash_canon (hashcode, t);
8256 if (!COMPLETE_TYPE_P (t))
8257 layout_type (t);
8259 if (TYPE_CANONICAL (t) == t)
8261 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8262 || TYPE_STRUCTURAL_EQUALITY_P (type))
8263 SET_TYPE_STRUCTURAL_EQUALITY (t);
8264 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
8265 || TYPE_CANONICAL (type) != type)
8266 TYPE_CANONICAL (t)
8267 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
8268 TYPE_CANONICAL (type));
8271 return t;
8274 /* Create a complex type whose components are COMPONENT_TYPE. */
8276 tree
8277 build_complex_type (tree component_type)
8279 tree t;
8280 hashval_t hashcode;
8282 gcc_assert (INTEGRAL_TYPE_P (component_type)
8283 || SCALAR_FLOAT_TYPE_P (component_type)
8284 || FIXED_POINT_TYPE_P (component_type));
8286 /* Make a node of the sort we want. */
8287 t = make_node (COMPLEX_TYPE);
8289 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
8291 /* If we already have such a type, use the old one. */
8292 hashcode = iterative_hash_object (TYPE_HASH (component_type), 0);
8293 t = type_hash_canon (hashcode, t);
8295 if (!COMPLETE_TYPE_P (t))
8296 layout_type (t);
8298 if (TYPE_CANONICAL (t) == t)
8300 if (TYPE_STRUCTURAL_EQUALITY_P (component_type))
8301 SET_TYPE_STRUCTURAL_EQUALITY (t);
8302 else if (TYPE_CANONICAL (component_type) != component_type)
8303 TYPE_CANONICAL (t)
8304 = build_complex_type (TYPE_CANONICAL (component_type));
8307 /* We need to create a name, since complex is a fundamental type. */
8308 if (! TYPE_NAME (t))
8310 const char *name;
8311 if (component_type == char_type_node)
8312 name = "complex char";
8313 else if (component_type == signed_char_type_node)
8314 name = "complex signed char";
8315 else if (component_type == unsigned_char_type_node)
8316 name = "complex unsigned char";
8317 else if (component_type == short_integer_type_node)
8318 name = "complex short int";
8319 else if (component_type == short_unsigned_type_node)
8320 name = "complex short unsigned int";
8321 else if (component_type == integer_type_node)
8322 name = "complex int";
8323 else if (component_type == unsigned_type_node)
8324 name = "complex unsigned int";
8325 else if (component_type == long_integer_type_node)
8326 name = "complex long int";
8327 else if (component_type == long_unsigned_type_node)
8328 name = "complex long unsigned int";
8329 else if (component_type == long_long_integer_type_node)
8330 name = "complex long long int";
8331 else if (component_type == long_long_unsigned_type_node)
8332 name = "complex long long unsigned int";
8333 else
8334 name = 0;
8336 if (name != 0)
8337 TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
8338 get_identifier (name), t);
8341 return build_qualified_type (t, TYPE_QUALS (component_type));
8344 /* If TYPE is a real or complex floating-point type and the target
8345 does not directly support arithmetic on TYPE then return the wider
8346 type to be used for arithmetic on TYPE. Otherwise, return
8347 NULL_TREE. */
8349 tree
8350 excess_precision_type (tree type)
8352 if (flag_excess_precision != EXCESS_PRECISION_FAST)
8354 int flt_eval_method = TARGET_FLT_EVAL_METHOD;
8355 switch (TREE_CODE (type))
8357 case REAL_TYPE:
8358 switch (flt_eval_method)
8360 case 1:
8361 if (TYPE_MODE (type) == TYPE_MODE (float_type_node))
8362 return double_type_node;
8363 break;
8364 case 2:
8365 if (TYPE_MODE (type) == TYPE_MODE (float_type_node)
8366 || TYPE_MODE (type) == TYPE_MODE (double_type_node))
8367 return long_double_type_node;
8368 break;
8369 default:
8370 gcc_unreachable ();
8372 break;
8373 case COMPLEX_TYPE:
8374 if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
8375 return NULL_TREE;
8376 switch (flt_eval_method)
8378 case 1:
8379 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node))
8380 return complex_double_type_node;
8381 break;
8382 case 2:
8383 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node)
8384 || (TYPE_MODE (TREE_TYPE (type))
8385 == TYPE_MODE (double_type_node)))
8386 return complex_long_double_type_node;
8387 break;
8388 default:
8389 gcc_unreachable ();
8391 break;
8392 default:
8393 break;
8396 return NULL_TREE;
8399 /* Return OP, stripped of any conversions to wider types as much as is safe.
8400 Converting the value back to OP's type makes a value equivalent to OP.
8402 If FOR_TYPE is nonzero, we return a value which, if converted to
8403 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
8405 OP must have integer, real or enumeral type. Pointers are not allowed!
8407 There are some cases where the obvious value we could return
8408 would regenerate to OP if converted to OP's type,
8409 but would not extend like OP to wider types.
8410 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
8411 For example, if OP is (unsigned short)(signed char)-1,
8412 we avoid returning (signed char)-1 if FOR_TYPE is int,
8413 even though extending that to an unsigned short would regenerate OP,
8414 since the result of extending (signed char)-1 to (int)
8415 is different from (int) OP. */
8417 tree
8418 get_unwidened (tree op, tree for_type)
8420 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
8421 tree type = TREE_TYPE (op);
8422 unsigned final_prec
8423 = TYPE_PRECISION (for_type != 0 ? for_type : type);
8424 int uns
8425 = (for_type != 0 && for_type != type
8426 && final_prec > TYPE_PRECISION (type)
8427 && TYPE_UNSIGNED (type));
8428 tree win = op;
8430 while (CONVERT_EXPR_P (op))
8432 int bitschange;
8434 /* TYPE_PRECISION on vector types has different meaning
8435 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
8436 so avoid them here. */
8437 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
8438 break;
8440 bitschange = TYPE_PRECISION (TREE_TYPE (op))
8441 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
8443 /* Truncations are many-one so cannot be removed.
8444 Unless we are later going to truncate down even farther. */
8445 if (bitschange < 0
8446 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
8447 break;
8449 /* See what's inside this conversion. If we decide to strip it,
8450 we will set WIN. */
8451 op = TREE_OPERAND (op, 0);
8453 /* If we have not stripped any zero-extensions (uns is 0),
8454 we can strip any kind of extension.
8455 If we have previously stripped a zero-extension,
8456 only zero-extensions can safely be stripped.
8457 Any extension can be stripped if the bits it would produce
8458 are all going to be discarded later by truncating to FOR_TYPE. */
8460 if (bitschange > 0)
8462 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
8463 win = op;
8464 /* TYPE_UNSIGNED says whether this is a zero-extension.
8465 Let's avoid computing it if it does not affect WIN
8466 and if UNS will not be needed again. */
8467 if ((uns
8468 || CONVERT_EXPR_P (op))
8469 && TYPE_UNSIGNED (TREE_TYPE (op)))
8471 uns = 1;
8472 win = op;
8477 /* If we finally reach a constant see if it fits in for_type and
8478 in that case convert it. */
8479 if (for_type
8480 && TREE_CODE (win) == INTEGER_CST
8481 && TREE_TYPE (win) != for_type
8482 && int_fits_type_p (win, for_type))
8483 win = fold_convert (for_type, win);
8485 return win;
8488 /* Return OP or a simpler expression for a narrower value
8489 which can be sign-extended or zero-extended to give back OP.
8490 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
8491 or 0 if the value should be sign-extended. */
8493 tree
8494 get_narrower (tree op, int *unsignedp_ptr)
8496 int uns = 0;
8497 int first = 1;
8498 tree win = op;
8499 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
8501 while (TREE_CODE (op) == NOP_EXPR)
8503 int bitschange
8504 = (TYPE_PRECISION (TREE_TYPE (op))
8505 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
8507 /* Truncations are many-one so cannot be removed. */
8508 if (bitschange < 0)
8509 break;
8511 /* See what's inside this conversion. If we decide to strip it,
8512 we will set WIN. */
8514 if (bitschange > 0)
8516 op = TREE_OPERAND (op, 0);
8517 /* An extension: the outermost one can be stripped,
8518 but remember whether it is zero or sign extension. */
8519 if (first)
8520 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8521 /* Otherwise, if a sign extension has been stripped,
8522 only sign extensions can now be stripped;
8523 if a zero extension has been stripped, only zero-extensions. */
8524 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
8525 break;
8526 first = 0;
8528 else /* bitschange == 0 */
8530 /* A change in nominal type can always be stripped, but we must
8531 preserve the unsignedness. */
8532 if (first)
8533 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8534 first = 0;
8535 op = TREE_OPERAND (op, 0);
8536 /* Keep trying to narrow, but don't assign op to win if it
8537 would turn an integral type into something else. */
8538 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
8539 continue;
8542 win = op;
8545 if (TREE_CODE (op) == COMPONENT_REF
8546 /* Since type_for_size always gives an integer type. */
8547 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
8548 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
8549 /* Ensure field is laid out already. */
8550 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
8551 && tree_fits_uhwi_p (DECL_SIZE (TREE_OPERAND (op, 1))))
8553 unsigned HOST_WIDE_INT innerprec
8554 = tree_to_uhwi (DECL_SIZE (TREE_OPERAND (op, 1)));
8555 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
8556 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
8557 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
8559 /* We can get this structure field in a narrower type that fits it,
8560 but the resulting extension to its nominal type (a fullword type)
8561 must satisfy the same conditions as for other extensions.
8563 Do this only for fields that are aligned (not bit-fields),
8564 because when bit-field insns will be used there is no
8565 advantage in doing this. */
8567 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
8568 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
8569 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
8570 && type != 0)
8572 if (first)
8573 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
8574 win = fold_convert (type, op);
8578 *unsignedp_ptr = uns;
8579 return win;
8582 /* Returns true if integer constant C has a value that is permissible
8583 for type TYPE (an INTEGER_TYPE). */
8585 bool
8586 int_fits_type_p (const_tree c, const_tree type)
8588 tree type_low_bound, type_high_bound;
8589 bool ok_for_low_bound, ok_for_high_bound;
8590 signop sgn_c = TYPE_SIGN (TREE_TYPE (c));
8592 retry:
8593 type_low_bound = TYPE_MIN_VALUE (type);
8594 type_high_bound = TYPE_MAX_VALUE (type);
8596 /* If at least one bound of the type is a constant integer, we can check
8597 ourselves and maybe make a decision. If no such decision is possible, but
8598 this type is a subtype, try checking against that. Otherwise, use
8599 fits_to_tree_p, which checks against the precision.
8601 Compute the status for each possibly constant bound, and return if we see
8602 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
8603 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
8604 for "constant known to fit". */
8606 /* Check if c >= type_low_bound. */
8607 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
8609 if (tree_int_cst_lt (c, type_low_bound))
8610 return false;
8611 ok_for_low_bound = true;
8613 else
8614 ok_for_low_bound = false;
8616 /* Check if c <= type_high_bound. */
8617 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
8619 if (tree_int_cst_lt (type_high_bound, c))
8620 return false;
8621 ok_for_high_bound = true;
8623 else
8624 ok_for_high_bound = false;
8626 /* If the constant fits both bounds, the result is known. */
8627 if (ok_for_low_bound && ok_for_high_bound)
8628 return true;
8630 /* Perform some generic filtering which may allow making a decision
8631 even if the bounds are not constant. First, negative integers
8632 never fit in unsigned types, */
8633 if (TYPE_UNSIGNED (type) && sgn_c == SIGNED && wi::neg_p (c))
8634 return false;
8636 /* Second, narrower types always fit in wider ones. */
8637 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
8638 return true;
8640 /* Third, unsigned integers with top bit set never fit signed types. */
8641 if (!TYPE_UNSIGNED (type) && sgn_c == UNSIGNED)
8643 int prec = GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (c))) - 1;
8644 if (prec < TYPE_PRECISION (TREE_TYPE (c)))
8646 /* When a tree_cst is converted to a wide-int, the precision
8647 is taken from the type. However, if the precision of the
8648 mode underneath the type is smaller than that, it is
8649 possible that the value will not fit. The test below
8650 fails if any bit is set between the sign bit of the
8651 underlying mode and the top bit of the type. */
8652 if (wi::ne_p (wi::zext (c, prec - 1), c))
8653 return false;
8655 else if (wi::neg_p (c))
8656 return false;
8659 /* If we haven't been able to decide at this point, there nothing more we
8660 can check ourselves here. Look at the base type if we have one and it
8661 has the same precision. */
8662 if (TREE_CODE (type) == INTEGER_TYPE
8663 && TREE_TYPE (type) != 0
8664 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
8666 type = TREE_TYPE (type);
8667 goto retry;
8670 /* Or to fits_to_tree_p, if nothing else. */
8671 return wi::fits_to_tree_p (c, type);
8674 /* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
8675 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
8676 represented (assuming two's-complement arithmetic) within the bit
8677 precision of the type are returned instead. */
8679 void
8680 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
8682 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
8683 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
8684 wi::to_mpz (TYPE_MIN_VALUE (type), min, TYPE_SIGN (type));
8685 else
8687 if (TYPE_UNSIGNED (type))
8688 mpz_set_ui (min, 0);
8689 else
8691 wide_int mn = wi::min_value (TYPE_PRECISION (type), SIGNED);
8692 wi::to_mpz (mn, min, SIGNED);
8696 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
8697 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
8698 wi::to_mpz (TYPE_MAX_VALUE (type), max, TYPE_SIGN (type));
8699 else
8701 wide_int mn = wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
8702 wi::to_mpz (mn, max, TYPE_SIGN (type));
8706 /* Return true if VAR is an automatic variable defined in function FN. */
8708 bool
8709 auto_var_in_fn_p (const_tree var, const_tree fn)
8711 return (DECL_P (var) && DECL_CONTEXT (var) == fn
8712 && ((((TREE_CODE (var) == VAR_DECL && ! DECL_EXTERNAL (var))
8713 || TREE_CODE (var) == PARM_DECL)
8714 && ! TREE_STATIC (var))
8715 || TREE_CODE (var) == LABEL_DECL
8716 || TREE_CODE (var) == RESULT_DECL));
8719 /* Subprogram of following function. Called by walk_tree.
8721 Return *TP if it is an automatic variable or parameter of the
8722 function passed in as DATA. */
8724 static tree
8725 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
8727 tree fn = (tree) data;
8729 if (TYPE_P (*tp))
8730 *walk_subtrees = 0;
8732 else if (DECL_P (*tp)
8733 && auto_var_in_fn_p (*tp, fn))
8734 return *tp;
8736 return NULL_TREE;
8739 /* Returns true if T is, contains, or refers to a type with variable
8740 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
8741 arguments, but not the return type. If FN is nonzero, only return
8742 true if a modifier of the type or position of FN is a variable or
8743 parameter inside FN.
8745 This concept is more general than that of C99 'variably modified types':
8746 in C99, a struct type is never variably modified because a VLA may not
8747 appear as a structure member. However, in GNU C code like:
8749 struct S { int i[f()]; };
8751 is valid, and other languages may define similar constructs. */
8753 bool
8754 variably_modified_type_p (tree type, tree fn)
8756 tree t;
8758 /* Test if T is either variable (if FN is zero) or an expression containing
8759 a variable in FN. If TYPE isn't gimplified, return true also if
8760 gimplify_one_sizepos would gimplify the expression into a local
8761 variable. */
8762 #define RETURN_TRUE_IF_VAR(T) \
8763 do { tree _t = (T); \
8764 if (_t != NULL_TREE \
8765 && _t != error_mark_node \
8766 && TREE_CODE (_t) != INTEGER_CST \
8767 && TREE_CODE (_t) != PLACEHOLDER_EXPR \
8768 && (!fn \
8769 || (!TYPE_SIZES_GIMPLIFIED (type) \
8770 && !is_gimple_sizepos (_t)) \
8771 || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
8772 return true; } while (0)
8774 if (type == error_mark_node)
8775 return false;
8777 /* If TYPE itself has variable size, it is variably modified. */
8778 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
8779 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
8781 switch (TREE_CODE (type))
8783 case POINTER_TYPE:
8784 case REFERENCE_TYPE:
8785 case VECTOR_TYPE:
8786 if (variably_modified_type_p (TREE_TYPE (type), fn))
8787 return true;
8788 break;
8790 case FUNCTION_TYPE:
8791 case METHOD_TYPE:
8792 /* If TYPE is a function type, it is variably modified if the
8793 return type is variably modified. */
8794 if (variably_modified_type_p (TREE_TYPE (type), fn))
8795 return true;
8796 break;
8798 case INTEGER_TYPE:
8799 case REAL_TYPE:
8800 case FIXED_POINT_TYPE:
8801 case ENUMERAL_TYPE:
8802 case BOOLEAN_TYPE:
8803 /* Scalar types are variably modified if their end points
8804 aren't constant. */
8805 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
8806 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
8807 break;
8809 case RECORD_TYPE:
8810 case UNION_TYPE:
8811 case QUAL_UNION_TYPE:
8812 /* We can't see if any of the fields are variably-modified by the
8813 definition we normally use, since that would produce infinite
8814 recursion via pointers. */
8815 /* This is variably modified if some field's type is. */
8816 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
8817 if (TREE_CODE (t) == FIELD_DECL)
8819 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
8820 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
8821 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
8823 if (TREE_CODE (type) == QUAL_UNION_TYPE)
8824 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
8826 break;
8828 case ARRAY_TYPE:
8829 /* Do not call ourselves to avoid infinite recursion. This is
8830 variably modified if the element type is. */
8831 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
8832 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
8833 break;
8835 default:
8836 break;
8839 /* The current language may have other cases to check, but in general,
8840 all other types are not variably modified. */
8841 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
8843 #undef RETURN_TRUE_IF_VAR
8846 /* Given a DECL or TYPE, return the scope in which it was declared, or
8847 NULL_TREE if there is no containing scope. */
8849 tree
8850 get_containing_scope (const_tree t)
8852 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
8855 /* Return the innermost context enclosing DECL that is
8856 a FUNCTION_DECL, or zero if none. */
8858 tree
8859 decl_function_context (const_tree decl)
8861 tree context;
8863 if (TREE_CODE (decl) == ERROR_MARK)
8864 return 0;
8866 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
8867 where we look up the function at runtime. Such functions always take
8868 a first argument of type 'pointer to real context'.
8870 C++ should really be fixed to use DECL_CONTEXT for the real context,
8871 and use something else for the "virtual context". */
8872 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
8873 context
8874 = TYPE_MAIN_VARIANT
8875 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
8876 else
8877 context = DECL_CONTEXT (decl);
8879 while (context && TREE_CODE (context) != FUNCTION_DECL)
8881 if (TREE_CODE (context) == BLOCK)
8882 context = BLOCK_SUPERCONTEXT (context);
8883 else
8884 context = get_containing_scope (context);
8887 return context;
8890 /* Return the innermost context enclosing DECL that is
8891 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
8892 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
8894 tree
8895 decl_type_context (const_tree decl)
8897 tree context = DECL_CONTEXT (decl);
8899 while (context)
8900 switch (TREE_CODE (context))
8902 case NAMESPACE_DECL:
8903 case TRANSLATION_UNIT_DECL:
8904 return NULL_TREE;
8906 case RECORD_TYPE:
8907 case UNION_TYPE:
8908 case QUAL_UNION_TYPE:
8909 return context;
8911 case TYPE_DECL:
8912 case FUNCTION_DECL:
8913 context = DECL_CONTEXT (context);
8914 break;
8916 case BLOCK:
8917 context = BLOCK_SUPERCONTEXT (context);
8918 break;
8920 default:
8921 gcc_unreachable ();
8924 return NULL_TREE;
8927 /* CALL is a CALL_EXPR. Return the declaration for the function
8928 called, or NULL_TREE if the called function cannot be
8929 determined. */
8931 tree
8932 get_callee_fndecl (const_tree call)
8934 tree addr;
8936 if (call == error_mark_node)
8937 return error_mark_node;
8939 /* It's invalid to call this function with anything but a
8940 CALL_EXPR. */
8941 gcc_assert (TREE_CODE (call) == CALL_EXPR);
8943 /* The first operand to the CALL is the address of the function
8944 called. */
8945 addr = CALL_EXPR_FN (call);
8947 STRIP_NOPS (addr);
8949 /* If this is a readonly function pointer, extract its initial value. */
8950 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
8951 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
8952 && DECL_INITIAL (addr))
8953 addr = DECL_INITIAL (addr);
8955 /* If the address is just `&f' for some function `f', then we know
8956 that `f' is being called. */
8957 if (TREE_CODE (addr) == ADDR_EXPR
8958 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
8959 return TREE_OPERAND (addr, 0);
8961 /* We couldn't figure out what was being called. */
8962 return NULL_TREE;
8965 /* Print debugging information about tree nodes generated during the compile,
8966 and any language-specific information. */
8968 void
8969 dump_tree_statistics (void)
8971 if (GATHER_STATISTICS)
8973 int i;
8974 int total_nodes, total_bytes;
8975 fprintf (stderr, "Kind Nodes Bytes\n");
8976 fprintf (stderr, "---------------------------------------\n");
8977 total_nodes = total_bytes = 0;
8978 for (i = 0; i < (int) all_kinds; i++)
8980 fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
8981 tree_node_counts[i], tree_node_sizes[i]);
8982 total_nodes += tree_node_counts[i];
8983 total_bytes += tree_node_sizes[i];
8985 fprintf (stderr, "---------------------------------------\n");
8986 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
8987 fprintf (stderr, "---------------------------------------\n");
8988 fprintf (stderr, "Code Nodes\n");
8989 fprintf (stderr, "----------------------------\n");
8990 for (i = 0; i < (int) MAX_TREE_CODES; i++)
8991 fprintf (stderr, "%-20s %7d\n", get_tree_code_name ((enum tree_code) i),
8992 tree_code_counts[i]);
8993 fprintf (stderr, "----------------------------\n");
8994 ssanames_print_statistics ();
8995 phinodes_print_statistics ();
8997 else
8998 fprintf (stderr, "(No per-node statistics)\n");
9000 print_type_hash_statistics ();
9001 print_debug_expr_statistics ();
9002 print_value_expr_statistics ();
9003 lang_hooks.print_statistics ();
9006 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
9008 /* Generate a crc32 of a byte. */
9010 static unsigned
9011 crc32_unsigned_bits (unsigned chksum, unsigned value, unsigned bits)
9013 unsigned ix;
9015 for (ix = bits; ix--; value <<= 1)
9017 unsigned feedback;
9019 feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
9020 chksum <<= 1;
9021 chksum ^= feedback;
9023 return chksum;
9026 /* Generate a crc32 of a 32-bit unsigned. */
9028 unsigned
9029 crc32_unsigned (unsigned chksum, unsigned value)
9031 return crc32_unsigned_bits (chksum, value, 32);
9034 /* Generate a crc32 of a byte. */
9036 unsigned
9037 crc32_byte (unsigned chksum, char byte)
9039 return crc32_unsigned_bits (chksum, (unsigned) byte << 24, 8);
9042 /* Generate a crc32 of a string. */
9044 unsigned
9045 crc32_string (unsigned chksum, const char *string)
9049 chksum = crc32_byte (chksum, *string);
9051 while (*string++);
9052 return chksum;
9055 /* P is a string that will be used in a symbol. Mask out any characters
9056 that are not valid in that context. */
9058 void
9059 clean_symbol_name (char *p)
9061 for (; *p; p++)
9062 if (! (ISALNUM (*p)
9063 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
9064 || *p == '$'
9065 #endif
9066 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
9067 || *p == '.'
9068 #endif
9070 *p = '_';
9073 /* Generate a name for a special-purpose function.
9074 The generated name may need to be unique across the whole link.
9075 Changes to this function may also require corresponding changes to
9076 xstrdup_mask_random.
9077 TYPE is some string to identify the purpose of this function to the
9078 linker or collect2; it must start with an uppercase letter,
9079 one of:
9080 I - for constructors
9081 D - for destructors
9082 N - for C++ anonymous namespaces
9083 F - for DWARF unwind frame information. */
9085 tree
9086 get_file_function_name (const char *type)
9088 char *buf;
9089 const char *p;
9090 char *q;
9092 /* If we already have a name we know to be unique, just use that. */
9093 if (first_global_object_name)
9094 p = q = ASTRDUP (first_global_object_name);
9095 /* If the target is handling the constructors/destructors, they
9096 will be local to this file and the name is only necessary for
9097 debugging purposes.
9098 We also assign sub_I and sub_D sufixes to constructors called from
9099 the global static constructors. These are always local. */
9100 else if (((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
9101 || (strncmp (type, "sub_", 4) == 0
9102 && (type[4] == 'I' || type[4] == 'D')))
9104 const char *file = main_input_filename;
9105 if (! file)
9106 file = LOCATION_FILE (input_location);
9107 /* Just use the file's basename, because the full pathname
9108 might be quite long. */
9109 p = q = ASTRDUP (lbasename (file));
9111 else
9113 /* Otherwise, the name must be unique across the entire link.
9114 We don't have anything that we know to be unique to this translation
9115 unit, so use what we do have and throw in some randomness. */
9116 unsigned len;
9117 const char *name = weak_global_object_name;
9118 const char *file = main_input_filename;
9120 if (! name)
9121 name = "";
9122 if (! file)
9123 file = LOCATION_FILE (input_location);
9125 len = strlen (file);
9126 q = (char *) alloca (9 + 17 + len + 1);
9127 memcpy (q, file, len + 1);
9129 snprintf (q + len, 9 + 17 + 1, "_%08X_" HOST_WIDE_INT_PRINT_HEX,
9130 crc32_string (0, name), get_random_seed (false));
9132 p = q;
9135 clean_symbol_name (q);
9136 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
9137 + strlen (type));
9139 /* Set up the name of the file-level functions we may need.
9140 Use a global object (which is already required to be unique over
9141 the program) rather than the file name (which imposes extra
9142 constraints). */
9143 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
9145 return get_identifier (buf);
9148 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
9150 /* Complain that the tree code of NODE does not match the expected 0
9151 terminated list of trailing codes. The trailing code list can be
9152 empty, for a more vague error message. FILE, LINE, and FUNCTION
9153 are of the caller. */
9155 void
9156 tree_check_failed (const_tree node, const char *file,
9157 int line, const char *function, ...)
9159 va_list args;
9160 const char *buffer;
9161 unsigned length = 0;
9162 enum tree_code code;
9164 va_start (args, function);
9165 while ((code = (enum tree_code) va_arg (args, int)))
9166 length += 4 + strlen (get_tree_code_name (code));
9167 va_end (args);
9168 if (length)
9170 char *tmp;
9171 va_start (args, function);
9172 length += strlen ("expected ");
9173 buffer = tmp = (char *) alloca (length);
9174 length = 0;
9175 while ((code = (enum tree_code) va_arg (args, int)))
9177 const char *prefix = length ? " or " : "expected ";
9179 strcpy (tmp + length, prefix);
9180 length += strlen (prefix);
9181 strcpy (tmp + length, get_tree_code_name (code));
9182 length += strlen (get_tree_code_name (code));
9184 va_end (args);
9186 else
9187 buffer = "unexpected node";
9189 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9190 buffer, get_tree_code_name (TREE_CODE (node)),
9191 function, trim_filename (file), line);
9194 /* Complain that the tree code of NODE does match the expected 0
9195 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
9196 the caller. */
9198 void
9199 tree_not_check_failed (const_tree node, const char *file,
9200 int line, const char *function, ...)
9202 va_list args;
9203 char *buffer;
9204 unsigned length = 0;
9205 enum tree_code code;
9207 va_start (args, function);
9208 while ((code = (enum tree_code) va_arg (args, int)))
9209 length += 4 + strlen (get_tree_code_name (code));
9210 va_end (args);
9211 va_start (args, function);
9212 buffer = (char *) alloca (length);
9213 length = 0;
9214 while ((code = (enum tree_code) va_arg (args, int)))
9216 if (length)
9218 strcpy (buffer + length, " or ");
9219 length += 4;
9221 strcpy (buffer + length, get_tree_code_name (code));
9222 length += strlen (get_tree_code_name (code));
9224 va_end (args);
9226 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
9227 buffer, get_tree_code_name (TREE_CODE (node)),
9228 function, trim_filename (file), line);
9231 /* Similar to tree_check_failed, except that we check for a class of tree
9232 code, given in CL. */
9234 void
9235 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
9236 const char *file, int line, const char *function)
9238 internal_error
9239 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
9240 TREE_CODE_CLASS_STRING (cl),
9241 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9242 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9245 /* Similar to tree_check_failed, except that instead of specifying a
9246 dozen codes, use the knowledge that they're all sequential. */
9248 void
9249 tree_range_check_failed (const_tree node, const char *file, int line,
9250 const char *function, enum tree_code c1,
9251 enum tree_code c2)
9253 char *buffer;
9254 unsigned length = 0;
9255 unsigned int c;
9257 for (c = c1; c <= c2; ++c)
9258 length += 4 + strlen (get_tree_code_name ((enum tree_code) c));
9260 length += strlen ("expected ");
9261 buffer = (char *) alloca (length);
9262 length = 0;
9264 for (c = c1; c <= c2; ++c)
9266 const char *prefix = length ? " or " : "expected ";
9268 strcpy (buffer + length, prefix);
9269 length += strlen (prefix);
9270 strcpy (buffer + length, get_tree_code_name ((enum tree_code) c));
9271 length += strlen (get_tree_code_name ((enum tree_code) c));
9274 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9275 buffer, get_tree_code_name (TREE_CODE (node)),
9276 function, trim_filename (file), line);
9280 /* Similar to tree_check_failed, except that we check that a tree does
9281 not have the specified code, given in CL. */
9283 void
9284 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
9285 const char *file, int line, const char *function)
9287 internal_error
9288 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
9289 TREE_CODE_CLASS_STRING (cl),
9290 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9291 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9295 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
9297 void
9298 omp_clause_check_failed (const_tree node, const char *file, int line,
9299 const char *function, enum omp_clause_code code)
9301 internal_error ("tree check: expected omp_clause %s, have %s in %s, at %s:%d",
9302 omp_clause_code_name[code], get_tree_code_name (TREE_CODE (node)),
9303 function, trim_filename (file), line);
9307 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
9309 void
9310 omp_clause_range_check_failed (const_tree node, const char *file, int line,
9311 const char *function, enum omp_clause_code c1,
9312 enum omp_clause_code c2)
9314 char *buffer;
9315 unsigned length = 0;
9316 unsigned int c;
9318 for (c = c1; c <= c2; ++c)
9319 length += 4 + strlen (omp_clause_code_name[c]);
9321 length += strlen ("expected ");
9322 buffer = (char *) alloca (length);
9323 length = 0;
9325 for (c = c1; c <= c2; ++c)
9327 const char *prefix = length ? " or " : "expected ";
9329 strcpy (buffer + length, prefix);
9330 length += strlen (prefix);
9331 strcpy (buffer + length, omp_clause_code_name[c]);
9332 length += strlen (omp_clause_code_name[c]);
9335 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9336 buffer, omp_clause_code_name[TREE_CODE (node)],
9337 function, trim_filename (file), line);
9341 #undef DEFTREESTRUCT
9342 #define DEFTREESTRUCT(VAL, NAME) NAME,
9344 static const char *ts_enum_names[] = {
9345 #include "treestruct.def"
9347 #undef DEFTREESTRUCT
9349 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
9351 /* Similar to tree_class_check_failed, except that we check for
9352 whether CODE contains the tree structure identified by EN. */
9354 void
9355 tree_contains_struct_check_failed (const_tree node,
9356 const enum tree_node_structure_enum en,
9357 const char *file, int line,
9358 const char *function)
9360 internal_error
9361 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
9362 TS_ENUM_NAME (en),
9363 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9367 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9368 (dynamically sized) vector. */
9370 void
9371 tree_int_cst_elt_check_failed (int idx, int len, const char *file, int line,
9372 const char *function)
9374 internal_error
9375 ("tree check: accessed elt %d of tree_int_cst with %d elts in %s, at %s:%d",
9376 idx + 1, len, function, trim_filename (file), line);
9379 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9380 (dynamically sized) vector. */
9382 void
9383 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
9384 const char *function)
9386 internal_error
9387 ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
9388 idx + 1, len, function, trim_filename (file), line);
9391 /* Similar to above, except that the check is for the bounds of the operand
9392 vector of an expression node EXP. */
9394 void
9395 tree_operand_check_failed (int idx, const_tree exp, const char *file,
9396 int line, const char *function)
9398 enum tree_code code = TREE_CODE (exp);
9399 internal_error
9400 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
9401 idx + 1, get_tree_code_name (code), TREE_OPERAND_LENGTH (exp),
9402 function, trim_filename (file), line);
9405 /* Similar to above, except that the check is for the number of
9406 operands of an OMP_CLAUSE node. */
9408 void
9409 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
9410 int line, const char *function)
9412 internal_error
9413 ("tree check: accessed operand %d of omp_clause %s with %d operands "
9414 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
9415 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
9416 trim_filename (file), line);
9418 #endif /* ENABLE_TREE_CHECKING */
9420 /* Create a new vector type node holding SUBPARTS units of type INNERTYPE,
9421 and mapped to the machine mode MODE. Initialize its fields and build
9422 the information necessary for debugging output. */
9424 static tree
9425 make_vector_type (tree innertype, int nunits, enum machine_mode mode)
9427 tree t;
9428 hashval_t hashcode = 0;
9430 t = make_node (VECTOR_TYPE);
9431 TREE_TYPE (t) = TYPE_MAIN_VARIANT (innertype);
9432 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
9433 SET_TYPE_MODE (t, mode);
9435 if (TYPE_STRUCTURAL_EQUALITY_P (innertype))
9436 SET_TYPE_STRUCTURAL_EQUALITY (t);
9437 else if (TYPE_CANONICAL (innertype) != innertype
9438 || mode != VOIDmode)
9439 TYPE_CANONICAL (t)
9440 = make_vector_type (TYPE_CANONICAL (innertype), nunits, VOIDmode);
9442 layout_type (t);
9444 hashcode = iterative_hash_host_wide_int (VECTOR_TYPE, hashcode);
9445 hashcode = iterative_hash_host_wide_int (nunits, hashcode);
9446 hashcode = iterative_hash_host_wide_int (mode, hashcode);
9447 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (t)), hashcode);
9448 t = type_hash_canon (hashcode, t);
9450 /* We have built a main variant, based on the main variant of the
9451 inner type. Use it to build the variant we return. */
9452 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
9453 && TREE_TYPE (t) != innertype)
9454 return build_type_attribute_qual_variant (t,
9455 TYPE_ATTRIBUTES (innertype),
9456 TYPE_QUALS (innertype));
9458 return t;
9461 static tree
9462 make_or_reuse_type (unsigned size, int unsignedp)
9464 if (size == INT_TYPE_SIZE)
9465 return unsignedp ? unsigned_type_node : integer_type_node;
9466 if (size == CHAR_TYPE_SIZE)
9467 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
9468 if (size == SHORT_TYPE_SIZE)
9469 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
9470 if (size == LONG_TYPE_SIZE)
9471 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
9472 if (size == LONG_LONG_TYPE_SIZE)
9473 return (unsignedp ? long_long_unsigned_type_node
9474 : long_long_integer_type_node);
9475 if (size == 128 && int128_integer_type_node)
9476 return (unsignedp ? int128_unsigned_type_node
9477 : int128_integer_type_node);
9479 if (unsignedp)
9480 return make_unsigned_type (size);
9481 else
9482 return make_signed_type (size);
9485 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
9487 static tree
9488 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
9490 if (satp)
9492 if (size == SHORT_FRACT_TYPE_SIZE)
9493 return unsignedp ? sat_unsigned_short_fract_type_node
9494 : sat_short_fract_type_node;
9495 if (size == FRACT_TYPE_SIZE)
9496 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
9497 if (size == LONG_FRACT_TYPE_SIZE)
9498 return unsignedp ? sat_unsigned_long_fract_type_node
9499 : sat_long_fract_type_node;
9500 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9501 return unsignedp ? sat_unsigned_long_long_fract_type_node
9502 : sat_long_long_fract_type_node;
9504 else
9506 if (size == SHORT_FRACT_TYPE_SIZE)
9507 return unsignedp ? unsigned_short_fract_type_node
9508 : short_fract_type_node;
9509 if (size == FRACT_TYPE_SIZE)
9510 return unsignedp ? unsigned_fract_type_node : fract_type_node;
9511 if (size == LONG_FRACT_TYPE_SIZE)
9512 return unsignedp ? unsigned_long_fract_type_node
9513 : long_fract_type_node;
9514 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9515 return unsignedp ? unsigned_long_long_fract_type_node
9516 : long_long_fract_type_node;
9519 return make_fract_type (size, unsignedp, satp);
9522 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
9524 static tree
9525 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
9527 if (satp)
9529 if (size == SHORT_ACCUM_TYPE_SIZE)
9530 return unsignedp ? sat_unsigned_short_accum_type_node
9531 : sat_short_accum_type_node;
9532 if (size == ACCUM_TYPE_SIZE)
9533 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
9534 if (size == LONG_ACCUM_TYPE_SIZE)
9535 return unsignedp ? sat_unsigned_long_accum_type_node
9536 : sat_long_accum_type_node;
9537 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9538 return unsignedp ? sat_unsigned_long_long_accum_type_node
9539 : sat_long_long_accum_type_node;
9541 else
9543 if (size == SHORT_ACCUM_TYPE_SIZE)
9544 return unsignedp ? unsigned_short_accum_type_node
9545 : short_accum_type_node;
9546 if (size == ACCUM_TYPE_SIZE)
9547 return unsignedp ? unsigned_accum_type_node : accum_type_node;
9548 if (size == LONG_ACCUM_TYPE_SIZE)
9549 return unsignedp ? unsigned_long_accum_type_node
9550 : long_accum_type_node;
9551 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9552 return unsignedp ? unsigned_long_long_accum_type_node
9553 : long_long_accum_type_node;
9556 return make_accum_type (size, unsignedp, satp);
9560 /* Create an atomic variant node for TYPE. This routine is called
9561 during initialization of data types to create the 5 basic atomic
9562 types. The generic build_variant_type function requires these to
9563 already be set up in order to function properly, so cannot be
9564 called from there. If ALIGN is non-zero, then ensure alignment is
9565 overridden to this value. */
9567 static tree
9568 build_atomic_base (tree type, unsigned int align)
9570 tree t;
9572 /* Make sure its not already registered. */
9573 if ((t = get_qualified_type (type, TYPE_QUAL_ATOMIC)))
9574 return t;
9576 t = build_variant_type_copy (type);
9577 set_type_quals (t, TYPE_QUAL_ATOMIC, NULL_TREE);
9579 if (align)
9580 TYPE_ALIGN (t) = align;
9582 return t;
9585 /* Create nodes for all integer types (and error_mark_node) using the sizes
9586 of C datatypes. SIGNED_CHAR specifies whether char is signed,
9587 SHORT_DOUBLE specifies whether double should be of the same precision
9588 as float. */
9590 void
9591 build_common_tree_nodes (bool signed_char, bool short_double)
9593 error_mark_node = make_node (ERROR_MARK);
9594 TREE_TYPE (error_mark_node) = error_mark_node;
9596 initialize_sizetypes ();
9598 /* Define both `signed char' and `unsigned char'. */
9599 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
9600 TYPE_STRING_FLAG (signed_char_type_node) = 1;
9601 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
9602 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
9604 /* Define `char', which is like either `signed char' or `unsigned char'
9605 but not the same as either. */
9606 char_type_node
9607 = (signed_char
9608 ? make_signed_type (CHAR_TYPE_SIZE)
9609 : make_unsigned_type (CHAR_TYPE_SIZE));
9610 TYPE_STRING_FLAG (char_type_node) = 1;
9612 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
9613 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
9614 integer_type_node = make_signed_type (INT_TYPE_SIZE);
9615 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
9616 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
9617 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
9618 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
9619 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
9620 #if HOST_BITS_PER_WIDE_INT >= 64
9621 /* TODO: This isn't correct, but as logic depends at the moment on
9622 host's instead of target's wide-integer.
9623 If there is a target not supporting TImode, but has an 128-bit
9624 integer-scalar register, this target check needs to be adjusted. */
9625 if (targetm.scalar_mode_supported_p (TImode))
9627 int128_integer_type_node = make_signed_type (128);
9628 int128_unsigned_type_node = make_unsigned_type (128);
9630 #endif
9632 /* Define a boolean type. This type only represents boolean values but
9633 may be larger than char depending on the value of BOOL_TYPE_SIZE. */
9634 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
9635 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
9636 TYPE_PRECISION (boolean_type_node) = 1;
9637 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
9639 /* Define what type to use for size_t. */
9640 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
9641 size_type_node = unsigned_type_node;
9642 else if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
9643 size_type_node = long_unsigned_type_node;
9644 else if (strcmp (SIZE_TYPE, "long long unsigned int") == 0)
9645 size_type_node = long_long_unsigned_type_node;
9646 else if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
9647 size_type_node = short_unsigned_type_node;
9648 else
9649 gcc_unreachable ();
9651 /* Fill in the rest of the sized types. Reuse existing type nodes
9652 when possible. */
9653 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
9654 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
9655 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
9656 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
9657 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
9659 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
9660 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
9661 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
9662 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
9663 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
9665 /* Don't call build_qualified type for atomics. That routine does
9666 special processing for atomics, and until they are initialized
9667 it's better not to make that call.
9669 Check to see if there is a target override for atomic types. */
9671 atomicQI_type_node = build_atomic_base (unsigned_intQI_type_node,
9672 targetm.atomic_align_for_mode (QImode));
9673 atomicHI_type_node = build_atomic_base (unsigned_intHI_type_node,
9674 targetm.atomic_align_for_mode (HImode));
9675 atomicSI_type_node = build_atomic_base (unsigned_intSI_type_node,
9676 targetm.atomic_align_for_mode (SImode));
9677 atomicDI_type_node = build_atomic_base (unsigned_intDI_type_node,
9678 targetm.atomic_align_for_mode (DImode));
9679 atomicTI_type_node = build_atomic_base (unsigned_intTI_type_node,
9680 targetm.atomic_align_for_mode (TImode));
9682 access_public_node = get_identifier ("public");
9683 access_protected_node = get_identifier ("protected");
9684 access_private_node = get_identifier ("private");
9686 /* Define these next since types below may used them. */
9687 integer_zero_node = build_int_cst (integer_type_node, 0);
9688 integer_one_node = build_int_cst (integer_type_node, 1);
9689 integer_three_node = build_int_cst (integer_type_node, 3);
9690 integer_minus_one_node = build_int_cst (integer_type_node, -1);
9692 size_zero_node = size_int (0);
9693 size_one_node = size_int (1);
9694 bitsize_zero_node = bitsize_int (0);
9695 bitsize_one_node = bitsize_int (1);
9696 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
9698 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
9699 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
9701 void_type_node = make_node (VOID_TYPE);
9702 layout_type (void_type_node);
9704 /* We are not going to have real types in C with less than byte alignment,
9705 so we might as well not have any types that claim to have it. */
9706 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
9707 TYPE_USER_ALIGN (void_type_node) = 0;
9709 void_node = make_node (VOID_CST);
9710 TREE_TYPE (void_node) = void_type_node;
9712 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
9713 layout_type (TREE_TYPE (null_pointer_node));
9715 ptr_type_node = build_pointer_type (void_type_node);
9716 const_ptr_type_node
9717 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
9718 fileptr_type_node = ptr_type_node;
9720 pointer_sized_int_node = build_nonstandard_integer_type (POINTER_SIZE, 1);
9722 float_type_node = make_node (REAL_TYPE);
9723 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
9724 layout_type (float_type_node);
9726 double_type_node = make_node (REAL_TYPE);
9727 if (short_double)
9728 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
9729 else
9730 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
9731 layout_type (double_type_node);
9733 long_double_type_node = make_node (REAL_TYPE);
9734 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
9735 layout_type (long_double_type_node);
9737 float_ptr_type_node = build_pointer_type (float_type_node);
9738 double_ptr_type_node = build_pointer_type (double_type_node);
9739 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
9740 integer_ptr_type_node = build_pointer_type (integer_type_node);
9742 /* Fixed size integer types. */
9743 uint16_type_node = build_nonstandard_integer_type (16, true);
9744 uint32_type_node = build_nonstandard_integer_type (32, true);
9745 uint64_type_node = build_nonstandard_integer_type (64, true);
9747 /* Decimal float types. */
9748 dfloat32_type_node = make_node (REAL_TYPE);
9749 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
9750 layout_type (dfloat32_type_node);
9751 SET_TYPE_MODE (dfloat32_type_node, SDmode);
9752 dfloat32_ptr_type_node = build_pointer_type (dfloat32_type_node);
9754 dfloat64_type_node = make_node (REAL_TYPE);
9755 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
9756 layout_type (dfloat64_type_node);
9757 SET_TYPE_MODE (dfloat64_type_node, DDmode);
9758 dfloat64_ptr_type_node = build_pointer_type (dfloat64_type_node);
9760 dfloat128_type_node = make_node (REAL_TYPE);
9761 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
9762 layout_type (dfloat128_type_node);
9763 SET_TYPE_MODE (dfloat128_type_node, TDmode);
9764 dfloat128_ptr_type_node = build_pointer_type (dfloat128_type_node);
9766 complex_integer_type_node = build_complex_type (integer_type_node);
9767 complex_float_type_node = build_complex_type (float_type_node);
9768 complex_double_type_node = build_complex_type (double_type_node);
9769 complex_long_double_type_node = build_complex_type (long_double_type_node);
9771 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
9772 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
9773 sat_ ## KIND ## _type_node = \
9774 make_sat_signed_ ## KIND ## _type (SIZE); \
9775 sat_unsigned_ ## KIND ## _type_node = \
9776 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9777 KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9778 unsigned_ ## KIND ## _type_node = \
9779 make_unsigned_ ## KIND ## _type (SIZE);
9781 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
9782 sat_ ## WIDTH ## KIND ## _type_node = \
9783 make_sat_signed_ ## KIND ## _type (SIZE); \
9784 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
9785 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9786 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9787 unsigned_ ## WIDTH ## KIND ## _type_node = \
9788 make_unsigned_ ## KIND ## _type (SIZE);
9790 /* Make fixed-point type nodes based on four different widths. */
9791 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
9792 MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
9793 MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
9794 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
9795 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
9797 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
9798 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
9799 NAME ## _type_node = \
9800 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
9801 u ## NAME ## _type_node = \
9802 make_or_reuse_unsigned_ ## KIND ## _type \
9803 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
9804 sat_ ## NAME ## _type_node = \
9805 make_or_reuse_sat_signed_ ## KIND ## _type \
9806 (GET_MODE_BITSIZE (MODE ## mode)); \
9807 sat_u ## NAME ## _type_node = \
9808 make_or_reuse_sat_unsigned_ ## KIND ## _type \
9809 (GET_MODE_BITSIZE (U ## MODE ## mode));
9811 /* Fixed-point type and mode nodes. */
9812 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
9813 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
9814 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
9815 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
9816 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
9817 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
9818 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
9819 MAKE_FIXED_MODE_NODE (accum, ha, HA)
9820 MAKE_FIXED_MODE_NODE (accum, sa, SA)
9821 MAKE_FIXED_MODE_NODE (accum, da, DA)
9822 MAKE_FIXED_MODE_NODE (accum, ta, TA)
9825 tree t = targetm.build_builtin_va_list ();
9827 /* Many back-ends define record types without setting TYPE_NAME.
9828 If we copied the record type here, we'd keep the original
9829 record type without a name. This breaks name mangling. So,
9830 don't copy record types and let c_common_nodes_and_builtins()
9831 declare the type to be __builtin_va_list. */
9832 if (TREE_CODE (t) != RECORD_TYPE)
9833 t = build_variant_type_copy (t);
9835 va_list_type_node = t;
9839 /* Modify DECL for given flags.
9840 TM_PURE attribute is set only on types, so the function will modify
9841 DECL's type when ECF_TM_PURE is used. */
9843 void
9844 set_call_expr_flags (tree decl, int flags)
9846 if (flags & ECF_NOTHROW)
9847 TREE_NOTHROW (decl) = 1;
9848 if (flags & ECF_CONST)
9849 TREE_READONLY (decl) = 1;
9850 if (flags & ECF_PURE)
9851 DECL_PURE_P (decl) = 1;
9852 if (flags & ECF_LOOPING_CONST_OR_PURE)
9853 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
9854 if (flags & ECF_NOVOPS)
9855 DECL_IS_NOVOPS (decl) = 1;
9856 if (flags & ECF_NORETURN)
9857 TREE_THIS_VOLATILE (decl) = 1;
9858 if (flags & ECF_MALLOC)
9859 DECL_IS_MALLOC (decl) = 1;
9860 if (flags & ECF_RETURNS_TWICE)
9861 DECL_IS_RETURNS_TWICE (decl) = 1;
9862 if (flags & ECF_LEAF)
9863 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("leaf"),
9864 NULL, DECL_ATTRIBUTES (decl));
9865 if ((flags & ECF_TM_PURE) && flag_tm)
9866 apply_tm_attr (decl, get_identifier ("transaction_pure"));
9867 /* Looping const or pure is implied by noreturn.
9868 There is currently no way to declare looping const or looping pure alone. */
9869 gcc_assert (!(flags & ECF_LOOPING_CONST_OR_PURE)
9870 || ((flags & ECF_NORETURN) && (flags & (ECF_CONST | ECF_PURE))));
9874 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
9876 static void
9877 local_define_builtin (const char *name, tree type, enum built_in_function code,
9878 const char *library_name, int ecf_flags)
9880 tree decl;
9882 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
9883 library_name, NULL_TREE);
9884 set_call_expr_flags (decl, ecf_flags);
9886 set_builtin_decl (code, decl, true);
9889 /* Call this function after instantiating all builtins that the language
9890 front end cares about. This will build the rest of the builtins that
9891 are relied upon by the tree optimizers and the middle-end. */
9893 void
9894 build_common_builtin_nodes (void)
9896 tree tmp, ftype;
9897 int ecf_flags;
9899 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE))
9901 ftype = build_function_type (void_type_node, void_list_node);
9902 local_define_builtin ("__builtin_unreachable", ftype, BUILT_IN_UNREACHABLE,
9903 "__builtin_unreachable",
9904 ECF_NOTHROW | ECF_LEAF | ECF_NORETURN
9905 | ECF_CONST | ECF_LEAF);
9908 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY)
9909 || !builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9911 ftype = build_function_type_list (ptr_type_node,
9912 ptr_type_node, const_ptr_type_node,
9913 size_type_node, NULL_TREE);
9915 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY))
9916 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
9917 "memcpy", ECF_NOTHROW | ECF_LEAF);
9918 if (!builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9919 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
9920 "memmove", ECF_NOTHROW | ECF_LEAF);
9923 if (!builtin_decl_explicit_p (BUILT_IN_MEMCMP))
9925 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
9926 const_ptr_type_node, size_type_node,
9927 NULL_TREE);
9928 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
9929 "memcmp", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9932 if (!builtin_decl_explicit_p (BUILT_IN_MEMSET))
9934 ftype = build_function_type_list (ptr_type_node,
9935 ptr_type_node, integer_type_node,
9936 size_type_node, NULL_TREE);
9937 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
9938 "memset", ECF_NOTHROW | ECF_LEAF);
9941 if (!builtin_decl_explicit_p (BUILT_IN_ALLOCA))
9943 ftype = build_function_type_list (ptr_type_node,
9944 size_type_node, NULL_TREE);
9945 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
9946 "alloca", ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
9949 ftype = build_function_type_list (ptr_type_node, size_type_node,
9950 size_type_node, NULL_TREE);
9951 local_define_builtin ("__builtin_alloca_with_align", ftype,
9952 BUILT_IN_ALLOCA_WITH_ALIGN, "alloca",
9953 ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
9955 /* If we're checking the stack, `alloca' can throw. */
9956 if (flag_stack_check)
9958 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA)) = 0;
9959 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN)) = 0;
9962 ftype = build_function_type_list (void_type_node,
9963 ptr_type_node, ptr_type_node,
9964 ptr_type_node, NULL_TREE);
9965 local_define_builtin ("__builtin_init_trampoline", ftype,
9966 BUILT_IN_INIT_TRAMPOLINE,
9967 "__builtin_init_trampoline", ECF_NOTHROW | ECF_LEAF);
9968 local_define_builtin ("__builtin_init_heap_trampoline", ftype,
9969 BUILT_IN_INIT_HEAP_TRAMPOLINE,
9970 "__builtin_init_heap_trampoline",
9971 ECF_NOTHROW | ECF_LEAF);
9973 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
9974 local_define_builtin ("__builtin_adjust_trampoline", ftype,
9975 BUILT_IN_ADJUST_TRAMPOLINE,
9976 "__builtin_adjust_trampoline",
9977 ECF_CONST | ECF_NOTHROW);
9979 ftype = build_function_type_list (void_type_node,
9980 ptr_type_node, ptr_type_node, NULL_TREE);
9981 local_define_builtin ("__builtin_nonlocal_goto", ftype,
9982 BUILT_IN_NONLOCAL_GOTO,
9983 "__builtin_nonlocal_goto",
9984 ECF_NORETURN | ECF_NOTHROW);
9986 ftype = build_function_type_list (void_type_node,
9987 ptr_type_node, ptr_type_node, NULL_TREE);
9988 local_define_builtin ("__builtin_setjmp_setup", ftype,
9989 BUILT_IN_SETJMP_SETUP,
9990 "__builtin_setjmp_setup", ECF_NOTHROW);
9992 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9993 local_define_builtin ("__builtin_setjmp_receiver", ftype,
9994 BUILT_IN_SETJMP_RECEIVER,
9995 "__builtin_setjmp_receiver", ECF_NOTHROW | ECF_LEAF);
9997 ftype = build_function_type_list (ptr_type_node, NULL_TREE);
9998 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
9999 "__builtin_stack_save", ECF_NOTHROW | ECF_LEAF);
10001 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10002 local_define_builtin ("__builtin_stack_restore", ftype,
10003 BUILT_IN_STACK_RESTORE,
10004 "__builtin_stack_restore", ECF_NOTHROW | ECF_LEAF);
10006 /* If there's a possibility that we might use the ARM EABI, build the
10007 alternate __cxa_end_cleanup node used to resume from C++ and Java. */
10008 if (targetm.arm_eabi_unwinder)
10010 ftype = build_function_type_list (void_type_node, NULL_TREE);
10011 local_define_builtin ("__builtin_cxa_end_cleanup", ftype,
10012 BUILT_IN_CXA_END_CLEANUP,
10013 "__cxa_end_cleanup", ECF_NORETURN | ECF_LEAF);
10016 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10017 local_define_builtin ("__builtin_unwind_resume", ftype,
10018 BUILT_IN_UNWIND_RESUME,
10019 ((targetm_common.except_unwind_info (&global_options)
10020 == UI_SJLJ)
10021 ? "_Unwind_SjLj_Resume" : "_Unwind_Resume"),
10022 ECF_NORETURN);
10024 if (builtin_decl_explicit (BUILT_IN_RETURN_ADDRESS) == NULL_TREE)
10026 ftype = build_function_type_list (ptr_type_node, integer_type_node,
10027 NULL_TREE);
10028 local_define_builtin ("__builtin_return_address", ftype,
10029 BUILT_IN_RETURN_ADDRESS,
10030 "__builtin_return_address",
10031 ECF_NOTHROW);
10034 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER)
10035 || !builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
10037 ftype = build_function_type_list (void_type_node, ptr_type_node,
10038 ptr_type_node, NULL_TREE);
10039 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER))
10040 local_define_builtin ("__cyg_profile_func_enter", ftype,
10041 BUILT_IN_PROFILE_FUNC_ENTER,
10042 "__cyg_profile_func_enter", 0);
10043 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
10044 local_define_builtin ("__cyg_profile_func_exit", ftype,
10045 BUILT_IN_PROFILE_FUNC_EXIT,
10046 "__cyg_profile_func_exit", 0);
10049 /* The exception object and filter values from the runtime. The argument
10050 must be zero before exception lowering, i.e. from the front end. After
10051 exception lowering, it will be the region number for the exception
10052 landing pad. These functions are PURE instead of CONST to prevent
10053 them from being hoisted past the exception edge that will initialize
10054 its value in the landing pad. */
10055 ftype = build_function_type_list (ptr_type_node,
10056 integer_type_node, NULL_TREE);
10057 ecf_flags = ECF_PURE | ECF_NOTHROW | ECF_LEAF;
10058 /* Only use TM_PURE if we we have TM language support. */
10059 if (builtin_decl_explicit_p (BUILT_IN_TM_LOAD_1))
10060 ecf_flags |= ECF_TM_PURE;
10061 local_define_builtin ("__builtin_eh_pointer", ftype, BUILT_IN_EH_POINTER,
10062 "__builtin_eh_pointer", ecf_flags);
10064 tmp = lang_hooks.types.type_for_mode (targetm.eh_return_filter_mode (), 0);
10065 ftype = build_function_type_list (tmp, integer_type_node, NULL_TREE);
10066 local_define_builtin ("__builtin_eh_filter", ftype, BUILT_IN_EH_FILTER,
10067 "__builtin_eh_filter", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10069 ftype = build_function_type_list (void_type_node,
10070 integer_type_node, integer_type_node,
10071 NULL_TREE);
10072 local_define_builtin ("__builtin_eh_copy_values", ftype,
10073 BUILT_IN_EH_COPY_VALUES,
10074 "__builtin_eh_copy_values", ECF_NOTHROW);
10076 /* Complex multiplication and division. These are handled as builtins
10077 rather than optabs because emit_library_call_value doesn't support
10078 complex. Further, we can do slightly better with folding these
10079 beasties if the real and complex parts of the arguments are separate. */
10081 int mode;
10083 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
10085 char mode_name_buf[4], *q;
10086 const char *p;
10087 enum built_in_function mcode, dcode;
10088 tree type, inner_type;
10089 const char *prefix = "__";
10091 if (targetm.libfunc_gnu_prefix)
10092 prefix = "__gnu_";
10094 type = lang_hooks.types.type_for_mode ((enum machine_mode) mode, 0);
10095 if (type == NULL)
10096 continue;
10097 inner_type = TREE_TYPE (type);
10099 ftype = build_function_type_list (type, inner_type, inner_type,
10100 inner_type, inner_type, NULL_TREE);
10102 mcode = ((enum built_in_function)
10103 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10104 dcode = ((enum built_in_function)
10105 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10107 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
10108 *q = TOLOWER (*p);
10109 *q = '\0';
10111 built_in_names[mcode] = concat (prefix, "mul", mode_name_buf, "3",
10112 NULL);
10113 local_define_builtin (built_in_names[mcode], ftype, mcode,
10114 built_in_names[mcode],
10115 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
10117 built_in_names[dcode] = concat (prefix, "div", mode_name_buf, "3",
10118 NULL);
10119 local_define_builtin (built_in_names[dcode], ftype, dcode,
10120 built_in_names[dcode],
10121 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
10126 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
10127 better way.
10129 If we requested a pointer to a vector, build up the pointers that
10130 we stripped off while looking for the inner type. Similarly for
10131 return values from functions.
10133 The argument TYPE is the top of the chain, and BOTTOM is the
10134 new type which we will point to. */
10136 tree
10137 reconstruct_complex_type (tree type, tree bottom)
10139 tree inner, outer;
10141 if (TREE_CODE (type) == POINTER_TYPE)
10143 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10144 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
10145 TYPE_REF_CAN_ALIAS_ALL (type));
10147 else if (TREE_CODE (type) == REFERENCE_TYPE)
10149 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10150 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
10151 TYPE_REF_CAN_ALIAS_ALL (type));
10153 else if (TREE_CODE (type) == ARRAY_TYPE)
10155 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10156 outer = build_array_type (inner, TYPE_DOMAIN (type));
10158 else if (TREE_CODE (type) == FUNCTION_TYPE)
10160 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10161 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
10163 else if (TREE_CODE (type) == METHOD_TYPE)
10165 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10166 /* The build_method_type_directly() routine prepends 'this' to argument list,
10167 so we must compensate by getting rid of it. */
10168 outer
10169 = build_method_type_directly
10170 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
10171 inner,
10172 TREE_CHAIN (TYPE_ARG_TYPES (type)));
10174 else if (TREE_CODE (type) == OFFSET_TYPE)
10176 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10177 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
10179 else
10180 return bottom;
10182 return build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
10183 TYPE_QUALS (type));
10186 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
10187 the inner type. */
10188 tree
10189 build_vector_type_for_mode (tree innertype, enum machine_mode mode)
10191 int nunits;
10193 switch (GET_MODE_CLASS (mode))
10195 case MODE_VECTOR_INT:
10196 case MODE_VECTOR_FLOAT:
10197 case MODE_VECTOR_FRACT:
10198 case MODE_VECTOR_UFRACT:
10199 case MODE_VECTOR_ACCUM:
10200 case MODE_VECTOR_UACCUM:
10201 nunits = GET_MODE_NUNITS (mode);
10202 break;
10204 case MODE_INT:
10205 /* Check that there are no leftover bits. */
10206 gcc_assert (GET_MODE_BITSIZE (mode)
10207 % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
10209 nunits = GET_MODE_BITSIZE (mode)
10210 / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
10211 break;
10213 default:
10214 gcc_unreachable ();
10217 return make_vector_type (innertype, nunits, mode);
10220 /* Similarly, but takes the inner type and number of units, which must be
10221 a power of two. */
10223 tree
10224 build_vector_type (tree innertype, int nunits)
10226 return make_vector_type (innertype, nunits, VOIDmode);
10229 /* Similarly, but builds a variant type with TYPE_VECTOR_OPAQUE set. */
10231 tree
10232 build_opaque_vector_type (tree innertype, int nunits)
10234 tree t = make_vector_type (innertype, nunits, VOIDmode);
10235 tree cand;
10236 /* We always build the non-opaque variant before the opaque one,
10237 so if it already exists, it is TYPE_NEXT_VARIANT of this one. */
10238 cand = TYPE_NEXT_VARIANT (t);
10239 if (cand
10240 && TYPE_VECTOR_OPAQUE (cand)
10241 && check_qualified_type (cand, t, TYPE_QUALS (t), NULL_TREE))
10242 return cand;
10243 /* Othewise build a variant type and make sure to queue it after
10244 the non-opaque type. */
10245 cand = build_distinct_type_copy (t);
10246 TYPE_VECTOR_OPAQUE (cand) = true;
10247 TYPE_CANONICAL (cand) = TYPE_CANONICAL (t);
10248 TYPE_NEXT_VARIANT (cand) = TYPE_NEXT_VARIANT (t);
10249 TYPE_NEXT_VARIANT (t) = cand;
10250 TYPE_MAIN_VARIANT (cand) = TYPE_MAIN_VARIANT (t);
10251 return cand;
10255 /* Given an initializer INIT, return TRUE if INIT is zero or some
10256 aggregate of zeros. Otherwise return FALSE. */
10257 bool
10258 initializer_zerop (const_tree init)
10260 tree elt;
10262 STRIP_NOPS (init);
10264 switch (TREE_CODE (init))
10266 case INTEGER_CST:
10267 return integer_zerop (init);
10269 case REAL_CST:
10270 /* ??? Note that this is not correct for C4X float formats. There,
10271 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
10272 negative exponent. */
10273 return real_zerop (init)
10274 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
10276 case FIXED_CST:
10277 return fixed_zerop (init);
10279 case COMPLEX_CST:
10280 return integer_zerop (init)
10281 || (real_zerop (init)
10282 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
10283 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
10285 case VECTOR_CST:
10287 unsigned i;
10288 for (i = 0; i < VECTOR_CST_NELTS (init); ++i)
10289 if (!initializer_zerop (VECTOR_CST_ELT (init, i)))
10290 return false;
10291 return true;
10294 case CONSTRUCTOR:
10296 unsigned HOST_WIDE_INT idx;
10298 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
10299 if (!initializer_zerop (elt))
10300 return false;
10301 return true;
10304 case STRING_CST:
10306 int i;
10308 /* We need to loop through all elements to handle cases like
10309 "\0" and "\0foobar". */
10310 for (i = 0; i < TREE_STRING_LENGTH (init); ++i)
10311 if (TREE_STRING_POINTER (init)[i] != '\0')
10312 return false;
10314 return true;
10317 default:
10318 return false;
10322 /* Check if vector VEC consists of all the equal elements and
10323 that the number of elements corresponds to the type of VEC.
10324 The function returns first element of the vector
10325 or NULL_TREE if the vector is not uniform. */
10326 tree
10327 uniform_vector_p (const_tree vec)
10329 tree first, t;
10330 unsigned i;
10332 if (vec == NULL_TREE)
10333 return NULL_TREE;
10335 gcc_assert (VECTOR_TYPE_P (TREE_TYPE (vec)));
10337 if (TREE_CODE (vec) == VECTOR_CST)
10339 first = VECTOR_CST_ELT (vec, 0);
10340 for (i = 1; i < VECTOR_CST_NELTS (vec); ++i)
10341 if (!operand_equal_p (first, VECTOR_CST_ELT (vec, i), 0))
10342 return NULL_TREE;
10344 return first;
10347 else if (TREE_CODE (vec) == CONSTRUCTOR)
10349 first = error_mark_node;
10351 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (vec), i, t)
10353 if (i == 0)
10355 first = t;
10356 continue;
10358 if (!operand_equal_p (first, t, 0))
10359 return NULL_TREE;
10361 if (i != TYPE_VECTOR_SUBPARTS (TREE_TYPE (vec)))
10362 return NULL_TREE;
10364 return first;
10367 return NULL_TREE;
10370 /* Build an empty statement at location LOC. */
10372 tree
10373 build_empty_stmt (location_t loc)
10375 tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
10376 SET_EXPR_LOCATION (t, loc);
10377 return t;
10381 /* Build an OpenMP clause with code CODE. LOC is the location of the
10382 clause. */
10384 tree
10385 build_omp_clause (location_t loc, enum omp_clause_code code)
10387 tree t;
10388 int size, length;
10390 length = omp_clause_num_ops[code];
10391 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
10393 record_node_allocation_statistics (OMP_CLAUSE, size);
10395 t = (tree) ggc_internal_alloc (size);
10396 memset (t, 0, size);
10397 TREE_SET_CODE (t, OMP_CLAUSE);
10398 OMP_CLAUSE_SET_CODE (t, code);
10399 OMP_CLAUSE_LOCATION (t) = loc;
10401 return t;
10404 /* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
10405 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
10406 Except for the CODE and operand count field, other storage for the
10407 object is initialized to zeros. */
10409 tree
10410 build_vl_exp_stat (enum tree_code code, int len MEM_STAT_DECL)
10412 tree t;
10413 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
10415 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
10416 gcc_assert (len >= 1);
10418 record_node_allocation_statistics (code, length);
10420 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
10422 TREE_SET_CODE (t, code);
10424 /* Can't use TREE_OPERAND to store the length because if checking is
10425 enabled, it will try to check the length before we store it. :-P */
10426 t->exp.operands[0] = build_int_cst (sizetype, len);
10428 return t;
10431 /* Helper function for build_call_* functions; build a CALL_EXPR with
10432 indicated RETURN_TYPE, FN, and NARGS, but do not initialize any of
10433 the argument slots. */
10435 static tree
10436 build_call_1 (tree return_type, tree fn, int nargs)
10438 tree t;
10440 t = build_vl_exp (CALL_EXPR, nargs + 3);
10441 TREE_TYPE (t) = return_type;
10442 CALL_EXPR_FN (t) = fn;
10443 CALL_EXPR_STATIC_CHAIN (t) = NULL;
10445 return t;
10448 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10449 FN and a null static chain slot. NARGS is the number of call arguments
10450 which are specified as "..." arguments. */
10452 tree
10453 build_call_nary (tree return_type, tree fn, int nargs, ...)
10455 tree ret;
10456 va_list args;
10457 va_start (args, nargs);
10458 ret = build_call_valist (return_type, fn, nargs, args);
10459 va_end (args);
10460 return ret;
10463 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10464 FN and a null static chain slot. NARGS is the number of call arguments
10465 which are specified as a va_list ARGS. */
10467 tree
10468 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
10470 tree t;
10471 int i;
10473 t = build_call_1 (return_type, fn, nargs);
10474 for (i = 0; i < nargs; i++)
10475 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
10476 process_call_operands (t);
10477 return t;
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 tree array ARGS. */
10484 tree
10485 build_call_array_loc (location_t loc, tree return_type, tree fn,
10486 int nargs, const tree *args)
10488 tree t;
10489 int i;
10491 t = build_call_1 (return_type, fn, nargs);
10492 for (i = 0; i < nargs; i++)
10493 CALL_EXPR_ARG (t, i) = args[i];
10494 process_call_operands (t);
10495 SET_EXPR_LOCATION (t, loc);
10496 return t;
10499 /* Like build_call_array, but takes a vec. */
10501 tree
10502 build_call_vec (tree return_type, tree fn, vec<tree, va_gc> *args)
10504 tree ret, t;
10505 unsigned int ix;
10507 ret = build_call_1 (return_type, fn, vec_safe_length (args));
10508 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
10509 CALL_EXPR_ARG (ret, ix) = t;
10510 process_call_operands (ret);
10511 return ret;
10514 /* Return true if T (assumed to be a DECL) must be assigned a memory
10515 location. */
10517 bool
10518 needs_to_live_in_memory (const_tree t)
10520 return (TREE_ADDRESSABLE (t)
10521 || is_global_var (t)
10522 || (TREE_CODE (t) == RESULT_DECL
10523 && !DECL_BY_REFERENCE (t)
10524 && aggregate_value_p (t, current_function_decl)));
10527 /* Return value of a constant X and sign-extend it. */
10529 HOST_WIDE_INT
10530 int_cst_value (const_tree x)
10532 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10533 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
10535 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10536 gcc_assert (cst_and_fits_in_hwi (x));
10538 if (bits < HOST_BITS_PER_WIDE_INT)
10540 bool negative = ((val >> (bits - 1)) & 1) != 0;
10541 if (negative)
10542 val |= (~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1;
10543 else
10544 val &= ~((~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1);
10547 return val;
10550 /* Return value of a constant X and sign-extend it. */
10552 HOST_WIDEST_INT
10553 widest_int_cst_value (const_tree x)
10555 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10556 unsigned HOST_WIDEST_INT val = TREE_INT_CST_LOW (x);
10558 #if HOST_BITS_PER_WIDEST_INT > HOST_BITS_PER_WIDE_INT
10559 gcc_assert (HOST_BITS_PER_WIDEST_INT >= HOST_BITS_PER_DOUBLE_INT);
10560 gcc_assert (TREE_INT_CST_NUNITS (x) == 2);
10562 if (TREE_INT_CST_NUNITS (x) == 1)
10563 val = HOST_WIDE_INT (val);
10564 else
10565 val |= (((unsigned HOST_WIDEST_INT) TREE_INT_CST_ELT (x, 1))
10566 << HOST_BITS_PER_WIDE_INT);
10567 #else
10568 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10569 gcc_assert (TREE_INT_CST_NUNITS (x) == 1);
10570 #endif
10572 if (bits < HOST_BITS_PER_WIDEST_INT)
10574 bool negative = ((val >> (bits - 1)) & 1) != 0;
10575 if (negative)
10576 val |= (~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1;
10577 else
10578 val &= ~((~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1);
10581 return val;
10584 /* If TYPE is an integral or pointer type, return an integer type with
10585 the same precision which is unsigned iff UNSIGNEDP is true, or itself
10586 if TYPE is already an integer type of signedness UNSIGNEDP. */
10588 tree
10589 signed_or_unsigned_type_for (int unsignedp, tree type)
10591 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type) == unsignedp)
10592 return type;
10594 if (TREE_CODE (type) == VECTOR_TYPE)
10596 tree inner = TREE_TYPE (type);
10597 tree inner2 = signed_or_unsigned_type_for (unsignedp, inner);
10598 if (!inner2)
10599 return NULL_TREE;
10600 if (inner == inner2)
10601 return type;
10602 return build_vector_type (inner2, TYPE_VECTOR_SUBPARTS (type));
10605 if (!INTEGRAL_TYPE_P (type)
10606 && !POINTER_TYPE_P (type)
10607 && TREE_CODE (type) != OFFSET_TYPE)
10608 return NULL_TREE;
10610 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
10613 /* If TYPE is an integral or pointer type, return an integer type with
10614 the same precision which is unsigned, or itself if TYPE is already an
10615 unsigned integer type. */
10617 tree
10618 unsigned_type_for (tree type)
10620 return signed_or_unsigned_type_for (1, type);
10623 /* If TYPE is an integral or pointer type, return an integer type with
10624 the same precision which is signed, or itself if TYPE is already a
10625 signed integer type. */
10627 tree
10628 signed_type_for (tree type)
10630 return signed_or_unsigned_type_for (0, type);
10633 /* If TYPE is a vector type, return a signed integer vector type with the
10634 same width and number of subparts. Otherwise return boolean_type_node. */
10636 tree
10637 truth_type_for (tree type)
10639 if (TREE_CODE (type) == VECTOR_TYPE)
10641 tree elem = lang_hooks.types.type_for_size
10642 (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))), 0);
10643 return build_opaque_vector_type (elem, TYPE_VECTOR_SUBPARTS (type));
10645 else
10646 return boolean_type_node;
10649 /* Returns the largest value obtainable by casting something in INNER type to
10650 OUTER type. */
10652 tree
10653 upper_bound_in_type (tree outer, tree inner)
10655 unsigned int det = 0;
10656 unsigned oprec = TYPE_PRECISION (outer);
10657 unsigned iprec = TYPE_PRECISION (inner);
10658 unsigned prec;
10660 /* Compute a unique number for every combination. */
10661 det |= (oprec > iprec) ? 4 : 0;
10662 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
10663 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
10665 /* Determine the exponent to use. */
10666 switch (det)
10668 case 0:
10669 case 1:
10670 /* oprec <= iprec, outer: signed, inner: don't care. */
10671 prec = oprec - 1;
10672 break;
10673 case 2:
10674 case 3:
10675 /* oprec <= iprec, outer: unsigned, inner: don't care. */
10676 prec = oprec;
10677 break;
10678 case 4:
10679 /* oprec > iprec, outer: signed, inner: signed. */
10680 prec = iprec - 1;
10681 break;
10682 case 5:
10683 /* oprec > iprec, outer: signed, inner: unsigned. */
10684 prec = iprec;
10685 break;
10686 case 6:
10687 /* oprec > iprec, outer: unsigned, inner: signed. */
10688 prec = oprec;
10689 break;
10690 case 7:
10691 /* oprec > iprec, outer: unsigned, inner: unsigned. */
10692 prec = iprec;
10693 break;
10694 default:
10695 gcc_unreachable ();
10698 return wide_int_to_tree (outer,
10699 wi::mask (prec, false, TYPE_PRECISION (outer)));
10702 /* Returns the smallest value obtainable by casting something in INNER type to
10703 OUTER type. */
10705 tree
10706 lower_bound_in_type (tree outer, tree inner)
10708 unsigned oprec = TYPE_PRECISION (outer);
10709 unsigned iprec = TYPE_PRECISION (inner);
10711 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
10712 and obtain 0. */
10713 if (TYPE_UNSIGNED (outer)
10714 /* If we are widening something of an unsigned type, OUTER type
10715 contains all values of INNER type. In particular, both INNER
10716 and OUTER types have zero in common. */
10717 || (oprec > iprec && TYPE_UNSIGNED (inner)))
10718 return build_int_cst (outer, 0);
10719 else
10721 /* If we are widening a signed type to another signed type, we
10722 want to obtain -2^^(iprec-1). If we are keeping the
10723 precision or narrowing to a signed type, we want to obtain
10724 -2^(oprec-1). */
10725 unsigned prec = oprec > iprec ? iprec : oprec;
10726 return wide_int_to_tree (outer,
10727 wi::mask (prec - 1, true,
10728 TYPE_PRECISION (outer)));
10732 /* Return nonzero if two operands that are suitable for PHI nodes are
10733 necessarily equal. Specifically, both ARG0 and ARG1 must be either
10734 SSA_NAME or invariant. Note that this is strictly an optimization.
10735 That is, callers of this function can directly call operand_equal_p
10736 and get the same result, only slower. */
10739 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
10741 if (arg0 == arg1)
10742 return 1;
10743 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
10744 return 0;
10745 return operand_equal_p (arg0, arg1, 0);
10748 /* Returns number of zeros at the end of binary representation of X. */
10750 tree
10751 num_ending_zeros (const_tree x)
10753 return build_int_cst (TREE_TYPE (x), wi::ctz (x));
10757 #define WALK_SUBTREE(NODE) \
10758 do \
10760 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
10761 if (result) \
10762 return result; \
10764 while (0)
10766 /* This is a subroutine of walk_tree that walks field of TYPE that are to
10767 be walked whenever a type is seen in the tree. Rest of operands and return
10768 value are as for walk_tree. */
10770 static tree
10771 walk_type_fields (tree type, walk_tree_fn func, void *data,
10772 struct pointer_set_t *pset, walk_tree_lh lh)
10774 tree result = NULL_TREE;
10776 switch (TREE_CODE (type))
10778 case POINTER_TYPE:
10779 case REFERENCE_TYPE:
10780 case VECTOR_TYPE:
10781 /* We have to worry about mutually recursive pointers. These can't
10782 be written in C. They can in Ada. It's pathological, but
10783 there's an ACATS test (c38102a) that checks it. Deal with this
10784 by checking if we're pointing to another pointer, that one
10785 points to another pointer, that one does too, and we have no htab.
10786 If so, get a hash table. We check three levels deep to avoid
10787 the cost of the hash table if we don't need one. */
10788 if (POINTER_TYPE_P (TREE_TYPE (type))
10789 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
10790 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
10791 && !pset)
10793 result = walk_tree_without_duplicates (&TREE_TYPE (type),
10794 func, data);
10795 if (result)
10796 return result;
10798 break;
10801 /* ... fall through ... */
10803 case COMPLEX_TYPE:
10804 WALK_SUBTREE (TREE_TYPE (type));
10805 break;
10807 case METHOD_TYPE:
10808 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
10810 /* Fall through. */
10812 case FUNCTION_TYPE:
10813 WALK_SUBTREE (TREE_TYPE (type));
10815 tree arg;
10817 /* We never want to walk into default arguments. */
10818 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
10819 WALK_SUBTREE (TREE_VALUE (arg));
10821 break;
10823 case ARRAY_TYPE:
10824 /* Don't follow this nodes's type if a pointer for fear that
10825 we'll have infinite recursion. If we have a PSET, then we
10826 need not fear. */
10827 if (pset
10828 || (!POINTER_TYPE_P (TREE_TYPE (type))
10829 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
10830 WALK_SUBTREE (TREE_TYPE (type));
10831 WALK_SUBTREE (TYPE_DOMAIN (type));
10832 break;
10834 case OFFSET_TYPE:
10835 WALK_SUBTREE (TREE_TYPE (type));
10836 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
10837 break;
10839 default:
10840 break;
10843 return NULL_TREE;
10846 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
10847 called with the DATA and the address of each sub-tree. If FUNC returns a
10848 non-NULL value, the traversal is stopped, and the value returned by FUNC
10849 is returned. If PSET is non-NULL it is used to record the nodes visited,
10850 and to avoid visiting a node more than once. */
10852 tree
10853 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
10854 struct pointer_set_t *pset, walk_tree_lh lh)
10856 enum tree_code code;
10857 int walk_subtrees;
10858 tree result;
10860 #define WALK_SUBTREE_TAIL(NODE) \
10861 do \
10863 tp = & (NODE); \
10864 goto tail_recurse; \
10866 while (0)
10868 tail_recurse:
10869 /* Skip empty subtrees. */
10870 if (!*tp)
10871 return NULL_TREE;
10873 /* Don't walk the same tree twice, if the user has requested
10874 that we avoid doing so. */
10875 if (pset && pointer_set_insert (pset, *tp))
10876 return NULL_TREE;
10878 /* Call the function. */
10879 walk_subtrees = 1;
10880 result = (*func) (tp, &walk_subtrees, data);
10882 /* If we found something, return it. */
10883 if (result)
10884 return result;
10886 code = TREE_CODE (*tp);
10888 /* Even if we didn't, FUNC may have decided that there was nothing
10889 interesting below this point in the tree. */
10890 if (!walk_subtrees)
10892 /* But we still need to check our siblings. */
10893 if (code == TREE_LIST)
10894 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
10895 else if (code == OMP_CLAUSE)
10896 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10897 else
10898 return NULL_TREE;
10901 if (lh)
10903 result = (*lh) (tp, &walk_subtrees, func, data, pset);
10904 if (result || !walk_subtrees)
10905 return result;
10908 switch (code)
10910 case ERROR_MARK:
10911 case IDENTIFIER_NODE:
10912 case INTEGER_CST:
10913 case REAL_CST:
10914 case FIXED_CST:
10915 case VECTOR_CST:
10916 case STRING_CST:
10917 case BLOCK:
10918 case PLACEHOLDER_EXPR:
10919 case SSA_NAME:
10920 case FIELD_DECL:
10921 case RESULT_DECL:
10922 /* None of these have subtrees other than those already walked
10923 above. */
10924 break;
10926 case TREE_LIST:
10927 WALK_SUBTREE (TREE_VALUE (*tp));
10928 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
10929 break;
10931 case TREE_VEC:
10933 int len = TREE_VEC_LENGTH (*tp);
10935 if (len == 0)
10936 break;
10938 /* Walk all elements but the first. */
10939 while (--len)
10940 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
10942 /* Now walk the first one as a tail call. */
10943 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
10946 case COMPLEX_CST:
10947 WALK_SUBTREE (TREE_REALPART (*tp));
10948 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
10950 case CONSTRUCTOR:
10952 unsigned HOST_WIDE_INT idx;
10953 constructor_elt *ce;
10955 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (*tp), idx, &ce);
10956 idx++)
10957 WALK_SUBTREE (ce->value);
10959 break;
10961 case SAVE_EXPR:
10962 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
10964 case BIND_EXPR:
10966 tree decl;
10967 for (decl = BIND_EXPR_VARS (*tp); decl; decl = DECL_CHAIN (decl))
10969 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
10970 into declarations that are just mentioned, rather than
10971 declared; they don't really belong to this part of the tree.
10972 And, we can see cycles: the initializer for a declaration
10973 can refer to the declaration itself. */
10974 WALK_SUBTREE (DECL_INITIAL (decl));
10975 WALK_SUBTREE (DECL_SIZE (decl));
10976 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
10978 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
10981 case STATEMENT_LIST:
10983 tree_stmt_iterator i;
10984 for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
10985 WALK_SUBTREE (*tsi_stmt_ptr (i));
10987 break;
10989 case OMP_CLAUSE:
10990 switch (OMP_CLAUSE_CODE (*tp))
10992 case OMP_CLAUSE_PRIVATE:
10993 case OMP_CLAUSE_SHARED:
10994 case OMP_CLAUSE_FIRSTPRIVATE:
10995 case OMP_CLAUSE_COPYIN:
10996 case OMP_CLAUSE_COPYPRIVATE:
10997 case OMP_CLAUSE_FINAL:
10998 case OMP_CLAUSE_IF:
10999 case OMP_CLAUSE_NUM_THREADS:
11000 case OMP_CLAUSE_SCHEDULE:
11001 case OMP_CLAUSE_UNIFORM:
11002 case OMP_CLAUSE_DEPEND:
11003 case OMP_CLAUSE_NUM_TEAMS:
11004 case OMP_CLAUSE_THREAD_LIMIT:
11005 case OMP_CLAUSE_DEVICE:
11006 case OMP_CLAUSE_DIST_SCHEDULE:
11007 case OMP_CLAUSE_SAFELEN:
11008 case OMP_CLAUSE_SIMDLEN:
11009 case OMP_CLAUSE__LOOPTEMP_:
11010 case OMP_CLAUSE__SIMDUID_:
11011 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0));
11012 /* FALLTHRU */
11014 case OMP_CLAUSE_NOWAIT:
11015 case OMP_CLAUSE_ORDERED:
11016 case OMP_CLAUSE_DEFAULT:
11017 case OMP_CLAUSE_UNTIED:
11018 case OMP_CLAUSE_MERGEABLE:
11019 case OMP_CLAUSE_PROC_BIND:
11020 case OMP_CLAUSE_INBRANCH:
11021 case OMP_CLAUSE_NOTINBRANCH:
11022 case OMP_CLAUSE_FOR:
11023 case OMP_CLAUSE_PARALLEL:
11024 case OMP_CLAUSE_SECTIONS:
11025 case OMP_CLAUSE_TASKGROUP:
11026 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11028 case OMP_CLAUSE_LASTPRIVATE:
11029 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11030 WALK_SUBTREE (OMP_CLAUSE_LASTPRIVATE_STMT (*tp));
11031 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11033 case OMP_CLAUSE_COLLAPSE:
11035 int i;
11036 for (i = 0; i < 3; i++)
11037 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
11038 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11041 case OMP_CLAUSE_LINEAR:
11042 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11043 WALK_SUBTREE (OMP_CLAUSE_LINEAR_STEP (*tp));
11044 WALK_SUBTREE (OMP_CLAUSE_LINEAR_STMT (*tp));
11045 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11047 case OMP_CLAUSE_ALIGNED:
11048 case OMP_CLAUSE_FROM:
11049 case OMP_CLAUSE_TO:
11050 case OMP_CLAUSE_MAP:
11051 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11052 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 1));
11053 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11055 case OMP_CLAUSE_REDUCTION:
11057 int i;
11058 for (i = 0; i < 4; i++)
11059 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
11060 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11063 default:
11064 gcc_unreachable ();
11066 break;
11068 case TARGET_EXPR:
11070 int i, len;
11072 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
11073 But, we only want to walk once. */
11074 len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
11075 for (i = 0; i < len; ++i)
11076 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11077 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
11080 case DECL_EXPR:
11081 /* If this is a TYPE_DECL, walk into the fields of the type that it's
11082 defining. We only want to walk into these fields of a type in this
11083 case and not in the general case of a mere reference to the type.
11085 The criterion is as follows: if the field can be an expression, it
11086 must be walked only here. This should be in keeping with the fields
11087 that are directly gimplified in gimplify_type_sizes in order for the
11088 mark/copy-if-shared/unmark machinery of the gimplifier to work with
11089 variable-sized types.
11091 Note that DECLs get walked as part of processing the BIND_EXPR. */
11092 if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
11094 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
11095 if (TREE_CODE (*type_p) == ERROR_MARK)
11096 return NULL_TREE;
11098 /* Call the function for the type. See if it returns anything or
11099 doesn't want us to continue. If we are to continue, walk both
11100 the normal fields and those for the declaration case. */
11101 result = (*func) (type_p, &walk_subtrees, data);
11102 if (result || !walk_subtrees)
11103 return result;
11105 /* But do not walk a pointed-to type since it may itself need to
11106 be walked in the declaration case if it isn't anonymous. */
11107 if (!POINTER_TYPE_P (*type_p))
11109 result = walk_type_fields (*type_p, func, data, pset, lh);
11110 if (result)
11111 return result;
11114 /* If this is a record type, also walk the fields. */
11115 if (RECORD_OR_UNION_TYPE_P (*type_p))
11117 tree field;
11119 for (field = TYPE_FIELDS (*type_p); field;
11120 field = DECL_CHAIN (field))
11122 /* We'd like to look at the type of the field, but we can
11123 easily get infinite recursion. So assume it's pointed
11124 to elsewhere in the tree. Also, ignore things that
11125 aren't fields. */
11126 if (TREE_CODE (field) != FIELD_DECL)
11127 continue;
11129 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
11130 WALK_SUBTREE (DECL_SIZE (field));
11131 WALK_SUBTREE (DECL_SIZE_UNIT (field));
11132 if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
11133 WALK_SUBTREE (DECL_QUALIFIER (field));
11137 /* Same for scalar types. */
11138 else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
11139 || TREE_CODE (*type_p) == ENUMERAL_TYPE
11140 || TREE_CODE (*type_p) == INTEGER_TYPE
11141 || TREE_CODE (*type_p) == FIXED_POINT_TYPE
11142 || TREE_CODE (*type_p) == REAL_TYPE)
11144 WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
11145 WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
11148 WALK_SUBTREE (TYPE_SIZE (*type_p));
11149 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
11151 /* FALLTHRU */
11153 default:
11154 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
11156 int i, len;
11158 /* Walk over all the sub-trees of this operand. */
11159 len = TREE_OPERAND_LENGTH (*tp);
11161 /* Go through the subtrees. We need to do this in forward order so
11162 that the scope of a FOR_EXPR is handled properly. */
11163 if (len)
11165 for (i = 0; i < len - 1; ++i)
11166 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11167 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
11170 /* If this is a type, walk the needed fields in the type. */
11171 else if (TYPE_P (*tp))
11172 return walk_type_fields (*tp, func, data, pset, lh);
11173 break;
11176 /* We didn't find what we were looking for. */
11177 return NULL_TREE;
11179 #undef WALK_SUBTREE_TAIL
11181 #undef WALK_SUBTREE
11183 /* Like walk_tree, but does not walk duplicate nodes more than once. */
11185 tree
11186 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
11187 walk_tree_lh lh)
11189 tree result;
11190 struct pointer_set_t *pset;
11192 pset = pointer_set_create ();
11193 result = walk_tree_1 (tp, func, data, pset, lh);
11194 pointer_set_destroy (pset);
11195 return result;
11199 tree
11200 tree_block (tree t)
11202 const enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
11204 if (IS_EXPR_CODE_CLASS (c))
11205 return LOCATION_BLOCK (t->exp.locus);
11206 gcc_unreachable ();
11207 return NULL;
11210 void
11211 tree_set_block (tree t, tree b)
11213 const enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
11215 if (IS_EXPR_CODE_CLASS (c))
11217 if (b)
11218 t->exp.locus = COMBINE_LOCATION_DATA (line_table, t->exp.locus, b);
11219 else
11220 t->exp.locus = LOCATION_LOCUS (t->exp.locus);
11222 else
11223 gcc_unreachable ();
11226 /* Create a nameless artificial label and put it in the current
11227 function context. The label has a location of LOC. Returns the
11228 newly created label. */
11230 tree
11231 create_artificial_label (location_t loc)
11233 tree lab = build_decl (loc,
11234 LABEL_DECL, NULL_TREE, void_type_node);
11236 DECL_ARTIFICIAL (lab) = 1;
11237 DECL_IGNORED_P (lab) = 1;
11238 DECL_CONTEXT (lab) = current_function_decl;
11239 return lab;
11242 /* Given a tree, try to return a useful variable name that we can use
11243 to prefix a temporary that is being assigned the value of the tree.
11244 I.E. given <temp> = &A, return A. */
11246 const char *
11247 get_name (tree t)
11249 tree stripped_decl;
11251 stripped_decl = t;
11252 STRIP_NOPS (stripped_decl);
11253 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
11254 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
11255 else if (TREE_CODE (stripped_decl) == SSA_NAME)
11257 tree name = SSA_NAME_IDENTIFIER (stripped_decl);
11258 if (!name)
11259 return NULL;
11260 return IDENTIFIER_POINTER (name);
11262 else
11264 switch (TREE_CODE (stripped_decl))
11266 case ADDR_EXPR:
11267 return get_name (TREE_OPERAND (stripped_decl, 0));
11268 default:
11269 return NULL;
11274 /* Return true if TYPE has a variable argument list. */
11276 bool
11277 stdarg_p (const_tree fntype)
11279 function_args_iterator args_iter;
11280 tree n = NULL_TREE, t;
11282 if (!fntype)
11283 return false;
11285 FOREACH_FUNCTION_ARGS (fntype, t, args_iter)
11287 n = t;
11290 return n != NULL_TREE && n != void_type_node;
11293 /* Return true if TYPE has a prototype. */
11295 bool
11296 prototype_p (tree fntype)
11298 tree t;
11300 gcc_assert (fntype != NULL_TREE);
11302 t = TYPE_ARG_TYPES (fntype);
11303 return (t != NULL_TREE);
11306 /* If BLOCK is inlined from an __attribute__((__artificial__))
11307 routine, return pointer to location from where it has been
11308 called. */
11309 location_t *
11310 block_nonartificial_location (tree block)
11312 location_t *ret = NULL;
11314 while (block && TREE_CODE (block) == BLOCK
11315 && BLOCK_ABSTRACT_ORIGIN (block))
11317 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
11319 while (TREE_CODE (ao) == BLOCK
11320 && BLOCK_ABSTRACT_ORIGIN (ao)
11321 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
11322 ao = BLOCK_ABSTRACT_ORIGIN (ao);
11324 if (TREE_CODE (ao) == FUNCTION_DECL)
11326 /* If AO is an artificial inline, point RET to the
11327 call site locus at which it has been inlined and continue
11328 the loop, in case AO's caller is also an artificial
11329 inline. */
11330 if (DECL_DECLARED_INLINE_P (ao)
11331 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
11332 ret = &BLOCK_SOURCE_LOCATION (block);
11333 else
11334 break;
11336 else if (TREE_CODE (ao) != BLOCK)
11337 break;
11339 block = BLOCK_SUPERCONTEXT (block);
11341 return ret;
11345 /* If EXP is inlined from an __attribute__((__artificial__))
11346 function, return the location of the original call expression. */
11348 location_t
11349 tree_nonartificial_location (tree exp)
11351 location_t *loc = block_nonartificial_location (TREE_BLOCK (exp));
11353 if (loc)
11354 return *loc;
11355 else
11356 return EXPR_LOCATION (exp);
11360 /* These are the hash table functions for the hash table of OPTIMIZATION_NODEq
11361 nodes. */
11363 /* Return the hash code code X, an OPTIMIZATION_NODE or TARGET_OPTION code. */
11365 static hashval_t
11366 cl_option_hash_hash (const void *x)
11368 const_tree const t = (const_tree) x;
11369 const char *p;
11370 size_t i;
11371 size_t len = 0;
11372 hashval_t hash = 0;
11374 if (TREE_CODE (t) == OPTIMIZATION_NODE)
11376 p = (const char *)TREE_OPTIMIZATION (t);
11377 len = sizeof (struct cl_optimization);
11380 else if (TREE_CODE (t) == TARGET_OPTION_NODE)
11382 p = (const char *)TREE_TARGET_OPTION (t);
11383 len = sizeof (struct cl_target_option);
11386 else
11387 gcc_unreachable ();
11389 /* assume most opt flags are just 0/1, some are 2-3, and a few might be
11390 something else. */
11391 for (i = 0; i < len; i++)
11392 if (p[i])
11393 hash = (hash << 4) ^ ((i << 2) | p[i]);
11395 return hash;
11398 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
11399 TARGET_OPTION tree node) is the same as that given by *Y, which is the
11400 same. */
11402 static int
11403 cl_option_hash_eq (const void *x, const void *y)
11405 const_tree const xt = (const_tree) x;
11406 const_tree const yt = (const_tree) y;
11407 const char *xp;
11408 const char *yp;
11409 size_t len;
11411 if (TREE_CODE (xt) != TREE_CODE (yt))
11412 return 0;
11414 if (TREE_CODE (xt) == OPTIMIZATION_NODE)
11416 xp = (const char *)TREE_OPTIMIZATION (xt);
11417 yp = (const char *)TREE_OPTIMIZATION (yt);
11418 len = sizeof (struct cl_optimization);
11421 else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
11423 xp = (const char *)TREE_TARGET_OPTION (xt);
11424 yp = (const char *)TREE_TARGET_OPTION (yt);
11425 len = sizeof (struct cl_target_option);
11428 else
11429 gcc_unreachable ();
11431 return (memcmp (xp, yp, len) == 0);
11434 /* Build an OPTIMIZATION_NODE based on the options in OPTS. */
11436 tree
11437 build_optimization_node (struct gcc_options *opts)
11439 tree t;
11440 void **slot;
11442 /* Use the cache of optimization nodes. */
11444 cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node),
11445 opts);
11447 slot = htab_find_slot (cl_option_hash_table, cl_optimization_node, INSERT);
11448 t = (tree) *slot;
11449 if (!t)
11451 /* Insert this one into the hash table. */
11452 t = cl_optimization_node;
11453 *slot = t;
11455 /* Make a new node for next time round. */
11456 cl_optimization_node = make_node (OPTIMIZATION_NODE);
11459 return t;
11462 /* Build a TARGET_OPTION_NODE based on the options in OPTS. */
11464 tree
11465 build_target_option_node (struct gcc_options *opts)
11467 tree t;
11468 void **slot;
11470 /* Use the cache of optimization nodes. */
11472 cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node),
11473 opts);
11475 slot = htab_find_slot (cl_option_hash_table, cl_target_option_node, INSERT);
11476 t = (tree) *slot;
11477 if (!t)
11479 /* Insert this one into the hash table. */
11480 t = cl_target_option_node;
11481 *slot = t;
11483 /* Make a new node for next time round. */
11484 cl_target_option_node = make_node (TARGET_OPTION_NODE);
11487 return t;
11490 /* Reset TREE_TARGET_GLOBALS cache for TARGET_OPTION_NODE.
11491 Called through htab_traverse. */
11493 static int
11494 prepare_target_option_node_for_pch (void **slot, void *)
11496 tree node = (tree) *slot;
11497 if (TREE_CODE (node) == TARGET_OPTION_NODE)
11498 TREE_TARGET_GLOBALS (node) = NULL;
11499 return 1;
11502 /* Clear TREE_TARGET_GLOBALS of all TARGET_OPTION_NODE trees,
11503 so that they aren't saved during PCH writing. */
11505 void
11506 prepare_target_option_nodes_for_pch (void)
11508 htab_traverse (cl_option_hash_table, prepare_target_option_node_for_pch,
11509 NULL);
11512 /* Determine the "ultimate origin" of a block. The block may be an inlined
11513 instance of an inlined instance of a block which is local to an inline
11514 function, so we have to trace all of the way back through the origin chain
11515 to find out what sort of node actually served as the original seed for the
11516 given block. */
11518 tree
11519 block_ultimate_origin (const_tree block)
11521 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
11523 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
11524 nodes in the function to point to themselves; ignore that if
11525 we're trying to output the abstract instance of this function. */
11526 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
11527 return NULL_TREE;
11529 if (immediate_origin == NULL_TREE)
11530 return NULL_TREE;
11531 else
11533 tree ret_val;
11534 tree lookahead = immediate_origin;
11538 ret_val = lookahead;
11539 lookahead = (TREE_CODE (ret_val) == BLOCK
11540 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
11542 while (lookahead != NULL && lookahead != ret_val);
11544 /* The block's abstract origin chain may not be the *ultimate* origin of
11545 the block. It could lead to a DECL that has an abstract origin set.
11546 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
11547 will give us if it has one). Note that DECL's abstract origins are
11548 supposed to be the most distant ancestor (or so decl_ultimate_origin
11549 claims), so we don't need to loop following the DECL origins. */
11550 if (DECL_P (ret_val))
11551 return DECL_ORIGIN (ret_val);
11553 return ret_val;
11557 /* Return true iff conversion in EXP generates no instruction. Mark
11558 it inline so that we fully inline into the stripping functions even
11559 though we have two uses of this function. */
11561 static inline bool
11562 tree_nop_conversion (const_tree exp)
11564 tree outer_type, inner_type;
11565 int outer_is_pts_p, inner_is_pts_p;
11567 if (!CONVERT_EXPR_P (exp)
11568 && TREE_CODE (exp) != NON_LVALUE_EXPR)
11569 return false;
11570 if (TREE_OPERAND (exp, 0) == error_mark_node)
11571 return false;
11573 outer_type = TREE_TYPE (exp);
11574 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11576 if (!inner_type)
11577 return false;
11579 outer_is_pts_p = (POINTER_TYPE_P (outer_type)
11580 && upc_shared_type_p (TREE_TYPE (outer_type)));
11581 inner_is_pts_p = (POINTER_TYPE_P (inner_type)
11582 && upc_shared_type_p (TREE_TYPE (inner_type)));
11584 /* UPC pointer-to-shared types have special
11585 equivalence rules that must be checked. */
11586 if (outer_is_pts_p && inner_is_pts_p
11587 && lang_hooks.types_compatible_p)
11588 return lang_hooks.types_compatible_p (outer_type, inner_type);
11590 /* UPC pointer-to-shared types are not interchangeable
11591 with integral types. */
11592 if (outer_is_pts_p || inner_is_pts_p)
11593 return false;
11595 /* Use precision rather then machine mode when we can, which gives
11596 the correct answer even for submode (bit-field) types. */
11597 if ((INTEGRAL_TYPE_P (outer_type)
11598 || POINTER_TYPE_P (outer_type)
11599 || TREE_CODE (outer_type) == OFFSET_TYPE)
11600 && (INTEGRAL_TYPE_P (inner_type)
11601 || POINTER_TYPE_P (inner_type)
11602 || TREE_CODE (inner_type) == OFFSET_TYPE))
11603 return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
11605 /* Otherwise fall back on comparing machine modes (e.g. for
11606 aggregate types, floats). */
11607 return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
11610 /* Return true iff conversion in EXP generates no instruction. Don't
11611 consider conversions changing the signedness. */
11613 static bool
11614 tree_sign_nop_conversion (const_tree exp)
11616 tree outer_type, inner_type;
11618 if (!tree_nop_conversion (exp))
11619 return false;
11621 outer_type = TREE_TYPE (exp);
11622 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11624 return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
11625 && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
11628 /* Strip conversions from EXP according to tree_nop_conversion and
11629 return the resulting expression. */
11631 tree
11632 tree_strip_nop_conversions (tree exp)
11634 while (tree_nop_conversion (exp))
11635 exp = TREE_OPERAND (exp, 0);
11636 return exp;
11639 /* Strip conversions from EXP according to tree_sign_nop_conversion
11640 and return the resulting expression. */
11642 tree
11643 tree_strip_sign_nop_conversions (tree exp)
11645 while (tree_sign_nop_conversion (exp))
11646 exp = TREE_OPERAND (exp, 0);
11647 return exp;
11650 /* Avoid any floating point extensions from EXP. */
11651 tree
11652 strip_float_extensions (tree exp)
11654 tree sub, expt, subt;
11656 /* For floating point constant look up the narrowest type that can hold
11657 it properly and handle it like (type)(narrowest_type)constant.
11658 This way we can optimize for instance a=a*2.0 where "a" is float
11659 but 2.0 is double constant. */
11660 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
11662 REAL_VALUE_TYPE orig;
11663 tree type = NULL;
11665 orig = TREE_REAL_CST (exp);
11666 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
11667 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
11668 type = float_type_node;
11669 else if (TYPE_PRECISION (TREE_TYPE (exp))
11670 > TYPE_PRECISION (double_type_node)
11671 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
11672 type = double_type_node;
11673 if (type)
11674 return build_real (type, real_value_truncate (TYPE_MODE (type), orig));
11677 if (!CONVERT_EXPR_P (exp))
11678 return exp;
11680 sub = TREE_OPERAND (exp, 0);
11681 subt = TREE_TYPE (sub);
11682 expt = TREE_TYPE (exp);
11684 if (!FLOAT_TYPE_P (subt))
11685 return exp;
11687 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
11688 return exp;
11690 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
11691 return exp;
11693 return strip_float_extensions (sub);
11696 /* Strip out all handled components that produce invariant
11697 offsets. */
11699 const_tree
11700 strip_invariant_refs (const_tree op)
11702 while (handled_component_p (op))
11704 switch (TREE_CODE (op))
11706 case ARRAY_REF:
11707 case ARRAY_RANGE_REF:
11708 if (!is_gimple_constant (TREE_OPERAND (op, 1))
11709 || TREE_OPERAND (op, 2) != NULL_TREE
11710 || TREE_OPERAND (op, 3) != NULL_TREE)
11711 return NULL;
11712 break;
11714 case COMPONENT_REF:
11715 if (TREE_OPERAND (op, 2) != NULL_TREE)
11716 return NULL;
11717 break;
11719 default:;
11721 op = TREE_OPERAND (op, 0);
11724 return op;
11727 static GTY(()) tree gcc_eh_personality_decl;
11729 /* Return the GCC personality function decl. */
11731 tree
11732 lhd_gcc_personality (void)
11734 if (!gcc_eh_personality_decl)
11735 gcc_eh_personality_decl = build_personality_function ("gcc");
11736 return gcc_eh_personality_decl;
11739 /* For languages with One Definition Rule, work out if
11740 trees are actually the same even if the tree representation
11741 differs. This handles only decls appearing in TYPE_NAME
11742 and TYPE_CONTEXT. That is NAMESPACE_DECL, TYPE_DECL,
11743 RECORD_TYPE and IDENTIFIER_NODE. */
11745 static bool
11746 same_for_odr (tree t1, tree t2)
11748 if (t1 == t2)
11749 return true;
11750 if (!t1 || !t2)
11751 return false;
11752 /* C and C++ FEs differ by using IDENTIFIER_NODE and TYPE_DECL. */
11753 if (TREE_CODE (t1) == IDENTIFIER_NODE
11754 && TREE_CODE (t2) == TYPE_DECL
11755 && DECL_FILE_SCOPE_P (t1))
11757 t2 = DECL_NAME (t2);
11758 gcc_assert (TREE_CODE (t2) == IDENTIFIER_NODE);
11760 if (TREE_CODE (t2) == IDENTIFIER_NODE
11761 && TREE_CODE (t1) == TYPE_DECL
11762 && DECL_FILE_SCOPE_P (t2))
11764 t1 = DECL_NAME (t1);
11765 gcc_assert (TREE_CODE (t1) == IDENTIFIER_NODE);
11767 if (TREE_CODE (t1) != TREE_CODE (t2))
11768 return false;
11769 if (TYPE_P (t1))
11770 return types_same_for_odr (t1, t2);
11771 if (DECL_P (t1))
11772 return decls_same_for_odr (t1, t2);
11773 return false;
11776 /* For languages with One Definition Rule, work out if
11777 decls are actually the same even if the tree representation
11778 differs. This handles only decls appearing in TYPE_NAME
11779 and TYPE_CONTEXT. That is NAMESPACE_DECL, TYPE_DECL,
11780 RECORD_TYPE and IDENTIFIER_NODE. */
11782 static bool
11783 decls_same_for_odr (tree decl1, tree decl2)
11785 if (decl1 && TREE_CODE (decl1) == TYPE_DECL
11786 && DECL_ORIGINAL_TYPE (decl1))
11787 decl1 = DECL_ORIGINAL_TYPE (decl1);
11788 if (decl2 && TREE_CODE (decl2) == TYPE_DECL
11789 && DECL_ORIGINAL_TYPE (decl2))
11790 decl2 = DECL_ORIGINAL_TYPE (decl2);
11791 if (decl1 == decl2)
11792 return true;
11793 if (!decl1 || !decl2)
11794 return false;
11795 gcc_checking_assert (DECL_P (decl1) && DECL_P (decl2));
11796 if (TREE_CODE (decl1) != TREE_CODE (decl2))
11797 return false;
11798 if (TREE_CODE (decl1) == TRANSLATION_UNIT_DECL)
11799 return true;
11800 if (TREE_CODE (decl1) != NAMESPACE_DECL
11801 && TREE_CODE (decl1) != TYPE_DECL)
11802 return false;
11803 if (!DECL_NAME (decl1))
11804 return false;
11805 gcc_checking_assert (TREE_CODE (DECL_NAME (decl1)) == IDENTIFIER_NODE);
11806 gcc_checking_assert (!DECL_NAME (decl2)
11807 || TREE_CODE (DECL_NAME (decl2)) == IDENTIFIER_NODE);
11808 if (DECL_NAME (decl1) != DECL_NAME (decl2))
11809 return false;
11810 return same_for_odr (DECL_CONTEXT (decl1),
11811 DECL_CONTEXT (decl2));
11814 /* For languages with One Definition Rule, work out if
11815 types are same even if the tree representation differs.
11816 This is non-trivial for LTO where minnor differences in
11817 the type representation may have prevented type merging
11818 to merge two copies of otherwise equivalent type. */
11820 bool
11821 types_same_for_odr (tree type1, tree type2)
11823 gcc_checking_assert (TYPE_P (type1) && TYPE_P (type2));
11824 type1 = TYPE_MAIN_VARIANT (type1);
11825 type2 = TYPE_MAIN_VARIANT (type2);
11826 if (type1 == type2)
11827 return true;
11829 #ifndef ENABLE_CHECKING
11830 if (!in_lto_p)
11831 return false;
11832 #endif
11834 /* Check for anonymous namespaces. Those have !TREE_PUBLIC
11835 on the corresponding TYPE_STUB_DECL. */
11836 if (type_in_anonymous_namespace_p (type1)
11837 || type_in_anonymous_namespace_p (type2))
11838 return false;
11839 /* When assembler name of virtual table is available, it is
11840 easy to compare types for equivalence. */
11841 if (TYPE_BINFO (type1) && TYPE_BINFO (type2)
11842 && BINFO_VTABLE (TYPE_BINFO (type1))
11843 && BINFO_VTABLE (TYPE_BINFO (type2)))
11845 tree v1 = BINFO_VTABLE (TYPE_BINFO (type1));
11846 tree v2 = BINFO_VTABLE (TYPE_BINFO (type2));
11848 if (TREE_CODE (v1) == POINTER_PLUS_EXPR)
11850 if (TREE_CODE (v2) != POINTER_PLUS_EXPR
11851 || !operand_equal_p (TREE_OPERAND (v1, 1),
11852 TREE_OPERAND (v2, 1), 0))
11853 return false;
11854 v1 = TREE_OPERAND (TREE_OPERAND (v1, 0), 0);
11855 v2 = TREE_OPERAND (TREE_OPERAND (v2, 0), 0);
11857 v1 = DECL_ASSEMBLER_NAME (v1);
11858 v2 = DECL_ASSEMBLER_NAME (v2);
11859 return (v1 == v2);
11862 /* FIXME: the code comparing type names consider all instantiations of the
11863 same template to have same name. This is because we have no access
11864 to template parameters. For types with no virtual method tables
11865 we thus can return false positives. At the moment we do not need
11866 to compare types in other scenarios than devirtualization. */
11868 /* If types are not structuraly same, do not bother to contnue.
11869 Match in the remainder of code would mean ODR violation. */
11870 if (!types_compatible_p (type1, type2))
11871 return false;
11872 if (!TYPE_NAME (type1))
11873 return false;
11874 if (!decls_same_for_odr (TYPE_NAME (type1), TYPE_NAME (type2)))
11875 return false;
11876 if (!same_for_odr (TYPE_CONTEXT (type1), TYPE_CONTEXT (type2)))
11877 return false;
11878 /* When not in LTO the MAIN_VARIANT check should be the same. */
11879 gcc_assert (in_lto_p);
11881 return true;
11884 /* TARGET is a call target of GIMPLE call statement
11885 (obtained by gimple_call_fn). Return true if it is
11886 OBJ_TYPE_REF representing an virtual call of C++ method.
11887 (As opposed to OBJ_TYPE_REF representing objc calls
11888 through a cast where middle-end devirtualization machinery
11889 can't apply.) */
11891 bool
11892 virtual_method_call_p (tree target)
11894 if (TREE_CODE (target) != OBJ_TYPE_REF)
11895 return false;
11896 target = TREE_TYPE (target);
11897 gcc_checking_assert (TREE_CODE (target) == POINTER_TYPE);
11898 target = TREE_TYPE (target);
11899 if (TREE_CODE (target) == FUNCTION_TYPE)
11900 return false;
11901 gcc_checking_assert (TREE_CODE (target) == METHOD_TYPE);
11902 return true;
11905 /* REF is OBJ_TYPE_REF, return the class the ref corresponds to. */
11907 tree
11908 obj_type_ref_class (tree ref)
11910 gcc_checking_assert (TREE_CODE (ref) == OBJ_TYPE_REF);
11911 ref = TREE_TYPE (ref);
11912 gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
11913 ref = TREE_TYPE (ref);
11914 /* We look for type THIS points to. ObjC also builds
11915 OBJ_TYPE_REF with non-method calls, Their first parameter
11916 ID however also corresponds to class type. */
11917 gcc_checking_assert (TREE_CODE (ref) == METHOD_TYPE
11918 || TREE_CODE (ref) == FUNCTION_TYPE);
11919 ref = TREE_VALUE (TYPE_ARG_TYPES (ref));
11920 gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
11921 return TREE_TYPE (ref);
11924 /* Return true if T is in anonymous namespace. */
11926 bool
11927 type_in_anonymous_namespace_p (tree t)
11929 return (TYPE_STUB_DECL (t) && !TREE_PUBLIC (TYPE_STUB_DECL (t)));
11932 /* Try to find a base info of BINFO that would have its field decl at offset
11933 OFFSET within the BINFO type and which is of EXPECTED_TYPE. If it can be
11934 found, return, otherwise return NULL_TREE. */
11936 tree
11937 get_binfo_at_offset (tree binfo, HOST_WIDE_INT offset, tree expected_type)
11939 tree type = BINFO_TYPE (binfo);
11941 while (true)
11943 HOST_WIDE_INT pos, size;
11944 tree fld;
11945 int i;
11947 if (types_same_for_odr (type, expected_type))
11948 return binfo;
11949 if (offset < 0)
11950 return NULL_TREE;
11952 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
11954 if (TREE_CODE (fld) != FIELD_DECL)
11955 continue;
11957 pos = int_bit_position (fld);
11958 size = tree_to_uhwi (DECL_SIZE (fld));
11959 if (pos <= offset && (pos + size) > offset)
11960 break;
11962 if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE)
11963 return NULL_TREE;
11965 if (!DECL_ARTIFICIAL (fld))
11967 binfo = TYPE_BINFO (TREE_TYPE (fld));
11968 if (!binfo)
11969 return NULL_TREE;
11971 /* Offset 0 indicates the primary base, whose vtable contents are
11972 represented in the binfo for the derived class. */
11973 else if (offset != 0)
11975 tree base_binfo, binfo2 = binfo;
11977 /* Find BINFO corresponding to FLD. This is bit harder
11978 by a fact that in virtual inheritance we may need to walk down
11979 the non-virtual inheritance chain. */
11980 while (true)
11982 tree containing_binfo = NULL, found_binfo = NULL;
11983 for (i = 0; BINFO_BASE_ITERATE (binfo2, i, base_binfo); i++)
11984 if (types_same_for_odr (TREE_TYPE (base_binfo), TREE_TYPE (fld)))
11986 found_binfo = base_binfo;
11987 break;
11989 else
11990 if ((tree_to_shwi (BINFO_OFFSET (base_binfo))
11991 - tree_to_shwi (BINFO_OFFSET (binfo)))
11992 * BITS_PER_UNIT < pos
11993 /* Rule out types with no virtual methods or we can get confused
11994 here by zero sized bases. */
11995 && BINFO_VTABLE (TYPE_BINFO (BINFO_TYPE (base_binfo)))
11996 && (!containing_binfo
11997 || (tree_to_shwi (BINFO_OFFSET (containing_binfo))
11998 < tree_to_shwi (BINFO_OFFSET (base_binfo)))))
11999 containing_binfo = base_binfo;
12000 if (found_binfo)
12002 binfo = found_binfo;
12003 break;
12005 if (!containing_binfo)
12006 return NULL_TREE;
12007 binfo2 = containing_binfo;
12011 type = TREE_TYPE (fld);
12012 offset -= pos;
12016 /* Returns true if X is a typedef decl. */
12018 bool
12019 is_typedef_decl (tree x)
12021 return (x && TREE_CODE (x) == TYPE_DECL
12022 && DECL_ORIGINAL_TYPE (x) != NULL_TREE);
12025 /* Returns true iff TYPE is a type variant created for a typedef. */
12027 bool
12028 typedef_variant_p (tree type)
12030 return is_typedef_decl (TYPE_NAME (type));
12033 /* Warn about a use of an identifier which was marked deprecated. */
12034 void
12035 warn_deprecated_use (tree node, tree attr)
12037 const char *msg;
12039 if (node == 0 || !warn_deprecated_decl)
12040 return;
12042 if (!attr)
12044 if (DECL_P (node))
12045 attr = DECL_ATTRIBUTES (node);
12046 else if (TYPE_P (node))
12048 tree decl = TYPE_STUB_DECL (node);
12049 if (decl)
12050 attr = lookup_attribute ("deprecated",
12051 TYPE_ATTRIBUTES (TREE_TYPE (decl)));
12055 if (attr)
12056 attr = lookup_attribute ("deprecated", attr);
12058 if (attr)
12059 msg = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr)));
12060 else
12061 msg = NULL;
12063 if (DECL_P (node))
12065 expanded_location xloc = expand_location (DECL_SOURCE_LOCATION (node));
12066 if (msg)
12067 warning (OPT_Wdeprecated_declarations,
12068 "%qD is deprecated (declared at %r%s:%d%R): %s",
12069 node, "locus", xloc.file, xloc.line, msg);
12070 else
12071 warning (OPT_Wdeprecated_declarations,
12072 "%qD is deprecated (declared at %r%s:%d%R)",
12073 node, "locus", xloc.file, xloc.line);
12075 else if (TYPE_P (node))
12077 tree what = NULL_TREE;
12078 tree decl = TYPE_STUB_DECL (node);
12080 if (TYPE_NAME (node))
12082 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
12083 what = TYPE_NAME (node);
12084 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
12085 && DECL_NAME (TYPE_NAME (node)))
12086 what = DECL_NAME (TYPE_NAME (node));
12089 if (decl)
12091 expanded_location xloc
12092 = expand_location (DECL_SOURCE_LOCATION (decl));
12093 if (what)
12095 if (msg)
12096 warning (OPT_Wdeprecated_declarations,
12097 "%qE is deprecated (declared at %r%s:%d%R): %s",
12098 what, "locus", xloc.file, xloc.line, msg);
12099 else
12100 warning (OPT_Wdeprecated_declarations,
12101 "%qE is deprecated (declared at %r%s:%d%R)",
12102 what, "locus", xloc.file, xloc.line);
12104 else
12106 if (msg)
12107 warning (OPT_Wdeprecated_declarations,
12108 "type is deprecated (declared at %r%s:%d%R): %s",
12109 "locus", xloc.file, xloc.line, msg);
12110 else
12111 warning (OPT_Wdeprecated_declarations,
12112 "type is deprecated (declared at %r%s:%d%R)",
12113 "locus", xloc.file, xloc.line);
12116 else
12118 if (what)
12120 if (msg)
12121 warning (OPT_Wdeprecated_declarations, "%qE is deprecated: %s",
12122 what, msg);
12123 else
12124 warning (OPT_Wdeprecated_declarations, "%qE is deprecated", what);
12126 else
12128 if (msg)
12129 warning (OPT_Wdeprecated_declarations, "type is deprecated: %s",
12130 msg);
12131 else
12132 warning (OPT_Wdeprecated_declarations, "type is deprecated");
12138 /* Return true if REF has a COMPONENT_REF with a bit-field field declaration
12139 somewhere in it. */
12141 bool
12142 contains_bitfld_component_ref_p (const_tree ref)
12144 while (handled_component_p (ref))
12146 if (TREE_CODE (ref) == COMPONENT_REF
12147 && DECL_BIT_FIELD (TREE_OPERAND (ref, 1)))
12148 return true;
12149 ref = TREE_OPERAND (ref, 0);
12152 return false;
12155 /* Try to determine whether a TRY_CATCH expression can fall through.
12156 This is a subroutine of block_may_fallthru. */
12158 static bool
12159 try_catch_may_fallthru (const_tree stmt)
12161 tree_stmt_iterator i;
12163 /* If the TRY block can fall through, the whole TRY_CATCH can
12164 fall through. */
12165 if (block_may_fallthru (TREE_OPERAND (stmt, 0)))
12166 return true;
12168 i = tsi_start (TREE_OPERAND (stmt, 1));
12169 switch (TREE_CODE (tsi_stmt (i)))
12171 case CATCH_EXPR:
12172 /* We expect to see a sequence of CATCH_EXPR trees, each with a
12173 catch expression and a body. The whole TRY_CATCH may fall
12174 through iff any of the catch bodies falls through. */
12175 for (; !tsi_end_p (i); tsi_next (&i))
12177 if (block_may_fallthru (CATCH_BODY (tsi_stmt (i))))
12178 return true;
12180 return false;
12182 case EH_FILTER_EXPR:
12183 /* The exception filter expression only matters if there is an
12184 exception. If the exception does not match EH_FILTER_TYPES,
12185 we will execute EH_FILTER_FAILURE, and we will fall through
12186 if that falls through. If the exception does match
12187 EH_FILTER_TYPES, the stack unwinder will continue up the
12188 stack, so we will not fall through. We don't know whether we
12189 will throw an exception which matches EH_FILTER_TYPES or not,
12190 so we just ignore EH_FILTER_TYPES and assume that we might
12191 throw an exception which doesn't match. */
12192 return block_may_fallthru (EH_FILTER_FAILURE (tsi_stmt (i)));
12194 default:
12195 /* This case represents statements to be executed when an
12196 exception occurs. Those statements are implicitly followed
12197 by a RESX statement to resume execution after the exception.
12198 So in this case the TRY_CATCH never falls through. */
12199 return false;
12203 /* Try to determine if we can fall out of the bottom of BLOCK. This guess
12204 need not be 100% accurate; simply be conservative and return true if we
12205 don't know. This is used only to avoid stupidly generating extra code.
12206 If we're wrong, we'll just delete the extra code later. */
12208 bool
12209 block_may_fallthru (const_tree block)
12211 /* This CONST_CAST is okay because expr_last returns its argument
12212 unmodified and we assign it to a const_tree. */
12213 const_tree stmt = expr_last (CONST_CAST_TREE (block));
12215 switch (stmt ? TREE_CODE (stmt) : ERROR_MARK)
12217 case GOTO_EXPR:
12218 case RETURN_EXPR:
12219 /* Easy cases. If the last statement of the block implies
12220 control transfer, then we can't fall through. */
12221 return false;
12223 case SWITCH_EXPR:
12224 /* If SWITCH_LABELS is set, this is lowered, and represents a
12225 branch to a selected label and hence can not fall through.
12226 Otherwise SWITCH_BODY is set, and the switch can fall
12227 through. */
12228 return SWITCH_LABELS (stmt) == NULL_TREE;
12230 case COND_EXPR:
12231 if (block_may_fallthru (COND_EXPR_THEN (stmt)))
12232 return true;
12233 return block_may_fallthru (COND_EXPR_ELSE (stmt));
12235 case BIND_EXPR:
12236 return block_may_fallthru (BIND_EXPR_BODY (stmt));
12238 case TRY_CATCH_EXPR:
12239 return try_catch_may_fallthru (stmt);
12241 case TRY_FINALLY_EXPR:
12242 /* The finally clause is always executed after the try clause,
12243 so if it does not fall through, then the try-finally will not
12244 fall through. Otherwise, if the try clause does not fall
12245 through, then when the finally clause falls through it will
12246 resume execution wherever the try clause was going. So the
12247 whole try-finally will only fall through if both the try
12248 clause and the finally clause fall through. */
12249 return (block_may_fallthru (TREE_OPERAND (stmt, 0))
12250 && block_may_fallthru (TREE_OPERAND (stmt, 1)));
12252 case MODIFY_EXPR:
12253 if (TREE_CODE (TREE_OPERAND (stmt, 1)) == CALL_EXPR)
12254 stmt = TREE_OPERAND (stmt, 1);
12255 else
12256 return true;
12257 /* FALLTHRU */
12259 case CALL_EXPR:
12260 /* Functions that do not return do not fall through. */
12261 return (call_expr_flags (stmt) & ECF_NORETURN) == 0;
12263 case CLEANUP_POINT_EXPR:
12264 return block_may_fallthru (TREE_OPERAND (stmt, 0));
12266 case TARGET_EXPR:
12267 return block_may_fallthru (TREE_OPERAND (stmt, 1));
12269 case ERROR_MARK:
12270 return true;
12272 default:
12273 return lang_hooks.block_may_fallthru (stmt);
12277 /* Garbage collection support for tree_type_common. */
12279 extern void gt_ggc_mx (tree&);
12280 extern void gt_ggc_mx_die_struct (void *);
12282 void gt_ggc_mx (tree_type_common *tt)
12284 tree t = (tree) tt;
12285 tree block_factor = TYPE_UPC_BLOCK_FACTOR (t);
12287 gt_ggc_mx (tt->common.typed.type);
12288 gt_ggc_mx (tt->common.chain);
12289 gt_ggc_mx (tt->size);
12290 gt_ggc_mx (tt->size_unit);
12291 gt_ggc_mx (tt->attributes);
12292 gt_ggc_mx (tt->pointer_to);
12293 gt_ggc_mx (tt->reference_to);
12294 switch (debug_hooks->tree_type_symtab_field)
12296 case TYPE_SYMTAB_IS_ADDRESS:
12297 break;
12298 case TYPE_SYMTAB_IS_POINTER:
12299 gt_ggc_m_S (tt->symtab.pointer);
12300 break;
12301 case TYPE_SYMTAB_IS_DIE:
12302 gt_ggc_mx_die_struct (tt->symtab.die);
12303 break;
12304 default:
12305 break;
12307 gt_ggc_mx (tt->name);
12308 gt_ggc_mx (tt->next_variant);
12309 gt_ggc_mx (tt->main_variant);
12310 gt_ggc_mx (tt->context);
12311 gt_ggc_mx (tt->canonical);
12313 if (TYPE_HAS_UPC_BLOCK_FACTOR_X (t))
12314 gt_ggc_mx (block_factor);
12317 /* PCH support for tree_type_common. */
12319 extern void gt_pch_nx (tree&);
12320 extern void gt_ggc_nx_die_struct (void *);
12322 void gt_pch_nx (tree_type_common *tt)
12324 tree t = (tree) tt;
12325 tree block_factor = TYPE_UPC_BLOCK_FACTOR (t);
12327 gt_pch_nx (tt->common.typed.type);
12328 gt_pch_nx (tt->common.chain);
12329 gt_pch_nx (tt->size);
12330 gt_pch_nx (tt->size_unit);
12331 gt_pch_nx (tt->attributes);
12332 gt_pch_nx (tt->pointer_to);
12333 gt_pch_nx (tt->reference_to);
12334 switch (debug_hooks->tree_type_symtab_field)
12336 case TYPE_SYMTAB_IS_ADDRESS:
12337 break;
12338 case TYPE_SYMTAB_IS_POINTER:
12339 gt_pch_n_S (tt->symtab.pointer);
12340 break;
12341 case TYPE_SYMTAB_IS_DIE:
12342 gt_pch_nx_die_struct (tt->symtab.die);
12343 break;
12344 default:
12345 break;
12347 gt_pch_nx (tt->name);
12348 gt_pch_nx (tt->next_variant);
12349 gt_pch_nx (tt->main_variant);
12350 gt_pch_nx (tt->context);
12351 gt_pch_nx (tt->canonical);
12353 if (TYPE_HAS_UPC_BLOCK_FACTOR_X (t))
12354 gt_pch_nx (block_factor);
12357 void gt_pch_nx (tree_type_common *tt, gt_pointer_operator op, void *cookie)
12359 tree t = (tree) tt;
12360 tree block_factor = TYPE_UPC_BLOCK_FACTOR (t);
12362 op (&(tt->common.typed.type), cookie);
12363 op (&(tt->common.chain), cookie);
12364 op (&(tt->size), cookie);
12365 op (&(tt->size_unit), cookie);
12366 op (&(tt->attributes), cookie);
12367 op (&(tt->pointer_to), cookie);
12368 op (&(tt->reference_to), cookie);
12369 switch (debug_hooks->tree_type_symtab_field)
12371 case TYPE_SYMTAB_IS_ADDRESS:
12372 break;
12373 case TYPE_SYMTAB_IS_POINTER:
12374 op (&(tt->symtab.pointer), cookie);
12375 break;
12376 case TYPE_SYMTAB_IS_DIE:
12377 op (&(tt->symtab.die), cookie);
12378 break;
12379 default:
12380 break;
12382 op (&(tt->name), cookie);
12383 op (&(tt->next_variant), cookie);
12384 op (&(tt->main_variant), cookie);
12385 op (&(tt->context), cookie);
12386 op (&(tt->canonical), cookie);
12388 if (TYPE_HAS_UPC_BLOCK_FACTOR_X (t))
12389 op (&(block_factor), cookie);
12392 /* True if we are using EH to handle cleanups. */
12393 static bool using_eh_for_cleanups_flag = false;
12395 /* This routine is called from front ends to indicate eh should be used for
12396 cleanups. */
12397 void
12398 using_eh_for_cleanups (void)
12400 using_eh_for_cleanups_flag = true;
12403 /* Query whether EH is used for cleanups. */
12404 bool
12405 using_eh_for_cleanups_p (void)
12407 return using_eh_for_cleanups_flag;
12410 /* Wrapper for tree_code_name to ensure that tree code is valid */
12411 const char *
12412 get_tree_code_name (enum tree_code code)
12414 const char *invalid = "<invalid tree code>";
12416 if (code >= MAX_TREE_CODES)
12417 return invalid;
12419 return tree_code_name[code];
12422 /* Drops the TREE_OVERFLOW flag from T. */
12424 tree
12425 drop_tree_overflow (tree t)
12427 gcc_checking_assert (TREE_OVERFLOW (t));
12429 /* For tree codes with a sharing machinery re-build the result. */
12430 if (TREE_CODE (t) == INTEGER_CST)
12431 return wide_int_to_tree (TREE_TYPE (t), t);
12433 /* Otherwise, as all tcc_constants are possibly shared, copy the node
12434 and drop the flag. */
12435 t = copy_node (t);
12436 TREE_OVERFLOW (t) = 0;
12437 return t;
12440 /* Given a memory reference expression T, return its base address.
12441 The base address of a memory reference expression is the main
12442 object being referenced. For instance, the base address for
12443 'array[i].fld[j]' is 'array'. You can think of this as stripping
12444 away the offset part from a memory address.
12446 This function calls handled_component_p to strip away all the inner
12447 parts of the memory reference until it reaches the base object. */
12449 tree
12450 get_base_address (tree t)
12452 while (handled_component_p (t))
12453 t = TREE_OPERAND (t, 0);
12455 if ((TREE_CODE (t) == MEM_REF
12456 || TREE_CODE (t) == TARGET_MEM_REF)
12457 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR)
12458 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
12460 /* ??? Either the alias oracle or all callers need to properly deal
12461 with WITH_SIZE_EXPRs before we can look through those. */
12462 if (TREE_CODE (t) == WITH_SIZE_EXPR)
12463 return NULL_TREE;
12465 return t;
12468 #include "gt-tree.h"