Merge trunk version 206792 into gupc branch.
[official-gcc.git] / gcc / tree.c
blobb76549c20f8ae2b6a655eb2fa635094c0dd1dfd7
1 /* Language-independent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This file contains the low level primitives for operating on tree nodes,
21 including allocation, list operations, interning of identifiers,
22 construction of data type nodes and statement nodes,
23 and construction of type conversion nodes. It also contains
24 tables index by tree code that describe how to take apart
25 nodes of that code.
27 It is intended to be language-independent, but occasionally
28 calls language-dependent routines defined (for C) in typecheck.c. */
30 #include "config.h"
31 #include "system.h"
32 #include "coretypes.h"
33 #include "tm.h"
34 #include "flags.h"
35 #include "tree.h"
36 #include "stor-layout.h"
37 #include "calls.h"
38 #include "attribs.h"
39 #include "varasm.h"
40 #include "tm_p.h"
41 #include "function.h"
42 #include "obstack.h"
43 #include "toplev.h" /* get_random_seed */
44 #include "hashtab.h"
45 #include "filenames.h"
46 #include "output.h"
47 #include "target.h"
48 #include "common/common-target.h"
49 #include "langhooks.h"
50 #include "tree-inline.h"
51 #include "tree-iterator.h"
52 #include "basic-block.h"
53 #include "bitmap.h"
54 #include "pointer-set.h"
55 #include "tree-ssa-alias.h"
56 #include "internal-fn.h"
57 #include "gimple-expr.h"
58 #include "is-a.h"
59 #include "gimple.h"
60 #include "gimple-iterator.h"
61 #include "gimplify.h"
62 #include "gimple-ssa.h"
63 #include "cgraph.h"
64 #include "tree-phinodes.h"
65 #include "stringpool.h"
66 #include "tree-ssanames.h"
67 #include "expr.h"
68 #include "tree-dfa.h"
69 #include "params.h"
70 #include "tree-pass.h"
71 #include "langhooks-def.h"
72 #include "diagnostic.h"
73 #include "tree-diagnostic.h"
74 #include "tree-pretty-print.h"
75 #include "except.h"
76 #include "debug.h"
77 #include "intl.h"
79 /* Tree code classes. */
81 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
82 #define END_OF_BASE_TREE_CODES tcc_exceptional,
84 const enum tree_code_class tree_code_type[] = {
85 #include "all-tree.def"
88 #undef DEFTREECODE
89 #undef END_OF_BASE_TREE_CODES
91 /* Table indexed by tree code giving number of expression
92 operands beyond the fixed part of the node structure.
93 Not used for types or decls. */
95 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
96 #define END_OF_BASE_TREE_CODES 0,
98 const unsigned char tree_code_length[] = {
99 #include "all-tree.def"
102 #undef DEFTREECODE
103 #undef END_OF_BASE_TREE_CODES
105 /* Names of tree components.
106 Used for printing out the tree and error messages. */
107 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
108 #define END_OF_BASE_TREE_CODES "@dummy",
110 static const char *const tree_code_name[] = {
111 #include "all-tree.def"
114 #undef DEFTREECODE
115 #undef END_OF_BASE_TREE_CODES
117 /* Each tree code class has an associated string representation.
118 These must correspond to the tree_code_class entries. */
120 const char *const tree_code_class_strings[] =
122 "exceptional",
123 "constant",
124 "type",
125 "declaration",
126 "reference",
127 "comparison",
128 "unary",
129 "binary",
130 "statement",
131 "vl_exp",
132 "expression"
135 /* obstack.[ch] explicitly declined to prototype this. */
136 extern int _obstack_allocated_p (struct obstack *h, void *obj);
138 /* Statistics-gathering stuff. */
140 static int tree_code_counts[MAX_TREE_CODES];
141 int tree_node_counts[(int) all_kinds];
142 int tree_node_sizes[(int) all_kinds];
144 /* Keep in sync with tree.h:enum tree_node_kind. */
145 static const char * const tree_node_kind_names[] = {
146 "decls",
147 "types",
148 "blocks",
149 "stmts",
150 "refs",
151 "exprs",
152 "constants",
153 "identifiers",
154 "vecs",
155 "binfos",
156 "ssa names",
157 "constructors",
158 "random kinds",
159 "lang_decl kinds",
160 "lang_type kinds",
161 "omp clauses",
164 /* Unique id for next decl created. */
165 static GTY(()) int next_decl_uid;
166 /* Unique id for next type created. */
167 static GTY(()) int next_type_uid = 1;
168 /* Unique id for next debug decl created. Use negative numbers,
169 to catch erroneous uses. */
170 static GTY(()) int next_debug_decl_uid;
172 /* Since we cannot rehash a type after it is in the table, we have to
173 keep the hash code. */
175 struct GTY(()) type_hash {
176 unsigned long hash;
177 tree type;
180 /* Initial size of the hash table (rounded to next prime). */
181 #define TYPE_HASH_INITIAL_SIZE 1000
183 /* Now here is the hash table. When recording a type, it is added to
184 the slot whose index is the hash code. Note that the hash table is
185 used for several kinds of types (function types, array types and
186 array index range types, for now). While all these live in the
187 same table, they are completely independent, and the hash code is
188 computed differently for each of these. */
190 static GTY ((if_marked ("type_hash_marked_p"), param_is (struct type_hash)))
191 htab_t type_hash_table;
193 /* Hash table for UPC block factor lookups when the block factor
194 is not 0 (the indefinite block factor) or 1 (the default). */
195 static GTY ((if_marked ("tree_map_marked_p"),
196 param_is (struct tree_map)))
197 htab_t block_factor_for_type;
199 /* Hash table and temporary node for larger integer const values. */
200 static GTY (()) tree int_cst_node;
201 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node)))
202 htab_t int_cst_hash_table;
204 /* Hash table for optimization flags and target option flags. Use the same
205 hash table for both sets of options. Nodes for building the current
206 optimization and target option nodes. The assumption is most of the time
207 the options created will already be in the hash table, so we avoid
208 allocating and freeing up a node repeatably. */
209 static GTY (()) tree cl_optimization_node;
210 static GTY (()) tree cl_target_option_node;
211 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node)))
212 htab_t cl_option_hash_table;
214 /* General tree->tree mapping structure for use in hash tables. */
217 static GTY ((if_marked ("tree_decl_map_marked_p"), param_is (struct tree_decl_map)))
218 htab_t debug_expr_for_decl;
220 static GTY ((if_marked ("tree_decl_map_marked_p"), param_is (struct tree_decl_map)))
221 htab_t value_expr_for_decl;
223 static GTY ((if_marked ("tree_vec_map_marked_p"), param_is (struct tree_vec_map)))
224 htab_t debug_args_for_decl;
226 static GTY ((if_marked ("tree_priority_map_marked_p"),
227 param_is (struct tree_priority_map)))
228 htab_t init_priority_for_decl;
230 static void set_type_quals (tree, int, tree);
231 static int type_hash_eq (const void *, const void *);
232 static hashval_t type_hash_hash (const void *);
233 static hashval_t int_cst_hash_hash (const void *);
234 static int int_cst_hash_eq (const void *, const void *);
235 static hashval_t cl_option_hash_hash (const void *);
236 static int cl_option_hash_eq (const void *, const void *);
237 static void print_type_hash_statistics (void);
238 static void print_debug_expr_statistics (void);
239 static void print_value_expr_statistics (void);
240 static int type_hash_marked_p (const void *);
241 static unsigned int type_hash_list (const_tree, hashval_t);
242 static unsigned int attribute_hash_list (const_tree, hashval_t);
243 static bool decls_same_for_odr (tree decl1, tree decl2);
245 tree global_trees[TI_MAX];
246 tree integer_types[itk_none];
248 unsigned char tree_contains_struct[MAX_TREE_CODES][64];
250 /* Number of operands for each OpenMP clause. */
251 unsigned const char omp_clause_num_ops[] =
253 0, /* OMP_CLAUSE_ERROR */
254 1, /* OMP_CLAUSE_PRIVATE */
255 1, /* OMP_CLAUSE_SHARED */
256 1, /* OMP_CLAUSE_FIRSTPRIVATE */
257 2, /* OMP_CLAUSE_LASTPRIVATE */
258 4, /* OMP_CLAUSE_REDUCTION */
259 1, /* OMP_CLAUSE_COPYIN */
260 1, /* OMP_CLAUSE_COPYPRIVATE */
261 2, /* OMP_CLAUSE_LINEAR */
262 2, /* OMP_CLAUSE_ALIGNED */
263 1, /* OMP_CLAUSE_DEPEND */
264 1, /* OMP_CLAUSE_UNIFORM */
265 2, /* OMP_CLAUSE_FROM */
266 2, /* OMP_CLAUSE_TO */
267 2, /* OMP_CLAUSE_MAP */
268 1, /* OMP_CLAUSE__LOOPTEMP_ */
269 1, /* OMP_CLAUSE_IF */
270 1, /* OMP_CLAUSE_NUM_THREADS */
271 1, /* OMP_CLAUSE_SCHEDULE */
272 0, /* OMP_CLAUSE_NOWAIT */
273 0, /* OMP_CLAUSE_ORDERED */
274 0, /* OMP_CLAUSE_DEFAULT */
275 3, /* OMP_CLAUSE_COLLAPSE */
276 0, /* OMP_CLAUSE_UNTIED */
277 1, /* OMP_CLAUSE_FINAL */
278 0, /* OMP_CLAUSE_MERGEABLE */
279 1, /* OMP_CLAUSE_DEVICE */
280 1, /* OMP_CLAUSE_DIST_SCHEDULE */
281 0, /* OMP_CLAUSE_INBRANCH */
282 0, /* OMP_CLAUSE_NOTINBRANCH */
283 1, /* OMP_CLAUSE_NUM_TEAMS */
284 1, /* OMP_CLAUSE_THREAD_LIMIT */
285 0, /* OMP_CLAUSE_PROC_BIND */
286 1, /* OMP_CLAUSE_SAFELEN */
287 1, /* OMP_CLAUSE_SIMDLEN */
288 0, /* OMP_CLAUSE_FOR */
289 0, /* OMP_CLAUSE_PARALLEL */
290 0, /* OMP_CLAUSE_SECTIONS */
291 0, /* OMP_CLAUSE_TASKGROUP */
292 1, /* OMP_CLAUSE__SIMDUID_ */
295 const char * const omp_clause_code_name[] =
297 "error_clause",
298 "private",
299 "shared",
300 "firstprivate",
301 "lastprivate",
302 "reduction",
303 "copyin",
304 "copyprivate",
305 "linear",
306 "aligned",
307 "depend",
308 "uniform",
309 "from",
310 "to",
311 "map",
312 "_looptemp_",
313 "if",
314 "num_threads",
315 "schedule",
316 "nowait",
317 "ordered",
318 "default",
319 "collapse",
320 "untied",
321 "final",
322 "mergeable",
323 "device",
324 "dist_schedule",
325 "inbranch",
326 "notinbranch",
327 "num_teams",
328 "thread_limit",
329 "proc_bind",
330 "safelen",
331 "simdlen",
332 "for",
333 "parallel",
334 "sections",
335 "taskgroup",
336 "_simduid_"
340 /* Return the tree node structure used by tree code CODE. */
342 static inline enum tree_node_structure_enum
343 tree_node_structure_for_code (enum tree_code code)
345 switch (TREE_CODE_CLASS (code))
347 case tcc_declaration:
349 switch (code)
351 case FIELD_DECL:
352 return TS_FIELD_DECL;
353 case PARM_DECL:
354 return TS_PARM_DECL;
355 case VAR_DECL:
356 return TS_VAR_DECL;
357 case LABEL_DECL:
358 return TS_LABEL_DECL;
359 case RESULT_DECL:
360 return TS_RESULT_DECL;
361 case DEBUG_EXPR_DECL:
362 return TS_DECL_WRTL;
363 case CONST_DECL:
364 return TS_CONST_DECL;
365 case TYPE_DECL:
366 return TS_TYPE_DECL;
367 case FUNCTION_DECL:
368 return TS_FUNCTION_DECL;
369 case TRANSLATION_UNIT_DECL:
370 return TS_TRANSLATION_UNIT_DECL;
371 default:
372 return TS_DECL_NON_COMMON;
375 case tcc_type:
376 return TS_TYPE_NON_COMMON;
377 case tcc_reference:
378 case tcc_comparison:
379 case tcc_unary:
380 case tcc_binary:
381 case tcc_expression:
382 case tcc_statement:
383 case tcc_vl_exp:
384 return TS_EXP;
385 default: /* tcc_constant and tcc_exceptional */
386 break;
388 switch (code)
390 /* tcc_constant cases. */
391 case INTEGER_CST: return TS_INT_CST;
392 case REAL_CST: return TS_REAL_CST;
393 case FIXED_CST: return TS_FIXED_CST;
394 case COMPLEX_CST: return TS_COMPLEX;
395 case VECTOR_CST: return TS_VECTOR;
396 case STRING_CST: return TS_STRING;
397 /* tcc_exceptional cases. */
398 case ERROR_MARK: return TS_COMMON;
399 case IDENTIFIER_NODE: return TS_IDENTIFIER;
400 case TREE_LIST: return TS_LIST;
401 case TREE_VEC: return TS_VEC;
402 case SSA_NAME: return TS_SSA_NAME;
403 case PLACEHOLDER_EXPR: return TS_COMMON;
404 case STATEMENT_LIST: return TS_STATEMENT_LIST;
405 case BLOCK: return TS_BLOCK;
406 case CONSTRUCTOR: return TS_CONSTRUCTOR;
407 case TREE_BINFO: return TS_BINFO;
408 case OMP_CLAUSE: return TS_OMP_CLAUSE;
409 case OPTIMIZATION_NODE: return TS_OPTIMIZATION;
410 case TARGET_OPTION_NODE: return TS_TARGET_OPTION;
412 default:
413 gcc_unreachable ();
418 /* Initialize tree_contains_struct to describe the hierarchy of tree
419 nodes. */
421 static void
422 initialize_tree_contains_struct (void)
424 unsigned i;
426 for (i = ERROR_MARK; i < LAST_AND_UNUSED_TREE_CODE; i++)
428 enum tree_code code;
429 enum tree_node_structure_enum ts_code;
431 code = (enum tree_code) i;
432 ts_code = tree_node_structure_for_code (code);
434 /* Mark the TS structure itself. */
435 tree_contains_struct[code][ts_code] = 1;
437 /* Mark all the structures that TS is derived from. */
438 switch (ts_code)
440 case TS_TYPED:
441 case TS_BLOCK:
442 MARK_TS_BASE (code);
443 break;
445 case TS_COMMON:
446 case TS_INT_CST:
447 case TS_REAL_CST:
448 case TS_FIXED_CST:
449 case TS_VECTOR:
450 case TS_STRING:
451 case TS_COMPLEX:
452 case TS_SSA_NAME:
453 case TS_CONSTRUCTOR:
454 case TS_EXP:
455 case TS_STATEMENT_LIST:
456 MARK_TS_TYPED (code);
457 break;
459 case TS_IDENTIFIER:
460 case TS_DECL_MINIMAL:
461 case TS_TYPE_COMMON:
462 case TS_LIST:
463 case TS_VEC:
464 case TS_BINFO:
465 case TS_OMP_CLAUSE:
466 case TS_OPTIMIZATION:
467 case TS_TARGET_OPTION:
468 MARK_TS_COMMON (code);
469 break;
471 case TS_TYPE_WITH_LANG_SPECIFIC:
472 MARK_TS_TYPE_COMMON (code);
473 break;
475 case TS_TYPE_NON_COMMON:
476 MARK_TS_TYPE_WITH_LANG_SPECIFIC (code);
477 break;
479 case TS_DECL_COMMON:
480 MARK_TS_DECL_MINIMAL (code);
481 break;
483 case TS_DECL_WRTL:
484 case TS_CONST_DECL:
485 MARK_TS_DECL_COMMON (code);
486 break;
488 case TS_DECL_NON_COMMON:
489 MARK_TS_DECL_WITH_VIS (code);
490 break;
492 case TS_DECL_WITH_VIS:
493 case TS_PARM_DECL:
494 case TS_LABEL_DECL:
495 case TS_RESULT_DECL:
496 MARK_TS_DECL_WRTL (code);
497 break;
499 case TS_FIELD_DECL:
500 MARK_TS_DECL_COMMON (code);
501 break;
503 case TS_VAR_DECL:
504 MARK_TS_DECL_WITH_VIS (code);
505 break;
507 case TS_TYPE_DECL:
508 case TS_FUNCTION_DECL:
509 MARK_TS_DECL_NON_COMMON (code);
510 break;
512 case TS_TRANSLATION_UNIT_DECL:
513 MARK_TS_DECL_COMMON (code);
514 break;
516 default:
517 gcc_unreachable ();
521 /* Basic consistency checks for attributes used in fold. */
522 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_NON_COMMON]);
523 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_NON_COMMON]);
524 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_COMMON]);
525 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_COMMON]);
526 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_COMMON]);
527 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_COMMON]);
528 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_COMMON]);
529 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_COMMON]);
530 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_COMMON]);
531 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_COMMON]);
532 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_COMMON]);
533 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WRTL]);
534 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_WRTL]);
535 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_WRTL]);
536 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WRTL]);
537 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_WRTL]);
538 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_MINIMAL]);
539 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_MINIMAL]);
540 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_MINIMAL]);
541 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_MINIMAL]);
542 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_MINIMAL]);
543 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_MINIMAL]);
544 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_MINIMAL]);
545 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_MINIMAL]);
546 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_MINIMAL]);
547 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WITH_VIS]);
548 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WITH_VIS]);
549 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_WITH_VIS]);
550 gcc_assert (tree_contains_struct[VAR_DECL][TS_VAR_DECL]);
551 gcc_assert (tree_contains_struct[FIELD_DECL][TS_FIELD_DECL]);
552 gcc_assert (tree_contains_struct[PARM_DECL][TS_PARM_DECL]);
553 gcc_assert (tree_contains_struct[LABEL_DECL][TS_LABEL_DECL]);
554 gcc_assert (tree_contains_struct[RESULT_DECL][TS_RESULT_DECL]);
555 gcc_assert (tree_contains_struct[CONST_DECL][TS_CONST_DECL]);
556 gcc_assert (tree_contains_struct[TYPE_DECL][TS_TYPE_DECL]);
557 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_FUNCTION_DECL]);
558 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_MINIMAL]);
559 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_COMMON]);
560 gcc_assert (tree_contains_struct[NAMELIST_DECL][TS_DECL_MINIMAL]);
561 gcc_assert (tree_contains_struct[NAMELIST_DECL][TS_DECL_COMMON]);
565 /* Init tree.c. */
567 void
568 init_ttree (void)
570 /* Initialize the hash table of types. */
571 type_hash_table = htab_create_ggc (TYPE_HASH_INITIAL_SIZE, type_hash_hash,
572 type_hash_eq, 0);
574 block_factor_for_type = htab_create_ggc (512, tree_map_hash,
575 tree_map_eq, 0);
577 debug_expr_for_decl = htab_create_ggc (512, tree_decl_map_hash,
578 tree_decl_map_eq, 0);
580 value_expr_for_decl = htab_create_ggc (512, tree_decl_map_hash,
581 tree_decl_map_eq, 0);
582 init_priority_for_decl = htab_create_ggc (512, tree_priority_map_hash,
583 tree_priority_map_eq, 0);
585 int_cst_hash_table = htab_create_ggc (1024, int_cst_hash_hash,
586 int_cst_hash_eq, NULL);
588 int_cst_node = make_node (INTEGER_CST);
590 cl_option_hash_table = htab_create_ggc (64, cl_option_hash_hash,
591 cl_option_hash_eq, NULL);
593 cl_optimization_node = make_node (OPTIMIZATION_NODE);
594 cl_target_option_node = make_node (TARGET_OPTION_NODE);
596 /* Initialize the tree_contains_struct array. */
597 initialize_tree_contains_struct ();
598 lang_hooks.init_ts ();
602 /* The name of the object as the assembler will see it (but before any
603 translations made by ASM_OUTPUT_LABELREF). Often this is the same
604 as DECL_NAME. It is an IDENTIFIER_NODE. */
605 tree
606 decl_assembler_name (tree decl)
608 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
609 lang_hooks.set_decl_assembler_name (decl);
610 return DECL_WITH_VIS_CHECK (decl)->decl_with_vis.assembler_name;
613 /* Compute the number of bytes occupied by a tree with code CODE.
614 This function cannot be used for nodes that have variable sizes,
615 including TREE_VEC, STRING_CST, and CALL_EXPR. */
616 size_t
617 tree_code_size (enum tree_code code)
619 switch (TREE_CODE_CLASS (code))
621 case tcc_declaration: /* A decl node */
623 switch (code)
625 case FIELD_DECL:
626 return sizeof (struct tree_field_decl);
627 case PARM_DECL:
628 return sizeof (struct tree_parm_decl);
629 case VAR_DECL:
630 return sizeof (struct tree_var_decl);
631 case LABEL_DECL:
632 return sizeof (struct tree_label_decl);
633 case RESULT_DECL:
634 return sizeof (struct tree_result_decl);
635 case CONST_DECL:
636 return sizeof (struct tree_const_decl);
637 case TYPE_DECL:
638 return sizeof (struct tree_type_decl);
639 case FUNCTION_DECL:
640 return sizeof (struct tree_function_decl);
641 case DEBUG_EXPR_DECL:
642 return sizeof (struct tree_decl_with_rtl);
643 default:
644 return sizeof (struct tree_decl_non_common);
648 case tcc_type: /* a type node */
649 return sizeof (struct tree_type_non_common);
651 case tcc_reference: /* a reference */
652 case tcc_expression: /* an expression */
653 case tcc_statement: /* an expression with side effects */
654 case tcc_comparison: /* a comparison expression */
655 case tcc_unary: /* a unary arithmetic expression */
656 case tcc_binary: /* a binary arithmetic expression */
657 return (sizeof (struct tree_exp)
658 + (TREE_CODE_LENGTH (code) - 1) * sizeof (tree));
660 case tcc_constant: /* a constant */
661 switch (code)
663 case INTEGER_CST: return sizeof (struct tree_int_cst);
664 case REAL_CST: return sizeof (struct tree_real_cst);
665 case FIXED_CST: return sizeof (struct tree_fixed_cst);
666 case COMPLEX_CST: return sizeof (struct tree_complex);
667 case VECTOR_CST: return sizeof (struct tree_vector);
668 case STRING_CST: gcc_unreachable ();
669 default:
670 return lang_hooks.tree_size (code);
673 case tcc_exceptional: /* something random, like an identifier. */
674 switch (code)
676 case IDENTIFIER_NODE: return lang_hooks.identifier_size;
677 case TREE_LIST: return sizeof (struct tree_list);
679 case ERROR_MARK:
680 case PLACEHOLDER_EXPR: return sizeof (struct tree_common);
682 case TREE_VEC:
683 case OMP_CLAUSE: gcc_unreachable ();
685 case SSA_NAME: return sizeof (struct tree_ssa_name);
687 case STATEMENT_LIST: return sizeof (struct tree_statement_list);
688 case BLOCK: return sizeof (struct tree_block);
689 case CONSTRUCTOR: return sizeof (struct tree_constructor);
690 case OPTIMIZATION_NODE: return sizeof (struct tree_optimization_option);
691 case TARGET_OPTION_NODE: return sizeof (struct tree_target_option);
693 default:
694 return lang_hooks.tree_size (code);
697 default:
698 gcc_unreachable ();
702 /* Compute the number of bytes occupied by NODE. This routine only
703 looks at TREE_CODE, except for those nodes that have variable sizes. */
704 size_t
705 tree_size (const_tree node)
707 const enum tree_code code = TREE_CODE (node);
708 switch (code)
710 case TREE_BINFO:
711 return (offsetof (struct tree_binfo, base_binfos)
712 + vec<tree, va_gc>
713 ::embedded_size (BINFO_N_BASE_BINFOS (node)));
715 case TREE_VEC:
716 return (sizeof (struct tree_vec)
717 + (TREE_VEC_LENGTH (node) - 1) * sizeof (tree));
719 case VECTOR_CST:
720 return (sizeof (struct tree_vector)
721 + (TYPE_VECTOR_SUBPARTS (TREE_TYPE (node)) - 1) * sizeof (tree));
723 case STRING_CST:
724 return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
726 case OMP_CLAUSE:
727 return (sizeof (struct tree_omp_clause)
728 + (omp_clause_num_ops[OMP_CLAUSE_CODE (node)] - 1)
729 * sizeof (tree));
731 default:
732 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
733 return (sizeof (struct tree_exp)
734 + (VL_EXP_OPERAND_LENGTH (node) - 1) * sizeof (tree));
735 else
736 return tree_code_size (code);
740 /* Record interesting allocation statistics for a tree node with CODE
741 and LENGTH. */
743 static void
744 record_node_allocation_statistics (enum tree_code code ATTRIBUTE_UNUSED,
745 size_t length ATTRIBUTE_UNUSED)
747 enum tree_code_class type = TREE_CODE_CLASS (code);
748 tree_node_kind kind;
750 if (!GATHER_STATISTICS)
751 return;
753 switch (type)
755 case tcc_declaration: /* A decl node */
756 kind = d_kind;
757 break;
759 case tcc_type: /* a type node */
760 kind = t_kind;
761 break;
763 case tcc_statement: /* an expression with side effects */
764 kind = s_kind;
765 break;
767 case tcc_reference: /* a reference */
768 kind = r_kind;
769 break;
771 case tcc_expression: /* an expression */
772 case tcc_comparison: /* a comparison expression */
773 case tcc_unary: /* a unary arithmetic expression */
774 case tcc_binary: /* a binary arithmetic expression */
775 kind = e_kind;
776 break;
778 case tcc_constant: /* a constant */
779 kind = c_kind;
780 break;
782 case tcc_exceptional: /* something random, like an identifier. */
783 switch (code)
785 case IDENTIFIER_NODE:
786 kind = id_kind;
787 break;
789 case TREE_VEC:
790 kind = vec_kind;
791 break;
793 case TREE_BINFO:
794 kind = binfo_kind;
795 break;
797 case SSA_NAME:
798 kind = ssa_name_kind;
799 break;
801 case BLOCK:
802 kind = b_kind;
803 break;
805 case CONSTRUCTOR:
806 kind = constr_kind;
807 break;
809 case OMP_CLAUSE:
810 kind = omp_clause_kind;
811 break;
813 default:
814 kind = x_kind;
815 break;
817 break;
819 case tcc_vl_exp:
820 kind = e_kind;
821 break;
823 default:
824 gcc_unreachable ();
827 tree_code_counts[(int) code]++;
828 tree_node_counts[(int) kind]++;
829 tree_node_sizes[(int) kind] += length;
832 /* Allocate and return a new UID from the DECL_UID namespace. */
835 allocate_decl_uid (void)
837 return next_decl_uid++;
840 /* Return a newly allocated node of code CODE. For decl and type
841 nodes, some other fields are initialized. The rest of the node is
842 initialized to zero. This function cannot be used for TREE_VEC or
843 OMP_CLAUSE nodes, which is enforced by asserts in tree_code_size.
845 Achoo! I got a code in the node. */
847 tree
848 make_node_stat (enum tree_code code MEM_STAT_DECL)
850 tree t;
851 enum tree_code_class type = TREE_CODE_CLASS (code);
852 size_t length = tree_code_size (code);
854 record_node_allocation_statistics (code, length);
856 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
857 TREE_SET_CODE (t, code);
859 switch (type)
861 case tcc_statement:
862 TREE_SIDE_EFFECTS (t) = 1;
863 break;
865 case tcc_declaration:
866 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
868 if (code == FUNCTION_DECL)
870 DECL_ALIGN (t) = FUNCTION_BOUNDARY;
871 DECL_MODE (t) = FUNCTION_MODE;
873 else
874 DECL_ALIGN (t) = 1;
876 DECL_SOURCE_LOCATION (t) = input_location;
877 if (TREE_CODE (t) == DEBUG_EXPR_DECL)
878 DECL_UID (t) = --next_debug_decl_uid;
879 else
881 DECL_UID (t) = allocate_decl_uid ();
882 SET_DECL_PT_UID (t, -1);
884 if (TREE_CODE (t) == LABEL_DECL)
885 LABEL_DECL_UID (t) = -1;
887 break;
889 case tcc_type:
890 TYPE_UID (t) = next_type_uid++;
891 TYPE_ALIGN (t) = BITS_PER_UNIT;
892 TYPE_USER_ALIGN (t) = 0;
893 TYPE_MAIN_VARIANT (t) = t;
894 TYPE_CANONICAL (t) = t;
896 /* Default to no attributes for type, but let target change that. */
897 TYPE_ATTRIBUTES (t) = NULL_TREE;
898 targetm.set_default_type_attributes (t);
900 /* We have not yet computed the alias set for this type. */
901 TYPE_ALIAS_SET (t) = -1;
902 break;
904 case tcc_constant:
905 TREE_CONSTANT (t) = 1;
906 break;
908 case tcc_expression:
909 switch (code)
911 case INIT_EXPR:
912 case MODIFY_EXPR:
913 case VA_ARG_EXPR:
914 case PREDECREMENT_EXPR:
915 case PREINCREMENT_EXPR:
916 case POSTDECREMENT_EXPR:
917 case POSTINCREMENT_EXPR:
918 /* All of these have side-effects, no matter what their
919 operands are. */
920 TREE_SIDE_EFFECTS (t) = 1;
921 break;
923 default:
924 break;
926 break;
928 default:
929 /* Other classes need no special treatment. */
930 break;
933 return t;
936 /* Return a new node with the same contents as NODE except that its
937 TREE_CHAIN, if it has one, is zero and it has a fresh uid. */
939 tree
940 copy_node_stat (tree node MEM_STAT_DECL)
942 tree t;
943 enum tree_code code = TREE_CODE (node);
944 size_t length;
946 gcc_assert (code != STATEMENT_LIST);
948 length = tree_size (node);
949 record_node_allocation_statistics (code, length);
950 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
951 memcpy (t, node, length);
953 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
954 TREE_CHAIN (t) = 0;
955 TREE_ASM_WRITTEN (t) = 0;
956 TREE_VISITED (t) = 0;
958 if (TREE_CODE_CLASS (code) == tcc_declaration)
960 if (code == DEBUG_EXPR_DECL)
961 DECL_UID (t) = --next_debug_decl_uid;
962 else
964 DECL_UID (t) = allocate_decl_uid ();
965 if (DECL_PT_UID_SET_P (node))
966 SET_DECL_PT_UID (t, DECL_PT_UID (node));
968 if ((TREE_CODE (node) == PARM_DECL || TREE_CODE (node) == VAR_DECL)
969 && DECL_HAS_VALUE_EXPR_P (node))
971 SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (node));
972 DECL_HAS_VALUE_EXPR_P (t) = 1;
974 /* DECL_DEBUG_EXPR is copied explicitely by callers. */
975 if (TREE_CODE (node) == VAR_DECL)
976 DECL_HAS_DEBUG_EXPR_P (t) = 0;
977 if (TREE_CODE (node) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (node))
979 SET_DECL_INIT_PRIORITY (t, DECL_INIT_PRIORITY (node));
980 DECL_HAS_INIT_PRIORITY_P (t) = 1;
982 if (TREE_CODE (node) == FUNCTION_DECL)
983 DECL_STRUCT_FUNCTION (t) = NULL;
985 else if (TREE_CODE_CLASS (code) == tcc_type)
987 TYPE_UID (t) = next_type_uid++;
988 /* The following is so that the debug code for
989 the copy is different from the original type.
990 The two statements usually duplicate each other
991 (because they clear fields of the same union),
992 but the optimizer should catch that. */
993 TYPE_SYMTAB_POINTER (t) = 0;
994 TYPE_SYMTAB_ADDRESS (t) = 0;
996 /* Do not copy the values cache. */
997 if (TYPE_CACHED_VALUES_P (t))
999 TYPE_CACHED_VALUES_P (t) = 0;
1000 TYPE_CACHED_VALUES (t) = NULL_TREE;
1003 if (TYPE_HAS_BLOCK_FACTOR (node))
1004 SET_TYPE_BLOCK_FACTOR (t, TYPE_BLOCK_FACTOR (node));
1007 return t;
1010 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
1011 For example, this can copy a list made of TREE_LIST nodes. */
1013 tree
1014 copy_list (tree list)
1016 tree head;
1017 tree prev, next;
1019 if (list == 0)
1020 return 0;
1022 head = prev = copy_node (list);
1023 next = TREE_CHAIN (list);
1024 while (next)
1026 TREE_CHAIN (prev) = copy_node (next);
1027 prev = TREE_CHAIN (prev);
1028 next = TREE_CHAIN (next);
1030 return head;
1034 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1036 tree
1037 build_int_cst (tree type, HOST_WIDE_INT low)
1039 /* Support legacy code. */
1040 if (!type)
1041 type = integer_type_node;
1043 return double_int_to_tree (type, double_int::from_shwi (low));
1046 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1048 tree
1049 build_int_cst_type (tree type, HOST_WIDE_INT low)
1051 gcc_assert (type);
1053 return double_int_to_tree (type, double_int::from_shwi (low));
1056 /* Constructs tree in type TYPE from with value given by CST. Signedness
1057 of CST is assumed to be the same as the signedness of TYPE. */
1059 tree
1060 double_int_to_tree (tree type, double_int cst)
1062 bool sign_extended_type = !TYPE_UNSIGNED (type);
1064 cst = cst.ext (TYPE_PRECISION (type), !sign_extended_type);
1066 return build_int_cst_wide (type, cst.low, cst.high);
1069 /* Returns true if CST fits into range of TYPE. Signedness of CST is assumed
1070 to be the same as the signedness of TYPE. */
1072 bool
1073 double_int_fits_to_tree_p (const_tree type, double_int cst)
1075 bool sign_extended_type = !TYPE_UNSIGNED (type);
1077 double_int ext
1078 = cst.ext (TYPE_PRECISION (type), !sign_extended_type);
1080 return cst == ext;
1083 /* We force the double_int CST to the range of the type TYPE by sign or
1084 zero extending it. OVERFLOWABLE indicates if we are interested in
1085 overflow of the value, when >0 we are only interested in signed
1086 overflow, for <0 we are interested in any overflow. OVERFLOWED
1087 indicates whether overflow has already occurred. CONST_OVERFLOWED
1088 indicates whether constant overflow has already occurred. We force
1089 T's value to be within range of T's type (by setting to 0 or 1 all
1090 the bits outside the type's range). We set TREE_OVERFLOWED if,
1091 OVERFLOWED is nonzero,
1092 or OVERFLOWABLE is >0 and signed overflow occurs
1093 or OVERFLOWABLE is <0 and any overflow occurs
1094 We return a new tree node for the extended double_int. The node
1095 is shared if no overflow flags are set. */
1098 tree
1099 force_fit_type_double (tree type, double_int cst, int overflowable,
1100 bool overflowed)
1102 bool sign_extended_type = !TYPE_UNSIGNED (type);
1104 /* If we need to set overflow flags, return a new unshared node. */
1105 if (overflowed || !double_int_fits_to_tree_p (type, cst))
1107 if (overflowed
1108 || overflowable < 0
1109 || (overflowable > 0 && sign_extended_type))
1111 tree t = make_node (INTEGER_CST);
1112 TREE_INT_CST (t)
1113 = cst.ext (TYPE_PRECISION (type), !sign_extended_type);
1114 TREE_TYPE (t) = type;
1115 TREE_OVERFLOW (t) = 1;
1116 return t;
1120 /* Else build a shared node. */
1121 return double_int_to_tree (type, cst);
1124 /* These are the hash table functions for the hash table of INTEGER_CST
1125 nodes of a sizetype. */
1127 /* Return the hash code code X, an INTEGER_CST. */
1129 static hashval_t
1130 int_cst_hash_hash (const void *x)
1132 const_tree const t = (const_tree) x;
1134 return (TREE_INT_CST_HIGH (t) ^ TREE_INT_CST_LOW (t)
1135 ^ htab_hash_pointer (TREE_TYPE (t)));
1138 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
1139 is the same as that given by *Y, which is the same. */
1141 static int
1142 int_cst_hash_eq (const void *x, const void *y)
1144 const_tree const xt = (const_tree) x;
1145 const_tree const yt = (const_tree) y;
1147 return (TREE_TYPE (xt) == TREE_TYPE (yt)
1148 && TREE_INT_CST_HIGH (xt) == TREE_INT_CST_HIGH (yt)
1149 && TREE_INT_CST_LOW (xt) == TREE_INT_CST_LOW (yt));
1152 /* Create an INT_CST node of TYPE and value HI:LOW.
1153 The returned node is always shared. For small integers we use a
1154 per-type vector cache, for larger ones we use a single hash table. */
1156 tree
1157 build_int_cst_wide (tree type, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
1159 tree t;
1160 int ix = -1;
1161 int limit = 0;
1163 gcc_assert (type);
1165 switch (TREE_CODE (type))
1167 case NULLPTR_TYPE:
1168 gcc_assert (hi == 0 && low == 0);
1169 /* Fallthru. */
1171 case POINTER_TYPE:
1172 case REFERENCE_TYPE:
1173 /* Cache NULL pointer. */
1174 if (!hi && !low)
1176 limit = 1;
1177 ix = 0;
1179 break;
1181 case BOOLEAN_TYPE:
1182 /* Cache false or true. */
1183 limit = 2;
1184 if (!hi && low < 2)
1185 ix = low;
1186 break;
1188 case INTEGER_TYPE:
1189 case OFFSET_TYPE:
1190 if (TYPE_UNSIGNED (type))
1192 /* Cache 0..N */
1193 limit = INTEGER_SHARE_LIMIT;
1194 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
1195 ix = low;
1197 else
1199 /* Cache -1..N */
1200 limit = INTEGER_SHARE_LIMIT + 1;
1201 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
1202 ix = low + 1;
1203 else if (hi == -1 && low == -(unsigned HOST_WIDE_INT)1)
1204 ix = 0;
1206 break;
1208 case ENUMERAL_TYPE:
1209 break;
1211 default:
1212 gcc_unreachable ();
1215 if (ix >= 0)
1217 /* Look for it in the type's vector of small shared ints. */
1218 if (!TYPE_CACHED_VALUES_P (type))
1220 TYPE_CACHED_VALUES_P (type) = 1;
1221 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1224 t = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix);
1225 if (t)
1227 /* Make sure no one is clobbering the shared constant. */
1228 gcc_assert (TREE_TYPE (t) == type);
1229 gcc_assert (TREE_INT_CST_LOW (t) == low);
1230 gcc_assert (TREE_INT_CST_HIGH (t) == hi);
1232 else
1234 /* Create a new shared int. */
1235 t = make_node (INTEGER_CST);
1237 TREE_INT_CST_LOW (t) = low;
1238 TREE_INT_CST_HIGH (t) = hi;
1239 TREE_TYPE (t) = type;
1241 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1244 else
1246 /* Use the cache of larger shared ints. */
1247 void **slot;
1249 TREE_INT_CST_LOW (int_cst_node) = low;
1250 TREE_INT_CST_HIGH (int_cst_node) = hi;
1251 TREE_TYPE (int_cst_node) = type;
1253 slot = htab_find_slot (int_cst_hash_table, int_cst_node, INSERT);
1254 t = (tree) *slot;
1255 if (!t)
1257 /* Insert this one into the hash table. */
1258 t = int_cst_node;
1259 *slot = t;
1260 /* Make a new node for next time round. */
1261 int_cst_node = make_node (INTEGER_CST);
1265 return t;
1268 void
1269 cache_integer_cst (tree t)
1271 tree type = TREE_TYPE (t);
1272 HOST_WIDE_INT hi = TREE_INT_CST_HIGH (t);
1273 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (t);
1274 int ix = -1;
1275 int limit = 0;
1277 gcc_assert (!TREE_OVERFLOW (t));
1279 switch (TREE_CODE (type))
1281 case NULLPTR_TYPE:
1282 gcc_assert (hi == 0 && low == 0);
1283 /* Fallthru. */
1285 case POINTER_TYPE:
1286 case REFERENCE_TYPE:
1287 /* Cache NULL pointer. */
1288 if (!hi && !low)
1290 limit = 1;
1291 ix = 0;
1293 break;
1295 case BOOLEAN_TYPE:
1296 /* Cache false or true. */
1297 limit = 2;
1298 if (!hi && low < 2)
1299 ix = low;
1300 break;
1302 case INTEGER_TYPE:
1303 case OFFSET_TYPE:
1304 if (TYPE_UNSIGNED (type))
1306 /* Cache 0..N */
1307 limit = INTEGER_SHARE_LIMIT;
1308 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
1309 ix = low;
1311 else
1313 /* Cache -1..N */
1314 limit = INTEGER_SHARE_LIMIT + 1;
1315 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
1316 ix = low + 1;
1317 else if (hi == -1 && low == -(unsigned HOST_WIDE_INT)1)
1318 ix = 0;
1320 break;
1322 case ENUMERAL_TYPE:
1323 break;
1325 default:
1326 gcc_unreachable ();
1329 if (ix >= 0)
1331 /* Look for it in the type's vector of small shared ints. */
1332 if (!TYPE_CACHED_VALUES_P (type))
1334 TYPE_CACHED_VALUES_P (type) = 1;
1335 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1338 gcc_assert (TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) == NULL_TREE);
1339 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1341 else
1343 /* Use the cache of larger shared ints. */
1344 void **slot;
1346 slot = htab_find_slot (int_cst_hash_table, t, INSERT);
1347 /* If there is already an entry for the number verify it's the
1348 same. */
1349 if (*slot)
1351 gcc_assert (TREE_INT_CST_LOW ((tree)*slot) == low
1352 && TREE_INT_CST_HIGH ((tree)*slot) == hi);
1353 return;
1355 /* Otherwise insert this one into the hash table. */
1356 *slot = t;
1361 /* Builds an integer constant in TYPE such that lowest BITS bits are ones
1362 and the rest are zeros. */
1364 tree
1365 build_low_bits_mask (tree type, unsigned bits)
1367 double_int mask;
1369 gcc_assert (bits <= TYPE_PRECISION (type));
1371 if (bits == TYPE_PRECISION (type)
1372 && !TYPE_UNSIGNED (type))
1373 /* Sign extended all-ones mask. */
1374 mask = double_int_minus_one;
1375 else
1376 mask = double_int::mask (bits);
1378 return build_int_cst_wide (type, mask.low, mask.high);
1381 /* Checks that X is integer constant that can be expressed in (unsigned)
1382 HOST_WIDE_INT without loss of precision. */
1384 bool
1385 cst_and_fits_in_hwi (const_tree x)
1387 if (TREE_CODE (x) != INTEGER_CST)
1388 return false;
1390 if (TYPE_PRECISION (TREE_TYPE (x)) > HOST_BITS_PER_WIDE_INT)
1391 return false;
1393 return (TREE_INT_CST_HIGH (x) == 0
1394 || TREE_INT_CST_HIGH (x) == -1);
1397 /* Build a newly constructed TREE_VEC node of length LEN. */
1399 tree
1400 make_vector_stat (unsigned len MEM_STAT_DECL)
1402 tree t;
1403 unsigned length = (len - 1) * sizeof (tree) + sizeof (struct tree_vector);
1405 record_node_allocation_statistics (VECTOR_CST, length);
1407 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1409 TREE_SET_CODE (t, VECTOR_CST);
1410 TREE_CONSTANT (t) = 1;
1412 return t;
1415 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1416 are in a list pointed to by VALS. */
1418 tree
1419 build_vector_stat (tree type, tree *vals MEM_STAT_DECL)
1421 int over = 0;
1422 unsigned cnt = 0;
1423 tree v = make_vector (TYPE_VECTOR_SUBPARTS (type));
1424 TREE_TYPE (v) = type;
1426 /* Iterate through elements and check for overflow. */
1427 for (cnt = 0; cnt < TYPE_VECTOR_SUBPARTS (type); ++cnt)
1429 tree value = vals[cnt];
1431 VECTOR_CST_ELT (v, cnt) = value;
1433 /* Don't crash if we get an address constant. */
1434 if (!CONSTANT_CLASS_P (value))
1435 continue;
1437 over |= TREE_OVERFLOW (value);
1440 TREE_OVERFLOW (v) = over;
1441 return v;
1444 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1445 are extracted from V, a vector of CONSTRUCTOR_ELT. */
1447 tree
1448 build_vector_from_ctor (tree type, vec<constructor_elt, va_gc> *v)
1450 tree *vec = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
1451 unsigned HOST_WIDE_INT idx;
1452 tree value;
1454 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1455 vec[idx] = value;
1456 for (; idx < TYPE_VECTOR_SUBPARTS (type); ++idx)
1457 vec[idx] = build_zero_cst (TREE_TYPE (type));
1459 return build_vector (type, vec);
1462 /* Build a vector of type VECTYPE where all the elements are SCs. */
1463 tree
1464 build_vector_from_val (tree vectype, tree sc)
1466 int i, nunits = TYPE_VECTOR_SUBPARTS (vectype);
1468 if (sc == error_mark_node)
1469 return sc;
1471 /* Verify that the vector type is suitable for SC. Note that there
1472 is some inconsistency in the type-system with respect to restrict
1473 qualifications of pointers. Vector types always have a main-variant
1474 element type and the qualification is applied to the vector-type.
1475 So TREE_TYPE (vector-type) does not return a properly qualified
1476 vector element-type. */
1477 gcc_checking_assert (types_compatible_p (TYPE_MAIN_VARIANT (TREE_TYPE (sc)),
1478 TREE_TYPE (vectype)));
1480 if (CONSTANT_CLASS_P (sc))
1482 tree *v = XALLOCAVEC (tree, nunits);
1483 for (i = 0; i < nunits; ++i)
1484 v[i] = sc;
1485 return build_vector (vectype, v);
1487 else
1489 vec<constructor_elt, va_gc> *v;
1490 vec_alloc (v, nunits);
1491 for (i = 0; i < nunits; ++i)
1492 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, sc);
1493 return build_constructor (vectype, v);
1497 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1498 are in the vec pointed to by VALS. */
1499 tree
1500 build_constructor (tree type, vec<constructor_elt, va_gc> *vals)
1502 tree c = make_node (CONSTRUCTOR);
1503 unsigned int i;
1504 constructor_elt *elt;
1505 bool constant_p = true;
1506 bool side_effects_p = false;
1508 TREE_TYPE (c) = type;
1509 CONSTRUCTOR_ELTS (c) = vals;
1511 FOR_EACH_VEC_SAFE_ELT (vals, i, elt)
1513 /* Mostly ctors will have elts that don't have side-effects, so
1514 the usual case is to scan all the elements. Hence a single
1515 loop for both const and side effects, rather than one loop
1516 each (with early outs). */
1517 if (!TREE_CONSTANT (elt->value))
1518 constant_p = false;
1519 if (TREE_SIDE_EFFECTS (elt->value))
1520 side_effects_p = true;
1523 TREE_SIDE_EFFECTS (c) = side_effects_p;
1524 TREE_CONSTANT (c) = constant_p;
1526 return c;
1529 /* Build a CONSTRUCTOR node made of a single initializer, with the specified
1530 INDEX and VALUE. */
1531 tree
1532 build_constructor_single (tree type, tree index, tree value)
1534 vec<constructor_elt, va_gc> *v;
1535 constructor_elt elt = {index, value};
1537 vec_alloc (v, 1);
1538 v->quick_push (elt);
1540 return build_constructor (type, v);
1544 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1545 are in a list pointed to by VALS. */
1546 tree
1547 build_constructor_from_list (tree type, tree vals)
1549 tree t;
1550 vec<constructor_elt, va_gc> *v = NULL;
1552 if (vals)
1554 vec_alloc (v, list_length (vals));
1555 for (t = vals; t; t = TREE_CHAIN (t))
1556 CONSTRUCTOR_APPEND_ELT (v, TREE_PURPOSE (t), TREE_VALUE (t));
1559 return build_constructor (type, v);
1562 /* Return a new CONSTRUCTOR node whose type is TYPE. NELTS is the number
1563 of elements, provided as index/value pairs. */
1565 tree
1566 build_constructor_va (tree type, int nelts, ...)
1568 vec<constructor_elt, va_gc> *v = NULL;
1569 va_list p;
1571 va_start (p, nelts);
1572 vec_alloc (v, nelts);
1573 while (nelts--)
1575 tree index = va_arg (p, tree);
1576 tree value = va_arg (p, tree);
1577 CONSTRUCTOR_APPEND_ELT (v, index, value);
1579 va_end (p);
1580 return build_constructor (type, v);
1583 /* Return a new FIXED_CST node whose type is TYPE and value is F. */
1585 tree
1586 build_fixed (tree type, FIXED_VALUE_TYPE f)
1588 tree v;
1589 FIXED_VALUE_TYPE *fp;
1591 v = make_node (FIXED_CST);
1592 fp = ggc_alloc_fixed_value ();
1593 memcpy (fp, &f, sizeof (FIXED_VALUE_TYPE));
1595 TREE_TYPE (v) = type;
1596 TREE_FIXED_CST_PTR (v) = fp;
1597 return v;
1600 /* Return a new REAL_CST node whose type is TYPE and value is D. */
1602 tree
1603 build_real (tree type, REAL_VALUE_TYPE d)
1605 tree v;
1606 REAL_VALUE_TYPE *dp;
1607 int overflow = 0;
1609 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
1610 Consider doing it via real_convert now. */
1612 v = make_node (REAL_CST);
1613 dp = ggc_alloc_real_value ();
1614 memcpy (dp, &d, sizeof (REAL_VALUE_TYPE));
1616 TREE_TYPE (v) = type;
1617 TREE_REAL_CST_PTR (v) = dp;
1618 TREE_OVERFLOW (v) = overflow;
1619 return v;
1622 /* Return a new REAL_CST node whose type is TYPE
1623 and whose value is the integer value of the INTEGER_CST node I. */
1625 REAL_VALUE_TYPE
1626 real_value_from_int_cst (const_tree type, const_tree i)
1628 REAL_VALUE_TYPE d;
1630 /* Clear all bits of the real value type so that we can later do
1631 bitwise comparisons to see if two values are the same. */
1632 memset (&d, 0, sizeof d);
1634 real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode,
1635 TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i),
1636 TYPE_UNSIGNED (TREE_TYPE (i)));
1637 return d;
1640 /* Given a tree representing an integer constant I, return a tree
1641 representing the same value as a floating-point constant of type TYPE. */
1643 tree
1644 build_real_from_int_cst (tree type, const_tree i)
1646 tree v;
1647 int overflow = TREE_OVERFLOW (i);
1649 v = build_real (type, real_value_from_int_cst (type, i));
1651 TREE_OVERFLOW (v) |= overflow;
1652 return v;
1655 /* Return a newly constructed STRING_CST node whose value is
1656 the LEN characters at STR.
1657 Note that for a C string literal, LEN should include the trailing NUL.
1658 The TREE_TYPE is not initialized. */
1660 tree
1661 build_string (int len, const char *str)
1663 tree s;
1664 size_t length;
1666 /* Do not waste bytes provided by padding of struct tree_string. */
1667 length = len + offsetof (struct tree_string, str) + 1;
1669 record_node_allocation_statistics (STRING_CST, length);
1671 s = ggc_alloc_tree_node (length);
1673 memset (s, 0, sizeof (struct tree_typed));
1674 TREE_SET_CODE (s, STRING_CST);
1675 TREE_CONSTANT (s) = 1;
1676 TREE_STRING_LENGTH (s) = len;
1677 memcpy (s->string.str, str, len);
1678 s->string.str[len] = '\0';
1680 return s;
1683 /* Return a newly constructed COMPLEX_CST node whose value is
1684 specified by the real and imaginary parts REAL and IMAG.
1685 Both REAL and IMAG should be constant nodes. TYPE, if specified,
1686 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
1688 tree
1689 build_complex (tree type, tree real, tree imag)
1691 tree t = make_node (COMPLEX_CST);
1693 TREE_REALPART (t) = real;
1694 TREE_IMAGPART (t) = imag;
1695 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
1696 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
1697 return t;
1700 /* Return a constant of arithmetic type TYPE which is the
1701 multiplicative identity of the set TYPE. */
1703 tree
1704 build_one_cst (tree type)
1706 switch (TREE_CODE (type))
1708 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1709 case POINTER_TYPE: case REFERENCE_TYPE:
1710 case OFFSET_TYPE:
1711 return build_int_cst (type, 1);
1713 case REAL_TYPE:
1714 return build_real (type, dconst1);
1716 case FIXED_POINT_TYPE:
1717 /* We can only generate 1 for accum types. */
1718 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
1719 return build_fixed (type, FCONST1 (TYPE_MODE (type)));
1721 case VECTOR_TYPE:
1723 tree scalar = build_one_cst (TREE_TYPE (type));
1725 return build_vector_from_val (type, scalar);
1728 case COMPLEX_TYPE:
1729 return build_complex (type,
1730 build_one_cst (TREE_TYPE (type)),
1731 build_zero_cst (TREE_TYPE (type)));
1733 default:
1734 gcc_unreachable ();
1738 /* Return an integer of type TYPE containing all 1's in as much precision as
1739 it contains, or a complex or vector whose subparts are such integers. */
1741 tree
1742 build_all_ones_cst (tree type)
1744 if (TREE_CODE (type) == COMPLEX_TYPE)
1746 tree scalar = build_all_ones_cst (TREE_TYPE (type));
1747 return build_complex (type, scalar, scalar);
1749 else
1750 return build_minus_one_cst (type);
1753 /* Return a constant of arithmetic type TYPE which is the
1754 opposite of the multiplicative identity of the set TYPE. */
1756 tree
1757 build_minus_one_cst (tree type)
1759 switch (TREE_CODE (type))
1761 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1762 case POINTER_TYPE: case REFERENCE_TYPE:
1763 case OFFSET_TYPE:
1764 return build_int_cst (type, -1);
1766 case REAL_TYPE:
1767 return build_real (type, dconstm1);
1769 case FIXED_POINT_TYPE:
1770 /* We can only generate 1 for accum types. */
1771 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
1772 return build_fixed (type, fixed_from_double_int (double_int_minus_one,
1773 TYPE_MODE (type)));
1775 case VECTOR_TYPE:
1777 tree scalar = build_minus_one_cst (TREE_TYPE (type));
1779 return build_vector_from_val (type, scalar);
1782 case COMPLEX_TYPE:
1783 return build_complex (type,
1784 build_minus_one_cst (TREE_TYPE (type)),
1785 build_zero_cst (TREE_TYPE (type)));
1787 default:
1788 gcc_unreachable ();
1792 /* Build 0 constant of type TYPE. This is used by constructor folding
1793 and thus the constant should be represented in memory by
1794 zero(es). */
1796 tree
1797 build_zero_cst (tree type)
1799 switch (TREE_CODE (type))
1801 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1802 case POINTER_TYPE: case REFERENCE_TYPE:
1803 case OFFSET_TYPE: case NULLPTR_TYPE:
1804 return build_int_cst (type, 0);
1806 case REAL_TYPE:
1807 return build_real (type, dconst0);
1809 case FIXED_POINT_TYPE:
1810 return build_fixed (type, FCONST0 (TYPE_MODE (type)));
1812 case VECTOR_TYPE:
1814 tree scalar = build_zero_cst (TREE_TYPE (type));
1816 return build_vector_from_val (type, scalar);
1819 case COMPLEX_TYPE:
1821 tree zero = build_zero_cst (TREE_TYPE (type));
1823 return build_complex (type, zero, zero);
1826 default:
1827 if (!AGGREGATE_TYPE_P (type))
1828 return fold_convert (type, integer_zero_node);
1829 return build_constructor (type, NULL);
1834 /* Build a BINFO with LEN language slots. */
1836 tree
1837 make_tree_binfo_stat (unsigned base_binfos MEM_STAT_DECL)
1839 tree t;
1840 size_t length = (offsetof (struct tree_binfo, base_binfos)
1841 + vec<tree, va_gc>::embedded_size (base_binfos));
1843 record_node_allocation_statistics (TREE_BINFO, length);
1845 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
1847 memset (t, 0, offsetof (struct tree_binfo, base_binfos));
1849 TREE_SET_CODE (t, TREE_BINFO);
1851 BINFO_BASE_BINFOS (t)->embedded_init (base_binfos);
1853 return t;
1856 /* Create a CASE_LABEL_EXPR tree node and return it. */
1858 tree
1859 build_case_label (tree low_value, tree high_value, tree label_decl)
1861 tree t = make_node (CASE_LABEL_EXPR);
1863 TREE_TYPE (t) = void_type_node;
1864 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (label_decl));
1866 CASE_LOW (t) = low_value;
1867 CASE_HIGH (t) = high_value;
1868 CASE_LABEL (t) = label_decl;
1869 CASE_CHAIN (t) = NULL_TREE;
1871 return t;
1874 /* Build a newly constructed TREE_VEC node of length LEN. */
1876 tree
1877 make_tree_vec_stat (int len MEM_STAT_DECL)
1879 tree t;
1880 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
1882 record_node_allocation_statistics (TREE_VEC, length);
1884 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1886 TREE_SET_CODE (t, TREE_VEC);
1887 TREE_VEC_LENGTH (t) = len;
1889 return t;
1892 /* Grow a TREE_VEC node to new length LEN. */
1894 tree
1895 grow_tree_vec_stat (tree v, int len MEM_STAT_DECL)
1897 gcc_assert (TREE_CODE (v) == TREE_VEC);
1899 int oldlen = TREE_VEC_LENGTH (v);
1900 gcc_assert (len > oldlen);
1902 int oldlength = (oldlen - 1) * sizeof (tree) + sizeof (struct tree_vec);
1903 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
1905 record_node_allocation_statistics (TREE_VEC, length - oldlength);
1907 v = (tree) ggc_realloc_stat (v, length PASS_MEM_STAT);
1909 TREE_VEC_LENGTH (v) = len;
1911 return v;
1914 /* Return 1 if EXPR is the integer constant zero or a complex constant
1915 of zero. */
1918 integer_zerop (const_tree expr)
1920 STRIP_NOPS (expr);
1922 switch (TREE_CODE (expr))
1924 case INTEGER_CST:
1925 return (TREE_INT_CST_LOW (expr) == 0
1926 && TREE_INT_CST_HIGH (expr) == 0);
1927 case COMPLEX_CST:
1928 return (integer_zerop (TREE_REALPART (expr))
1929 && integer_zerop (TREE_IMAGPART (expr)));
1930 case VECTOR_CST:
1932 unsigned i;
1933 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
1934 if (!integer_zerop (VECTOR_CST_ELT (expr, i)))
1935 return false;
1936 return true;
1938 default:
1939 return false;
1943 /* Return 1 if EXPR is the integer constant one or the corresponding
1944 complex constant. */
1947 integer_onep (const_tree expr)
1949 STRIP_NOPS (expr);
1951 switch (TREE_CODE (expr))
1953 case INTEGER_CST:
1954 return (TREE_INT_CST_LOW (expr) == 1
1955 && TREE_INT_CST_HIGH (expr) == 0);
1956 case COMPLEX_CST:
1957 return (integer_onep (TREE_REALPART (expr))
1958 && integer_zerop (TREE_IMAGPART (expr)));
1959 case VECTOR_CST:
1961 unsigned i;
1962 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
1963 if (!integer_onep (VECTOR_CST_ELT (expr, i)))
1964 return false;
1965 return true;
1967 default:
1968 return false;
1972 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
1973 it contains, or a complex or vector whose subparts are such integers. */
1976 integer_all_onesp (const_tree expr)
1978 int prec;
1979 int uns;
1981 STRIP_NOPS (expr);
1983 if (TREE_CODE (expr) == COMPLEX_CST
1984 && integer_all_onesp (TREE_REALPART (expr))
1985 && integer_all_onesp (TREE_IMAGPART (expr)))
1986 return 1;
1988 else if (TREE_CODE (expr) == VECTOR_CST)
1990 unsigned i;
1991 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
1992 if (!integer_all_onesp (VECTOR_CST_ELT (expr, i)))
1993 return 0;
1994 return 1;
1997 else if (TREE_CODE (expr) != INTEGER_CST)
1998 return 0;
2000 uns = TYPE_UNSIGNED (TREE_TYPE (expr));
2001 if (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
2002 && TREE_INT_CST_HIGH (expr) == -1)
2003 return 1;
2004 if (!uns)
2005 return 0;
2007 prec = TYPE_PRECISION (TREE_TYPE (expr));
2008 if (prec >= HOST_BITS_PER_WIDE_INT)
2010 HOST_WIDE_INT high_value;
2011 int shift_amount;
2013 shift_amount = prec - HOST_BITS_PER_WIDE_INT;
2015 /* Can not handle precisions greater than twice the host int size. */
2016 gcc_assert (shift_amount <= HOST_BITS_PER_WIDE_INT);
2017 if (shift_amount == HOST_BITS_PER_WIDE_INT)
2018 /* Shifting by the host word size is undefined according to the ANSI
2019 standard, so we must handle this as a special case. */
2020 high_value = -1;
2021 else
2022 high_value = ((HOST_WIDE_INT) 1 << shift_amount) - 1;
2024 return (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
2025 && TREE_INT_CST_HIGH (expr) == high_value);
2027 else
2028 return TREE_INT_CST_LOW (expr) == ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
2031 /* Return 1 if EXPR is the integer constant minus one. */
2034 integer_minus_onep (const_tree expr)
2036 STRIP_NOPS (expr);
2038 if (TREE_CODE (expr) == COMPLEX_CST)
2039 return (integer_all_onesp (TREE_REALPART (expr))
2040 && integer_zerop (TREE_IMAGPART (expr)));
2041 else
2042 return integer_all_onesp (expr);
2045 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
2046 one bit on). */
2049 integer_pow2p (const_tree expr)
2051 int prec;
2052 unsigned HOST_WIDE_INT high, low;
2054 STRIP_NOPS (expr);
2056 if (TREE_CODE (expr) == COMPLEX_CST
2057 && integer_pow2p (TREE_REALPART (expr))
2058 && integer_zerop (TREE_IMAGPART (expr)))
2059 return 1;
2061 if (TREE_CODE (expr) != INTEGER_CST)
2062 return 0;
2064 prec = TYPE_PRECISION (TREE_TYPE (expr));
2065 high = TREE_INT_CST_HIGH (expr);
2066 low = TREE_INT_CST_LOW (expr);
2068 /* First clear all bits that are beyond the type's precision in case
2069 we've been sign extended. */
2071 if (prec == HOST_BITS_PER_DOUBLE_INT)
2073 else if (prec > HOST_BITS_PER_WIDE_INT)
2074 high &= ~(HOST_WIDE_INT_M1U << (prec - HOST_BITS_PER_WIDE_INT));
2075 else
2077 high = 0;
2078 if (prec < HOST_BITS_PER_WIDE_INT)
2079 low &= ~(HOST_WIDE_INT_M1U << prec);
2082 if (high == 0 && low == 0)
2083 return 0;
2085 return ((high == 0 && (low & (low - 1)) == 0)
2086 || (low == 0 && (high & (high - 1)) == 0));
2089 /* Return 1 if EXPR is an integer constant other than zero or a
2090 complex constant other than zero. */
2093 integer_nonzerop (const_tree expr)
2095 STRIP_NOPS (expr);
2097 return ((TREE_CODE (expr) == INTEGER_CST
2098 && (TREE_INT_CST_LOW (expr) != 0
2099 || TREE_INT_CST_HIGH (expr) != 0))
2100 || (TREE_CODE (expr) == COMPLEX_CST
2101 && (integer_nonzerop (TREE_REALPART (expr))
2102 || integer_nonzerop (TREE_IMAGPART (expr)))));
2105 /* Return 1 if EXPR is the fixed-point constant zero. */
2108 fixed_zerop (const_tree expr)
2110 return (TREE_CODE (expr) == FIXED_CST
2111 && TREE_FIXED_CST (expr).data.is_zero ());
2114 /* Return the power of two represented by a tree node known to be a
2115 power of two. */
2118 tree_log2 (const_tree expr)
2120 int prec;
2121 HOST_WIDE_INT high, low;
2123 STRIP_NOPS (expr);
2125 if (TREE_CODE (expr) == COMPLEX_CST)
2126 return tree_log2 (TREE_REALPART (expr));
2128 prec = TYPE_PRECISION (TREE_TYPE (expr));
2129 high = TREE_INT_CST_HIGH (expr);
2130 low = TREE_INT_CST_LOW (expr);
2132 /* First clear all bits that are beyond the type's precision in case
2133 we've been sign extended. */
2135 if (prec == HOST_BITS_PER_DOUBLE_INT)
2137 else if (prec > HOST_BITS_PER_WIDE_INT)
2138 high &= ~(HOST_WIDE_INT_M1U << (prec - HOST_BITS_PER_WIDE_INT));
2139 else
2141 high = 0;
2142 if (prec < HOST_BITS_PER_WIDE_INT)
2143 low &= ~(HOST_WIDE_INT_M1U << prec);
2146 return (high != 0 ? HOST_BITS_PER_WIDE_INT + exact_log2 (high)
2147 : exact_log2 (low));
2150 /* Similar, but return the largest integer Y such that 2 ** Y is less
2151 than or equal to EXPR. */
2154 tree_floor_log2 (const_tree expr)
2156 int prec;
2157 HOST_WIDE_INT high, low;
2159 STRIP_NOPS (expr);
2161 if (TREE_CODE (expr) == COMPLEX_CST)
2162 return tree_log2 (TREE_REALPART (expr));
2164 prec = TYPE_PRECISION (TREE_TYPE (expr));
2165 high = TREE_INT_CST_HIGH (expr);
2166 low = TREE_INT_CST_LOW (expr);
2168 /* First clear all bits that are beyond the type's precision in case
2169 we've been sign extended. Ignore if type's precision hasn't been set
2170 since what we are doing is setting it. */
2172 if (prec == HOST_BITS_PER_DOUBLE_INT || prec == 0)
2174 else if (prec > HOST_BITS_PER_WIDE_INT)
2175 high &= ~(HOST_WIDE_INT_M1U << (prec - HOST_BITS_PER_WIDE_INT));
2176 else
2178 high = 0;
2179 if (prec < HOST_BITS_PER_WIDE_INT)
2180 low &= ~(HOST_WIDE_INT_M1U << prec);
2183 return (high != 0 ? HOST_BITS_PER_WIDE_INT + floor_log2 (high)
2184 : floor_log2 (low));
2187 /* Return number of known trailing zero bits in EXPR, or, if the value of
2188 EXPR is known to be zero, the precision of it's type. */
2190 unsigned int
2191 tree_ctz (const_tree expr)
2193 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
2194 && !POINTER_TYPE_P (TREE_TYPE (expr)))
2195 return 0;
2197 unsigned int ret1, ret2, prec = TYPE_PRECISION (TREE_TYPE (expr));
2198 switch (TREE_CODE (expr))
2200 case INTEGER_CST:
2201 ret1 = tree_to_double_int (expr).trailing_zeros ();
2202 return MIN (ret1, prec);
2203 case SSA_NAME:
2204 ret1 = get_nonzero_bits (expr).trailing_zeros ();
2205 return MIN (ret1, prec);
2206 case PLUS_EXPR:
2207 case MINUS_EXPR:
2208 case BIT_IOR_EXPR:
2209 case BIT_XOR_EXPR:
2210 case MIN_EXPR:
2211 case MAX_EXPR:
2212 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2213 if (ret1 == 0)
2214 return ret1;
2215 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2216 return MIN (ret1, ret2);
2217 case POINTER_PLUS_EXPR:
2218 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2219 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2220 /* Second operand is sizetype, which could be in theory
2221 wider than pointer's precision. Make sure we never
2222 return more than prec. */
2223 ret2 = MIN (ret2, prec);
2224 return MIN (ret1, ret2);
2225 case BIT_AND_EXPR:
2226 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2227 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2228 return MAX (ret1, ret2);
2229 case MULT_EXPR:
2230 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2231 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2232 return MIN (ret1 + ret2, prec);
2233 case LSHIFT_EXPR:
2234 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2235 if (tree_fits_uhwi_p (TREE_OPERAND (expr, 1))
2236 && (tree_to_uhwi (TREE_OPERAND (expr, 1)) < prec))
2238 ret2 = tree_to_uhwi (TREE_OPERAND (expr, 1));
2239 return MIN (ret1 + ret2, prec);
2241 return ret1;
2242 case RSHIFT_EXPR:
2243 if (tree_fits_uhwi_p (TREE_OPERAND (expr, 1))
2244 && (tree_to_uhwi (TREE_OPERAND (expr, 1)) < prec))
2246 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2247 ret2 = tree_to_uhwi (TREE_OPERAND (expr, 1));
2248 if (ret1 > ret2)
2249 return ret1 - ret2;
2251 return 0;
2252 case TRUNC_DIV_EXPR:
2253 case CEIL_DIV_EXPR:
2254 case FLOOR_DIV_EXPR:
2255 case ROUND_DIV_EXPR:
2256 case EXACT_DIV_EXPR:
2257 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
2258 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) == 1)
2260 int l = tree_log2 (TREE_OPERAND (expr, 1));
2261 if (l >= 0)
2263 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2264 ret2 = l;
2265 if (ret1 > ret2)
2266 return ret1 - ret2;
2269 return 0;
2270 CASE_CONVERT:
2271 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2272 if (ret1 && ret1 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2273 ret1 = prec;
2274 return MIN (ret1, prec);
2275 case SAVE_EXPR:
2276 return tree_ctz (TREE_OPERAND (expr, 0));
2277 case COND_EXPR:
2278 ret1 = tree_ctz (TREE_OPERAND (expr, 1));
2279 if (ret1 == 0)
2280 return 0;
2281 ret2 = tree_ctz (TREE_OPERAND (expr, 2));
2282 return MIN (ret1, ret2);
2283 case COMPOUND_EXPR:
2284 return tree_ctz (TREE_OPERAND (expr, 1));
2285 case ADDR_EXPR:
2286 ret1 = get_pointer_alignment (CONST_CAST_TREE (expr));
2287 if (ret1 > BITS_PER_UNIT)
2289 ret1 = ctz_hwi (ret1 / BITS_PER_UNIT);
2290 return MIN (ret1, prec);
2292 return 0;
2293 default:
2294 return 0;
2298 /* Return 1 if EXPR is the real constant zero. Trailing zeroes matter for
2299 decimal float constants, so don't return 1 for them. */
2302 real_zerop (const_tree expr)
2304 STRIP_NOPS (expr);
2306 switch (TREE_CODE (expr))
2308 case REAL_CST:
2309 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0)
2310 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2311 case COMPLEX_CST:
2312 return real_zerop (TREE_REALPART (expr))
2313 && real_zerop (TREE_IMAGPART (expr));
2314 case VECTOR_CST:
2316 unsigned i;
2317 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2318 if (!real_zerop (VECTOR_CST_ELT (expr, i)))
2319 return false;
2320 return true;
2322 default:
2323 return false;
2327 /* Return 1 if EXPR is the real constant one in real or complex form.
2328 Trailing zeroes matter for decimal float constants, so don't return
2329 1 for them. */
2332 real_onep (const_tree expr)
2334 STRIP_NOPS (expr);
2336 switch (TREE_CODE (expr))
2338 case REAL_CST:
2339 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1)
2340 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2341 case COMPLEX_CST:
2342 return real_onep (TREE_REALPART (expr))
2343 && real_zerop (TREE_IMAGPART (expr));
2344 case VECTOR_CST:
2346 unsigned i;
2347 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2348 if (!real_onep (VECTOR_CST_ELT (expr, i)))
2349 return false;
2350 return true;
2352 default:
2353 return false;
2357 /* Return 1 if EXPR is the real constant minus one. Trailing zeroes
2358 matter for decimal float constants, so don't return 1 for them. */
2361 real_minus_onep (const_tree expr)
2363 STRIP_NOPS (expr);
2365 switch (TREE_CODE (expr))
2367 case REAL_CST:
2368 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconstm1)
2369 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2370 case COMPLEX_CST:
2371 return real_minus_onep (TREE_REALPART (expr))
2372 && real_zerop (TREE_IMAGPART (expr));
2373 case VECTOR_CST:
2375 unsigned i;
2376 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2377 if (!real_minus_onep (VECTOR_CST_ELT (expr, i)))
2378 return false;
2379 return true;
2381 default:
2382 return false;
2386 /* Nonzero if EXP is a constant or a cast of a constant. */
2389 really_constant_p (const_tree exp)
2391 /* This is not quite the same as STRIP_NOPS. It does more. */
2392 while (CONVERT_EXPR_P (exp)
2393 || TREE_CODE (exp) == NON_LVALUE_EXPR)
2394 exp = TREE_OPERAND (exp, 0);
2395 return TREE_CONSTANT (exp);
2398 /* Return first list element whose TREE_VALUE is ELEM.
2399 Return 0 if ELEM is not in LIST. */
2401 tree
2402 value_member (tree elem, tree list)
2404 while (list)
2406 if (elem == TREE_VALUE (list))
2407 return list;
2408 list = TREE_CHAIN (list);
2410 return NULL_TREE;
2413 /* Return first list element whose TREE_PURPOSE is ELEM.
2414 Return 0 if ELEM is not in LIST. */
2416 tree
2417 purpose_member (const_tree elem, tree list)
2419 while (list)
2421 if (elem == TREE_PURPOSE (list))
2422 return list;
2423 list = TREE_CHAIN (list);
2425 return NULL_TREE;
2428 /* Return true if ELEM is in V. */
2430 bool
2431 vec_member (const_tree elem, vec<tree, va_gc> *v)
2433 unsigned ix;
2434 tree t;
2435 FOR_EACH_VEC_SAFE_ELT (v, ix, t)
2436 if (elem == t)
2437 return true;
2438 return false;
2441 /* Returns element number IDX (zero-origin) of chain CHAIN, or
2442 NULL_TREE. */
2444 tree
2445 chain_index (int idx, tree chain)
2447 for (; chain && idx > 0; --idx)
2448 chain = TREE_CHAIN (chain);
2449 return chain;
2452 /* Return nonzero if ELEM is part of the chain CHAIN. */
2455 chain_member (const_tree elem, const_tree chain)
2457 while (chain)
2459 if (elem == chain)
2460 return 1;
2461 chain = DECL_CHAIN (chain);
2464 return 0;
2467 /* Return the length of a chain of nodes chained through TREE_CHAIN.
2468 We expect a null pointer to mark the end of the chain.
2469 This is the Lisp primitive `length'. */
2472 list_length (const_tree t)
2474 const_tree p = t;
2475 #ifdef ENABLE_TREE_CHECKING
2476 const_tree q = t;
2477 #endif
2478 int len = 0;
2480 while (p)
2482 p = TREE_CHAIN (p);
2483 #ifdef ENABLE_TREE_CHECKING
2484 if (len % 2)
2485 q = TREE_CHAIN (q);
2486 gcc_assert (p != q);
2487 #endif
2488 len++;
2491 return len;
2494 /* Returns the first FIELD_DECL in the TYPE_FIELDS of the RECORD_TYPE or
2495 UNION_TYPE TYPE, or NULL_TREE if none. */
2497 tree
2498 first_field (const_tree type)
2500 tree t = TYPE_FIELDS (type);
2501 while (t && TREE_CODE (t) != FIELD_DECL)
2502 t = TREE_CHAIN (t);
2503 return t;
2506 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
2507 by modifying the last node in chain 1 to point to chain 2.
2508 This is the Lisp primitive `nconc'. */
2510 tree
2511 chainon (tree op1, tree op2)
2513 tree t1;
2515 if (!op1)
2516 return op2;
2517 if (!op2)
2518 return op1;
2520 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
2521 continue;
2522 TREE_CHAIN (t1) = op2;
2524 #ifdef ENABLE_TREE_CHECKING
2526 tree t2;
2527 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
2528 gcc_assert (t2 != t1);
2530 #endif
2532 return op1;
2535 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
2537 tree
2538 tree_last (tree chain)
2540 tree next;
2541 if (chain)
2542 while ((next = TREE_CHAIN (chain)))
2543 chain = next;
2544 return chain;
2547 /* Reverse the order of elements in the chain T,
2548 and return the new head of the chain (old last element). */
2550 tree
2551 nreverse (tree t)
2553 tree prev = 0, decl, next;
2554 for (decl = t; decl; decl = next)
2556 /* We shouldn't be using this function to reverse BLOCK chains; we
2557 have blocks_nreverse for that. */
2558 gcc_checking_assert (TREE_CODE (decl) != BLOCK);
2559 next = TREE_CHAIN (decl);
2560 TREE_CHAIN (decl) = prev;
2561 prev = decl;
2563 return prev;
2566 /* Return a newly created TREE_LIST node whose
2567 purpose and value fields are PARM and VALUE. */
2569 tree
2570 build_tree_list_stat (tree parm, tree value MEM_STAT_DECL)
2572 tree t = make_node_stat (TREE_LIST PASS_MEM_STAT);
2573 TREE_PURPOSE (t) = parm;
2574 TREE_VALUE (t) = value;
2575 return t;
2578 /* Build a chain of TREE_LIST nodes from a vector. */
2580 tree
2581 build_tree_list_vec_stat (const vec<tree, va_gc> *vec MEM_STAT_DECL)
2583 tree ret = NULL_TREE;
2584 tree *pp = &ret;
2585 unsigned int i;
2586 tree t;
2587 FOR_EACH_VEC_SAFE_ELT (vec, i, t)
2589 *pp = build_tree_list_stat (NULL, t PASS_MEM_STAT);
2590 pp = &TREE_CHAIN (*pp);
2592 return ret;
2595 /* Return a newly created TREE_LIST node whose
2596 purpose and value fields are PURPOSE and VALUE
2597 and whose TREE_CHAIN is CHAIN. */
2599 tree
2600 tree_cons_stat (tree purpose, tree value, tree chain MEM_STAT_DECL)
2602 tree node;
2604 node = ggc_alloc_tree_node_stat (sizeof (struct tree_list) PASS_MEM_STAT);
2605 memset (node, 0, sizeof (struct tree_common));
2607 record_node_allocation_statistics (TREE_LIST, sizeof (struct tree_list));
2609 TREE_SET_CODE (node, TREE_LIST);
2610 TREE_CHAIN (node) = chain;
2611 TREE_PURPOSE (node) = purpose;
2612 TREE_VALUE (node) = value;
2613 return node;
2616 /* Return the values of the elements of a CONSTRUCTOR as a vector of
2617 trees. */
2619 vec<tree, va_gc> *
2620 ctor_to_vec (tree ctor)
2622 vec<tree, va_gc> *vec;
2623 vec_alloc (vec, CONSTRUCTOR_NELTS (ctor));
2624 unsigned int ix;
2625 tree val;
2627 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), ix, val)
2628 vec->quick_push (val);
2630 return vec;
2633 /* Return the size nominally occupied by an object of type TYPE
2634 when it resides in memory. The value is measured in units of bytes,
2635 and its data type is that normally used for type sizes
2636 (which is the first type created by make_signed_type or
2637 make_unsigned_type). */
2639 tree
2640 size_in_bytes (const_tree type)
2642 tree t;
2644 if (type == error_mark_node)
2645 return integer_zero_node;
2647 type = TYPE_MAIN_VARIANT (type);
2648 t = TYPE_SIZE_UNIT (type);
2650 if (t == 0)
2652 lang_hooks.types.incomplete_type_error (NULL_TREE, type);
2653 return size_zero_node;
2656 return t;
2659 /* Returns a tree for the size of EXP in bytes. */
2661 tree
2662 tree_expr_size (const_tree exp)
2664 if (DECL_P (exp)
2665 && DECL_SIZE_UNIT (exp) != 0)
2666 return DECL_SIZE_UNIT (exp);
2667 else
2668 return size_in_bytes (TREE_TYPE (exp));
2671 /* Return the size of TYPE (in bytes) as a wide integer
2672 or return -1 if the size can vary or is larger than an integer. */
2674 HOST_WIDE_INT
2675 int_size_in_bytes (const_tree type)
2677 tree t;
2679 if (type == error_mark_node)
2680 return 0;
2682 type = TYPE_MAIN_VARIANT (type);
2683 t = TYPE_SIZE_UNIT (type);
2684 if (t == 0
2685 || TREE_CODE (t) != INTEGER_CST
2686 || TREE_INT_CST_HIGH (t) != 0
2687 /* If the result would appear negative, it's too big to represent. */
2688 || (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0)
2689 return -1;
2691 return TREE_INT_CST_LOW (t);
2694 /* Return the maximum size of TYPE (in bytes) as a wide integer
2695 or return -1 if the size can vary or is larger than an integer. */
2697 HOST_WIDE_INT
2698 max_int_size_in_bytes (const_tree type)
2700 HOST_WIDE_INT size = -1;
2701 tree size_tree;
2703 /* If this is an array type, check for a possible MAX_SIZE attached. */
2705 if (TREE_CODE (type) == ARRAY_TYPE)
2707 size_tree = TYPE_ARRAY_MAX_SIZE (type);
2709 if (size_tree && tree_fits_uhwi_p (size_tree))
2710 size = tree_to_uhwi (size_tree);
2713 /* If we still haven't been able to get a size, see if the language
2714 can compute a maximum size. */
2716 if (size == -1)
2718 size_tree = lang_hooks.types.max_size (type);
2720 if (size_tree && tree_fits_uhwi_p (size_tree))
2721 size = tree_to_uhwi (size_tree);
2724 return size;
2727 /* Return the bit position of FIELD, in bits from the start of the record.
2728 This is a tree of type bitsizetype. */
2730 tree
2731 bit_position (const_tree field)
2733 return bit_from_pos (DECL_FIELD_OFFSET (field),
2734 DECL_FIELD_BIT_OFFSET (field));
2737 /* Likewise, but return as an integer. It must be representable in
2738 that way (since it could be a signed value, we don't have the
2739 option of returning -1 like int_size_in_byte can. */
2741 HOST_WIDE_INT
2742 int_bit_position (const_tree field)
2744 return tree_to_shwi (bit_position (field));
2747 /* Return the byte position of FIELD, in bytes from the start of the record.
2748 This is a tree of type sizetype. */
2750 tree
2751 byte_position (const_tree field)
2753 return byte_from_pos (DECL_FIELD_OFFSET (field),
2754 DECL_FIELD_BIT_OFFSET (field));
2757 /* Likewise, but return as an integer. It must be representable in
2758 that way (since it could be a signed value, we don't have the
2759 option of returning -1 like int_size_in_byte can. */
2761 HOST_WIDE_INT
2762 int_byte_position (const_tree field)
2764 return tree_to_shwi (byte_position (field));
2767 /* Return the strictest alignment, in bits, that T is known to have. */
2769 unsigned int
2770 expr_align (const_tree t)
2772 unsigned int align0, align1;
2774 switch (TREE_CODE (t))
2776 CASE_CONVERT: case NON_LVALUE_EXPR:
2777 /* If we have conversions, we know that the alignment of the
2778 object must meet each of the alignments of the types. */
2779 align0 = expr_align (TREE_OPERAND (t, 0));
2780 align1 = TYPE_ALIGN (TREE_TYPE (t));
2781 return MAX (align0, align1);
2783 case SAVE_EXPR: case COMPOUND_EXPR: case MODIFY_EXPR:
2784 case INIT_EXPR: case TARGET_EXPR: case WITH_CLEANUP_EXPR:
2785 case CLEANUP_POINT_EXPR:
2786 /* These don't change the alignment of an object. */
2787 return expr_align (TREE_OPERAND (t, 0));
2789 case COND_EXPR:
2790 /* The best we can do is say that the alignment is the least aligned
2791 of the two arms. */
2792 align0 = expr_align (TREE_OPERAND (t, 1));
2793 align1 = expr_align (TREE_OPERAND (t, 2));
2794 return MIN (align0, align1);
2796 /* FIXME: LABEL_DECL and CONST_DECL never have DECL_ALIGN set
2797 meaningfully, it's always 1. */
2798 case LABEL_DECL: case CONST_DECL:
2799 case VAR_DECL: case PARM_DECL: case RESULT_DECL:
2800 case FUNCTION_DECL:
2801 gcc_assert (DECL_ALIGN (t) != 0);
2802 return DECL_ALIGN (t);
2804 default:
2805 break;
2808 /* Otherwise take the alignment from that of the type. */
2809 return TYPE_ALIGN (TREE_TYPE (t));
2812 /* Return, as a tree node, the number of elements for TYPE (which is an
2813 ARRAY_TYPE) minus one. This counts only elements of the top array. */
2815 tree
2816 array_type_nelts (const_tree type)
2818 tree index_type, min, max;
2820 /* If they did it with unspecified bounds, then we should have already
2821 given an error about it before we got here. */
2822 if (! TYPE_DOMAIN (type))
2823 return error_mark_node;
2825 index_type = TYPE_DOMAIN (type);
2826 min = TYPE_MIN_VALUE (index_type);
2827 max = TYPE_MAX_VALUE (index_type);
2829 /* TYPE_MAX_VALUE may not be set if the array has unknown length. */
2830 if (!max)
2831 return error_mark_node;
2833 return (integer_zerop (min)
2834 ? max
2835 : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
2838 /* If arg is static -- a reference to an object in static storage -- then
2839 return the object. This is not the same as the C meaning of `static'.
2840 If arg isn't static, return NULL. */
2842 tree
2843 staticp (tree arg)
2845 switch (TREE_CODE (arg))
2847 case FUNCTION_DECL:
2848 /* Nested functions are static, even though taking their address will
2849 involve a trampoline as we unnest the nested function and create
2850 the trampoline on the tree level. */
2851 return arg;
2853 case VAR_DECL:
2854 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2855 && ! DECL_THREAD_LOCAL_P (arg)
2856 && ! DECL_DLLIMPORT_P (arg)
2857 ? arg : NULL);
2859 case CONST_DECL:
2860 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2861 ? arg : NULL);
2863 case CONSTRUCTOR:
2864 return TREE_STATIC (arg) ? arg : NULL;
2866 case LABEL_DECL:
2867 case STRING_CST:
2868 return arg;
2870 case COMPONENT_REF:
2871 /* If the thing being referenced is not a field, then it is
2872 something language specific. */
2873 gcc_assert (TREE_CODE (TREE_OPERAND (arg, 1)) == FIELD_DECL);
2875 /* If we are referencing a bitfield, we can't evaluate an
2876 ADDR_EXPR at compile time and so it isn't a constant. */
2877 if (DECL_BIT_FIELD (TREE_OPERAND (arg, 1)))
2878 return NULL;
2880 return staticp (TREE_OPERAND (arg, 0));
2882 case BIT_FIELD_REF:
2883 return NULL;
2885 case INDIRECT_REF:
2886 return TREE_CONSTANT (TREE_OPERAND (arg, 0)) ? arg : NULL;
2888 case ARRAY_REF:
2889 case ARRAY_RANGE_REF:
2890 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
2891 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
2892 return staticp (TREE_OPERAND (arg, 0));
2893 else
2894 return NULL;
2896 case COMPOUND_LITERAL_EXPR:
2897 return TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (arg)) ? arg : NULL;
2899 default:
2900 return NULL;
2907 /* Return whether OP is a DECL whose address is function-invariant. */
2909 bool
2910 decl_address_invariant_p (const_tree op)
2912 /* The conditions below are slightly less strict than the one in
2913 staticp. */
2915 switch (TREE_CODE (op))
2917 case PARM_DECL:
2918 case RESULT_DECL:
2919 case LABEL_DECL:
2920 case FUNCTION_DECL:
2921 return true;
2923 case VAR_DECL:
2924 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
2925 || DECL_THREAD_LOCAL_P (op)
2926 || DECL_CONTEXT (op) == current_function_decl
2927 || decl_function_context (op) == current_function_decl)
2928 return true;
2929 break;
2931 case CONST_DECL:
2932 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
2933 || decl_function_context (op) == current_function_decl)
2934 return true;
2935 break;
2937 default:
2938 break;
2941 return false;
2944 /* Return whether OP is a DECL whose address is interprocedural-invariant. */
2946 bool
2947 decl_address_ip_invariant_p (const_tree op)
2949 /* The conditions below are slightly less strict than the one in
2950 staticp. */
2952 switch (TREE_CODE (op))
2954 case LABEL_DECL:
2955 case FUNCTION_DECL:
2956 case STRING_CST:
2957 return true;
2959 case VAR_DECL:
2960 if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
2961 && !DECL_DLLIMPORT_P (op))
2962 || DECL_THREAD_LOCAL_P (op))
2963 return true;
2964 break;
2966 case CONST_DECL:
2967 if ((TREE_STATIC (op) || DECL_EXTERNAL (op)))
2968 return true;
2969 break;
2971 default:
2972 break;
2975 return false;
2979 /* Return true if T is function-invariant (internal function, does
2980 not handle arithmetic; that's handled in skip_simple_arithmetic and
2981 tree_invariant_p). */
2983 static bool tree_invariant_p (tree t);
2985 static bool
2986 tree_invariant_p_1 (tree t)
2988 tree op;
2990 if (TREE_CONSTANT (t)
2991 || (TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t)))
2992 return true;
2994 switch (TREE_CODE (t))
2996 case SAVE_EXPR:
2997 return true;
2999 case ADDR_EXPR:
3000 op = TREE_OPERAND (t, 0);
3001 while (handled_component_p (op))
3003 switch (TREE_CODE (op))
3005 case ARRAY_REF:
3006 case ARRAY_RANGE_REF:
3007 if (!tree_invariant_p (TREE_OPERAND (op, 1))
3008 || TREE_OPERAND (op, 2) != NULL_TREE
3009 || TREE_OPERAND (op, 3) != NULL_TREE)
3010 return false;
3011 break;
3013 case COMPONENT_REF:
3014 if (TREE_OPERAND (op, 2) != NULL_TREE)
3015 return false;
3016 break;
3018 default:;
3020 op = TREE_OPERAND (op, 0);
3023 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
3025 default:
3026 break;
3029 return false;
3032 /* Return true if T is function-invariant. */
3034 static bool
3035 tree_invariant_p (tree t)
3037 tree inner = skip_simple_arithmetic (t);
3038 return tree_invariant_p_1 (inner);
3041 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
3042 Do this to any expression which may be used in more than one place,
3043 but must be evaluated only once.
3045 Normally, expand_expr would reevaluate the expression each time.
3046 Calling save_expr produces something that is evaluated and recorded
3047 the first time expand_expr is called on it. Subsequent calls to
3048 expand_expr just reuse the recorded value.
3050 The call to expand_expr that generates code that actually computes
3051 the value is the first call *at compile time*. Subsequent calls
3052 *at compile time* generate code to use the saved value.
3053 This produces correct result provided that *at run time* control
3054 always flows through the insns made by the first expand_expr
3055 before reaching the other places where the save_expr was evaluated.
3056 You, the caller of save_expr, must make sure this is so.
3058 Constants, and certain read-only nodes, are returned with no
3059 SAVE_EXPR because that is safe. Expressions containing placeholders
3060 are not touched; see tree.def for an explanation of what these
3061 are used for. */
3063 tree
3064 save_expr (tree expr)
3066 tree t = fold (expr);
3067 tree inner;
3069 /* If the tree evaluates to a constant, then we don't want to hide that
3070 fact (i.e. this allows further folding, and direct checks for constants).
3071 However, a read-only object that has side effects cannot be bypassed.
3072 Since it is no problem to reevaluate literals, we just return the
3073 literal node. */
3074 inner = skip_simple_arithmetic (t);
3075 if (TREE_CODE (inner) == ERROR_MARK)
3076 return inner;
3078 if (tree_invariant_p_1 (inner))
3079 return t;
3081 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
3082 it means that the size or offset of some field of an object depends on
3083 the value within another field.
3085 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
3086 and some variable since it would then need to be both evaluated once and
3087 evaluated more than once. Front-ends must assure this case cannot
3088 happen by surrounding any such subexpressions in their own SAVE_EXPR
3089 and forcing evaluation at the proper time. */
3090 if (contains_placeholder_p (inner))
3091 return t;
3093 t = build1 (SAVE_EXPR, TREE_TYPE (expr), t);
3094 SET_EXPR_LOCATION (t, EXPR_LOCATION (expr));
3096 /* This expression might be placed ahead of a jump to ensure that the
3097 value was computed on both sides of the jump. So make sure it isn't
3098 eliminated as dead. */
3099 TREE_SIDE_EFFECTS (t) = 1;
3100 return t;
3103 /* Look inside EXPR into any simple arithmetic operations. Return the
3104 outermost non-arithmetic or non-invariant node. */
3106 tree
3107 skip_simple_arithmetic (tree expr)
3109 /* We don't care about whether this can be used as an lvalue in this
3110 context. */
3111 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3112 expr = TREE_OPERAND (expr, 0);
3114 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
3115 a constant, it will be more efficient to not make another SAVE_EXPR since
3116 it will allow better simplification and GCSE will be able to merge the
3117 computations if they actually occur. */
3118 while (true)
3120 if (UNARY_CLASS_P (expr))
3121 expr = TREE_OPERAND (expr, 0);
3122 else if (BINARY_CLASS_P (expr))
3124 if (tree_invariant_p (TREE_OPERAND (expr, 1)))
3125 expr = TREE_OPERAND (expr, 0);
3126 else if (tree_invariant_p (TREE_OPERAND (expr, 0)))
3127 expr = TREE_OPERAND (expr, 1);
3128 else
3129 break;
3131 else
3132 break;
3135 return expr;
3138 /* Look inside EXPR into simple arithmetic operations involving constants.
3139 Return the outermost non-arithmetic or non-constant node. */
3141 tree
3142 skip_simple_constant_arithmetic (tree expr)
3144 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3145 expr = TREE_OPERAND (expr, 0);
3147 while (true)
3149 if (UNARY_CLASS_P (expr))
3150 expr = TREE_OPERAND (expr, 0);
3151 else if (BINARY_CLASS_P (expr))
3153 if (TREE_CONSTANT (TREE_OPERAND (expr, 1)))
3154 expr = TREE_OPERAND (expr, 0);
3155 else if (TREE_CONSTANT (TREE_OPERAND (expr, 0)))
3156 expr = TREE_OPERAND (expr, 1);
3157 else
3158 break;
3160 else
3161 break;
3164 return expr;
3167 /* Return which tree structure is used by T. */
3169 enum tree_node_structure_enum
3170 tree_node_structure (const_tree t)
3172 const enum tree_code code = TREE_CODE (t);
3173 return tree_node_structure_for_code (code);
3176 /* Set various status flags when building a CALL_EXPR object T. */
3178 static void
3179 process_call_operands (tree t)
3181 bool side_effects = TREE_SIDE_EFFECTS (t);
3182 bool read_only = false;
3183 int i = call_expr_flags (t);
3185 /* Calls have side-effects, except those to const or pure functions. */
3186 if ((i & ECF_LOOPING_CONST_OR_PURE) || !(i & (ECF_CONST | ECF_PURE)))
3187 side_effects = true;
3188 /* Propagate TREE_READONLY of arguments for const functions. */
3189 if (i & ECF_CONST)
3190 read_only = true;
3192 if (!side_effects || read_only)
3193 for (i = 1; i < TREE_OPERAND_LENGTH (t); i++)
3195 tree op = TREE_OPERAND (t, i);
3196 if (op && TREE_SIDE_EFFECTS (op))
3197 side_effects = true;
3198 if (op && !TREE_READONLY (op) && !CONSTANT_CLASS_P (op))
3199 read_only = false;
3202 TREE_SIDE_EFFECTS (t) = side_effects;
3203 TREE_READONLY (t) = read_only;
3206 /* Return true if EXP contains a PLACEHOLDER_EXPR, i.e. if it represents a
3207 size or offset that depends on a field within a record. */
3209 bool
3210 contains_placeholder_p (const_tree exp)
3212 enum tree_code code;
3214 if (!exp)
3215 return 0;
3217 code = TREE_CODE (exp);
3218 if (code == PLACEHOLDER_EXPR)
3219 return 1;
3221 switch (TREE_CODE_CLASS (code))
3223 case tcc_reference:
3224 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
3225 position computations since they will be converted into a
3226 WITH_RECORD_EXPR involving the reference, which will assume
3227 here will be valid. */
3228 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3230 case tcc_exceptional:
3231 if (code == TREE_LIST)
3232 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
3233 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
3234 break;
3236 case tcc_unary:
3237 case tcc_binary:
3238 case tcc_comparison:
3239 case tcc_expression:
3240 switch (code)
3242 case COMPOUND_EXPR:
3243 /* Ignoring the first operand isn't quite right, but works best. */
3244 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
3246 case COND_EXPR:
3247 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3248 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
3249 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
3251 case SAVE_EXPR:
3252 /* The save_expr function never wraps anything containing
3253 a PLACEHOLDER_EXPR. */
3254 return 0;
3256 default:
3257 break;
3260 switch (TREE_CODE_LENGTH (code))
3262 case 1:
3263 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3264 case 2:
3265 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3266 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
3267 default:
3268 return 0;
3271 case tcc_vl_exp:
3272 switch (code)
3274 case CALL_EXPR:
3276 const_tree arg;
3277 const_call_expr_arg_iterator iter;
3278 FOR_EACH_CONST_CALL_EXPR_ARG (arg, iter, exp)
3279 if (CONTAINS_PLACEHOLDER_P (arg))
3280 return 1;
3281 return 0;
3283 default:
3284 return 0;
3287 default:
3288 return 0;
3290 return 0;
3293 /* Return true if any part of the structure of TYPE involves a PLACEHOLDER_EXPR
3294 directly. This includes size, bounds, qualifiers (for QUAL_UNION_TYPE) and
3295 field positions. */
3297 static bool
3298 type_contains_placeholder_1 (const_tree type)
3300 /* If the size contains a placeholder or the parent type (component type in
3301 the case of arrays) type involves a placeholder, this type does. */
3302 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
3303 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
3304 || (!POINTER_TYPE_P (type)
3305 && TREE_TYPE (type)
3306 && type_contains_placeholder_p (TREE_TYPE (type))))
3307 return true;
3309 /* Now do type-specific checks. Note that the last part of the check above
3310 greatly limits what we have to do below. */
3311 switch (TREE_CODE (type))
3313 case VOID_TYPE:
3314 case COMPLEX_TYPE:
3315 case ENUMERAL_TYPE:
3316 case BOOLEAN_TYPE:
3317 case POINTER_TYPE:
3318 case OFFSET_TYPE:
3319 case REFERENCE_TYPE:
3320 case METHOD_TYPE:
3321 case FUNCTION_TYPE:
3322 case VECTOR_TYPE:
3323 case NULLPTR_TYPE:
3324 return false;
3326 case INTEGER_TYPE:
3327 case REAL_TYPE:
3328 case FIXED_POINT_TYPE:
3329 /* Here we just check the bounds. */
3330 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
3331 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
3333 case ARRAY_TYPE:
3334 /* We have already checked the component type above, so just check the
3335 domain type. */
3336 return type_contains_placeholder_p (TYPE_DOMAIN (type));
3338 case RECORD_TYPE:
3339 case UNION_TYPE:
3340 case QUAL_UNION_TYPE:
3342 tree field;
3344 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3345 if (TREE_CODE (field) == FIELD_DECL
3346 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
3347 || (TREE_CODE (type) == QUAL_UNION_TYPE
3348 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
3349 || type_contains_placeholder_p (TREE_TYPE (field))))
3350 return true;
3352 return false;
3355 default:
3356 gcc_unreachable ();
3360 /* Wrapper around above function used to cache its result. */
3362 bool
3363 type_contains_placeholder_p (tree type)
3365 bool result;
3367 /* If the contains_placeholder_bits field has been initialized,
3368 then we know the answer. */
3369 if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
3370 return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
3372 /* Indicate that we've seen this type node, and the answer is false.
3373 This is what we want to return if we run into recursion via fields. */
3374 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
3376 /* Compute the real value. */
3377 result = type_contains_placeholder_1 (type);
3379 /* Store the real value. */
3380 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
3382 return result;
3385 /* Push tree EXP onto vector QUEUE if it is not already present. */
3387 static void
3388 push_without_duplicates (tree exp, vec<tree> *queue)
3390 unsigned int i;
3391 tree iter;
3393 FOR_EACH_VEC_ELT (*queue, i, iter)
3394 if (simple_cst_equal (iter, exp) == 1)
3395 break;
3397 if (!iter)
3398 queue->safe_push (exp);
3401 /* Given a tree EXP, find all occurrences of references to fields
3402 in a PLACEHOLDER_EXPR and place them in vector REFS without
3403 duplicates. Also record VAR_DECLs and CONST_DECLs. Note that
3404 we assume here that EXP contains only arithmetic expressions
3405 or CALL_EXPRs with PLACEHOLDER_EXPRs occurring only in their
3406 argument list. */
3408 void
3409 find_placeholder_in_expr (tree exp, vec<tree> *refs)
3411 enum tree_code code = TREE_CODE (exp);
3412 tree inner;
3413 int i;
3415 /* We handle TREE_LIST and COMPONENT_REF separately. */
3416 if (code == TREE_LIST)
3418 FIND_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), refs);
3419 FIND_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), refs);
3421 else if (code == COMPONENT_REF)
3423 for (inner = TREE_OPERAND (exp, 0);
3424 REFERENCE_CLASS_P (inner);
3425 inner = TREE_OPERAND (inner, 0))
3428 if (TREE_CODE (inner) == PLACEHOLDER_EXPR)
3429 push_without_duplicates (exp, refs);
3430 else
3431 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), refs);
3433 else
3434 switch (TREE_CODE_CLASS (code))
3436 case tcc_constant:
3437 break;
3439 case tcc_declaration:
3440 /* Variables allocated to static storage can stay. */
3441 if (!TREE_STATIC (exp))
3442 push_without_duplicates (exp, refs);
3443 break;
3445 case tcc_expression:
3446 /* This is the pattern built in ada/make_aligning_type. */
3447 if (code == ADDR_EXPR
3448 && TREE_CODE (TREE_OPERAND (exp, 0)) == PLACEHOLDER_EXPR)
3450 push_without_duplicates (exp, refs);
3451 break;
3454 /* Fall through... */
3456 case tcc_exceptional:
3457 case tcc_unary:
3458 case tcc_binary:
3459 case tcc_comparison:
3460 case tcc_reference:
3461 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
3462 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3463 break;
3465 case tcc_vl_exp:
3466 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3467 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3468 break;
3470 default:
3471 gcc_unreachable ();
3475 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
3476 return a tree with all occurrences of references to F in a
3477 PLACEHOLDER_EXPR replaced by R. Also handle VAR_DECLs and
3478 CONST_DECLs. Note that we assume here that EXP contains only
3479 arithmetic expressions or CALL_EXPRs with PLACEHOLDER_EXPRs
3480 occurring only in their argument list. */
3482 tree
3483 substitute_in_expr (tree exp, tree f, tree r)
3485 enum tree_code code = TREE_CODE (exp);
3486 tree op0, op1, op2, op3;
3487 tree new_tree;
3489 /* We handle TREE_LIST and COMPONENT_REF separately. */
3490 if (code == TREE_LIST)
3492 op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
3493 op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
3494 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3495 return exp;
3497 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3499 else if (code == COMPONENT_REF)
3501 tree inner;
3503 /* If this expression is getting a value from a PLACEHOLDER_EXPR
3504 and it is the right field, replace it with R. */
3505 for (inner = TREE_OPERAND (exp, 0);
3506 REFERENCE_CLASS_P (inner);
3507 inner = TREE_OPERAND (inner, 0))
3510 /* The field. */
3511 op1 = TREE_OPERAND (exp, 1);
3513 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f)
3514 return r;
3516 /* If this expression hasn't been completed let, leave it alone. */
3517 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && !TREE_TYPE (inner))
3518 return exp;
3520 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3521 if (op0 == TREE_OPERAND (exp, 0))
3522 return exp;
3524 new_tree
3525 = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE);
3527 else
3528 switch (TREE_CODE_CLASS (code))
3530 case tcc_constant:
3531 return exp;
3533 case tcc_declaration:
3534 if (exp == f)
3535 return r;
3536 else
3537 return exp;
3539 case tcc_expression:
3540 if (exp == f)
3541 return r;
3543 /* Fall through... */
3545 case tcc_exceptional:
3546 case tcc_unary:
3547 case tcc_binary:
3548 case tcc_comparison:
3549 case tcc_reference:
3550 switch (TREE_CODE_LENGTH (code))
3552 case 0:
3553 return exp;
3555 case 1:
3556 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3557 if (op0 == TREE_OPERAND (exp, 0))
3558 return exp;
3560 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3561 break;
3563 case 2:
3564 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3565 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3567 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3568 return exp;
3570 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3571 break;
3573 case 3:
3574 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3575 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3576 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3578 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3579 && op2 == TREE_OPERAND (exp, 2))
3580 return exp;
3582 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3583 break;
3585 case 4:
3586 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3587 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3588 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3589 op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
3591 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3592 && op2 == TREE_OPERAND (exp, 2)
3593 && op3 == TREE_OPERAND (exp, 3))
3594 return exp;
3596 new_tree
3597 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3598 break;
3600 default:
3601 gcc_unreachable ();
3603 break;
3605 case tcc_vl_exp:
3607 int i;
3609 new_tree = NULL_TREE;
3611 /* If we are trying to replace F with a constant, inline back
3612 functions which do nothing else than computing a value from
3613 the arguments they are passed. This makes it possible to
3614 fold partially or entirely the replacement expression. */
3615 if (CONSTANT_CLASS_P (r) && code == CALL_EXPR)
3617 tree t = maybe_inline_call_in_expr (exp);
3618 if (t)
3619 return SUBSTITUTE_IN_EXPR (t, f, r);
3622 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3624 tree op = TREE_OPERAND (exp, i);
3625 tree new_op = SUBSTITUTE_IN_EXPR (op, f, r);
3626 if (new_op != op)
3628 if (!new_tree)
3629 new_tree = copy_node (exp);
3630 TREE_OPERAND (new_tree, i) = new_op;
3634 if (new_tree)
3636 new_tree = fold (new_tree);
3637 if (TREE_CODE (new_tree) == CALL_EXPR)
3638 process_call_operands (new_tree);
3640 else
3641 return exp;
3643 break;
3645 default:
3646 gcc_unreachable ();
3649 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3651 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3652 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3654 return new_tree;
3657 /* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
3658 for it within OBJ, a tree that is an object or a chain of references. */
3660 tree
3661 substitute_placeholder_in_expr (tree exp, tree obj)
3663 enum tree_code code = TREE_CODE (exp);
3664 tree op0, op1, op2, op3;
3665 tree new_tree;
3667 /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
3668 in the chain of OBJ. */
3669 if (code == PLACEHOLDER_EXPR)
3671 tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
3672 tree elt;
3674 for (elt = obj; elt != 0;
3675 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3676 || TREE_CODE (elt) == COND_EXPR)
3677 ? TREE_OPERAND (elt, 1)
3678 : (REFERENCE_CLASS_P (elt)
3679 || UNARY_CLASS_P (elt)
3680 || BINARY_CLASS_P (elt)
3681 || VL_EXP_CLASS_P (elt)
3682 || EXPRESSION_CLASS_P (elt))
3683 ? TREE_OPERAND (elt, 0) : 0))
3684 if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
3685 return elt;
3687 for (elt = obj; elt != 0;
3688 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3689 || TREE_CODE (elt) == COND_EXPR)
3690 ? TREE_OPERAND (elt, 1)
3691 : (REFERENCE_CLASS_P (elt)
3692 || UNARY_CLASS_P (elt)
3693 || BINARY_CLASS_P (elt)
3694 || VL_EXP_CLASS_P (elt)
3695 || EXPRESSION_CLASS_P (elt))
3696 ? TREE_OPERAND (elt, 0) : 0))
3697 if (POINTER_TYPE_P (TREE_TYPE (elt))
3698 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
3699 == need_type))
3700 return fold_build1 (INDIRECT_REF, need_type, elt);
3702 /* If we didn't find it, return the original PLACEHOLDER_EXPR. If it
3703 survives until RTL generation, there will be an error. */
3704 return exp;
3707 /* TREE_LIST is special because we need to look at TREE_VALUE
3708 and TREE_CHAIN, not TREE_OPERANDS. */
3709 else if (code == TREE_LIST)
3711 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
3712 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
3713 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3714 return exp;
3716 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3718 else
3719 switch (TREE_CODE_CLASS (code))
3721 case tcc_constant:
3722 case tcc_declaration:
3723 return exp;
3725 case tcc_exceptional:
3726 case tcc_unary:
3727 case tcc_binary:
3728 case tcc_comparison:
3729 case tcc_expression:
3730 case tcc_reference:
3731 case tcc_statement:
3732 switch (TREE_CODE_LENGTH (code))
3734 case 0:
3735 return exp;
3737 case 1:
3738 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3739 if (op0 == TREE_OPERAND (exp, 0))
3740 return exp;
3742 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3743 break;
3745 case 2:
3746 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3747 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3749 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3750 return exp;
3752 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3753 break;
3755 case 3:
3756 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3757 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3758 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3760 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3761 && op2 == TREE_OPERAND (exp, 2))
3762 return exp;
3764 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3765 break;
3767 case 4:
3768 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3769 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3770 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3771 op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
3773 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3774 && op2 == TREE_OPERAND (exp, 2)
3775 && op3 == TREE_OPERAND (exp, 3))
3776 return exp;
3778 new_tree
3779 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3780 break;
3782 default:
3783 gcc_unreachable ();
3785 break;
3787 case tcc_vl_exp:
3789 int i;
3791 new_tree = NULL_TREE;
3793 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3795 tree op = TREE_OPERAND (exp, i);
3796 tree new_op = SUBSTITUTE_PLACEHOLDER_IN_EXPR (op, obj);
3797 if (new_op != op)
3799 if (!new_tree)
3800 new_tree = copy_node (exp);
3801 TREE_OPERAND (new_tree, i) = new_op;
3805 if (new_tree)
3807 new_tree = fold (new_tree);
3808 if (TREE_CODE (new_tree) == CALL_EXPR)
3809 process_call_operands (new_tree);
3811 else
3812 return exp;
3814 break;
3816 default:
3817 gcc_unreachable ();
3820 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3822 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3823 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3825 return new_tree;
3829 /* Subroutine of stabilize_reference; this is called for subtrees of
3830 references. Any expression with side-effects must be put in a SAVE_EXPR
3831 to ensure that it is only evaluated once.
3833 We don't put SAVE_EXPR nodes around everything, because assigning very
3834 simple expressions to temporaries causes us to miss good opportunities
3835 for optimizations. Among other things, the opportunity to fold in the
3836 addition of a constant into an addressing mode often gets lost, e.g.
3837 "y[i+1] += x;". In general, we take the approach that we should not make
3838 an assignment unless we are forced into it - i.e., that any non-side effect
3839 operator should be allowed, and that cse should take care of coalescing
3840 multiple utterances of the same expression should that prove fruitful. */
3842 static tree
3843 stabilize_reference_1 (tree e)
3845 tree result;
3846 enum tree_code code = TREE_CODE (e);
3848 /* We cannot ignore const expressions because it might be a reference
3849 to a const array but whose index contains side-effects. But we can
3850 ignore things that are actual constant or that already have been
3851 handled by this function. */
3853 if (tree_invariant_p (e))
3854 return e;
3856 switch (TREE_CODE_CLASS (code))
3858 case tcc_exceptional:
3859 case tcc_type:
3860 case tcc_declaration:
3861 case tcc_comparison:
3862 case tcc_statement:
3863 case tcc_expression:
3864 case tcc_reference:
3865 case tcc_vl_exp:
3866 /* If the expression has side-effects, then encase it in a SAVE_EXPR
3867 so that it will only be evaluated once. */
3868 /* The reference (r) and comparison (<) classes could be handled as
3869 below, but it is generally faster to only evaluate them once. */
3870 if (TREE_SIDE_EFFECTS (e))
3871 return save_expr (e);
3872 return e;
3874 case tcc_constant:
3875 /* Constants need no processing. In fact, we should never reach
3876 here. */
3877 return e;
3879 case tcc_binary:
3880 /* Division is slow and tends to be compiled with jumps,
3881 especially the division by powers of 2 that is often
3882 found inside of an array reference. So do it just once. */
3883 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
3884 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
3885 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
3886 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
3887 return save_expr (e);
3888 /* Recursively stabilize each operand. */
3889 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
3890 stabilize_reference_1 (TREE_OPERAND (e, 1)));
3891 break;
3893 case tcc_unary:
3894 /* Recursively stabilize each operand. */
3895 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
3896 break;
3898 default:
3899 gcc_unreachable ();
3902 TREE_TYPE (result) = TREE_TYPE (e);
3903 TREE_READONLY (result) = TREE_READONLY (e);
3904 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
3905 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
3906 TREE_SHARED (result) = TREE_SHARED (e);
3907 TREE_STRICT (result) = TREE_STRICT (e);
3908 TREE_RELAXED (result) = TREE_RELAXED (e);
3910 return result;
3913 /* Stabilize a reference so that we can use it any number of times
3914 without causing its operands to be evaluated more than once.
3915 Returns the stabilized reference. This works by means of save_expr,
3916 so see the caveats in the comments about save_expr.
3918 Also allows conversion expressions whose operands are references.
3919 Any other kind of expression is returned unchanged. */
3921 tree
3922 stabilize_reference (tree ref)
3924 tree result;
3925 enum tree_code code = TREE_CODE (ref);
3927 switch (code)
3929 case VAR_DECL:
3930 case PARM_DECL:
3931 case RESULT_DECL:
3932 /* No action is needed in this case. */
3933 return ref;
3935 CASE_CONVERT:
3936 case FLOAT_EXPR:
3937 case FIX_TRUNC_EXPR:
3938 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
3939 break;
3941 case INDIRECT_REF:
3942 result = build_nt (INDIRECT_REF,
3943 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
3944 break;
3946 case COMPONENT_REF:
3947 result = build_nt (COMPONENT_REF,
3948 stabilize_reference (TREE_OPERAND (ref, 0)),
3949 TREE_OPERAND (ref, 1), NULL_TREE);
3950 break;
3952 case BIT_FIELD_REF:
3953 result = build_nt (BIT_FIELD_REF,
3954 stabilize_reference (TREE_OPERAND (ref, 0)),
3955 TREE_OPERAND (ref, 1), TREE_OPERAND (ref, 2));
3956 break;
3958 case ARRAY_REF:
3959 result = build_nt (ARRAY_REF,
3960 stabilize_reference (TREE_OPERAND (ref, 0)),
3961 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
3962 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
3963 break;
3965 case ARRAY_RANGE_REF:
3966 result = build_nt (ARRAY_RANGE_REF,
3967 stabilize_reference (TREE_OPERAND (ref, 0)),
3968 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
3969 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
3970 break;
3972 case COMPOUND_EXPR:
3973 /* We cannot wrap the first expression in a SAVE_EXPR, as then
3974 it wouldn't be ignored. This matters when dealing with
3975 volatiles. */
3976 return stabilize_reference_1 (ref);
3978 /* If arg isn't a kind of lvalue we recognize, make no change.
3979 Caller should recognize the error for an invalid lvalue. */
3980 default:
3981 return ref;
3983 case ERROR_MARK:
3984 return error_mark_node;
3987 TREE_TYPE (result) = TREE_TYPE (ref);
3988 TREE_READONLY (result) = TREE_READONLY (ref);
3989 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
3990 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
3991 TREE_SHARED (result) = TREE_SHARED (ref);
3992 TREE_STRICT (result) = TREE_STRICT (ref);
3993 TREE_RELAXED (result) = TREE_RELAXED (ref);
3995 return result;
3999 /* Low-level constructors for expressions. */
4001 /* A helper function for build1 and constant folders. Set TREE_CONSTANT,
4002 and TREE_SIDE_EFFECTS for an ADDR_EXPR. */
4004 void
4005 recompute_tree_invariant_for_addr_expr (tree t)
4007 tree node;
4008 bool tc = true, se = false;
4010 /* We started out assuming this address is both invariant and constant, but
4011 does not have side effects. Now go down any handled components and see if
4012 any of them involve offsets that are either non-constant or non-invariant.
4013 Also check for side-effects.
4015 ??? Note that this code makes no attempt to deal with the case where
4016 taking the address of something causes a copy due to misalignment. */
4018 #define UPDATE_FLAGS(NODE) \
4019 do { tree _node = (NODE); \
4020 if (_node && !TREE_CONSTANT (_node)) tc = false; \
4021 if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
4023 for (node = TREE_OPERAND (t, 0); handled_component_p (node);
4024 node = TREE_OPERAND (node, 0))
4026 /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
4027 array reference (probably made temporarily by the G++ front end),
4028 so ignore all the operands. */
4029 if ((TREE_CODE (node) == ARRAY_REF
4030 || TREE_CODE (node) == ARRAY_RANGE_REF)
4031 && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
4033 UPDATE_FLAGS (TREE_OPERAND (node, 1));
4034 if (TREE_OPERAND (node, 2))
4035 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4036 if (TREE_OPERAND (node, 3))
4037 UPDATE_FLAGS (TREE_OPERAND (node, 3));
4039 /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
4040 FIELD_DECL, apparently. The G++ front end can put something else
4041 there, at least temporarily. */
4042 else if (TREE_CODE (node) == COMPONENT_REF
4043 && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
4045 if (TREE_OPERAND (node, 2))
4046 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4050 node = lang_hooks.expr_to_decl (node, &tc, &se);
4052 /* Now see what's inside. If it's an INDIRECT_REF, copy our properties from
4053 the address, since &(*a)->b is a form of addition. If it's a constant, the
4054 address is constant too. If it's a decl, its address is constant if the
4055 decl is static. Everything else is not constant and, furthermore,
4056 taking the address of a volatile variable is not volatile. */
4057 if (TREE_CODE (node) == INDIRECT_REF
4058 || TREE_CODE (node) == MEM_REF)
4059 UPDATE_FLAGS (TREE_OPERAND (node, 0));
4060 else if (CONSTANT_CLASS_P (node))
4062 else if (DECL_P (node))
4063 tc &= (staticp (node) != NULL_TREE);
4064 else
4066 tc = false;
4067 se |= TREE_SIDE_EFFECTS (node);
4071 TREE_CONSTANT (t) = tc;
4072 TREE_SIDE_EFFECTS (t) = se;
4073 #undef UPDATE_FLAGS
4076 /* Build an expression of code CODE, data type TYPE, and operands as
4077 specified. Expressions and reference nodes can be created this way.
4078 Constants, decls, types and misc nodes cannot be.
4080 We define 5 non-variadic functions, from 0 to 4 arguments. This is
4081 enough for all extant tree codes. */
4083 tree
4084 build0_stat (enum tree_code code, tree tt MEM_STAT_DECL)
4086 tree t;
4088 gcc_assert (TREE_CODE_LENGTH (code) == 0);
4090 t = make_node_stat (code PASS_MEM_STAT);
4091 TREE_TYPE (t) = tt;
4093 return t;
4096 tree
4097 build1_stat (enum tree_code code, tree type, tree node MEM_STAT_DECL)
4099 int length = sizeof (struct tree_exp);
4100 tree t;
4102 record_node_allocation_statistics (code, length);
4104 gcc_assert (TREE_CODE_LENGTH (code) == 1);
4106 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
4108 memset (t, 0, sizeof (struct tree_common));
4110 TREE_SET_CODE (t, code);
4112 TREE_TYPE (t) = type;
4113 SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
4114 TREE_OPERAND (t, 0) = node;
4115 if (node && !TYPE_P (node))
4117 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
4118 TREE_READONLY (t) = TREE_READONLY (node);
4121 if (TREE_CODE_CLASS (code) == tcc_statement)
4122 TREE_SIDE_EFFECTS (t) = 1;
4123 else switch (code)
4125 case VA_ARG_EXPR:
4126 /* All of these have side-effects, no matter what their
4127 operands are. */
4128 TREE_SIDE_EFFECTS (t) = 1;
4129 TREE_READONLY (t) = 0;
4130 break;
4132 case INDIRECT_REF:
4133 /* Whether a dereference is readonly has nothing to do with whether
4134 its operand is readonly. */
4135 TREE_READONLY (t) = 0;
4136 TREE_SHARED (t) = upc_shared_type_p (type);
4137 break;
4139 case ADDR_EXPR:
4140 if (node)
4141 recompute_tree_invariant_for_addr_expr (t);
4142 break;
4144 default:
4145 if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
4146 && node && !TYPE_P (node)
4147 && TREE_CONSTANT (node))
4148 TREE_CONSTANT (t) = 1;
4149 if (TREE_CODE_CLASS (code) == tcc_reference
4150 && node && TREE_THIS_VOLATILE (node))
4151 TREE_THIS_VOLATILE (t) = 1;
4152 /* Drop the UPC "shared" type qualifier for
4153 expressions involving UPC shared objects. */
4154 if (TREE_CODE_CLASS (code) == tcc_unary
4155 && node && !TYPE_P (node)
4156 && upc_shared_type_p (type))
4157 TREE_TYPE (t) = build_upc_unshared_type (type);
4158 break;
4161 return t;
4164 #define PROCESS_ARG(N) \
4165 do { \
4166 TREE_OPERAND (t, N) = arg##N; \
4167 if (arg##N &&!TYPE_P (arg##N)) \
4169 if (TREE_SIDE_EFFECTS (arg##N)) \
4170 side_effects = 1; \
4171 if (!TREE_READONLY (arg##N) \
4172 && !CONSTANT_CLASS_P (arg##N)) \
4173 (void) (read_only = 0); \
4174 if (!TREE_CONSTANT (arg##N)) \
4175 (void) (constant = 0); \
4177 } while (0)
4179 tree
4180 build2_stat (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
4182 bool constant, read_only, side_effects;
4183 tree t;
4185 gcc_assert (TREE_CODE_LENGTH (code) == 2);
4187 if ((code == MINUS_EXPR || code == PLUS_EXPR || code == MULT_EXPR)
4188 && arg0 && arg1 && tt && POINTER_TYPE_P (tt)
4189 /* When sizetype precision doesn't match that of pointers
4190 we need to be able to build explicit extensions or truncations
4191 of the offset argument. */
4192 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (tt))
4193 gcc_assert (TREE_CODE (arg0) == INTEGER_CST
4194 && TREE_CODE (arg1) == INTEGER_CST);
4196 if (code == POINTER_PLUS_EXPR && arg0 && arg1 && tt)
4197 gcc_assert (POINTER_TYPE_P (tt) && POINTER_TYPE_P (TREE_TYPE (arg0))
4198 && ptrofftype_p (TREE_TYPE (arg1)));
4200 t = make_node_stat (code PASS_MEM_STAT);
4202 /* Remove UPC shared type qualifiers from the result type. */
4203 if (upc_shared_type_p (tt))
4204 tt = build_upc_unshared_type (tt);
4205 TREE_TYPE (t) = tt;
4207 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
4208 result based on those same flags for the arguments. But if the
4209 arguments aren't really even `tree' expressions, we shouldn't be trying
4210 to do this. */
4212 /* Expressions without side effects may be constant if their
4213 arguments are as well. */
4214 constant = (TREE_CODE_CLASS (code) == tcc_comparison
4215 || TREE_CODE_CLASS (code) == tcc_binary);
4216 read_only = 1;
4217 side_effects = TREE_SIDE_EFFECTS (t);
4219 PROCESS_ARG (0);
4220 PROCESS_ARG (1);
4222 TREE_READONLY (t) = read_only;
4223 TREE_CONSTANT (t) = constant;
4224 TREE_SIDE_EFFECTS (t) = side_effects;
4225 TREE_THIS_VOLATILE (t)
4226 = (TREE_CODE_CLASS (code) == tcc_reference
4227 && arg0 && TREE_THIS_VOLATILE (arg0));
4229 return t;
4233 tree
4234 build3_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4235 tree arg2 MEM_STAT_DECL)
4237 bool constant, read_only, side_effects;
4238 tree t;
4240 gcc_assert (TREE_CODE_LENGTH (code) == 3);
4241 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4243 t = make_node_stat (code PASS_MEM_STAT);
4244 TREE_TYPE (t) = tt;
4246 read_only = 1;
4248 /* As a special exception, if COND_EXPR has NULL branches, we
4249 assume that it is a gimple statement and always consider
4250 it to have side effects. */
4251 if (code == COND_EXPR
4252 && tt == void_type_node
4253 && arg1 == NULL_TREE
4254 && arg2 == NULL_TREE)
4255 side_effects = true;
4256 else
4257 side_effects = TREE_SIDE_EFFECTS (t);
4259 PROCESS_ARG (0);
4260 PROCESS_ARG (1);
4261 PROCESS_ARG (2);
4263 if (code == COND_EXPR)
4264 TREE_READONLY (t) = read_only;
4266 TREE_SIDE_EFFECTS (t) = side_effects;
4267 TREE_THIS_VOLATILE (t)
4268 = (TREE_CODE_CLASS (code) == tcc_reference
4269 && arg0 && TREE_THIS_VOLATILE (arg0));
4270 TREE_SHARED (t)
4271 = (TREE_CODE_CLASS (code) == tcc_reference
4272 && arg0 && TREE_SHARED (arg0));
4273 if (TREE_SHARED (t))
4275 TREE_STRICT (t) = TREE_STRICT (arg0);
4276 TREE_RELAXED (t) = TREE_RELAXED (arg0);
4279 return t;
4282 tree
4283 build4_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4284 tree arg2, tree arg3 MEM_STAT_DECL)
4286 bool constant, read_only, side_effects;
4287 tree t;
4289 gcc_assert (TREE_CODE_LENGTH (code) == 4);
4291 t = make_node_stat (code PASS_MEM_STAT);
4292 TREE_TYPE (t) = tt;
4294 side_effects = TREE_SIDE_EFFECTS (t);
4296 PROCESS_ARG (0);
4297 PROCESS_ARG (1);
4298 PROCESS_ARG (2);
4299 PROCESS_ARG (3);
4301 TREE_SIDE_EFFECTS (t) = side_effects;
4302 TREE_THIS_VOLATILE (t)
4303 = (TREE_CODE_CLASS (code) == tcc_reference
4304 && arg0 && TREE_THIS_VOLATILE (arg0));
4306 return t;
4309 tree
4310 build5_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4311 tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
4313 bool constant, read_only, side_effects;
4314 tree t;
4316 gcc_assert (TREE_CODE_LENGTH (code) == 5);
4318 t = make_node_stat (code PASS_MEM_STAT);
4319 TREE_TYPE (t) = tt;
4321 side_effects = TREE_SIDE_EFFECTS (t);
4323 PROCESS_ARG (0);
4324 PROCESS_ARG (1);
4325 PROCESS_ARG (2);
4326 PROCESS_ARG (3);
4327 PROCESS_ARG (4);
4329 TREE_SIDE_EFFECTS (t) = side_effects;
4330 TREE_THIS_VOLATILE (t)
4331 = (TREE_CODE_CLASS (code) == tcc_reference
4332 && arg0 && TREE_THIS_VOLATILE (arg0));
4334 return t;
4337 /* Build a simple MEM_REF tree with the sematics of a plain INDIRECT_REF
4338 on the pointer PTR. */
4340 tree
4341 build_simple_mem_ref_loc (location_t loc, tree ptr)
4343 HOST_WIDE_INT offset = 0;
4344 tree ptype = TREE_TYPE (ptr);
4345 tree tem;
4346 /* For convenience allow addresses that collapse to a simple base
4347 and offset. */
4348 if (TREE_CODE (ptr) == ADDR_EXPR
4349 && (handled_component_p (TREE_OPERAND (ptr, 0))
4350 || TREE_CODE (TREE_OPERAND (ptr, 0)) == MEM_REF))
4352 ptr = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &offset);
4353 gcc_assert (ptr);
4354 ptr = build_fold_addr_expr (ptr);
4355 gcc_assert (is_gimple_reg (ptr) || is_gimple_min_invariant (ptr));
4357 tem = build2 (MEM_REF, TREE_TYPE (ptype),
4358 ptr, build_int_cst (ptype, offset));
4359 SET_EXPR_LOCATION (tem, loc);
4360 return tem;
4363 /* Return the constant offset of a MEM_REF or TARGET_MEM_REF tree T. */
4365 double_int
4366 mem_ref_offset (const_tree t)
4368 tree toff = TREE_OPERAND (t, 1);
4369 return tree_to_double_int (toff).sext (TYPE_PRECISION (TREE_TYPE (toff)));
4372 /* Return an invariant ADDR_EXPR of type TYPE taking the address of BASE
4373 offsetted by OFFSET units. */
4375 tree
4376 build_invariant_address (tree type, tree base, HOST_WIDE_INT offset)
4378 tree ref = fold_build2 (MEM_REF, TREE_TYPE (type),
4379 build_fold_addr_expr (base),
4380 build_int_cst (ptr_type_node, offset));
4381 tree addr = build1 (ADDR_EXPR, type, ref);
4382 recompute_tree_invariant_for_addr_expr (addr);
4383 return addr;
4386 /* Similar except don't specify the TREE_TYPE
4387 and leave the TREE_SIDE_EFFECTS as 0.
4388 It is permissible for arguments to be null,
4389 or even garbage if their values do not matter. */
4391 tree
4392 build_nt (enum tree_code code, ...)
4394 tree t;
4395 int length;
4396 int i;
4397 va_list p;
4399 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4401 va_start (p, code);
4403 t = make_node (code);
4404 length = TREE_CODE_LENGTH (code);
4406 for (i = 0; i < length; i++)
4407 TREE_OPERAND (t, i) = va_arg (p, tree);
4409 va_end (p);
4410 return t;
4413 /* Similar to build_nt, but for creating a CALL_EXPR object with a
4414 tree vec. */
4416 tree
4417 build_nt_call_vec (tree fn, vec<tree, va_gc> *args)
4419 tree ret, t;
4420 unsigned int ix;
4422 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
4423 CALL_EXPR_FN (ret) = fn;
4424 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
4425 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
4426 CALL_EXPR_ARG (ret, ix) = t;
4427 return ret;
4430 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
4431 We do NOT enter this node in any sort of symbol table.
4433 LOC is the location of the decl.
4435 layout_decl is used to set up the decl's storage layout.
4436 Other slots are initialized to 0 or null pointers. */
4438 tree
4439 build_decl_stat (location_t loc, enum tree_code code, tree name,
4440 tree type MEM_STAT_DECL)
4442 tree t;
4444 t = make_node_stat (code PASS_MEM_STAT);
4445 DECL_SOURCE_LOCATION (t) = loc;
4447 /* if (type == error_mark_node)
4448 type = integer_type_node; */
4449 /* That is not done, deliberately, so that having error_mark_node
4450 as the type can suppress useless errors in the use of this variable. */
4452 DECL_NAME (t) = name;
4453 TREE_TYPE (t) = type;
4455 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
4456 layout_decl (t, 0);
4458 return t;
4461 /* Builds and returns function declaration with NAME and TYPE. */
4463 tree
4464 build_fn_decl (const char *name, tree type)
4466 tree id = get_identifier (name);
4467 tree decl = build_decl (input_location, FUNCTION_DECL, id, type);
4469 DECL_EXTERNAL (decl) = 1;
4470 TREE_PUBLIC (decl) = 1;
4471 DECL_ARTIFICIAL (decl) = 1;
4472 TREE_NOTHROW (decl) = 1;
4474 return decl;
4477 vec<tree, va_gc> *all_translation_units;
4479 /* Builds a new translation-unit decl with name NAME, queues it in the
4480 global list of translation-unit decls and returns it. */
4482 tree
4483 build_translation_unit_decl (tree name)
4485 tree tu = build_decl (UNKNOWN_LOCATION, TRANSLATION_UNIT_DECL,
4486 name, NULL_TREE);
4487 TRANSLATION_UNIT_LANGUAGE (tu) = lang_hooks.name;
4488 vec_safe_push (all_translation_units, tu);
4489 return tu;
4493 /* BLOCK nodes are used to represent the structure of binding contours
4494 and declarations, once those contours have been exited and their contents
4495 compiled. This information is used for outputting debugging info. */
4497 tree
4498 build_block (tree vars, tree subblocks, tree supercontext, tree chain)
4500 tree block = make_node (BLOCK);
4502 BLOCK_VARS (block) = vars;
4503 BLOCK_SUBBLOCKS (block) = subblocks;
4504 BLOCK_SUPERCONTEXT (block) = supercontext;
4505 BLOCK_CHAIN (block) = chain;
4506 return block;
4510 /* Like SET_EXPR_LOCATION, but make sure the tree can have a location.
4512 LOC is the location to use in tree T. */
4514 void
4515 protected_set_expr_location (tree t, location_t loc)
4517 if (t && CAN_HAVE_LOCATION_P (t))
4518 SET_EXPR_LOCATION (t, loc);
4521 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
4522 is ATTRIBUTE. */
4524 tree
4525 build_decl_attribute_variant (tree ddecl, tree attribute)
4527 DECL_ATTRIBUTES (ddecl) = attribute;
4528 return ddecl;
4531 /* Borrowed from hashtab.c iterative_hash implementation. */
4532 #define mix(a,b,c) \
4534 a -= b; a -= c; a ^= (c>>13); \
4535 b -= c; b -= a; b ^= (a<< 8); \
4536 c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \
4537 a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \
4538 b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \
4539 c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \
4540 a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \
4541 b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \
4542 c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \
4546 /* Produce good hash value combining VAL and VAL2. */
4547 hashval_t
4548 iterative_hash_hashval_t (hashval_t val, hashval_t val2)
4550 /* the golden ratio; an arbitrary value. */
4551 hashval_t a = 0x9e3779b9;
4553 mix (a, val, val2);
4554 return val2;
4557 /* Produce good hash value combining VAL and VAL2. */
4558 hashval_t
4559 iterative_hash_host_wide_int (HOST_WIDE_INT val, hashval_t val2)
4561 if (sizeof (HOST_WIDE_INT) == sizeof (hashval_t))
4562 return iterative_hash_hashval_t (val, val2);
4563 else
4565 hashval_t a = (hashval_t) val;
4566 /* Avoid warnings about shifting of more than the width of the type on
4567 hosts that won't execute this path. */
4568 int zero = 0;
4569 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 8 + zero));
4570 mix (a, b, val2);
4571 if (sizeof (HOST_WIDE_INT) > 2 * sizeof (hashval_t))
4573 hashval_t a = (hashval_t) (val >> (sizeof (hashval_t) * 16 + zero));
4574 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 24 + zero));
4575 mix (a, b, val2);
4577 return val2;
4581 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4582 is ATTRIBUTE and its qualifiers are QUALS.
4584 Record such modified types already made so we don't make duplicates. */
4586 tree
4587 build_type_attribute_qual_variant (tree ttype, tree attribute, int quals)
4589 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
4591 hashval_t hashcode = 0;
4592 tree ntype;
4593 enum tree_code code = TREE_CODE (ttype);
4595 /* Building a distinct copy of a tagged type is inappropriate; it
4596 causes breakage in code that expects there to be a one-to-one
4597 relationship between a struct and its fields.
4598 build_duplicate_type is another solution (as used in
4599 handle_transparent_union_attribute), but that doesn't play well
4600 with the stronger C++ type identity model. */
4601 if (TREE_CODE (ttype) == RECORD_TYPE
4602 || TREE_CODE (ttype) == UNION_TYPE
4603 || TREE_CODE (ttype) == QUAL_UNION_TYPE
4604 || TREE_CODE (ttype) == ENUMERAL_TYPE)
4606 warning (OPT_Wattributes,
4607 "ignoring attributes applied to %qT after definition",
4608 TYPE_MAIN_VARIANT (ttype));
4609 return build_qualified_type (ttype, quals);
4612 ttype = build_qualified_type (ttype, TYPE_UNQUALIFIED);
4613 ntype = build_distinct_type_copy (ttype);
4615 TYPE_ATTRIBUTES (ntype) = attribute;
4617 hashcode = iterative_hash_object (code, hashcode);
4618 if (TREE_TYPE (ntype))
4619 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (ntype)),
4620 hashcode);
4621 hashcode = attribute_hash_list (attribute, hashcode);
4623 switch (TREE_CODE (ntype))
4625 case FUNCTION_TYPE:
4626 hashcode = type_hash_list (TYPE_ARG_TYPES (ntype), hashcode);
4627 break;
4628 case ARRAY_TYPE:
4629 if (TYPE_DOMAIN (ntype))
4630 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (ntype)),
4631 hashcode);
4632 break;
4633 case INTEGER_TYPE:
4634 hashcode = iterative_hash_object
4635 (TREE_INT_CST_LOW (TYPE_MAX_VALUE (ntype)), hashcode);
4636 hashcode = iterative_hash_object
4637 (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (ntype)), hashcode);
4638 break;
4639 case REAL_TYPE:
4640 case FIXED_POINT_TYPE:
4642 unsigned int precision = TYPE_PRECISION (ntype);
4643 hashcode = iterative_hash_object (precision, hashcode);
4645 break;
4646 default:
4647 break;
4650 ntype = type_hash_canon (hashcode, ntype);
4652 /* If the target-dependent attributes make NTYPE different from
4653 its canonical type, we will need to use structural equality
4654 checks for this type. */
4655 if (TYPE_STRUCTURAL_EQUALITY_P (ttype)
4656 || !comp_type_attributes (ntype, ttype))
4657 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
4658 else if (TYPE_CANONICAL (ntype) == ntype)
4659 TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype);
4661 ttype = build_qualified_type (ntype, quals);
4663 else if (TYPE_QUALS (ttype) != quals)
4664 ttype = build_qualified_type (ttype, quals);
4666 return ttype;
4669 /* Check if "omp declare simd" attribute arguments, CLAUSES1 and CLAUSES2, are
4670 the same. */
4672 static bool
4673 omp_declare_simd_clauses_equal (tree clauses1, tree clauses2)
4675 tree cl1, cl2;
4676 for (cl1 = clauses1, cl2 = clauses2;
4677 cl1 && cl2;
4678 cl1 = OMP_CLAUSE_CHAIN (cl1), cl2 = OMP_CLAUSE_CHAIN (cl2))
4680 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_CODE (cl2))
4681 return false;
4682 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_SIMDLEN)
4684 if (simple_cst_equal (OMP_CLAUSE_DECL (cl1),
4685 OMP_CLAUSE_DECL (cl2)) != 1)
4686 return false;
4688 switch (OMP_CLAUSE_CODE (cl1))
4690 case OMP_CLAUSE_ALIGNED:
4691 if (simple_cst_equal (OMP_CLAUSE_ALIGNED_ALIGNMENT (cl1),
4692 OMP_CLAUSE_ALIGNED_ALIGNMENT (cl2)) != 1)
4693 return false;
4694 break;
4695 case OMP_CLAUSE_LINEAR:
4696 if (simple_cst_equal (OMP_CLAUSE_LINEAR_STEP (cl1),
4697 OMP_CLAUSE_LINEAR_STEP (cl2)) != 1)
4698 return false;
4699 break;
4700 case OMP_CLAUSE_SIMDLEN:
4701 if (simple_cst_equal (OMP_CLAUSE_SIMDLEN_EXPR (cl1),
4702 OMP_CLAUSE_SIMDLEN_EXPR (cl2)) != 1)
4703 return false;
4704 default:
4705 break;
4708 return true;
4711 /* Compare two constructor-element-type constants. Return 1 if the lists
4712 are known to be equal; otherwise return 0. */
4714 static bool
4715 simple_cst_list_equal (const_tree l1, const_tree l2)
4717 while (l1 != NULL_TREE && l2 != NULL_TREE)
4719 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
4720 return false;
4722 l1 = TREE_CHAIN (l1);
4723 l2 = TREE_CHAIN (l2);
4726 return l1 == l2;
4729 /* Compare two attributes for their value identity. Return true if the
4730 attribute values are known to be equal; otherwise return false.
4733 static bool
4734 attribute_value_equal (const_tree attr1, const_tree attr2)
4736 if (TREE_VALUE (attr1) == TREE_VALUE (attr2))
4737 return true;
4739 if (TREE_VALUE (attr1) != NULL_TREE
4740 && TREE_CODE (TREE_VALUE (attr1)) == TREE_LIST
4741 && TREE_VALUE (attr2) != NULL
4742 && TREE_CODE (TREE_VALUE (attr2)) == TREE_LIST)
4743 return (simple_cst_list_equal (TREE_VALUE (attr1),
4744 TREE_VALUE (attr2)) == 1);
4746 if ((flag_openmp || flag_openmp_simd)
4747 && TREE_VALUE (attr1) && TREE_VALUE (attr2)
4748 && TREE_CODE (TREE_VALUE (attr1)) == OMP_CLAUSE
4749 && TREE_CODE (TREE_VALUE (attr2)) == OMP_CLAUSE)
4750 return omp_declare_simd_clauses_equal (TREE_VALUE (attr1),
4751 TREE_VALUE (attr2));
4753 return (simple_cst_equal (TREE_VALUE (attr1), TREE_VALUE (attr2)) == 1);
4756 /* Return 0 if the attributes for two types are incompatible, 1 if they
4757 are compatible, and 2 if they are nearly compatible (which causes a
4758 warning to be generated). */
4760 comp_type_attributes (const_tree type1, const_tree type2)
4762 const_tree a1 = TYPE_ATTRIBUTES (type1);
4763 const_tree a2 = TYPE_ATTRIBUTES (type2);
4764 const_tree a;
4766 if (a1 == a2)
4767 return 1;
4768 for (a = a1; a != NULL_TREE; a = TREE_CHAIN (a))
4770 const struct attribute_spec *as;
4771 const_tree attr;
4773 as = lookup_attribute_spec (get_attribute_name (a));
4774 if (!as || as->affects_type_identity == false)
4775 continue;
4777 attr = lookup_attribute (as->name, CONST_CAST_TREE (a2));
4778 if (!attr || !attribute_value_equal (a, attr))
4779 break;
4781 if (!a)
4783 for (a = a2; a != NULL_TREE; a = TREE_CHAIN (a))
4785 const struct attribute_spec *as;
4787 as = lookup_attribute_spec (get_attribute_name (a));
4788 if (!as || as->affects_type_identity == false)
4789 continue;
4791 if (!lookup_attribute (as->name, CONST_CAST_TREE (a1)))
4792 break;
4793 /* We don't need to compare trees again, as we did this
4794 already in first loop. */
4796 /* All types - affecting identity - are equal, so
4797 there is no need to call target hook for comparison. */
4798 if (!a)
4799 return 1;
4801 /* As some type combinations - like default calling-convention - might
4802 be compatible, we have to call the target hook to get the final result. */
4803 return targetm.comp_type_attributes (type1, type2);
4806 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4807 is ATTRIBUTE.
4809 Record such modified types already made so we don't make duplicates. */
4811 tree
4812 build_type_attribute_variant (tree ttype, tree attribute)
4814 return build_type_attribute_qual_variant (ttype, attribute,
4815 TYPE_QUALS (ttype));
4819 /* Reset the expression *EXPR_P, a size or position.
4821 ??? We could reset all non-constant sizes or positions. But it's cheap
4822 enough to not do so and refrain from adding workarounds to dwarf2out.c.
4824 We need to reset self-referential sizes or positions because they cannot
4825 be gimplified and thus can contain a CALL_EXPR after the gimplification
4826 is finished, which will run afoul of LTO streaming. And they need to be
4827 reset to something essentially dummy but not constant, so as to preserve
4828 the properties of the object they are attached to. */
4830 static inline void
4831 free_lang_data_in_one_sizepos (tree *expr_p)
4833 tree expr = *expr_p;
4834 if (CONTAINS_PLACEHOLDER_P (expr))
4835 *expr_p = build0 (PLACEHOLDER_EXPR, TREE_TYPE (expr));
4839 /* Reset all the fields in a binfo node BINFO. We only keep
4840 BINFO_VTABLE, which is used by gimple_fold_obj_type_ref. */
4842 static void
4843 free_lang_data_in_binfo (tree binfo)
4845 unsigned i;
4846 tree t;
4848 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
4850 BINFO_VIRTUALS (binfo) = NULL_TREE;
4851 BINFO_BASE_ACCESSES (binfo) = NULL;
4852 BINFO_INHERITANCE_CHAIN (binfo) = NULL_TREE;
4853 BINFO_SUBVTT_INDEX (binfo) = NULL_TREE;
4855 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (binfo), i, t)
4856 free_lang_data_in_binfo (t);
4860 /* Reset all language specific information still present in TYPE. */
4862 static void
4863 free_lang_data_in_type (tree type)
4865 gcc_assert (TYPE_P (type));
4867 /* Give the FE a chance to remove its own data first. */
4868 lang_hooks.free_lang_data (type);
4870 TREE_LANG_FLAG_0 (type) = 0;
4871 TREE_LANG_FLAG_1 (type) = 0;
4872 TREE_LANG_FLAG_2 (type) = 0;
4873 TREE_LANG_FLAG_3 (type) = 0;
4874 TREE_LANG_FLAG_4 (type) = 0;
4875 TREE_LANG_FLAG_5 (type) = 0;
4876 TREE_LANG_FLAG_6 (type) = 0;
4878 if (TREE_CODE (type) == FUNCTION_TYPE)
4880 /* Remove the const and volatile qualifiers from arguments. The
4881 C++ front end removes them, but the C front end does not,
4882 leading to false ODR violation errors when merging two
4883 instances of the same function signature compiled by
4884 different front ends. */
4885 tree p;
4887 for (p = TYPE_ARG_TYPES (type); p; p = TREE_CHAIN (p))
4889 tree arg_type = TREE_VALUE (p);
4891 if (TYPE_READONLY (arg_type) || TYPE_VOLATILE (arg_type))
4893 int quals = TYPE_QUALS (arg_type)
4894 & ~TYPE_QUAL_CONST
4895 & ~TYPE_QUAL_VOLATILE;
4896 TREE_VALUE (p) = build_qualified_type (arg_type, quals);
4897 free_lang_data_in_type (TREE_VALUE (p));
4902 /* Remove members that are not actually FIELD_DECLs from the field
4903 list of an aggregate. These occur in C++. */
4904 if (RECORD_OR_UNION_TYPE_P (type))
4906 tree prev, member;
4908 /* Note that TYPE_FIELDS can be shared across distinct
4909 TREE_TYPEs. Therefore, if the first field of TYPE_FIELDS is
4910 to be removed, we cannot set its TREE_CHAIN to NULL.
4911 Otherwise, we would not be able to find all the other fields
4912 in the other instances of this TREE_TYPE.
4914 This was causing an ICE in testsuite/g++.dg/lto/20080915.C. */
4915 prev = NULL_TREE;
4916 member = TYPE_FIELDS (type);
4917 while (member)
4919 if (TREE_CODE (member) == FIELD_DECL
4920 || TREE_CODE (member) == TYPE_DECL)
4922 if (prev)
4923 TREE_CHAIN (prev) = member;
4924 else
4925 TYPE_FIELDS (type) = member;
4926 prev = member;
4929 member = TREE_CHAIN (member);
4932 if (prev)
4933 TREE_CHAIN (prev) = NULL_TREE;
4934 else
4935 TYPE_FIELDS (type) = NULL_TREE;
4937 TYPE_METHODS (type) = NULL_TREE;
4938 if (TYPE_BINFO (type))
4939 free_lang_data_in_binfo (TYPE_BINFO (type));
4941 else
4943 /* For non-aggregate types, clear out the language slot (which
4944 overloads TYPE_BINFO). */
4945 TYPE_LANG_SLOT_1 (type) = NULL_TREE;
4947 if (INTEGRAL_TYPE_P (type)
4948 || SCALAR_FLOAT_TYPE_P (type)
4949 || FIXED_POINT_TYPE_P (type))
4951 free_lang_data_in_one_sizepos (&TYPE_MIN_VALUE (type));
4952 free_lang_data_in_one_sizepos (&TYPE_MAX_VALUE (type));
4956 free_lang_data_in_one_sizepos (&TYPE_SIZE (type));
4957 free_lang_data_in_one_sizepos (&TYPE_SIZE_UNIT (type));
4959 if (TYPE_CONTEXT (type)
4960 && TREE_CODE (TYPE_CONTEXT (type)) == BLOCK)
4962 tree ctx = TYPE_CONTEXT (type);
4965 ctx = BLOCK_SUPERCONTEXT (ctx);
4967 while (ctx && TREE_CODE (ctx) == BLOCK);
4968 TYPE_CONTEXT (type) = ctx;
4973 /* Return true if DECL may need an assembler name to be set. */
4975 static inline bool
4976 need_assembler_name_p (tree decl)
4978 /* Only FUNCTION_DECLs and VAR_DECLs are considered. */
4979 if (TREE_CODE (decl) != FUNCTION_DECL
4980 && TREE_CODE (decl) != VAR_DECL)
4981 return false;
4983 /* If DECL already has its assembler name set, it does not need a
4984 new one. */
4985 if (!HAS_DECL_ASSEMBLER_NAME_P (decl)
4986 || DECL_ASSEMBLER_NAME_SET_P (decl))
4987 return false;
4989 /* Abstract decls do not need an assembler name. */
4990 if (DECL_ABSTRACT (decl))
4991 return false;
4993 /* For VAR_DECLs, only static, public and external symbols need an
4994 assembler name. */
4995 if (TREE_CODE (decl) == VAR_DECL
4996 && !TREE_STATIC (decl)
4997 && !TREE_PUBLIC (decl)
4998 && !DECL_EXTERNAL (decl))
4999 return false;
5001 if (TREE_CODE (decl) == FUNCTION_DECL)
5003 /* Do not set assembler name on builtins. Allow RTL expansion to
5004 decide whether to expand inline or via a regular call. */
5005 if (DECL_BUILT_IN (decl)
5006 && DECL_BUILT_IN_CLASS (decl) != BUILT_IN_FRONTEND)
5007 return false;
5009 /* Functions represented in the callgraph need an assembler name. */
5010 if (cgraph_get_node (decl) != NULL)
5011 return true;
5013 /* Unused and not public functions don't need an assembler name. */
5014 if (!TREE_USED (decl) && !TREE_PUBLIC (decl))
5015 return false;
5018 return true;
5022 /* Reset all language specific information still present in symbol
5023 DECL. */
5025 static void
5026 free_lang_data_in_decl (tree decl)
5028 gcc_assert (DECL_P (decl));
5030 /* Give the FE a chance to remove its own data first. */
5031 lang_hooks.free_lang_data (decl);
5033 TREE_LANG_FLAG_0 (decl) = 0;
5034 TREE_LANG_FLAG_1 (decl) = 0;
5035 TREE_LANG_FLAG_2 (decl) = 0;
5036 TREE_LANG_FLAG_3 (decl) = 0;
5037 TREE_LANG_FLAG_4 (decl) = 0;
5038 TREE_LANG_FLAG_5 (decl) = 0;
5039 TREE_LANG_FLAG_6 (decl) = 0;
5041 free_lang_data_in_one_sizepos (&DECL_SIZE (decl));
5042 free_lang_data_in_one_sizepos (&DECL_SIZE_UNIT (decl));
5043 if (TREE_CODE (decl) == FIELD_DECL)
5045 free_lang_data_in_one_sizepos (&DECL_FIELD_OFFSET (decl));
5046 if (TREE_CODE (DECL_CONTEXT (decl)) == QUAL_UNION_TYPE)
5047 DECL_QUALIFIER (decl) = NULL_TREE;
5050 if (TREE_CODE (decl) == FUNCTION_DECL)
5052 struct cgraph_node *node;
5053 if (!(node = cgraph_get_node (decl))
5054 || (!node->definition && !node->clones))
5056 if (node)
5057 cgraph_release_function_body (node);
5058 else
5060 release_function_body (decl);
5061 DECL_ARGUMENTS (decl) = NULL;
5062 DECL_RESULT (decl) = NULL;
5063 DECL_INITIAL (decl) = error_mark_node;
5066 if (gimple_has_body_p (decl))
5068 tree t;
5070 /* If DECL has a gimple body, then the context for its
5071 arguments must be DECL. Otherwise, it doesn't really
5072 matter, as we will not be emitting any code for DECL. In
5073 general, there may be other instances of DECL created by
5074 the front end and since PARM_DECLs are generally shared,
5075 their DECL_CONTEXT changes as the replicas of DECL are
5076 created. The only time where DECL_CONTEXT is important
5077 is for the FUNCTION_DECLs that have a gimple body (since
5078 the PARM_DECL will be used in the function's body). */
5079 for (t = DECL_ARGUMENTS (decl); t; t = TREE_CHAIN (t))
5080 DECL_CONTEXT (t) = decl;
5083 /* DECL_SAVED_TREE holds the GENERIC representation for DECL.
5084 At this point, it is not needed anymore. */
5085 DECL_SAVED_TREE (decl) = NULL_TREE;
5087 /* Clear the abstract origin if it refers to a method. Otherwise
5088 dwarf2out.c will ICE as we clear TYPE_METHODS and thus the
5089 origin will not be output correctly. */
5090 if (DECL_ABSTRACT_ORIGIN (decl)
5091 && DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))
5092 && RECORD_OR_UNION_TYPE_P
5093 (DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))))
5094 DECL_ABSTRACT_ORIGIN (decl) = NULL_TREE;
5096 /* Sometimes the C++ frontend doesn't manage to transform a temporary
5097 DECL_VINDEX referring to itself into a vtable slot number as it
5098 should. Happens with functions that are copied and then forgotten
5099 about. Just clear it, it won't matter anymore. */
5100 if (DECL_VINDEX (decl) && !tree_fits_shwi_p (DECL_VINDEX (decl)))
5101 DECL_VINDEX (decl) = NULL_TREE;
5103 else if (TREE_CODE (decl) == VAR_DECL)
5105 if ((DECL_EXTERNAL (decl)
5106 && (!TREE_STATIC (decl) || !TREE_READONLY (decl)))
5107 || (decl_function_context (decl) && !TREE_STATIC (decl)))
5108 DECL_INITIAL (decl) = NULL_TREE;
5110 else if (TREE_CODE (decl) == TYPE_DECL
5111 || TREE_CODE (decl) == FIELD_DECL)
5112 DECL_INITIAL (decl) = NULL_TREE;
5113 else if (TREE_CODE (decl) == TRANSLATION_UNIT_DECL
5114 && DECL_INITIAL (decl)
5115 && TREE_CODE (DECL_INITIAL (decl)) == BLOCK)
5117 /* Strip builtins from the translation-unit BLOCK. We still have targets
5118 without builtin_decl_explicit support and also builtins are shared
5119 nodes and thus we can't use TREE_CHAIN in multiple lists. */
5120 tree *nextp = &BLOCK_VARS (DECL_INITIAL (decl));
5121 while (*nextp)
5123 tree var = *nextp;
5124 if (TREE_CODE (var) == FUNCTION_DECL
5125 && DECL_BUILT_IN (var))
5126 *nextp = TREE_CHAIN (var);
5127 else
5128 nextp = &TREE_CHAIN (var);
5134 /* Data used when collecting DECLs and TYPEs for language data removal. */
5136 struct free_lang_data_d
5138 /* Worklist to avoid excessive recursion. */
5139 vec<tree> worklist;
5141 /* Set of traversed objects. Used to avoid duplicate visits. */
5142 struct pointer_set_t *pset;
5144 /* Array of symbols to process with free_lang_data_in_decl. */
5145 vec<tree> decls;
5147 /* Array of types to process with free_lang_data_in_type. */
5148 vec<tree> types;
5152 /* Save all language fields needed to generate proper debug information
5153 for DECL. This saves most fields cleared out by free_lang_data_in_decl. */
5155 static void
5156 save_debug_info_for_decl (tree t)
5158 /*struct saved_debug_info_d *sdi;*/
5160 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && DECL_P (t));
5162 /* FIXME. Partial implementation for saving debug info removed. */
5166 /* Save all language fields needed to generate proper debug information
5167 for TYPE. This saves most fields cleared out by free_lang_data_in_type. */
5169 static void
5170 save_debug_info_for_type (tree t)
5172 /*struct saved_debug_info_d *sdi;*/
5174 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && TYPE_P (t));
5176 /* FIXME. Partial implementation for saving debug info removed. */
5180 /* Add type or decl T to one of the list of tree nodes that need their
5181 language data removed. The lists are held inside FLD. */
5183 static void
5184 add_tree_to_fld_list (tree t, struct free_lang_data_d *fld)
5186 if (DECL_P (t))
5188 fld->decls.safe_push (t);
5189 if (debug_info_level > DINFO_LEVEL_TERSE)
5190 save_debug_info_for_decl (t);
5192 else if (TYPE_P (t))
5194 fld->types.safe_push (t);
5195 if (debug_info_level > DINFO_LEVEL_TERSE)
5196 save_debug_info_for_type (t);
5198 else
5199 gcc_unreachable ();
5202 /* Push tree node T into FLD->WORKLIST. */
5204 static inline void
5205 fld_worklist_push (tree t, struct free_lang_data_d *fld)
5207 if (t && !is_lang_specific (t) && !pointer_set_contains (fld->pset, t))
5208 fld->worklist.safe_push ((t));
5212 /* Operand callback helper for free_lang_data_in_node. *TP is the
5213 subtree operand being considered. */
5215 static tree
5216 find_decls_types_r (tree *tp, int *ws, void *data)
5218 tree t = *tp;
5219 struct free_lang_data_d *fld = (struct free_lang_data_d *) data;
5221 if (TREE_CODE (t) == TREE_LIST)
5222 return NULL_TREE;
5224 /* Language specific nodes will be removed, so there is no need
5225 to gather anything under them. */
5226 if (is_lang_specific (t))
5228 *ws = 0;
5229 return NULL_TREE;
5232 if (DECL_P (t))
5234 /* Note that walk_tree does not traverse every possible field in
5235 decls, so we have to do our own traversals here. */
5236 add_tree_to_fld_list (t, fld);
5238 fld_worklist_push (DECL_NAME (t), fld);
5239 fld_worklist_push (DECL_CONTEXT (t), fld);
5240 fld_worklist_push (DECL_SIZE (t), fld);
5241 fld_worklist_push (DECL_SIZE_UNIT (t), fld);
5243 /* We are going to remove everything under DECL_INITIAL for
5244 TYPE_DECLs. No point walking them. */
5245 if (TREE_CODE (t) != TYPE_DECL)
5246 fld_worklist_push (DECL_INITIAL (t), fld);
5248 fld_worklist_push (DECL_ATTRIBUTES (t), fld);
5249 fld_worklist_push (DECL_ABSTRACT_ORIGIN (t), fld);
5251 if (TREE_CODE (t) == FUNCTION_DECL)
5253 fld_worklist_push (DECL_ARGUMENTS (t), fld);
5254 fld_worklist_push (DECL_RESULT (t), fld);
5256 else if (TREE_CODE (t) == TYPE_DECL)
5258 fld_worklist_push (DECL_ARGUMENT_FLD (t), fld);
5259 fld_worklist_push (DECL_VINDEX (t), fld);
5260 fld_worklist_push (DECL_ORIGINAL_TYPE (t), fld);
5262 else if (TREE_CODE (t) == FIELD_DECL)
5264 fld_worklist_push (DECL_FIELD_OFFSET (t), fld);
5265 fld_worklist_push (DECL_BIT_FIELD_TYPE (t), fld);
5266 fld_worklist_push (DECL_FIELD_BIT_OFFSET (t), fld);
5267 fld_worklist_push (DECL_FCONTEXT (t), fld);
5269 else if (TREE_CODE (t) == VAR_DECL)
5271 fld_worklist_push (DECL_SECTION_NAME (t), fld);
5272 fld_worklist_push (DECL_COMDAT_GROUP (t), fld);
5275 if ((TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
5276 && DECL_HAS_VALUE_EXPR_P (t))
5277 fld_worklist_push (DECL_VALUE_EXPR (t), fld);
5279 if (TREE_CODE (t) != FIELD_DECL
5280 && TREE_CODE (t) != TYPE_DECL)
5281 fld_worklist_push (TREE_CHAIN (t), fld);
5282 *ws = 0;
5284 else if (TYPE_P (t))
5286 /* Note that walk_tree does not traverse every possible field in
5287 types, so we have to do our own traversals here. */
5288 add_tree_to_fld_list (t, fld);
5290 if (!RECORD_OR_UNION_TYPE_P (t))
5291 fld_worklist_push (TYPE_CACHED_VALUES (t), fld);
5292 fld_worklist_push (TYPE_SIZE (t), fld);
5293 fld_worklist_push (TYPE_SIZE_UNIT (t), fld);
5294 fld_worklist_push (TYPE_ATTRIBUTES (t), fld);
5295 fld_worklist_push (TYPE_POINTER_TO (t), fld);
5296 fld_worklist_push (TYPE_REFERENCE_TO (t), fld);
5297 fld_worklist_push (TYPE_NAME (t), fld);
5298 /* Do not walk TYPE_NEXT_PTR_TO or TYPE_NEXT_REF_TO. We do not stream
5299 them and thus do not and want not to reach unused pointer types
5300 this way. */
5301 if (!POINTER_TYPE_P (t))
5302 fld_worklist_push (TYPE_MINVAL (t), fld);
5303 if (!RECORD_OR_UNION_TYPE_P (t))
5304 fld_worklist_push (TYPE_MAXVAL (t), fld);
5305 fld_worklist_push (TYPE_MAIN_VARIANT (t), fld);
5306 /* Do not walk TYPE_NEXT_VARIANT. We do not stream it and thus
5307 do not and want not to reach unused variants this way. */
5308 if (TYPE_CONTEXT (t))
5310 tree ctx = TYPE_CONTEXT (t);
5311 /* We adjust BLOCK TYPE_CONTEXTs to the innermost non-BLOCK one.
5312 So push that instead. */
5313 while (ctx && TREE_CODE (ctx) == BLOCK)
5314 ctx = BLOCK_SUPERCONTEXT (ctx);
5315 fld_worklist_push (ctx, fld);
5317 /* Do not walk TYPE_CANONICAL. We do not stream it and thus do not
5318 and want not to reach unused types this way. */
5320 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t))
5322 unsigned i;
5323 tree tem;
5324 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (TYPE_BINFO (t)), i, tem)
5325 fld_worklist_push (TREE_TYPE (tem), fld);
5326 tem = BINFO_VIRTUALS (TYPE_BINFO (t));
5327 if (tem
5328 /* The Java FE overloads BINFO_VIRTUALS for its own purpose. */
5329 && TREE_CODE (tem) == TREE_LIST)
5332 fld_worklist_push (TREE_VALUE (tem), fld);
5333 tem = TREE_CHAIN (tem);
5335 while (tem);
5337 if (RECORD_OR_UNION_TYPE_P (t))
5339 tree tem;
5340 /* Push all TYPE_FIELDS - there can be interleaving interesting
5341 and non-interesting things. */
5342 tem = TYPE_FIELDS (t);
5343 while (tem)
5345 if (TREE_CODE (tem) == FIELD_DECL
5346 || TREE_CODE (tem) == TYPE_DECL)
5347 fld_worklist_push (tem, fld);
5348 tem = TREE_CHAIN (tem);
5352 fld_worklist_push (TYPE_STUB_DECL (t), fld);
5353 *ws = 0;
5355 else if (TREE_CODE (t) == BLOCK)
5357 tree tem;
5358 for (tem = BLOCK_VARS (t); tem; tem = TREE_CHAIN (tem))
5359 fld_worklist_push (tem, fld);
5360 for (tem = BLOCK_SUBBLOCKS (t); tem; tem = BLOCK_CHAIN (tem))
5361 fld_worklist_push (tem, fld);
5362 fld_worklist_push (BLOCK_ABSTRACT_ORIGIN (t), fld);
5365 if (TREE_CODE (t) != IDENTIFIER_NODE
5366 && CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED))
5367 fld_worklist_push (TREE_TYPE (t), fld);
5369 return NULL_TREE;
5373 /* Find decls and types in T. */
5375 static void
5376 find_decls_types (tree t, struct free_lang_data_d *fld)
5378 while (1)
5380 if (!pointer_set_contains (fld->pset, t))
5381 walk_tree (&t, find_decls_types_r, fld, fld->pset);
5382 if (fld->worklist.is_empty ())
5383 break;
5384 t = fld->worklist.pop ();
5388 /* Translate all the types in LIST with the corresponding runtime
5389 types. */
5391 static tree
5392 get_eh_types_for_runtime (tree list)
5394 tree head, prev;
5396 if (list == NULL_TREE)
5397 return NULL_TREE;
5399 head = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5400 prev = head;
5401 list = TREE_CHAIN (list);
5402 while (list)
5404 tree n = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5405 TREE_CHAIN (prev) = n;
5406 prev = TREE_CHAIN (prev);
5407 list = TREE_CHAIN (list);
5410 return head;
5414 /* Find decls and types referenced in EH region R and store them in
5415 FLD->DECLS and FLD->TYPES. */
5417 static void
5418 find_decls_types_in_eh_region (eh_region r, struct free_lang_data_d *fld)
5420 switch (r->type)
5422 case ERT_CLEANUP:
5423 break;
5425 case ERT_TRY:
5427 eh_catch c;
5429 /* The types referenced in each catch must first be changed to the
5430 EH types used at runtime. This removes references to FE types
5431 in the region. */
5432 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
5434 c->type_list = get_eh_types_for_runtime (c->type_list);
5435 walk_tree (&c->type_list, find_decls_types_r, fld, fld->pset);
5438 break;
5440 case ERT_ALLOWED_EXCEPTIONS:
5441 r->u.allowed.type_list
5442 = get_eh_types_for_runtime (r->u.allowed.type_list);
5443 walk_tree (&r->u.allowed.type_list, find_decls_types_r, fld, fld->pset);
5444 break;
5446 case ERT_MUST_NOT_THROW:
5447 walk_tree (&r->u.must_not_throw.failure_decl,
5448 find_decls_types_r, fld, fld->pset);
5449 break;
5454 /* Find decls and types referenced in cgraph node N and store them in
5455 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5456 look for *every* kind of DECL and TYPE node reachable from N,
5457 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5458 NAMESPACE_DECLs, etc). */
5460 static void
5461 find_decls_types_in_node (struct cgraph_node *n, struct free_lang_data_d *fld)
5463 basic_block bb;
5464 struct function *fn;
5465 unsigned ix;
5466 tree t;
5468 find_decls_types (n->decl, fld);
5470 if (!gimple_has_body_p (n->decl))
5471 return;
5473 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
5475 fn = DECL_STRUCT_FUNCTION (n->decl);
5477 /* Traverse locals. */
5478 FOR_EACH_LOCAL_DECL (fn, ix, t)
5479 find_decls_types (t, fld);
5481 /* Traverse EH regions in FN. */
5483 eh_region r;
5484 FOR_ALL_EH_REGION_FN (r, fn)
5485 find_decls_types_in_eh_region (r, fld);
5488 /* Traverse every statement in FN. */
5489 FOR_EACH_BB_FN (bb, fn)
5491 gimple_stmt_iterator si;
5492 unsigned i;
5494 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
5496 gimple phi = gsi_stmt (si);
5498 for (i = 0; i < gimple_phi_num_args (phi); i++)
5500 tree *arg_p = gimple_phi_arg_def_ptr (phi, i);
5501 find_decls_types (*arg_p, fld);
5505 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
5507 gimple stmt = gsi_stmt (si);
5509 if (is_gimple_call (stmt))
5510 find_decls_types (gimple_call_fntype (stmt), fld);
5512 for (i = 0; i < gimple_num_ops (stmt); i++)
5514 tree arg = gimple_op (stmt, i);
5515 find_decls_types (arg, fld);
5522 /* Find decls and types referenced in varpool node N and store them in
5523 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5524 look for *every* kind of DECL and TYPE node reachable from N,
5525 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5526 NAMESPACE_DECLs, etc). */
5528 static void
5529 find_decls_types_in_var (varpool_node *v, struct free_lang_data_d *fld)
5531 find_decls_types (v->decl, fld);
5534 /* If T needs an assembler name, have one created for it. */
5536 void
5537 assign_assembler_name_if_neeeded (tree t)
5539 if (need_assembler_name_p (t))
5541 /* When setting DECL_ASSEMBLER_NAME, the C++ mangler may emit
5542 diagnostics that use input_location to show locus
5543 information. The problem here is that, at this point,
5544 input_location is generally anchored to the end of the file
5545 (since the parser is long gone), so we don't have a good
5546 position to pin it to.
5548 To alleviate this problem, this uses the location of T's
5549 declaration. Examples of this are
5550 testsuite/g++.dg/template/cond2.C and
5551 testsuite/g++.dg/template/pr35240.C. */
5552 location_t saved_location = input_location;
5553 input_location = DECL_SOURCE_LOCATION (t);
5555 decl_assembler_name (t);
5557 input_location = saved_location;
5562 /* Free language specific information for every operand and expression
5563 in every node of the call graph. This process operates in three stages:
5565 1- Every callgraph node and varpool node is traversed looking for
5566 decls and types embedded in them. This is a more exhaustive
5567 search than that done by find_referenced_vars, because it will
5568 also collect individual fields, decls embedded in types, etc.
5570 2- All the decls found are sent to free_lang_data_in_decl.
5572 3- All the types found are sent to free_lang_data_in_type.
5574 The ordering between decls and types is important because
5575 free_lang_data_in_decl sets assembler names, which includes
5576 mangling. So types cannot be freed up until assembler names have
5577 been set up. */
5579 static void
5580 free_lang_data_in_cgraph (void)
5582 struct cgraph_node *n;
5583 varpool_node *v;
5584 struct free_lang_data_d fld;
5585 tree t;
5586 unsigned i;
5587 alias_pair *p;
5589 /* Initialize sets and arrays to store referenced decls and types. */
5590 fld.pset = pointer_set_create ();
5591 fld.worklist.create (0);
5592 fld.decls.create (100);
5593 fld.types.create (100);
5595 /* Find decls and types in the body of every function in the callgraph. */
5596 FOR_EACH_FUNCTION (n)
5597 find_decls_types_in_node (n, &fld);
5599 FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
5600 find_decls_types (p->decl, &fld);
5602 /* Find decls and types in every varpool symbol. */
5603 FOR_EACH_VARIABLE (v)
5604 find_decls_types_in_var (v, &fld);
5606 /* Set the assembler name on every decl found. We need to do this
5607 now because free_lang_data_in_decl will invalidate data needed
5608 for mangling. This breaks mangling on interdependent decls. */
5609 FOR_EACH_VEC_ELT (fld.decls, i, t)
5610 assign_assembler_name_if_neeeded (t);
5612 /* Traverse every decl found freeing its language data. */
5613 FOR_EACH_VEC_ELT (fld.decls, i, t)
5614 free_lang_data_in_decl (t);
5616 /* Traverse every type found freeing its language data. */
5617 FOR_EACH_VEC_ELT (fld.types, i, t)
5618 free_lang_data_in_type (t);
5620 pointer_set_destroy (fld.pset);
5621 fld.worklist.release ();
5622 fld.decls.release ();
5623 fld.types.release ();
5627 /* Free resources that are used by FE but are not needed once they are done. */
5629 static unsigned
5630 free_lang_data (void)
5632 unsigned i;
5634 /* If we are the LTO frontend we have freed lang-specific data already. */
5635 if (in_lto_p
5636 || !flag_generate_lto)
5637 return 0;
5639 /* Allocate and assign alias sets to the standard integer types
5640 while the slots are still in the way the frontends generated them. */
5641 for (i = 0; i < itk_none; ++i)
5642 if (integer_types[i])
5643 TYPE_ALIAS_SET (integer_types[i]) = get_alias_set (integer_types[i]);
5645 /* Traverse the IL resetting language specific information for
5646 operands, expressions, etc. */
5647 free_lang_data_in_cgraph ();
5649 /* Create gimple variants for common types. */
5650 ptrdiff_type_node = integer_type_node;
5651 fileptr_type_node = ptr_type_node;
5653 /* Reset some langhooks. Do not reset types_compatible_p, it may
5654 still be used indirectly via the get_alias_set langhook. */
5655 lang_hooks.dwarf_name = lhd_dwarf_name;
5656 lang_hooks.decl_printable_name = gimple_decl_printable_name;
5657 /* We do not want the default decl_assembler_name implementation,
5658 rather if we have fixed everything we want a wrapper around it
5659 asserting that all non-local symbols already got their assembler
5660 name and only produce assembler names for local symbols. Or rather
5661 make sure we never call decl_assembler_name on local symbols and
5662 devise a separate, middle-end private scheme for it. */
5664 /* Reset diagnostic machinery. */
5665 tree_diagnostics_defaults (global_dc);
5667 return 0;
5671 namespace {
5673 const pass_data pass_data_ipa_free_lang_data =
5675 SIMPLE_IPA_PASS, /* type */
5676 "*free_lang_data", /* name */
5677 OPTGROUP_NONE, /* optinfo_flags */
5678 false, /* has_gate */
5679 true, /* has_execute */
5680 TV_IPA_FREE_LANG_DATA, /* tv_id */
5681 0, /* properties_required */
5682 0, /* properties_provided */
5683 0, /* properties_destroyed */
5684 0, /* todo_flags_start */
5685 0, /* todo_flags_finish */
5688 class pass_ipa_free_lang_data : public simple_ipa_opt_pass
5690 public:
5691 pass_ipa_free_lang_data (gcc::context *ctxt)
5692 : simple_ipa_opt_pass (pass_data_ipa_free_lang_data, ctxt)
5695 /* opt_pass methods: */
5696 unsigned int execute () { return free_lang_data (); }
5698 }; // class pass_ipa_free_lang_data
5700 } // anon namespace
5702 simple_ipa_opt_pass *
5703 make_pass_ipa_free_lang_data (gcc::context *ctxt)
5705 return new pass_ipa_free_lang_data (ctxt);
5708 /* The backbone of is_attribute_p(). ATTR_LEN is the string length of
5709 ATTR_NAME. Also used internally by remove_attribute(). */
5710 bool
5711 private_is_attribute_p (const char *attr_name, size_t attr_len, const_tree ident)
5713 size_t ident_len = IDENTIFIER_LENGTH (ident);
5715 if (ident_len == attr_len)
5717 if (strcmp (attr_name, IDENTIFIER_POINTER (ident)) == 0)
5718 return true;
5720 else if (ident_len == attr_len + 4)
5722 /* There is the possibility that ATTR is 'text' and IDENT is
5723 '__text__'. */
5724 const char *p = IDENTIFIER_POINTER (ident);
5725 if (p[0] == '_' && p[1] == '_'
5726 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5727 && strncmp (attr_name, p + 2, attr_len) == 0)
5728 return true;
5731 return false;
5734 /* The backbone of lookup_attribute(). ATTR_LEN is the string length
5735 of ATTR_NAME, and LIST is not NULL_TREE. */
5736 tree
5737 private_lookup_attribute (const char *attr_name, size_t attr_len, tree list)
5739 while (list)
5741 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5743 if (ident_len == attr_len)
5745 if (!strcmp (attr_name,
5746 IDENTIFIER_POINTER (get_attribute_name (list))))
5747 break;
5749 /* TODO: If we made sure that attributes were stored in the
5750 canonical form without '__...__' (ie, as in 'text' as opposed
5751 to '__text__') then we could avoid the following case. */
5752 else if (ident_len == attr_len + 4)
5754 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5755 if (p[0] == '_' && p[1] == '_'
5756 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5757 && strncmp (attr_name, p + 2, attr_len) == 0)
5758 break;
5760 list = TREE_CHAIN (list);
5763 return list;
5766 /* A variant of lookup_attribute() that can be used with an identifier
5767 as the first argument, and where the identifier can be either
5768 'text' or '__text__'.
5770 Given an attribute ATTR_IDENTIFIER, and a list of attributes LIST,
5771 return a pointer to the attribute's list element if the attribute
5772 is part of the list, or NULL_TREE if not found. If the attribute
5773 appears more than once, this only returns the first occurrence; the
5774 TREE_CHAIN of the return value should be passed back in if further
5775 occurrences are wanted. ATTR_IDENTIFIER must be an identifier but
5776 can be in the form 'text' or '__text__'. */
5777 static tree
5778 lookup_ident_attribute (tree attr_identifier, tree list)
5780 gcc_checking_assert (TREE_CODE (attr_identifier) == IDENTIFIER_NODE);
5782 while (list)
5784 gcc_checking_assert (TREE_CODE (get_attribute_name (list))
5785 == IDENTIFIER_NODE);
5787 /* Identifiers can be compared directly for equality. */
5788 if (attr_identifier == get_attribute_name (list))
5789 break;
5791 /* If they are not equal, they may still be one in the form
5792 'text' while the other one is in the form '__text__'. TODO:
5793 If we were storing attributes in normalized 'text' form, then
5794 this could all go away and we could take full advantage of
5795 the fact that we're comparing identifiers. :-) */
5797 size_t attr_len = IDENTIFIER_LENGTH (attr_identifier);
5798 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5800 if (ident_len == attr_len + 4)
5802 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5803 const char *q = IDENTIFIER_POINTER (attr_identifier);
5804 if (p[0] == '_' && p[1] == '_'
5805 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5806 && strncmp (q, p + 2, attr_len) == 0)
5807 break;
5809 else if (ident_len + 4 == attr_len)
5811 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5812 const char *q = IDENTIFIER_POINTER (attr_identifier);
5813 if (q[0] == '_' && q[1] == '_'
5814 && q[attr_len - 2] == '_' && q[attr_len - 1] == '_'
5815 && strncmp (q + 2, p, ident_len) == 0)
5816 break;
5819 list = TREE_CHAIN (list);
5822 return list;
5825 /* Remove any instances of attribute ATTR_NAME in LIST and return the
5826 modified list. */
5828 tree
5829 remove_attribute (const char *attr_name, tree list)
5831 tree *p;
5832 size_t attr_len = strlen (attr_name);
5834 gcc_checking_assert (attr_name[0] != '_');
5836 for (p = &list; *p; )
5838 tree l = *p;
5839 /* TODO: If we were storing attributes in normalized form, here
5840 we could use a simple strcmp(). */
5841 if (private_is_attribute_p (attr_name, attr_len, get_attribute_name (l)))
5842 *p = TREE_CHAIN (l);
5843 else
5844 p = &TREE_CHAIN (l);
5847 return list;
5850 /* Return an attribute list that is the union of a1 and a2. */
5852 tree
5853 merge_attributes (tree a1, tree a2)
5855 tree attributes;
5857 /* Either one unset? Take the set one. */
5859 if ((attributes = a1) == 0)
5860 attributes = a2;
5862 /* One that completely contains the other? Take it. */
5864 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
5866 if (attribute_list_contained (a2, a1))
5867 attributes = a2;
5868 else
5870 /* Pick the longest list, and hang on the other list. */
5872 if (list_length (a1) < list_length (a2))
5873 attributes = a2, a2 = a1;
5875 for (; a2 != 0; a2 = TREE_CHAIN (a2))
5877 tree a;
5878 for (a = lookup_ident_attribute (get_attribute_name (a2),
5879 attributes);
5880 a != NULL_TREE && !attribute_value_equal (a, a2);
5881 a = lookup_ident_attribute (get_attribute_name (a2),
5882 TREE_CHAIN (a)))
5884 if (a == NULL_TREE)
5886 a1 = copy_node (a2);
5887 TREE_CHAIN (a1) = attributes;
5888 attributes = a1;
5893 return attributes;
5896 /* Given types T1 and T2, merge their attributes and return
5897 the result. */
5899 tree
5900 merge_type_attributes (tree t1, tree t2)
5902 return merge_attributes (TYPE_ATTRIBUTES (t1),
5903 TYPE_ATTRIBUTES (t2));
5906 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
5907 the result. */
5909 tree
5910 merge_decl_attributes (tree olddecl, tree newdecl)
5912 return merge_attributes (DECL_ATTRIBUTES (olddecl),
5913 DECL_ATTRIBUTES (newdecl));
5916 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
5918 /* Specialization of merge_decl_attributes for various Windows targets.
5920 This handles the following situation:
5922 __declspec (dllimport) int foo;
5923 int foo;
5925 The second instance of `foo' nullifies the dllimport. */
5927 tree
5928 merge_dllimport_decl_attributes (tree old, tree new_tree)
5930 tree a;
5931 int delete_dllimport_p = 1;
5933 /* What we need to do here is remove from `old' dllimport if it doesn't
5934 appear in `new'. dllimport behaves like extern: if a declaration is
5935 marked dllimport and a definition appears later, then the object
5936 is not dllimport'd. We also remove a `new' dllimport if the old list
5937 contains dllexport: dllexport always overrides dllimport, regardless
5938 of the order of declaration. */
5939 if (!VAR_OR_FUNCTION_DECL_P (new_tree))
5940 delete_dllimport_p = 0;
5941 else if (DECL_DLLIMPORT_P (new_tree)
5942 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
5944 DECL_DLLIMPORT_P (new_tree) = 0;
5945 warning (OPT_Wattributes, "%q+D already declared with dllexport attribute: "
5946 "dllimport ignored", new_tree);
5948 else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new_tree))
5950 /* Warn about overriding a symbol that has already been used, e.g.:
5951 extern int __attribute__ ((dllimport)) foo;
5952 int* bar () {return &foo;}
5953 int foo;
5955 if (TREE_USED (old))
5957 warning (0, "%q+D redeclared without dllimport attribute "
5958 "after being referenced with dll linkage", new_tree);
5959 /* If we have used a variable's address with dllimport linkage,
5960 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
5961 decl may already have had TREE_CONSTANT computed.
5962 We still remove the attribute so that assembler code refers
5963 to '&foo rather than '_imp__foo'. */
5964 if (TREE_CODE (old) == VAR_DECL && TREE_ADDRESSABLE (old))
5965 DECL_DLLIMPORT_P (new_tree) = 1;
5968 /* Let an inline definition silently override the external reference,
5969 but otherwise warn about attribute inconsistency. */
5970 else if (TREE_CODE (new_tree) == VAR_DECL
5971 || !DECL_DECLARED_INLINE_P (new_tree))
5972 warning (OPT_Wattributes, "%q+D redeclared without dllimport attribute: "
5973 "previous dllimport ignored", new_tree);
5975 else
5976 delete_dllimport_p = 0;
5978 a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new_tree));
5980 if (delete_dllimport_p)
5981 a = remove_attribute ("dllimport", a);
5983 return a;
5986 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
5987 struct attribute_spec.handler. */
5989 tree
5990 handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
5991 bool *no_add_attrs)
5993 tree node = *pnode;
5994 bool is_dllimport;
5996 /* These attributes may apply to structure and union types being created,
5997 but otherwise should pass to the declaration involved. */
5998 if (!DECL_P (node))
6000 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
6001 | (int) ATTR_FLAG_ARRAY_NEXT))
6003 *no_add_attrs = true;
6004 return tree_cons (name, args, NULL_TREE);
6006 if (TREE_CODE (node) == RECORD_TYPE
6007 || TREE_CODE (node) == UNION_TYPE)
6009 node = TYPE_NAME (node);
6010 if (!node)
6011 return NULL_TREE;
6013 else
6015 warning (OPT_Wattributes, "%qE attribute ignored",
6016 name);
6017 *no_add_attrs = true;
6018 return NULL_TREE;
6022 if (TREE_CODE (node) != FUNCTION_DECL
6023 && TREE_CODE (node) != VAR_DECL
6024 && TREE_CODE (node) != TYPE_DECL)
6026 *no_add_attrs = true;
6027 warning (OPT_Wattributes, "%qE attribute ignored",
6028 name);
6029 return NULL_TREE;
6032 if (TREE_CODE (node) == TYPE_DECL
6033 && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE
6034 && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE)
6036 *no_add_attrs = true;
6037 warning (OPT_Wattributes, "%qE attribute ignored",
6038 name);
6039 return NULL_TREE;
6042 is_dllimport = is_attribute_p ("dllimport", name);
6044 /* Report error on dllimport ambiguities seen now before they cause
6045 any damage. */
6046 if (is_dllimport)
6048 /* Honor any target-specific overrides. */
6049 if (!targetm.valid_dllimport_attribute_p (node))
6050 *no_add_attrs = true;
6052 else if (TREE_CODE (node) == FUNCTION_DECL
6053 && DECL_DECLARED_INLINE_P (node))
6055 warning (OPT_Wattributes, "inline function %q+D declared as "
6056 " dllimport: attribute ignored", node);
6057 *no_add_attrs = true;
6059 /* Like MS, treat definition of dllimported variables and
6060 non-inlined functions on declaration as syntax errors. */
6061 else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
6063 error ("function %q+D definition is marked dllimport", node);
6064 *no_add_attrs = true;
6067 else if (TREE_CODE (node) == VAR_DECL)
6069 if (DECL_INITIAL (node))
6071 error ("variable %q+D definition is marked dllimport",
6072 node);
6073 *no_add_attrs = true;
6076 /* `extern' needn't be specified with dllimport.
6077 Specify `extern' now and hope for the best. Sigh. */
6078 DECL_EXTERNAL (node) = 1;
6079 /* Also, implicitly give dllimport'd variables declared within
6080 a function global scope, unless declared static. */
6081 if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
6082 TREE_PUBLIC (node) = 1;
6085 if (*no_add_attrs == false)
6086 DECL_DLLIMPORT_P (node) = 1;
6088 else if (TREE_CODE (node) == FUNCTION_DECL
6089 && DECL_DECLARED_INLINE_P (node)
6090 && flag_keep_inline_dllexport)
6091 /* An exported function, even if inline, must be emitted. */
6092 DECL_EXTERNAL (node) = 0;
6094 /* Report error if symbol is not accessible at global scope. */
6095 if (!TREE_PUBLIC (node)
6096 && (TREE_CODE (node) == VAR_DECL
6097 || TREE_CODE (node) == FUNCTION_DECL))
6099 error ("external linkage required for symbol %q+D because of "
6100 "%qE attribute", node, name);
6101 *no_add_attrs = true;
6104 /* A dllexport'd entity must have default visibility so that other
6105 program units (shared libraries or the main executable) can see
6106 it. A dllimport'd entity must have default visibility so that
6107 the linker knows that undefined references within this program
6108 unit can be resolved by the dynamic linker. */
6109 if (!*no_add_attrs)
6111 if (DECL_VISIBILITY_SPECIFIED (node)
6112 && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT)
6113 error ("%qE implies default visibility, but %qD has already "
6114 "been declared with a different visibility",
6115 name, node);
6116 DECL_VISIBILITY (node) = VISIBILITY_DEFAULT;
6117 DECL_VISIBILITY_SPECIFIED (node) = 1;
6120 return NULL_TREE;
6123 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
6125 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
6126 of the various TYPE_QUAL values. Also, set the UPC layout qualifier,
6127 which is either null or a reference to an integral constant. */
6129 static void
6130 set_type_quals (tree type, int type_quals, tree layout_qualifier)
6132 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
6133 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
6134 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
6135 TYPE_ATOMIC (type) = (type_quals & TYPE_QUAL_ATOMIC) != 0;
6136 TYPE_ADDR_SPACE (type) = DECODE_QUAL_ADDR_SPACE (type_quals);
6137 TYPE_SHARED (type) = (type_quals & TYPE_QUAL_SHARED) != 0;
6138 TYPE_STRICT (type) = (type_quals & TYPE_QUAL_STRICT) != 0;
6139 TYPE_RELAXED (type) = (type_quals & TYPE_QUAL_RELAXED) != 0;
6140 if (TYPE_SHARED (type))
6141 SET_TYPE_BLOCK_FACTOR (type, layout_qualifier);
6144 /* Returns true iff CAND is equivalent to BASE with
6145 TYPE_QUALS and LAYOUT_QUALIFIER. */
6147 bool
6148 check_qualified_type (const_tree cand, const_tree base,
6149 int type_quals, tree layout_qualifier)
6151 return (TYPE_QUALS (cand) == type_quals
6152 && TYPE_BLOCK_FACTOR (cand) == layout_qualifier
6153 && TYPE_NAME (cand) == TYPE_NAME (base)
6154 /* Apparently this is needed for Objective-C. */
6155 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
6156 /* Check alignment. */
6157 && TYPE_ALIGN (cand) == TYPE_ALIGN (base)
6158 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
6159 TYPE_ATTRIBUTES (base)));
6162 /* Returns true iff CAND is equivalent to BASE with ALIGN. */
6164 static bool
6165 check_aligned_type (const_tree cand, const_tree base, unsigned int align)
6167 return (TYPE_QUALS (cand) == TYPE_QUALS (base)
6168 && TYPE_BLOCK_FACTOR (cand) == TYPE_BLOCK_FACTOR (base)
6169 && TYPE_NAME (cand) == TYPE_NAME (base)
6170 /* Apparently this is needed for Objective-C. */
6171 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
6172 /* Check alignment. */
6173 && TYPE_ALIGN (cand) == align
6174 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
6175 TYPE_ATTRIBUTES (base)));
6178 /* This function checks to see if TYPE matches the size one of the built-in
6179 atomic types, and returns that core atomic type. */
6181 static tree
6182 find_atomic_core_type (tree type)
6184 tree base_atomic_type;
6186 /* Only handle complete types. */
6187 if (TYPE_SIZE (type) == NULL_TREE)
6188 return NULL_TREE;
6190 HOST_WIDE_INT type_size = tree_to_uhwi (TYPE_SIZE (type));
6191 switch (type_size)
6193 case 8:
6194 base_atomic_type = atomicQI_type_node;
6195 break;
6197 case 16:
6198 base_atomic_type = atomicHI_type_node;
6199 break;
6201 case 32:
6202 base_atomic_type = atomicSI_type_node;
6203 break;
6205 case 64:
6206 base_atomic_type = atomicDI_type_node;
6207 break;
6209 case 128:
6210 base_atomic_type = atomicTI_type_node;
6211 break;
6213 default:
6214 base_atomic_type = NULL_TREE;
6217 return base_atomic_type;
6220 /* Return a version of the TYPE, qualified as indicated by the
6221 TYPE_QUALS, if one exists. If no qualified version exists yet,
6222 return NULL_TREE. */
6224 tree
6225 get_qualified_type_1 (tree type, int type_quals, tree layout_qualifier)
6227 tree t;
6229 if (TYPE_QUALS (type) == type_quals)
6230 return type;
6232 /* Search the chain of variants to see if there is already one there just
6233 like the one we need to have. If so, use that existing one. We must
6234 preserve the TYPE_NAME, since there is code that depends on this. */
6235 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6236 if (check_qualified_type (t, type, type_quals, layout_qualifier))
6237 return t;
6239 return NULL_TREE;
6242 /* Like get_qualified_type_1, but creates the type if it does not
6243 exist. This function never returns NULL_TREE. */
6245 tree
6246 build_qualified_type_1 (tree type, int type_quals, tree layout_qualifier)
6248 tree t;
6250 /* See if we already have the appropriate qualified variant. */
6251 t = get_qualified_type_1 (type, type_quals, layout_qualifier);
6253 /* If not, build it. */
6254 if (!t)
6256 t = build_variant_type_copy (type);
6257 set_type_quals (t, type_quals, layout_qualifier);
6259 if (((type_quals & TYPE_QUAL_ATOMIC) == TYPE_QUAL_ATOMIC))
6261 /* See if this object can map to a basic atomic type. */
6262 tree atomic_type = find_atomic_core_type (type);
6263 if (atomic_type)
6265 /* Ensure the alignment of this type is compatible with
6266 the required alignment of the atomic type. */
6267 if (TYPE_ALIGN (atomic_type) > TYPE_ALIGN (t))
6268 TYPE_ALIGN (t) = TYPE_ALIGN (atomic_type);
6272 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6273 /* Propagate structural equality. */
6274 SET_TYPE_STRUCTURAL_EQUALITY (t);
6275 else if (TYPE_CANONICAL (type) != type)
6276 /* Build the underlying canonical type, since it is different
6277 from TYPE. */
6278 TYPE_CANONICAL (t) = build_qualified_type (TYPE_CANONICAL (type),
6279 type_quals);
6280 else
6281 /* T is its own canonical type. */
6282 TYPE_CANONICAL (t) = t;
6286 return t;
6289 /* Lookup the UPC block size of TYPE, and return it if we find one. */
6291 tree
6292 block_factor_lookup (const_tree type)
6294 struct tree_map *h, in;
6295 union
6297 const_tree ct;
6298 tree t;
6299 } ct_to_t;
6300 ct_to_t.ct = type;
6301 /* Drop the const qualifier, avoid the warning. */
6302 in.base.from = ct_to_t.t;
6304 h = (struct tree_map *)
6305 htab_find_with_hash (block_factor_for_type, &in, TYPE_HASH (type));
6306 if (h)
6307 return h->to;
6308 return NULL_TREE;
6311 /* Insert a mapping TYPE->BLOCK_FACTOR in the UPC block factor hashtable. */
6313 void
6314 block_factor_insert (tree type,
6315 tree block_factor)
6317 struct tree_map *h;
6318 void **loc;
6320 gcc_assert (type && TYPE_P (type));
6321 gcc_assert (block_factor && INTEGRAL_TYPE_P (TREE_TYPE (block_factor)));
6322 gcc_assert (!(integer_zerop (block_factor) || integer_onep (block_factor)));
6323 h = ggc_alloc_tree_map ();
6324 h->base.from = type;
6325 h->to = (tree) block_factor;
6326 loc = htab_find_slot_with_hash (block_factor_for_type,
6327 h, TYPE_HASH (type), INSERT);
6328 *(struct tree_map **) loc = h;
6331 /* Return the blocking factor of the UPC shared type, TYPE.
6332 If the blocking factor is NULL, then return the default blocking
6333 factor of 1. */
6335 tree
6336 upc_get_block_factor (const tree type)
6338 tree block_factor = size_one_node;
6339 const tree elt_type = strip_array_types (type);
6340 if (elt_type && (TREE_CODE (elt_type) != ERROR_MARK)
6341 && TYPE_HAS_BLOCK_FACTOR (elt_type))
6342 block_factor = TYPE_BLOCK_FACTOR (elt_type);
6343 return block_factor;
6346 /* Return a variant of TYPE, where all UPC qualifiers
6347 have been removed. */
6349 tree
6350 build_upc_unshared_type (tree type)
6352 tree u_type = type;
6353 if (TREE_CODE (type) == ARRAY_TYPE)
6355 const tree elem_type = TREE_TYPE(type);
6356 const tree u_elem_type = build_upc_unshared_type (elem_type);
6357 if (u_elem_type != elem_type)
6359 for (u_type = TYPE_MAIN_VARIANT (type);
6360 u_type && TREE_TYPE(u_type) != u_elem_type;
6361 u_type = TYPE_NEXT_VARIANT (u_type)) /* loop */;
6362 if (!u_type)
6364 u_type = build_variant_type_copy (type);
6365 TREE_TYPE (u_type) = u_elem_type;
6369 else
6371 const int quals = TYPE_QUALS (type);
6372 const int u_quals = quals & ~(TYPE_QUAL_SHARED
6373 | TYPE_QUAL_RELAXED
6374 | TYPE_QUAL_STRICT);
6375 u_type = build_qualified_type (type, u_quals);
6377 return u_type;
6380 /* Create a variant of type T with alignment ALIGN. */
6382 tree
6383 build_aligned_type (tree type, unsigned int align)
6385 tree t;
6387 if (TYPE_PACKED (type)
6388 || TYPE_ALIGN (type) == align)
6389 return type;
6391 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6392 if (check_aligned_type (t, type, align))
6393 return t;
6395 t = build_variant_type_copy (type);
6396 TYPE_ALIGN (t) = align;
6398 return t;
6401 /* Create a new distinct copy of TYPE. The new type is made its own
6402 MAIN_VARIANT. If TYPE requires structural equality checks, the
6403 resulting type requires structural equality checks; otherwise, its
6404 TYPE_CANONICAL points to itself. */
6406 tree
6407 build_distinct_type_copy (tree type)
6409 tree t = copy_node (type);
6411 TYPE_POINTER_TO (t) = 0;
6412 TYPE_REFERENCE_TO (t) = 0;
6414 /* Set the canonical type either to a new equivalence class, or
6415 propagate the need for structural equality checks. */
6416 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6417 SET_TYPE_STRUCTURAL_EQUALITY (t);
6418 else
6419 TYPE_CANONICAL (t) = t;
6421 /* Make it its own variant. */
6422 TYPE_MAIN_VARIANT (t) = t;
6423 TYPE_NEXT_VARIANT (t) = 0;
6425 /* Note that it is now possible for TYPE_MIN_VALUE to be a value
6426 whose TREE_TYPE is not t. This can also happen in the Ada
6427 frontend when using subtypes. */
6429 return t;
6432 /* Create a new variant of TYPE, equivalent but distinct. This is so
6433 the caller can modify it. TYPE_CANONICAL for the return type will
6434 be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
6435 are considered equal by the language itself (or that both types
6436 require structural equality checks). */
6438 tree
6439 build_variant_type_copy (tree type)
6441 tree t, m = TYPE_MAIN_VARIANT (type);
6443 t = build_distinct_type_copy (type);
6445 /* Since we're building a variant, assume that it is a non-semantic
6446 variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
6447 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
6449 /* Add the new type to the chain of variants of TYPE. */
6450 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
6451 TYPE_NEXT_VARIANT (m) = t;
6452 TYPE_MAIN_VARIANT (t) = m;
6454 return t;
6457 /* Return true if the from tree in both tree maps are equal. */
6460 tree_map_base_eq (const void *va, const void *vb)
6462 const struct tree_map_base *const a = (const struct tree_map_base *) va,
6463 *const b = (const struct tree_map_base *) vb;
6464 return (a->from == b->from);
6467 /* Hash a from tree in a tree_base_map. */
6469 unsigned int
6470 tree_map_base_hash (const void *item)
6472 return htab_hash_pointer (((const struct tree_map_base *)item)->from);
6475 /* Return true if this tree map structure is marked for garbage collection
6476 purposes. We simply return true if the from tree is marked, so that this
6477 structure goes away when the from tree goes away. */
6480 tree_map_base_marked_p (const void *p)
6482 return ggc_marked_p (((const struct tree_map_base *) p)->from);
6485 /* Hash a from tree in a tree_map. */
6487 unsigned int
6488 tree_map_hash (const void *item)
6490 return (((const struct tree_map *) item)->hash);
6493 /* Hash a from tree in a tree_decl_map. */
6495 unsigned int
6496 tree_decl_map_hash (const void *item)
6498 return DECL_UID (((const struct tree_decl_map *) item)->base.from);
6501 /* Return the initialization priority for DECL. */
6503 priority_type
6504 decl_init_priority_lookup (tree decl)
6506 struct tree_priority_map *h;
6507 struct tree_map_base in;
6509 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
6510 in.from = decl;
6511 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
6512 return h ? h->init : DEFAULT_INIT_PRIORITY;
6515 /* Return the finalization priority for DECL. */
6517 priority_type
6518 decl_fini_priority_lookup (tree decl)
6520 struct tree_priority_map *h;
6521 struct tree_map_base in;
6523 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
6524 in.from = decl;
6525 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
6526 return h ? h->fini : DEFAULT_INIT_PRIORITY;
6529 /* Return the initialization and finalization priority information for
6530 DECL. If there is no previous priority information, a freshly
6531 allocated structure is returned. */
6533 static struct tree_priority_map *
6534 decl_priority_info (tree decl)
6536 struct tree_priority_map in;
6537 struct tree_priority_map *h;
6538 void **loc;
6540 in.base.from = decl;
6541 loc = htab_find_slot (init_priority_for_decl, &in, INSERT);
6542 h = (struct tree_priority_map *) *loc;
6543 if (!h)
6545 h = ggc_alloc_cleared_tree_priority_map ();
6546 *loc = h;
6547 h->base.from = decl;
6548 h->init = DEFAULT_INIT_PRIORITY;
6549 h->fini = DEFAULT_INIT_PRIORITY;
6552 return h;
6555 /* Set the initialization priority for DECL to PRIORITY. */
6557 void
6558 decl_init_priority_insert (tree decl, priority_type priority)
6560 struct tree_priority_map *h;
6562 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
6563 if (priority == DEFAULT_INIT_PRIORITY)
6564 return;
6565 h = decl_priority_info (decl);
6566 h->init = priority;
6569 /* Set the finalization priority for DECL to PRIORITY. */
6571 void
6572 decl_fini_priority_insert (tree decl, priority_type priority)
6574 struct tree_priority_map *h;
6576 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
6577 if (priority == DEFAULT_INIT_PRIORITY)
6578 return;
6579 h = decl_priority_info (decl);
6580 h->fini = priority;
6583 /* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
6585 static void
6586 print_debug_expr_statistics (void)
6588 fprintf (stderr, "DECL_DEBUG_EXPR hash: size %ld, %ld elements, %f collisions\n",
6589 (long) htab_size (debug_expr_for_decl),
6590 (long) htab_elements (debug_expr_for_decl),
6591 htab_collisions (debug_expr_for_decl));
6594 /* Print out the statistics for the DECL_VALUE_EXPR hash table. */
6596 static void
6597 print_value_expr_statistics (void)
6599 fprintf (stderr, "DECL_VALUE_EXPR hash: size %ld, %ld elements, %f collisions\n",
6600 (long) htab_size (value_expr_for_decl),
6601 (long) htab_elements (value_expr_for_decl),
6602 htab_collisions (value_expr_for_decl));
6605 /* Lookup a debug expression for FROM, and return it if we find one. */
6607 tree
6608 decl_debug_expr_lookup (tree from)
6610 struct tree_decl_map *h, in;
6611 in.base.from = from;
6613 h = (struct tree_decl_map *)
6614 htab_find_with_hash (debug_expr_for_decl, &in, DECL_UID (from));
6615 if (h)
6616 return h->to;
6617 return NULL_TREE;
6620 /* Insert a mapping FROM->TO in the debug expression hashtable. */
6622 void
6623 decl_debug_expr_insert (tree from, tree to)
6625 struct tree_decl_map *h;
6626 void **loc;
6628 h = ggc_alloc_tree_decl_map ();
6629 h->base.from = from;
6630 h->to = to;
6631 loc = htab_find_slot_with_hash (debug_expr_for_decl, h, DECL_UID (from),
6632 INSERT);
6633 *(struct tree_decl_map **) loc = h;
6636 /* Lookup a value expression for FROM, and return it if we find one. */
6638 tree
6639 decl_value_expr_lookup (tree from)
6641 struct tree_decl_map *h, in;
6642 in.base.from = from;
6644 h = (struct tree_decl_map *)
6645 htab_find_with_hash (value_expr_for_decl, &in, DECL_UID (from));
6646 if (h)
6647 return h->to;
6648 return NULL_TREE;
6651 /* Insert a mapping FROM->TO in the value expression hashtable. */
6653 void
6654 decl_value_expr_insert (tree from, tree to)
6656 struct tree_decl_map *h;
6657 void **loc;
6659 h = ggc_alloc_tree_decl_map ();
6660 h->base.from = from;
6661 h->to = to;
6662 loc = htab_find_slot_with_hash (value_expr_for_decl, h, DECL_UID (from),
6663 INSERT);
6664 *(struct tree_decl_map **) loc = h;
6667 /* Lookup a vector of debug arguments for FROM, and return it if we
6668 find one. */
6670 vec<tree, va_gc> **
6671 decl_debug_args_lookup (tree from)
6673 struct tree_vec_map *h, in;
6675 if (!DECL_HAS_DEBUG_ARGS_P (from))
6676 return NULL;
6677 gcc_checking_assert (debug_args_for_decl != NULL);
6678 in.base.from = from;
6679 h = (struct tree_vec_map *)
6680 htab_find_with_hash (debug_args_for_decl, &in, DECL_UID (from));
6681 if (h)
6682 return &h->to;
6683 return NULL;
6686 /* Insert a mapping FROM->empty vector of debug arguments in the value
6687 expression hashtable. */
6689 vec<tree, va_gc> **
6690 decl_debug_args_insert (tree from)
6692 struct tree_vec_map *h;
6693 void **loc;
6695 if (DECL_HAS_DEBUG_ARGS_P (from))
6696 return decl_debug_args_lookup (from);
6697 if (debug_args_for_decl == NULL)
6698 debug_args_for_decl = htab_create_ggc (64, tree_vec_map_hash,
6699 tree_vec_map_eq, 0);
6700 h = ggc_alloc_tree_vec_map ();
6701 h->base.from = from;
6702 h->to = NULL;
6703 loc = htab_find_slot_with_hash (debug_args_for_decl, h, DECL_UID (from),
6704 INSERT);
6705 *(struct tree_vec_map **) loc = h;
6706 DECL_HAS_DEBUG_ARGS_P (from) = 1;
6707 return &h->to;
6710 /* Hashing of types so that we don't make duplicates.
6711 The entry point is `type_hash_canon'. */
6713 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
6714 with types in the TREE_VALUE slots), by adding the hash codes
6715 of the individual types. */
6717 static unsigned int
6718 type_hash_list (const_tree list, hashval_t hashcode)
6720 const_tree tail;
6722 for (tail = list; tail; tail = TREE_CHAIN (tail))
6723 if (TREE_VALUE (tail) != error_mark_node)
6724 hashcode = iterative_hash_object (TYPE_HASH (TREE_VALUE (tail)),
6725 hashcode);
6727 return hashcode;
6730 /* These are the Hashtable callback functions. */
6732 /* Returns true iff the types are equivalent. */
6734 static int
6735 type_hash_eq (const void *va, const void *vb)
6737 const struct type_hash *const a = (const struct type_hash *) va,
6738 *const b = (const struct type_hash *) vb;
6740 /* First test the things that are the same for all types. */
6741 if (a->hash != b->hash
6742 || TREE_CODE (a->type) != TREE_CODE (b->type)
6743 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
6744 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
6745 TYPE_ATTRIBUTES (b->type))
6746 || (TREE_CODE (a->type) != COMPLEX_TYPE
6747 && TYPE_NAME (a->type) != TYPE_NAME (b->type)))
6748 return 0;
6750 /* Be careful about comparing arrays before and after the element type
6751 has been completed; don't compare TYPE_ALIGN unless both types are
6752 complete. */
6753 if (COMPLETE_TYPE_P (a->type) && COMPLETE_TYPE_P (b->type)
6754 && (TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
6755 || TYPE_MODE (a->type) != TYPE_MODE (b->type)))
6756 return 0;
6758 switch (TREE_CODE (a->type))
6760 case VOID_TYPE:
6761 case COMPLEX_TYPE:
6762 case POINTER_TYPE:
6763 case REFERENCE_TYPE:
6764 case NULLPTR_TYPE:
6765 return 1;
6767 case VECTOR_TYPE:
6768 return TYPE_VECTOR_SUBPARTS (a->type) == TYPE_VECTOR_SUBPARTS (b->type);
6770 case ENUMERAL_TYPE:
6771 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
6772 && !(TYPE_VALUES (a->type)
6773 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
6774 && TYPE_VALUES (b->type)
6775 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
6776 && type_list_equal (TYPE_VALUES (a->type),
6777 TYPE_VALUES (b->type))))
6778 return 0;
6780 /* ... fall through ... */
6782 case INTEGER_TYPE:
6783 case REAL_TYPE:
6784 case BOOLEAN_TYPE:
6785 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
6786 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
6787 TYPE_MAX_VALUE (b->type)))
6788 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
6789 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
6790 TYPE_MIN_VALUE (b->type))));
6792 case FIXED_POINT_TYPE:
6793 return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
6795 case OFFSET_TYPE:
6796 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
6798 case METHOD_TYPE:
6799 if (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
6800 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6801 || (TYPE_ARG_TYPES (a->type)
6802 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6803 && TYPE_ARG_TYPES (b->type)
6804 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6805 && type_list_equal (TYPE_ARG_TYPES (a->type),
6806 TYPE_ARG_TYPES (b->type)))))
6807 break;
6808 return 0;
6809 case ARRAY_TYPE:
6810 return TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type);
6812 case RECORD_TYPE:
6813 case UNION_TYPE:
6814 case QUAL_UNION_TYPE:
6815 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
6816 || (TYPE_FIELDS (a->type)
6817 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
6818 && TYPE_FIELDS (b->type)
6819 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
6820 && type_list_equal (TYPE_FIELDS (a->type),
6821 TYPE_FIELDS (b->type))));
6823 case FUNCTION_TYPE:
6824 if (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6825 || (TYPE_ARG_TYPES (a->type)
6826 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6827 && TYPE_ARG_TYPES (b->type)
6828 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6829 && type_list_equal (TYPE_ARG_TYPES (a->type),
6830 TYPE_ARG_TYPES (b->type))))
6831 break;
6832 return 0;
6834 default:
6835 return 0;
6838 if (lang_hooks.types.type_hash_eq != NULL)
6839 return lang_hooks.types.type_hash_eq (a->type, b->type);
6841 return 1;
6844 /* Return the cached hash value. */
6846 static hashval_t
6847 type_hash_hash (const void *item)
6849 return ((const struct type_hash *) item)->hash;
6852 /* Look in the type hash table for a type isomorphic to TYPE.
6853 If one is found, return it. Otherwise return 0. */
6855 static tree
6856 type_hash_lookup (hashval_t hashcode, tree type)
6858 struct type_hash *h, in;
6860 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
6861 must call that routine before comparing TYPE_ALIGNs. */
6862 layout_type (type);
6864 in.hash = hashcode;
6865 in.type = type;
6867 h = (struct type_hash *) htab_find_with_hash (type_hash_table, &in,
6868 hashcode);
6869 if (h)
6870 return h->type;
6871 return NULL_TREE;
6874 /* Add an entry to the type-hash-table
6875 for a type TYPE whose hash code is HASHCODE. */
6877 static void
6878 type_hash_add (hashval_t hashcode, tree type)
6880 struct type_hash *h;
6881 void **loc;
6883 h = ggc_alloc_type_hash ();
6884 h->hash = hashcode;
6885 h->type = type;
6886 loc = htab_find_slot_with_hash (type_hash_table, h, hashcode, INSERT);
6887 *loc = (void *)h;
6890 /* Given TYPE, and HASHCODE its hash code, return the canonical
6891 object for an identical type if one already exists.
6892 Otherwise, return TYPE, and record it as the canonical object.
6894 To use this function, first create a type of the sort you want.
6895 Then compute its hash code from the fields of the type that
6896 make it different from other similar types.
6897 Then call this function and use the value. */
6899 tree
6900 type_hash_canon (unsigned int hashcode, tree type)
6902 tree t1;
6904 /* The hash table only contains main variants, so ensure that's what we're
6905 being passed. */
6906 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
6908 /* See if the type is in the hash table already. If so, return it.
6909 Otherwise, add the type. */
6910 t1 = type_hash_lookup (hashcode, type);
6911 if (t1 != 0)
6913 if (GATHER_STATISTICS)
6915 tree_code_counts[(int) TREE_CODE (type)]--;
6916 tree_node_counts[(int) t_kind]--;
6917 tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type_non_common);
6919 return t1;
6921 else
6923 type_hash_add (hashcode, type);
6924 return type;
6928 /* See if the data pointed to by the type hash table is marked. We consider
6929 it marked if the type is marked or if a debug type number or symbol
6930 table entry has been made for the type. */
6932 static int
6933 type_hash_marked_p (const void *p)
6935 const_tree const type = ((const struct type_hash *) p)->type;
6937 return ggc_marked_p (type);
6940 static void
6941 print_type_hash_statistics (void)
6943 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
6944 (long) htab_size (type_hash_table),
6945 (long) htab_elements (type_hash_table),
6946 htab_collisions (type_hash_table));
6949 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
6950 with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
6951 by adding the hash codes of the individual attributes. */
6953 static unsigned int
6954 attribute_hash_list (const_tree list, hashval_t hashcode)
6956 const_tree tail;
6958 for (tail = list; tail; tail = TREE_CHAIN (tail))
6959 /* ??? Do we want to add in TREE_VALUE too? */
6960 hashcode = iterative_hash_object
6961 (IDENTIFIER_HASH_VALUE (get_attribute_name (tail)), hashcode);
6962 return hashcode;
6965 /* Given two lists of attributes, return true if list l2 is
6966 equivalent to l1. */
6969 attribute_list_equal (const_tree l1, const_tree l2)
6971 if (l1 == l2)
6972 return 1;
6974 return attribute_list_contained (l1, l2)
6975 && attribute_list_contained (l2, l1);
6978 /* Given two lists of attributes, return true if list L2 is
6979 completely contained within L1. */
6980 /* ??? This would be faster if attribute names were stored in a canonicalized
6981 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
6982 must be used to show these elements are equivalent (which they are). */
6983 /* ??? It's not clear that attributes with arguments will always be handled
6984 correctly. */
6987 attribute_list_contained (const_tree l1, const_tree l2)
6989 const_tree t1, t2;
6991 /* First check the obvious, maybe the lists are identical. */
6992 if (l1 == l2)
6993 return 1;
6995 /* Maybe the lists are similar. */
6996 for (t1 = l1, t2 = l2;
6997 t1 != 0 && t2 != 0
6998 && get_attribute_name (t1) == get_attribute_name (t2)
6999 && TREE_VALUE (t1) == TREE_VALUE (t2);
7000 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
7003 /* Maybe the lists are equal. */
7004 if (t1 == 0 && t2 == 0)
7005 return 1;
7007 for (; t2 != 0; t2 = TREE_CHAIN (t2))
7009 const_tree attr;
7010 /* This CONST_CAST is okay because lookup_attribute does not
7011 modify its argument and the return value is assigned to a
7012 const_tree. */
7013 for (attr = lookup_ident_attribute (get_attribute_name (t2),
7014 CONST_CAST_TREE (l1));
7015 attr != NULL_TREE && !attribute_value_equal (t2, attr);
7016 attr = lookup_ident_attribute (get_attribute_name (t2),
7017 TREE_CHAIN (attr)))
7020 if (attr == NULL_TREE)
7021 return 0;
7024 return 1;
7027 /* Given two lists of types
7028 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
7029 return 1 if the lists contain the same types in the same order.
7030 Also, the TREE_PURPOSEs must match. */
7033 type_list_equal (const_tree l1, const_tree l2)
7035 const_tree t1, t2;
7037 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
7038 if (TREE_VALUE (t1) != TREE_VALUE (t2)
7039 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
7040 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
7041 && (TREE_TYPE (TREE_PURPOSE (t1))
7042 == TREE_TYPE (TREE_PURPOSE (t2))))))
7043 return 0;
7045 return t1 == t2;
7048 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
7049 given by TYPE. If the argument list accepts variable arguments,
7050 then this function counts only the ordinary arguments. */
7053 type_num_arguments (const_tree type)
7055 int i = 0;
7056 tree t;
7058 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
7059 /* If the function does not take a variable number of arguments,
7060 the last element in the list will have type `void'. */
7061 if (VOID_TYPE_P (TREE_VALUE (t)))
7062 break;
7063 else
7064 ++i;
7066 return i;
7069 /* Nonzero if integer constants T1 and T2
7070 represent the same constant value. */
7073 tree_int_cst_equal (const_tree t1, const_tree t2)
7075 if (t1 == t2)
7076 return 1;
7078 if (t1 == 0 || t2 == 0)
7079 return 0;
7081 if (TREE_CODE (t1) == INTEGER_CST
7082 && TREE_CODE (t2) == INTEGER_CST
7083 && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
7084 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
7085 return 1;
7087 return 0;
7090 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
7091 The precise way of comparison depends on their data type. */
7094 tree_int_cst_lt (const_tree t1, const_tree t2)
7096 if (t1 == t2)
7097 return 0;
7099 if (TYPE_UNSIGNED (TREE_TYPE (t1)) != TYPE_UNSIGNED (TREE_TYPE (t2)))
7101 int t1_sgn = tree_int_cst_sgn (t1);
7102 int t2_sgn = tree_int_cst_sgn (t2);
7104 if (t1_sgn < t2_sgn)
7105 return 1;
7106 else if (t1_sgn > t2_sgn)
7107 return 0;
7108 /* Otherwise, both are non-negative, so we compare them as
7109 unsigned just in case one of them would overflow a signed
7110 type. */
7112 else if (!TYPE_UNSIGNED (TREE_TYPE (t1)))
7113 return INT_CST_LT (t1, t2);
7115 return INT_CST_LT_UNSIGNED (t1, t2);
7118 /* Returns -1 if T1 < T2, 0 if T1 == T2, and 1 if T1 > T2. */
7121 tree_int_cst_compare (const_tree t1, const_tree t2)
7123 if (tree_int_cst_lt (t1, t2))
7124 return -1;
7125 else if (tree_int_cst_lt (t2, t1))
7126 return 1;
7127 else
7128 return 0;
7131 /* Return true if T is an INTEGER_CST whose numerical value (extended
7132 according to TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. */
7134 bool
7135 tree_fits_shwi_p (const_tree t)
7137 return (t != NULL_TREE
7138 && TREE_CODE (t) == INTEGER_CST
7139 && ((TREE_INT_CST_HIGH (t) == 0
7140 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) >= 0)
7141 || (TREE_INT_CST_HIGH (t) == -1
7142 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0
7143 && !TYPE_UNSIGNED (TREE_TYPE (t)))));
7146 /* Return true if T is an INTEGER_CST whose numerical value (extended
7147 according to TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. */
7149 bool
7150 tree_fits_uhwi_p (const_tree t)
7152 return (t != NULL_TREE
7153 && TREE_CODE (t) == INTEGER_CST
7154 && TREE_INT_CST_HIGH (t) == 0);
7157 /* T is an INTEGER_CST whose numerical value (extended according to
7158 TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. Return that
7159 HOST_WIDE_INT. */
7161 HOST_WIDE_INT
7162 tree_to_shwi (const_tree t)
7164 gcc_assert (tree_fits_shwi_p (t));
7165 return TREE_INT_CST_LOW (t);
7168 /* T is an INTEGER_CST whose numerical value (extended according to
7169 TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. Return that
7170 HOST_WIDE_INT. */
7172 unsigned HOST_WIDE_INT
7173 tree_to_uhwi (const_tree t)
7175 gcc_assert (tree_fits_uhwi_p (t));
7176 return TREE_INT_CST_LOW (t);
7179 /* Return the most significant (sign) bit of T. */
7182 tree_int_cst_sign_bit (const_tree t)
7184 unsigned bitno = TYPE_PRECISION (TREE_TYPE (t)) - 1;
7185 unsigned HOST_WIDE_INT w;
7187 if (bitno < HOST_BITS_PER_WIDE_INT)
7188 w = TREE_INT_CST_LOW (t);
7189 else
7191 w = TREE_INT_CST_HIGH (t);
7192 bitno -= HOST_BITS_PER_WIDE_INT;
7195 return (w >> bitno) & 1;
7198 /* Return an indication of the sign of the integer constant T.
7199 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
7200 Note that -1 will never be returned if T's type is unsigned. */
7203 tree_int_cst_sgn (const_tree t)
7205 if (TREE_INT_CST_LOW (t) == 0 && TREE_INT_CST_HIGH (t) == 0)
7206 return 0;
7207 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
7208 return 1;
7209 else if (TREE_INT_CST_HIGH (t) < 0)
7210 return -1;
7211 else
7212 return 1;
7215 /* Return the minimum number of bits needed to represent VALUE in a
7216 signed or unsigned type, UNSIGNEDP says which. */
7218 unsigned int
7219 tree_int_cst_min_precision (tree value, bool unsignedp)
7221 /* If the value is negative, compute its negative minus 1. The latter
7222 adjustment is because the absolute value of the largest negative value
7223 is one larger than the largest positive value. This is equivalent to
7224 a bit-wise negation, so use that operation instead. */
7226 if (tree_int_cst_sgn (value) < 0)
7227 value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
7229 /* Return the number of bits needed, taking into account the fact
7230 that we need one more bit for a signed than unsigned type.
7231 If value is 0 or -1, the minimum precision is 1 no matter
7232 whether unsignedp is true or false. */
7234 if (integer_zerop (value))
7235 return 1;
7236 else
7237 return tree_floor_log2 (value) + 1 + !unsignedp;
7240 /* Return truthvalue of whether T1 is the same tree structure as T2.
7241 Return 1 if they are the same.
7242 Return 0 if they are understandably different.
7243 Return -1 if either contains tree structure not understood by
7244 this function. */
7247 simple_cst_equal (const_tree t1, const_tree t2)
7249 enum tree_code code1, code2;
7250 int cmp;
7251 int i;
7253 if (t1 == t2)
7254 return 1;
7255 if (t1 == 0 || t2 == 0)
7256 return 0;
7258 code1 = TREE_CODE (t1);
7259 code2 = TREE_CODE (t2);
7261 if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
7263 if (CONVERT_EXPR_CODE_P (code2)
7264 || code2 == NON_LVALUE_EXPR)
7265 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7266 else
7267 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
7270 else if (CONVERT_EXPR_CODE_P (code2)
7271 || code2 == NON_LVALUE_EXPR)
7272 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
7274 if (code1 != code2)
7275 return 0;
7277 switch (code1)
7279 case INTEGER_CST:
7280 return (TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
7281 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2));
7283 case REAL_CST:
7284 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
7286 case FIXED_CST:
7287 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
7289 case STRING_CST:
7290 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
7291 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
7292 TREE_STRING_LENGTH (t1)));
7294 case CONSTRUCTOR:
7296 unsigned HOST_WIDE_INT idx;
7297 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (t1);
7298 vec<constructor_elt, va_gc> *v2 = CONSTRUCTOR_ELTS (t2);
7300 if (vec_safe_length (v1) != vec_safe_length (v2))
7301 return false;
7303 for (idx = 0; idx < vec_safe_length (v1); ++idx)
7304 /* ??? Should we handle also fields here? */
7305 if (!simple_cst_equal ((*v1)[idx].value, (*v2)[idx].value))
7306 return false;
7307 return true;
7310 case SAVE_EXPR:
7311 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7313 case CALL_EXPR:
7314 cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
7315 if (cmp <= 0)
7316 return cmp;
7317 if (call_expr_nargs (t1) != call_expr_nargs (t2))
7318 return 0;
7320 const_tree arg1, arg2;
7321 const_call_expr_arg_iterator iter1, iter2;
7322 for (arg1 = first_const_call_expr_arg (t1, &iter1),
7323 arg2 = first_const_call_expr_arg (t2, &iter2);
7324 arg1 && arg2;
7325 arg1 = next_const_call_expr_arg (&iter1),
7326 arg2 = next_const_call_expr_arg (&iter2))
7328 cmp = simple_cst_equal (arg1, arg2);
7329 if (cmp <= 0)
7330 return cmp;
7332 return arg1 == arg2;
7335 case TARGET_EXPR:
7336 /* Special case: if either target is an unallocated VAR_DECL,
7337 it means that it's going to be unified with whatever the
7338 TARGET_EXPR is really supposed to initialize, so treat it
7339 as being equivalent to anything. */
7340 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
7341 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
7342 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
7343 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
7344 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
7345 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
7346 cmp = 1;
7347 else
7348 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7350 if (cmp <= 0)
7351 return cmp;
7353 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
7355 case WITH_CLEANUP_EXPR:
7356 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7357 if (cmp <= 0)
7358 return cmp;
7360 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
7362 case COMPONENT_REF:
7363 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
7364 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7366 return 0;
7368 case VAR_DECL:
7369 case PARM_DECL:
7370 case CONST_DECL:
7371 case FUNCTION_DECL:
7372 return 0;
7374 default:
7375 break;
7378 /* This general rule works for most tree codes. All exceptions should be
7379 handled above. If this is a language-specific tree code, we can't
7380 trust what might be in the operand, so say we don't know
7381 the situation. */
7382 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
7383 return -1;
7385 switch (TREE_CODE_CLASS (code1))
7387 case tcc_unary:
7388 case tcc_binary:
7389 case tcc_comparison:
7390 case tcc_expression:
7391 case tcc_reference:
7392 case tcc_statement:
7393 cmp = 1;
7394 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
7396 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
7397 if (cmp <= 0)
7398 return cmp;
7401 return cmp;
7403 default:
7404 return -1;
7408 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
7409 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
7410 than U, respectively. */
7413 compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
7415 if (tree_int_cst_sgn (t) < 0)
7416 return -1;
7417 else if (TREE_INT_CST_HIGH (t) != 0)
7418 return 1;
7419 else if (TREE_INT_CST_LOW (t) == u)
7420 return 0;
7421 else if (TREE_INT_CST_LOW (t) < u)
7422 return -1;
7423 else
7424 return 1;
7427 /* Return true if SIZE represents a constant size that is in bounds of
7428 what the middle-end and the backend accepts (covering not more than
7429 half of the address-space). */
7431 bool
7432 valid_constant_size_p (const_tree size)
7434 if (! tree_fits_uhwi_p (size)
7435 || TREE_OVERFLOW (size)
7436 || tree_int_cst_sign_bit (size) != 0)
7437 return false;
7438 return true;
7441 /* Return the precision of the type, or for a complex or vector type the
7442 precision of the type of its elements. */
7444 unsigned int
7445 element_precision (const_tree type)
7447 enum tree_code code = TREE_CODE (type);
7448 if (code == COMPLEX_TYPE || code == VECTOR_TYPE)
7449 type = TREE_TYPE (type);
7451 return TYPE_PRECISION (type);
7454 /* Return true if CODE represents an associative tree code. Otherwise
7455 return false. */
7456 bool
7457 associative_tree_code (enum tree_code code)
7459 switch (code)
7461 case BIT_IOR_EXPR:
7462 case BIT_AND_EXPR:
7463 case BIT_XOR_EXPR:
7464 case PLUS_EXPR:
7465 case MULT_EXPR:
7466 case MIN_EXPR:
7467 case MAX_EXPR:
7468 return true;
7470 default:
7471 break;
7473 return false;
7476 /* Return true if CODE represents a commutative tree code. Otherwise
7477 return false. */
7478 bool
7479 commutative_tree_code (enum tree_code code)
7481 switch (code)
7483 case PLUS_EXPR:
7484 case MULT_EXPR:
7485 case MULT_HIGHPART_EXPR:
7486 case MIN_EXPR:
7487 case MAX_EXPR:
7488 case BIT_IOR_EXPR:
7489 case BIT_XOR_EXPR:
7490 case BIT_AND_EXPR:
7491 case NE_EXPR:
7492 case EQ_EXPR:
7493 case UNORDERED_EXPR:
7494 case ORDERED_EXPR:
7495 case UNEQ_EXPR:
7496 case LTGT_EXPR:
7497 case TRUTH_AND_EXPR:
7498 case TRUTH_XOR_EXPR:
7499 case TRUTH_OR_EXPR:
7500 case WIDEN_MULT_EXPR:
7501 case VEC_WIDEN_MULT_HI_EXPR:
7502 case VEC_WIDEN_MULT_LO_EXPR:
7503 case VEC_WIDEN_MULT_EVEN_EXPR:
7504 case VEC_WIDEN_MULT_ODD_EXPR:
7505 return true;
7507 default:
7508 break;
7510 return false;
7513 /* Return true if CODE represents a ternary tree code for which the
7514 first two operands are commutative. Otherwise return false. */
7515 bool
7516 commutative_ternary_tree_code (enum tree_code code)
7518 switch (code)
7520 case WIDEN_MULT_PLUS_EXPR:
7521 case WIDEN_MULT_MINUS_EXPR:
7522 return true;
7524 default:
7525 break;
7527 return false;
7530 /* Generate a hash value for an expression. This can be used iteratively
7531 by passing a previous result as the VAL argument.
7533 This function is intended to produce the same hash for expressions which
7534 would compare equal using operand_equal_p. */
7536 hashval_t
7537 iterative_hash_expr (const_tree t, hashval_t val)
7539 int i;
7540 enum tree_code code;
7541 char tclass;
7543 if (t == NULL_TREE)
7544 return iterative_hash_hashval_t (0, val);
7546 code = TREE_CODE (t);
7548 switch (code)
7550 /* Alas, constants aren't shared, so we can't rely on pointer
7551 identity. */
7552 case INTEGER_CST:
7553 val = iterative_hash_host_wide_int (TREE_INT_CST_LOW (t), val);
7554 return iterative_hash_host_wide_int (TREE_INT_CST_HIGH (t), val);
7555 case REAL_CST:
7557 unsigned int val2 = real_hash (TREE_REAL_CST_PTR (t));
7559 return iterative_hash_hashval_t (val2, val);
7561 case FIXED_CST:
7563 unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
7565 return iterative_hash_hashval_t (val2, val);
7567 case STRING_CST:
7568 return iterative_hash (TREE_STRING_POINTER (t),
7569 TREE_STRING_LENGTH (t), val);
7570 case COMPLEX_CST:
7571 val = iterative_hash_expr (TREE_REALPART (t), val);
7572 return iterative_hash_expr (TREE_IMAGPART (t), val);
7573 case VECTOR_CST:
7575 unsigned i;
7576 for (i = 0; i < VECTOR_CST_NELTS (t); ++i)
7577 val = iterative_hash_expr (VECTOR_CST_ELT (t, i), val);
7578 return val;
7580 case SSA_NAME:
7581 /* We can just compare by pointer. */
7582 return iterative_hash_host_wide_int (SSA_NAME_VERSION (t), val);
7583 case PLACEHOLDER_EXPR:
7584 /* The node itself doesn't matter. */
7585 return val;
7586 case TREE_LIST:
7587 /* A list of expressions, for a CALL_EXPR or as the elements of a
7588 VECTOR_CST. */
7589 for (; t; t = TREE_CHAIN (t))
7590 val = iterative_hash_expr (TREE_VALUE (t), val);
7591 return val;
7592 case CONSTRUCTOR:
7594 unsigned HOST_WIDE_INT idx;
7595 tree field, value;
7596 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
7598 val = iterative_hash_expr (field, val);
7599 val = iterative_hash_expr (value, val);
7601 return val;
7603 case FUNCTION_DECL:
7604 /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
7605 Otherwise nodes that compare equal according to operand_equal_p might
7606 get different hash codes. However, don't do this for machine specific
7607 or front end builtins, since the function code is overloaded in those
7608 cases. */
7609 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
7610 && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
7612 t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
7613 code = TREE_CODE (t);
7615 /* FALL THROUGH */
7616 default:
7617 tclass = TREE_CODE_CLASS (code);
7619 if (tclass == tcc_declaration)
7621 /* DECL's have a unique ID */
7622 val = iterative_hash_host_wide_int (DECL_UID (t), val);
7624 else
7626 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
7628 val = iterative_hash_object (code, val);
7630 /* Don't hash the type, that can lead to having nodes which
7631 compare equal according to operand_equal_p, but which
7632 have different hash codes. */
7633 if (CONVERT_EXPR_CODE_P (code)
7634 || code == NON_LVALUE_EXPR)
7636 /* Make sure to include signness in the hash computation. */
7637 val += TYPE_UNSIGNED (TREE_TYPE (t));
7638 val = iterative_hash_expr (TREE_OPERAND (t, 0), val);
7641 else if (commutative_tree_code (code))
7643 /* It's a commutative expression. We want to hash it the same
7644 however it appears. We do this by first hashing both operands
7645 and then rehashing based on the order of their independent
7646 hashes. */
7647 hashval_t one = iterative_hash_expr (TREE_OPERAND (t, 0), 0);
7648 hashval_t two = iterative_hash_expr (TREE_OPERAND (t, 1), 0);
7649 hashval_t t;
7651 if (one > two)
7652 t = one, one = two, two = t;
7654 val = iterative_hash_hashval_t (one, val);
7655 val = iterative_hash_hashval_t (two, val);
7657 else
7658 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
7659 val = iterative_hash_expr (TREE_OPERAND (t, i), val);
7661 return val;
7665 /* Constructors for pointer, array and function types.
7666 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
7667 constructed by language-dependent code, not here.) */
7669 /* Construct, lay out and return the type of pointers to TO_TYPE with
7670 mode MODE. If CAN_ALIAS_ALL is TRUE, indicate this type can
7671 reference all of memory. If such a type has already been
7672 constructed, reuse it. */
7674 tree
7675 build_pointer_type_for_mode (tree to_type, enum machine_mode mode,
7676 bool can_alias_all)
7678 tree t;
7680 if (to_type == error_mark_node)
7681 return error_mark_node;
7683 /* If the pointed-to type has the may_alias attribute set, force
7684 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7685 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7686 can_alias_all = true;
7688 /* In some cases, languages will have things that aren't a POINTER_TYPE
7689 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
7690 In that case, return that type without regard to the rest of our
7691 operands.
7693 ??? This is a kludge, but consistent with the way this function has
7694 always operated and there doesn't seem to be a good way to avoid this
7695 at the moment. */
7696 if (TYPE_POINTER_TO (to_type) != 0
7697 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
7698 return TYPE_POINTER_TO (to_type);
7700 /* First, if we already have a type for pointers to TO_TYPE and it's
7701 the proper mode, use it. */
7702 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
7703 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7704 return t;
7706 t = make_node (POINTER_TYPE);
7708 TREE_TYPE (t) = to_type;
7709 SET_TYPE_MODE (t, mode);
7710 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7711 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
7712 TYPE_POINTER_TO (to_type) = t;
7714 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7715 SET_TYPE_STRUCTURAL_EQUALITY (t);
7716 else if (TYPE_CANONICAL (to_type) != to_type)
7717 TYPE_CANONICAL (t)
7718 = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
7719 mode, can_alias_all);
7721 /* Lay out the type. This function has many callers that are concerned
7722 with expression-construction, and this simplifies them all. */
7723 layout_type (t);
7725 return t;
7728 /* By default build pointers in ptr_mode. */
7730 tree
7731 build_pointer_type (tree to_type)
7733 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7734 : TYPE_ADDR_SPACE (to_type);
7735 enum machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7736 if (upc_shared_type_p (to_type))
7738 tree upc_pts_type;
7739 pointer_mode = TYPE_MODE (upc_pts_rep_type_node);
7740 upc_pts_type = build_pointer_type_for_mode (to_type, pointer_mode,
7741 false);
7742 TYPE_USER_ALIGN (upc_pts_type) = TYPE_USER_ALIGN (upc_pts_rep_type_node);
7743 TYPE_ALIGN (upc_pts_type) = TYPE_ALIGN (upc_pts_rep_type_node);
7744 return upc_pts_type;
7746 return build_pointer_type_for_mode (to_type, pointer_mode, false);
7749 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
7751 tree
7752 build_reference_type_for_mode (tree to_type, enum machine_mode mode,
7753 bool can_alias_all)
7755 tree t;
7757 if (to_type == error_mark_node)
7758 return error_mark_node;
7760 /* If the pointed-to type has the may_alias attribute set, force
7761 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7762 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7763 can_alias_all = true;
7765 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
7766 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
7767 In that case, return that type without regard to the rest of our
7768 operands.
7770 ??? This is a kludge, but consistent with the way this function has
7771 always operated and there doesn't seem to be a good way to avoid this
7772 at the moment. */
7773 if (TYPE_REFERENCE_TO (to_type) != 0
7774 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
7775 return TYPE_REFERENCE_TO (to_type);
7777 /* First, if we already have a type for pointers to TO_TYPE and it's
7778 the proper mode, use it. */
7779 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
7780 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7781 return t;
7783 t = make_node (REFERENCE_TYPE);
7785 TREE_TYPE (t) = to_type;
7786 SET_TYPE_MODE (t, mode);
7787 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7788 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
7789 TYPE_REFERENCE_TO (to_type) = t;
7791 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7792 SET_TYPE_STRUCTURAL_EQUALITY (t);
7793 else if (TYPE_CANONICAL (to_type) != to_type)
7794 TYPE_CANONICAL (t)
7795 = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
7796 mode, can_alias_all);
7798 layout_type (t);
7800 return t;
7804 /* Build the node for the type of references-to-TO_TYPE by default
7805 in ptr_mode. */
7807 tree
7808 build_reference_type (tree to_type)
7810 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7811 : TYPE_ADDR_SPACE (to_type);
7812 enum machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7813 return build_reference_type_for_mode (to_type, pointer_mode, false);
7816 #define MAX_INT_CACHED_PREC \
7817 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
7818 static GTY(()) tree nonstandard_integer_type_cache[2 * MAX_INT_CACHED_PREC + 2];
7820 /* Builds a signed or unsigned integer type of precision PRECISION.
7821 Used for C bitfields whose precision does not match that of
7822 built-in target types. */
7823 tree
7824 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
7825 int unsignedp)
7827 tree itype, ret;
7829 if (unsignedp)
7830 unsignedp = MAX_INT_CACHED_PREC + 1;
7832 if (precision <= MAX_INT_CACHED_PREC)
7834 itype = nonstandard_integer_type_cache[precision + unsignedp];
7835 if (itype)
7836 return itype;
7839 itype = make_node (INTEGER_TYPE);
7840 TYPE_PRECISION (itype) = precision;
7842 if (unsignedp)
7843 fixup_unsigned_type (itype);
7844 else
7845 fixup_signed_type (itype);
7847 ret = itype;
7848 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (itype)))
7849 ret = type_hash_canon (tree_to_uhwi (TYPE_MAX_VALUE (itype)), itype);
7850 if (precision <= MAX_INT_CACHED_PREC)
7851 nonstandard_integer_type_cache[precision + unsignedp] = ret;
7853 return ret;
7856 /* Create a range of some discrete type TYPE (an INTEGER_TYPE, ENUMERAL_TYPE
7857 or BOOLEAN_TYPE) with low bound LOWVAL and high bound HIGHVAL. If SHARED
7858 is true, reuse such a type that has already been constructed. */
7860 static tree
7861 build_range_type_1 (tree type, tree lowval, tree highval, bool shared)
7863 tree itype = make_node (INTEGER_TYPE);
7864 hashval_t hashcode = 0;
7866 TREE_TYPE (itype) = type;
7868 TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
7869 TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
7871 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
7872 SET_TYPE_MODE (itype, TYPE_MODE (type));
7873 TYPE_SIZE (itype) = TYPE_SIZE (type);
7874 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
7875 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
7876 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
7878 if (!shared)
7879 return itype;
7881 if ((TYPE_MIN_VALUE (itype)
7882 && TREE_CODE (TYPE_MIN_VALUE (itype)) != INTEGER_CST)
7883 || (TYPE_MAX_VALUE (itype)
7884 && TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST))
7886 /* Since we cannot reliably merge this type, we need to compare it using
7887 structural equality checks. */
7888 SET_TYPE_STRUCTURAL_EQUALITY (itype);
7889 return itype;
7892 hashcode = iterative_hash_expr (TYPE_MIN_VALUE (itype), hashcode);
7893 hashcode = iterative_hash_expr (TYPE_MAX_VALUE (itype), hashcode);
7894 hashcode = iterative_hash_hashval_t (TYPE_HASH (type), hashcode);
7895 itype = type_hash_canon (hashcode, itype);
7897 return itype;
7900 /* Wrapper around build_range_type_1 with SHARED set to true. */
7902 tree
7903 build_range_type (tree type, tree lowval, tree highval)
7905 return build_range_type_1 (type, lowval, highval, true);
7908 /* Wrapper around build_range_type_1 with SHARED set to false. */
7910 tree
7911 build_nonshared_range_type (tree type, tree lowval, tree highval)
7913 return build_range_type_1 (type, lowval, highval, false);
7916 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
7917 MAXVAL should be the maximum value in the domain
7918 (one less than the length of the array).
7920 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
7921 We don't enforce this limit, that is up to caller (e.g. language front end).
7922 The limit exists because the result is a signed type and we don't handle
7923 sizes that use more than one HOST_WIDE_INT. */
7925 tree
7926 build_index_type (tree maxval)
7928 return build_range_type (sizetype, size_zero_node, maxval);
7931 /* Return true if the debug information for TYPE, a subtype, should be emitted
7932 as a subrange type. If so, set LOWVAL to the low bound and HIGHVAL to the
7933 high bound, respectively. Sometimes doing so unnecessarily obfuscates the
7934 debug info and doesn't reflect the source code. */
7936 bool
7937 subrange_type_for_debug_p (const_tree type, tree *lowval, tree *highval)
7939 tree base_type = TREE_TYPE (type), low, high;
7941 /* Subrange types have a base type which is an integral type. */
7942 if (!INTEGRAL_TYPE_P (base_type))
7943 return false;
7945 /* Get the real bounds of the subtype. */
7946 if (lang_hooks.types.get_subrange_bounds)
7947 lang_hooks.types.get_subrange_bounds (type, &low, &high);
7948 else
7950 low = TYPE_MIN_VALUE (type);
7951 high = TYPE_MAX_VALUE (type);
7954 /* If the type and its base type have the same representation and the same
7955 name, then the type is not a subrange but a copy of the base type. */
7956 if ((TREE_CODE (base_type) == INTEGER_TYPE
7957 || TREE_CODE (base_type) == BOOLEAN_TYPE)
7958 && int_size_in_bytes (type) == int_size_in_bytes (base_type)
7959 && tree_int_cst_equal (low, TYPE_MIN_VALUE (base_type))
7960 && tree_int_cst_equal (high, TYPE_MAX_VALUE (base_type)))
7962 tree type_name = TYPE_NAME (type);
7963 tree base_type_name = TYPE_NAME (base_type);
7965 if (type_name && TREE_CODE (type_name) == TYPE_DECL)
7966 type_name = DECL_NAME (type_name);
7968 if (base_type_name && TREE_CODE (base_type_name) == TYPE_DECL)
7969 base_type_name = DECL_NAME (base_type_name);
7971 if (type_name == base_type_name)
7972 return false;
7975 if (lowval)
7976 *lowval = low;
7977 if (highval)
7978 *highval = high;
7979 return true;
7982 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
7983 and number of elements specified by the range of values of INDEX_TYPE.
7984 If SHARED is true, reuse such a type that has already been constructed. */
7986 static tree
7987 build_array_type_1 (tree elt_type, tree index_type, bool shared)
7989 tree t;
7991 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
7993 error ("arrays of functions are not meaningful");
7994 elt_type = integer_type_node;
7997 t = make_node (ARRAY_TYPE);
7998 TREE_TYPE (t) = elt_type;
7999 TYPE_DOMAIN (t) = index_type;
8000 TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (elt_type);
8001 layout_type (t);
8003 /* If the element type is incomplete at this point we get marked for
8004 structural equality. Do not record these types in the canonical
8005 type hashtable. */
8006 if (TYPE_STRUCTURAL_EQUALITY_P (t))
8007 return t;
8009 if (shared)
8011 hashval_t hashcode = iterative_hash_object (TYPE_HASH (elt_type), 0);
8012 if (index_type)
8013 hashcode = iterative_hash_object (TYPE_HASH (index_type), hashcode);
8014 t = type_hash_canon (hashcode, t);
8017 if (TYPE_CANONICAL (t) == t)
8019 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
8020 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
8021 SET_TYPE_STRUCTURAL_EQUALITY (t);
8022 else if (TYPE_CANONICAL (elt_type) != elt_type
8023 || (index_type && TYPE_CANONICAL (index_type) != index_type))
8024 TYPE_CANONICAL (t)
8025 = build_array_type_1 (TYPE_CANONICAL (elt_type),
8026 index_type
8027 ? TYPE_CANONICAL (index_type) : NULL_TREE,
8028 shared);
8031 return t;
8034 /* Wrapper around build_array_type_1 with SHARED set to true. */
8036 tree
8037 build_array_type (tree elt_type, tree index_type)
8039 return build_array_type_1 (elt_type, index_type, true);
8042 /* Wrapper around build_array_type_1 with SHARED set to false. */
8044 tree
8045 build_nonshared_array_type (tree elt_type, tree index_type)
8047 return build_array_type_1 (elt_type, index_type, false);
8050 /* Return a representation of ELT_TYPE[NELTS], using indices of type
8051 sizetype. */
8053 tree
8054 build_array_type_nelts (tree elt_type, unsigned HOST_WIDE_INT nelts)
8056 return build_array_type (elt_type, build_index_type (size_int (nelts - 1)));
8059 /* Recursively examines the array elements of TYPE, until a non-array
8060 element type is found. */
8062 tree
8063 strip_array_types (tree type)
8065 while (TREE_CODE (type) == ARRAY_TYPE)
8066 type = TREE_TYPE (type);
8068 return type;
8071 /* Computes the canonical argument types from the argument type list
8072 ARGTYPES.
8074 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
8075 on entry to this function, or if any of the ARGTYPES are
8076 structural.
8078 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
8079 true on entry to this function, or if any of the ARGTYPES are
8080 non-canonical.
8082 Returns a canonical argument list, which may be ARGTYPES when the
8083 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
8084 true) or would not differ from ARGTYPES. */
8086 static tree
8087 maybe_canonicalize_argtypes (tree argtypes,
8088 bool *any_structural_p,
8089 bool *any_noncanonical_p)
8091 tree arg;
8092 bool any_noncanonical_argtypes_p = false;
8094 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
8096 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
8097 /* Fail gracefully by stating that the type is structural. */
8098 *any_structural_p = true;
8099 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
8100 *any_structural_p = true;
8101 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
8102 || TREE_PURPOSE (arg))
8103 /* If the argument has a default argument, we consider it
8104 non-canonical even though the type itself is canonical.
8105 That way, different variants of function and method types
8106 with default arguments will all point to the variant with
8107 no defaults as their canonical type. */
8108 any_noncanonical_argtypes_p = true;
8111 if (*any_structural_p)
8112 return argtypes;
8114 if (any_noncanonical_argtypes_p)
8116 /* Build the canonical list of argument types. */
8117 tree canon_argtypes = NULL_TREE;
8118 bool is_void = false;
8120 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
8122 if (arg == void_list_node)
8123 is_void = true;
8124 else
8125 canon_argtypes = tree_cons (NULL_TREE,
8126 TYPE_CANONICAL (TREE_VALUE (arg)),
8127 canon_argtypes);
8130 canon_argtypes = nreverse (canon_argtypes);
8131 if (is_void)
8132 canon_argtypes = chainon (canon_argtypes, void_list_node);
8134 /* There is a non-canonical type. */
8135 *any_noncanonical_p = true;
8136 return canon_argtypes;
8139 /* The canonical argument types are the same as ARGTYPES. */
8140 return argtypes;
8143 /* Construct, lay out and return
8144 the type of functions returning type VALUE_TYPE
8145 given arguments of types ARG_TYPES.
8146 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
8147 are data type nodes for the arguments of the function.
8148 If such a type has already been constructed, reuse it. */
8150 tree
8151 build_function_type (tree value_type, tree arg_types)
8153 tree t;
8154 hashval_t hashcode = 0;
8155 bool any_structural_p, any_noncanonical_p;
8156 tree canon_argtypes;
8158 if (TREE_CODE (value_type) == FUNCTION_TYPE)
8160 error ("function return type cannot be function");
8161 value_type = integer_type_node;
8164 /* Make a node of the sort we want. */
8165 t = make_node (FUNCTION_TYPE);
8166 TREE_TYPE (t) = value_type;
8167 TYPE_ARG_TYPES (t) = arg_types;
8169 /* If we already have such a type, use the old one. */
8170 hashcode = iterative_hash_object (TYPE_HASH (value_type), hashcode);
8171 hashcode = type_hash_list (arg_types, hashcode);
8172 t = type_hash_canon (hashcode, t);
8174 /* Set up the canonical type. */
8175 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
8176 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
8177 canon_argtypes = maybe_canonicalize_argtypes (arg_types,
8178 &any_structural_p,
8179 &any_noncanonical_p);
8180 if (any_structural_p)
8181 SET_TYPE_STRUCTURAL_EQUALITY (t);
8182 else if (any_noncanonical_p)
8183 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
8184 canon_argtypes);
8186 if (!COMPLETE_TYPE_P (t))
8187 layout_type (t);
8188 return t;
8191 /* Build a function type. The RETURN_TYPE is the type returned by the
8192 function. If VAARGS is set, no void_type_node is appended to the
8193 the list. ARGP must be always be terminated be a NULL_TREE. */
8195 static tree
8196 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
8198 tree t, args, last;
8200 t = va_arg (argp, tree);
8201 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
8202 args = tree_cons (NULL_TREE, t, args);
8204 if (vaargs)
8206 last = args;
8207 if (args != NULL_TREE)
8208 args = nreverse (args);
8209 gcc_assert (last != void_list_node);
8211 else if (args == NULL_TREE)
8212 args = void_list_node;
8213 else
8215 last = args;
8216 args = nreverse (args);
8217 TREE_CHAIN (last) = void_list_node;
8219 args = build_function_type (return_type, args);
8221 return args;
8224 /* Build a function type. The RETURN_TYPE is the type returned by the
8225 function. If additional arguments are provided, they are
8226 additional argument types. The list of argument types must always
8227 be terminated by NULL_TREE. */
8229 tree
8230 build_function_type_list (tree return_type, ...)
8232 tree args;
8233 va_list p;
8235 va_start (p, return_type);
8236 args = build_function_type_list_1 (false, return_type, p);
8237 va_end (p);
8238 return args;
8241 /* Build a variable argument function type. The RETURN_TYPE is the
8242 type returned by the function. If additional arguments are provided,
8243 they are additional argument types. The list of argument types must
8244 always be terminated by NULL_TREE. */
8246 tree
8247 build_varargs_function_type_list (tree return_type, ...)
8249 tree args;
8250 va_list p;
8252 va_start (p, return_type);
8253 args = build_function_type_list_1 (true, return_type, p);
8254 va_end (p);
8256 return args;
8259 /* Build a function type. RETURN_TYPE is the type returned by the
8260 function; VAARGS indicates whether the function takes varargs. The
8261 function takes N named arguments, the types of which are provided in
8262 ARG_TYPES. */
8264 static tree
8265 build_function_type_array_1 (bool vaargs, tree return_type, int n,
8266 tree *arg_types)
8268 int i;
8269 tree t = vaargs ? NULL_TREE : void_list_node;
8271 for (i = n - 1; i >= 0; i--)
8272 t = tree_cons (NULL_TREE, arg_types[i], t);
8274 return build_function_type (return_type, t);
8277 /* Build a function type. RETURN_TYPE is the type returned by the
8278 function. The function takes N named arguments, the types of which
8279 are provided in ARG_TYPES. */
8281 tree
8282 build_function_type_array (tree return_type, int n, tree *arg_types)
8284 return build_function_type_array_1 (false, return_type, n, arg_types);
8287 /* Build a variable argument function type. RETURN_TYPE is the type
8288 returned by the function. The function takes N named arguments, the
8289 types of which are provided in ARG_TYPES. */
8291 tree
8292 build_varargs_function_type_array (tree return_type, int n, tree *arg_types)
8294 return build_function_type_array_1 (true, return_type, n, arg_types);
8297 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
8298 and ARGTYPES (a TREE_LIST) are the return type and arguments types
8299 for the method. An implicit additional parameter (of type
8300 pointer-to-BASETYPE) is added to the ARGTYPES. */
8302 tree
8303 build_method_type_directly (tree basetype,
8304 tree rettype,
8305 tree argtypes)
8307 tree t;
8308 tree ptype;
8309 int hashcode = 0;
8310 bool any_structural_p, any_noncanonical_p;
8311 tree canon_argtypes;
8313 /* Make a node of the sort we want. */
8314 t = make_node (METHOD_TYPE);
8316 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8317 TREE_TYPE (t) = rettype;
8318 ptype = build_pointer_type (basetype);
8320 /* The actual arglist for this function includes a "hidden" argument
8321 which is "this". Put it into the list of argument types. */
8322 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
8323 TYPE_ARG_TYPES (t) = argtypes;
8325 /* If we already have such a type, use the old one. */
8326 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
8327 hashcode = iterative_hash_object (TYPE_HASH (rettype), hashcode);
8328 hashcode = type_hash_list (argtypes, hashcode);
8329 t = type_hash_canon (hashcode, t);
8331 /* Set up the canonical type. */
8332 any_structural_p
8333 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8334 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
8335 any_noncanonical_p
8336 = (TYPE_CANONICAL (basetype) != basetype
8337 || TYPE_CANONICAL (rettype) != rettype);
8338 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
8339 &any_structural_p,
8340 &any_noncanonical_p);
8341 if (any_structural_p)
8342 SET_TYPE_STRUCTURAL_EQUALITY (t);
8343 else if (any_noncanonical_p)
8344 TYPE_CANONICAL (t)
8345 = build_method_type_directly (TYPE_CANONICAL (basetype),
8346 TYPE_CANONICAL (rettype),
8347 canon_argtypes);
8348 if (!COMPLETE_TYPE_P (t))
8349 layout_type (t);
8351 return t;
8354 /* Construct, lay out and return the type of methods belonging to class
8355 BASETYPE and whose arguments and values are described by TYPE.
8356 If that type exists already, reuse it.
8357 TYPE must be a FUNCTION_TYPE node. */
8359 tree
8360 build_method_type (tree basetype, tree type)
8362 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8364 return build_method_type_directly (basetype,
8365 TREE_TYPE (type),
8366 TYPE_ARG_TYPES (type));
8369 /* Construct, lay out and return the type of offsets to a value
8370 of type TYPE, within an object of type BASETYPE.
8371 If a suitable offset type exists already, reuse it. */
8373 tree
8374 build_offset_type (tree basetype, tree type)
8376 tree t;
8377 hashval_t hashcode = 0;
8379 /* Make a node of the sort we want. */
8380 t = make_node (OFFSET_TYPE);
8382 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8383 TREE_TYPE (t) = type;
8385 /* If we already have such a type, use the old one. */
8386 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
8387 hashcode = iterative_hash_object (TYPE_HASH (type), hashcode);
8388 t = type_hash_canon (hashcode, t);
8390 if (!COMPLETE_TYPE_P (t))
8391 layout_type (t);
8393 if (TYPE_CANONICAL (t) == t)
8395 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8396 || TYPE_STRUCTURAL_EQUALITY_P (type))
8397 SET_TYPE_STRUCTURAL_EQUALITY (t);
8398 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
8399 || TYPE_CANONICAL (type) != type)
8400 TYPE_CANONICAL (t)
8401 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
8402 TYPE_CANONICAL (type));
8405 return t;
8408 /* Create a complex type whose components are COMPONENT_TYPE. */
8410 tree
8411 build_complex_type (tree component_type)
8413 tree t;
8414 hashval_t hashcode;
8416 gcc_assert (INTEGRAL_TYPE_P (component_type)
8417 || SCALAR_FLOAT_TYPE_P (component_type)
8418 || FIXED_POINT_TYPE_P (component_type));
8420 /* Make a node of the sort we want. */
8421 t = make_node (COMPLEX_TYPE);
8423 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
8425 /* If we already have such a type, use the old one. */
8426 hashcode = iterative_hash_object (TYPE_HASH (component_type), 0);
8427 t = type_hash_canon (hashcode, t);
8429 if (!COMPLETE_TYPE_P (t))
8430 layout_type (t);
8432 if (TYPE_CANONICAL (t) == t)
8434 if (TYPE_STRUCTURAL_EQUALITY_P (component_type))
8435 SET_TYPE_STRUCTURAL_EQUALITY (t);
8436 else if (TYPE_CANONICAL (component_type) != component_type)
8437 TYPE_CANONICAL (t)
8438 = build_complex_type (TYPE_CANONICAL (component_type));
8441 /* We need to create a name, since complex is a fundamental type. */
8442 if (! TYPE_NAME (t))
8444 const char *name;
8445 if (component_type == char_type_node)
8446 name = "complex char";
8447 else if (component_type == signed_char_type_node)
8448 name = "complex signed char";
8449 else if (component_type == unsigned_char_type_node)
8450 name = "complex unsigned char";
8451 else if (component_type == short_integer_type_node)
8452 name = "complex short int";
8453 else if (component_type == short_unsigned_type_node)
8454 name = "complex short unsigned int";
8455 else if (component_type == integer_type_node)
8456 name = "complex int";
8457 else if (component_type == unsigned_type_node)
8458 name = "complex unsigned int";
8459 else if (component_type == long_integer_type_node)
8460 name = "complex long int";
8461 else if (component_type == long_unsigned_type_node)
8462 name = "complex long unsigned int";
8463 else if (component_type == long_long_integer_type_node)
8464 name = "complex long long int";
8465 else if (component_type == long_long_unsigned_type_node)
8466 name = "complex long long unsigned int";
8467 else
8468 name = 0;
8470 if (name != 0)
8471 TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
8472 get_identifier (name), t);
8475 return build_qualified_type (t, TYPE_QUALS (component_type));
8478 /* If TYPE is a real or complex floating-point type and the target
8479 does not directly support arithmetic on TYPE then return the wider
8480 type to be used for arithmetic on TYPE. Otherwise, return
8481 NULL_TREE. */
8483 tree
8484 excess_precision_type (tree type)
8486 if (flag_excess_precision != EXCESS_PRECISION_FAST)
8488 int flt_eval_method = TARGET_FLT_EVAL_METHOD;
8489 switch (TREE_CODE (type))
8491 case REAL_TYPE:
8492 switch (flt_eval_method)
8494 case 1:
8495 if (TYPE_MODE (type) == TYPE_MODE (float_type_node))
8496 return double_type_node;
8497 break;
8498 case 2:
8499 if (TYPE_MODE (type) == TYPE_MODE (float_type_node)
8500 || TYPE_MODE (type) == TYPE_MODE (double_type_node))
8501 return long_double_type_node;
8502 break;
8503 default:
8504 gcc_unreachable ();
8506 break;
8507 case COMPLEX_TYPE:
8508 if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
8509 return NULL_TREE;
8510 switch (flt_eval_method)
8512 case 1:
8513 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node))
8514 return complex_double_type_node;
8515 break;
8516 case 2:
8517 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node)
8518 || (TYPE_MODE (TREE_TYPE (type))
8519 == TYPE_MODE (double_type_node)))
8520 return complex_long_double_type_node;
8521 break;
8522 default:
8523 gcc_unreachable ();
8525 break;
8526 default:
8527 break;
8530 return NULL_TREE;
8533 /* Return OP, stripped of any conversions to wider types as much as is safe.
8534 Converting the value back to OP's type makes a value equivalent to OP.
8536 If FOR_TYPE is nonzero, we return a value which, if converted to
8537 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
8539 OP must have integer, real or enumeral type. Pointers are not allowed!
8541 There are some cases where the obvious value we could return
8542 would regenerate to OP if converted to OP's type,
8543 but would not extend like OP to wider types.
8544 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
8545 For example, if OP is (unsigned short)(signed char)-1,
8546 we avoid returning (signed char)-1 if FOR_TYPE is int,
8547 even though extending that to an unsigned short would regenerate OP,
8548 since the result of extending (signed char)-1 to (int)
8549 is different from (int) OP. */
8551 tree
8552 get_unwidened (tree op, tree for_type)
8554 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
8555 tree type = TREE_TYPE (op);
8556 unsigned final_prec
8557 = TYPE_PRECISION (for_type != 0 ? for_type : type);
8558 int uns
8559 = (for_type != 0 && for_type != type
8560 && final_prec > TYPE_PRECISION (type)
8561 && TYPE_UNSIGNED (type));
8562 tree win = op;
8564 while (CONVERT_EXPR_P (op))
8566 int bitschange;
8568 /* TYPE_PRECISION on vector types has different meaning
8569 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
8570 so avoid them here. */
8571 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
8572 break;
8574 bitschange = TYPE_PRECISION (TREE_TYPE (op))
8575 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
8577 /* Truncations are many-one so cannot be removed.
8578 Unless we are later going to truncate down even farther. */
8579 if (bitschange < 0
8580 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
8581 break;
8583 /* See what's inside this conversion. If we decide to strip it,
8584 we will set WIN. */
8585 op = TREE_OPERAND (op, 0);
8587 /* If we have not stripped any zero-extensions (uns is 0),
8588 we can strip any kind of extension.
8589 If we have previously stripped a zero-extension,
8590 only zero-extensions can safely be stripped.
8591 Any extension can be stripped if the bits it would produce
8592 are all going to be discarded later by truncating to FOR_TYPE. */
8594 if (bitschange > 0)
8596 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
8597 win = op;
8598 /* TYPE_UNSIGNED says whether this is a zero-extension.
8599 Let's avoid computing it if it does not affect WIN
8600 and if UNS will not be needed again. */
8601 if ((uns
8602 || CONVERT_EXPR_P (op))
8603 && TYPE_UNSIGNED (TREE_TYPE (op)))
8605 uns = 1;
8606 win = op;
8611 /* If we finally reach a constant see if it fits in for_type and
8612 in that case convert it. */
8613 if (for_type
8614 && TREE_CODE (win) == INTEGER_CST
8615 && TREE_TYPE (win) != for_type
8616 && int_fits_type_p (win, for_type))
8617 win = fold_convert (for_type, win);
8619 return win;
8622 /* Return OP or a simpler expression for a narrower value
8623 which can be sign-extended or zero-extended to give back OP.
8624 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
8625 or 0 if the value should be sign-extended. */
8627 tree
8628 get_narrower (tree op, int *unsignedp_ptr)
8630 int uns = 0;
8631 int first = 1;
8632 tree win = op;
8633 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
8635 while (TREE_CODE (op) == NOP_EXPR)
8637 int bitschange
8638 = (TYPE_PRECISION (TREE_TYPE (op))
8639 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
8641 /* Truncations are many-one so cannot be removed. */
8642 if (bitschange < 0)
8643 break;
8645 /* See what's inside this conversion. If we decide to strip it,
8646 we will set WIN. */
8648 if (bitschange > 0)
8650 op = TREE_OPERAND (op, 0);
8651 /* An extension: the outermost one can be stripped,
8652 but remember whether it is zero or sign extension. */
8653 if (first)
8654 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8655 /* Otherwise, if a sign extension has been stripped,
8656 only sign extensions can now be stripped;
8657 if a zero extension has been stripped, only zero-extensions. */
8658 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
8659 break;
8660 first = 0;
8662 else /* bitschange == 0 */
8664 /* A change in nominal type can always be stripped, but we must
8665 preserve the unsignedness. */
8666 if (first)
8667 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8668 first = 0;
8669 op = TREE_OPERAND (op, 0);
8670 /* Keep trying to narrow, but don't assign op to win if it
8671 would turn an integral type into something else. */
8672 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
8673 continue;
8676 win = op;
8679 if (TREE_CODE (op) == COMPONENT_REF
8680 /* Since type_for_size always gives an integer type. */
8681 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
8682 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
8683 /* Ensure field is laid out already. */
8684 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
8685 && tree_fits_uhwi_p (DECL_SIZE (TREE_OPERAND (op, 1))))
8687 unsigned HOST_WIDE_INT innerprec
8688 = tree_to_uhwi (DECL_SIZE (TREE_OPERAND (op, 1)));
8689 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
8690 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
8691 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
8693 /* We can get this structure field in a narrower type that fits it,
8694 but the resulting extension to its nominal type (a fullword type)
8695 must satisfy the same conditions as for other extensions.
8697 Do this only for fields that are aligned (not bit-fields),
8698 because when bit-field insns will be used there is no
8699 advantage in doing this. */
8701 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
8702 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
8703 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
8704 && type != 0)
8706 if (first)
8707 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
8708 win = fold_convert (type, op);
8712 *unsignedp_ptr = uns;
8713 return win;
8716 /* Returns true if integer constant C has a value that is permissible
8717 for type TYPE (an INTEGER_TYPE). */
8719 bool
8720 int_fits_type_p (const_tree c, const_tree type)
8722 tree type_low_bound, type_high_bound;
8723 bool ok_for_low_bound, ok_for_high_bound, unsc;
8724 double_int dc, dd;
8726 dc = tree_to_double_int (c);
8727 unsc = TYPE_UNSIGNED (TREE_TYPE (c));
8729 retry:
8730 type_low_bound = TYPE_MIN_VALUE (type);
8731 type_high_bound = TYPE_MAX_VALUE (type);
8733 /* If at least one bound of the type is a constant integer, we can check
8734 ourselves and maybe make a decision. If no such decision is possible, but
8735 this type is a subtype, try checking against that. Otherwise, use
8736 double_int_fits_to_tree_p, which checks against the precision.
8738 Compute the status for each possibly constant bound, and return if we see
8739 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
8740 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
8741 for "constant known to fit". */
8743 /* Check if c >= type_low_bound. */
8744 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
8746 dd = tree_to_double_int (type_low_bound);
8747 if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_low_bound)))
8749 int c_neg = (!unsc && dc.is_negative ());
8750 int t_neg = (unsc && dd.is_negative ());
8752 if (c_neg && !t_neg)
8753 return false;
8754 if ((c_neg || !t_neg) && dc.ult (dd))
8755 return false;
8757 else if (dc.cmp (dd, unsc) < 0)
8758 return false;
8759 ok_for_low_bound = true;
8761 else
8762 ok_for_low_bound = false;
8764 /* Check if c <= type_high_bound. */
8765 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
8767 dd = tree_to_double_int (type_high_bound);
8768 if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_high_bound)))
8770 int c_neg = (!unsc && dc.is_negative ());
8771 int t_neg = (unsc && dd.is_negative ());
8773 if (t_neg && !c_neg)
8774 return false;
8775 if ((t_neg || !c_neg) && dc.ugt (dd))
8776 return false;
8778 else if (dc.cmp (dd, unsc) > 0)
8779 return false;
8780 ok_for_high_bound = true;
8782 else
8783 ok_for_high_bound = false;
8785 /* If the constant fits both bounds, the result is known. */
8786 if (ok_for_low_bound && ok_for_high_bound)
8787 return true;
8789 /* Perform some generic filtering which may allow making a decision
8790 even if the bounds are not constant. First, negative integers
8791 never fit in unsigned types, */
8792 if (TYPE_UNSIGNED (type) && !unsc && dc.is_negative ())
8793 return false;
8795 /* Second, narrower types always fit in wider ones. */
8796 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
8797 return true;
8799 /* Third, unsigned integers with top bit set never fit signed types. */
8800 if (! TYPE_UNSIGNED (type) && unsc)
8802 int prec = GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (c))) - 1;
8803 if (prec < HOST_BITS_PER_WIDE_INT)
8805 if (((((unsigned HOST_WIDE_INT) 1) << prec) & dc.low) != 0)
8806 return false;
8808 else if (((((unsigned HOST_WIDE_INT) 1)
8809 << (prec - HOST_BITS_PER_WIDE_INT)) & dc.high) != 0)
8810 return false;
8813 /* If we haven't been able to decide at this point, there nothing more we
8814 can check ourselves here. Look at the base type if we have one and it
8815 has the same precision. */
8816 if (TREE_CODE (type) == INTEGER_TYPE
8817 && TREE_TYPE (type) != 0
8818 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
8820 type = TREE_TYPE (type);
8821 goto retry;
8824 /* Or to double_int_fits_to_tree_p, if nothing else. */
8825 return double_int_fits_to_tree_p (type, dc);
8828 /* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
8829 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
8830 represented (assuming two's-complement arithmetic) within the bit
8831 precision of the type are returned instead. */
8833 void
8834 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
8836 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
8837 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
8838 mpz_set_double_int (min, tree_to_double_int (TYPE_MIN_VALUE (type)),
8839 TYPE_UNSIGNED (type));
8840 else
8842 if (TYPE_UNSIGNED (type))
8843 mpz_set_ui (min, 0);
8844 else
8846 double_int mn;
8847 mn = double_int::mask (TYPE_PRECISION (type) - 1);
8848 mn = (mn + double_int_one).sext (TYPE_PRECISION (type));
8849 mpz_set_double_int (min, mn, false);
8853 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
8854 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
8855 mpz_set_double_int (max, tree_to_double_int (TYPE_MAX_VALUE (type)),
8856 TYPE_UNSIGNED (type));
8857 else
8859 if (TYPE_UNSIGNED (type))
8860 mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type)),
8861 true);
8862 else
8863 mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type) - 1),
8864 true);
8868 /* Return true if VAR is an automatic variable defined in function FN. */
8870 bool
8871 auto_var_in_fn_p (const_tree var, const_tree fn)
8873 return (DECL_P (var) && DECL_CONTEXT (var) == fn
8874 && ((((TREE_CODE (var) == VAR_DECL && ! DECL_EXTERNAL (var))
8875 || TREE_CODE (var) == PARM_DECL)
8876 && ! TREE_STATIC (var))
8877 || TREE_CODE (var) == LABEL_DECL
8878 || TREE_CODE (var) == RESULT_DECL));
8881 /* Subprogram of following function. Called by walk_tree.
8883 Return *TP if it is an automatic variable or parameter of the
8884 function passed in as DATA. */
8886 static tree
8887 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
8889 tree fn = (tree) data;
8891 if (TYPE_P (*tp))
8892 *walk_subtrees = 0;
8894 else if (DECL_P (*tp)
8895 && auto_var_in_fn_p (*tp, fn))
8896 return *tp;
8898 return NULL_TREE;
8901 /* Returns true if T is, contains, or refers to a type with variable
8902 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
8903 arguments, but not the return type. If FN is nonzero, only return
8904 true if a modifier of the type or position of FN is a variable or
8905 parameter inside FN.
8907 This concept is more general than that of C99 'variably modified types':
8908 in C99, a struct type is never variably modified because a VLA may not
8909 appear as a structure member. However, in GNU C code like:
8911 struct S { int i[f()]; };
8913 is valid, and other languages may define similar constructs. */
8915 bool
8916 variably_modified_type_p (tree type, tree fn)
8918 tree t;
8920 /* Test if T is either variable (if FN is zero) or an expression containing
8921 a variable in FN. If TYPE isn't gimplified, return true also if
8922 gimplify_one_sizepos would gimplify the expression into a local
8923 variable. */
8924 #define RETURN_TRUE_IF_VAR(T) \
8925 do { tree _t = (T); \
8926 if (_t != NULL_TREE \
8927 && _t != error_mark_node \
8928 && TREE_CODE (_t) != INTEGER_CST \
8929 && TREE_CODE (_t) != PLACEHOLDER_EXPR \
8930 && (!fn \
8931 || (!TYPE_SIZES_GIMPLIFIED (type) \
8932 && !is_gimple_sizepos (_t)) \
8933 || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
8934 return true; } while (0)
8936 if (type == error_mark_node)
8937 return false;
8939 /* If TYPE itself has variable size, it is variably modified. */
8940 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
8941 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
8943 switch (TREE_CODE (type))
8945 case POINTER_TYPE:
8946 case REFERENCE_TYPE:
8947 case VECTOR_TYPE:
8948 if (variably_modified_type_p (TREE_TYPE (type), fn))
8949 return true;
8950 break;
8952 case FUNCTION_TYPE:
8953 case METHOD_TYPE:
8954 /* If TYPE is a function type, it is variably modified if the
8955 return type is variably modified. */
8956 if (variably_modified_type_p (TREE_TYPE (type), fn))
8957 return true;
8958 break;
8960 case INTEGER_TYPE:
8961 case REAL_TYPE:
8962 case FIXED_POINT_TYPE:
8963 case ENUMERAL_TYPE:
8964 case BOOLEAN_TYPE:
8965 /* Scalar types are variably modified if their end points
8966 aren't constant. */
8967 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
8968 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
8969 break;
8971 case RECORD_TYPE:
8972 case UNION_TYPE:
8973 case QUAL_UNION_TYPE:
8974 /* We can't see if any of the fields are variably-modified by the
8975 definition we normally use, since that would produce infinite
8976 recursion via pointers. */
8977 /* This is variably modified if some field's type is. */
8978 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
8979 if (TREE_CODE (t) == FIELD_DECL)
8981 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
8982 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
8983 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
8985 if (TREE_CODE (type) == QUAL_UNION_TYPE)
8986 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
8988 break;
8990 case ARRAY_TYPE:
8991 /* Do not call ourselves to avoid infinite recursion. This is
8992 variably modified if the element type is. */
8993 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
8994 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
8995 break;
8997 default:
8998 break;
9001 /* The current language may have other cases to check, but in general,
9002 all other types are not variably modified. */
9003 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
9005 #undef RETURN_TRUE_IF_VAR
9008 /* Given a DECL or TYPE, return the scope in which it was declared, or
9009 NULL_TREE if there is no containing scope. */
9011 tree
9012 get_containing_scope (const_tree t)
9014 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
9017 /* Return the innermost context enclosing DECL that is
9018 a FUNCTION_DECL, or zero if none. */
9020 tree
9021 decl_function_context (const_tree decl)
9023 tree context;
9025 if (TREE_CODE (decl) == ERROR_MARK)
9026 return 0;
9028 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
9029 where we look up the function at runtime. Such functions always take
9030 a first argument of type 'pointer to real context'.
9032 C++ should really be fixed to use DECL_CONTEXT for the real context,
9033 and use something else for the "virtual context". */
9034 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
9035 context
9036 = TYPE_MAIN_VARIANT
9037 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
9038 else
9039 context = DECL_CONTEXT (decl);
9041 while (context && TREE_CODE (context) != FUNCTION_DECL)
9043 if (TREE_CODE (context) == BLOCK)
9044 context = BLOCK_SUPERCONTEXT (context);
9045 else
9046 context = get_containing_scope (context);
9049 return context;
9052 /* Return the innermost context enclosing DECL that is
9053 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
9054 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
9056 tree
9057 decl_type_context (const_tree decl)
9059 tree context = DECL_CONTEXT (decl);
9061 while (context)
9062 switch (TREE_CODE (context))
9064 case NAMESPACE_DECL:
9065 case TRANSLATION_UNIT_DECL:
9066 return NULL_TREE;
9068 case RECORD_TYPE:
9069 case UNION_TYPE:
9070 case QUAL_UNION_TYPE:
9071 return context;
9073 case TYPE_DECL:
9074 case FUNCTION_DECL:
9075 context = DECL_CONTEXT (context);
9076 break;
9078 case BLOCK:
9079 context = BLOCK_SUPERCONTEXT (context);
9080 break;
9082 default:
9083 gcc_unreachable ();
9086 return NULL_TREE;
9089 /* CALL is a CALL_EXPR. Return the declaration for the function
9090 called, or NULL_TREE if the called function cannot be
9091 determined. */
9093 tree
9094 get_callee_fndecl (const_tree call)
9096 tree addr;
9098 if (call == error_mark_node)
9099 return error_mark_node;
9101 /* It's invalid to call this function with anything but a
9102 CALL_EXPR. */
9103 gcc_assert (TREE_CODE (call) == CALL_EXPR);
9105 /* The first operand to the CALL is the address of the function
9106 called. */
9107 addr = CALL_EXPR_FN (call);
9109 STRIP_NOPS (addr);
9111 /* If this is a readonly function pointer, extract its initial value. */
9112 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
9113 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
9114 && DECL_INITIAL (addr))
9115 addr = DECL_INITIAL (addr);
9117 /* If the address is just `&f' for some function `f', then we know
9118 that `f' is being called. */
9119 if (TREE_CODE (addr) == ADDR_EXPR
9120 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
9121 return TREE_OPERAND (addr, 0);
9123 /* We couldn't figure out what was being called. */
9124 return NULL_TREE;
9127 /* Print debugging information about tree nodes generated during the compile,
9128 and any language-specific information. */
9130 void
9131 dump_tree_statistics (void)
9133 if (GATHER_STATISTICS)
9135 int i;
9136 int total_nodes, total_bytes;
9137 fprintf (stderr, "Kind Nodes Bytes\n");
9138 fprintf (stderr, "---------------------------------------\n");
9139 total_nodes = total_bytes = 0;
9140 for (i = 0; i < (int) all_kinds; i++)
9142 fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
9143 tree_node_counts[i], tree_node_sizes[i]);
9144 total_nodes += tree_node_counts[i];
9145 total_bytes += tree_node_sizes[i];
9147 fprintf (stderr, "---------------------------------------\n");
9148 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
9149 fprintf (stderr, "---------------------------------------\n");
9150 fprintf (stderr, "Code Nodes\n");
9151 fprintf (stderr, "----------------------------\n");
9152 for (i = 0; i < (int) MAX_TREE_CODES; i++)
9153 fprintf (stderr, "%-20s %7d\n", get_tree_code_name ((enum tree_code) i),
9154 tree_code_counts[i]);
9155 fprintf (stderr, "----------------------------\n");
9156 ssanames_print_statistics ();
9157 phinodes_print_statistics ();
9159 else
9160 fprintf (stderr, "(No per-node statistics)\n");
9162 print_type_hash_statistics ();
9163 print_debug_expr_statistics ();
9164 print_value_expr_statistics ();
9165 lang_hooks.print_statistics ();
9168 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
9170 /* Generate a crc32 of a byte. */
9172 static unsigned
9173 crc32_unsigned_bits (unsigned chksum, unsigned value, unsigned bits)
9175 unsigned ix;
9177 for (ix = bits; ix--; value <<= 1)
9179 unsigned feedback;
9181 feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
9182 chksum <<= 1;
9183 chksum ^= feedback;
9185 return chksum;
9188 /* Generate a crc32 of a 32-bit unsigned. */
9190 unsigned
9191 crc32_unsigned (unsigned chksum, unsigned value)
9193 return crc32_unsigned_bits (chksum, value, 32);
9196 /* Generate a crc32 of a byte. */
9198 unsigned
9199 crc32_byte (unsigned chksum, char byte)
9201 return crc32_unsigned_bits (chksum, (unsigned) byte << 24, 8);
9204 /* Generate a crc32 of a string. */
9206 unsigned
9207 crc32_string (unsigned chksum, const char *string)
9211 chksum = crc32_byte (chksum, *string);
9213 while (*string++);
9214 return chksum;
9217 /* P is a string that will be used in a symbol. Mask out any characters
9218 that are not valid in that context. */
9220 void
9221 clean_symbol_name (char *p)
9223 for (; *p; p++)
9224 if (! (ISALNUM (*p)
9225 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
9226 || *p == '$'
9227 #endif
9228 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
9229 || *p == '.'
9230 #endif
9232 *p = '_';
9235 /* Generate a name for a special-purpose function.
9236 The generated name may need to be unique across the whole link.
9237 Changes to this function may also require corresponding changes to
9238 xstrdup_mask_random.
9239 TYPE is some string to identify the purpose of this function to the
9240 linker or collect2; it must start with an uppercase letter,
9241 one of:
9242 I - for constructors
9243 D - for destructors
9244 N - for C++ anonymous namespaces
9245 F - for DWARF unwind frame information. */
9247 tree
9248 get_file_function_name (const char *type)
9250 char *buf;
9251 const char *p;
9252 char *q;
9254 /* If we already have a name we know to be unique, just use that. */
9255 if (first_global_object_name)
9256 p = q = ASTRDUP (first_global_object_name);
9257 /* If the target is handling the constructors/destructors, they
9258 will be local to this file and the name is only necessary for
9259 debugging purposes.
9260 We also assign sub_I and sub_D sufixes to constructors called from
9261 the global static constructors. These are always local. */
9262 else if (((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
9263 || (strncmp (type, "sub_", 4) == 0
9264 && (type[4] == 'I' || type[4] == 'D')))
9266 const char *file = main_input_filename;
9267 if (! file)
9268 file = LOCATION_FILE (input_location);
9269 /* Just use the file's basename, because the full pathname
9270 might be quite long. */
9271 p = q = ASTRDUP (lbasename (file));
9273 else
9275 /* Otherwise, the name must be unique across the entire link.
9276 We don't have anything that we know to be unique to this translation
9277 unit, so use what we do have and throw in some randomness. */
9278 unsigned len;
9279 const char *name = weak_global_object_name;
9280 const char *file = main_input_filename;
9282 if (! name)
9283 name = "";
9284 if (! file)
9285 file = LOCATION_FILE (input_location);
9287 len = strlen (file);
9288 q = (char *) alloca (9 + 17 + len + 1);
9289 memcpy (q, file, len + 1);
9291 snprintf (q + len, 9 + 17 + 1, "_%08X_" HOST_WIDE_INT_PRINT_HEX,
9292 crc32_string (0, name), get_random_seed (false));
9294 p = q;
9297 clean_symbol_name (q);
9298 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
9299 + strlen (type));
9301 /* Set up the name of the file-level functions we may need.
9302 Use a global object (which is already required to be unique over
9303 the program) rather than the file name (which imposes extra
9304 constraints). */
9305 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
9307 return get_identifier (buf);
9310 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
9312 /* Complain that the tree code of NODE does not match the expected 0
9313 terminated list of trailing codes. The trailing code list can be
9314 empty, for a more vague error message. FILE, LINE, and FUNCTION
9315 are of the caller. */
9317 void
9318 tree_check_failed (const_tree node, const char *file,
9319 int line, const char *function, ...)
9321 va_list args;
9322 const char *buffer;
9323 unsigned length = 0;
9324 enum tree_code code;
9326 va_start (args, function);
9327 while ((code = (enum tree_code) va_arg (args, int)))
9328 length += 4 + strlen (get_tree_code_name (code));
9329 va_end (args);
9330 if (length)
9332 char *tmp;
9333 va_start (args, function);
9334 length += strlen ("expected ");
9335 buffer = tmp = (char *) alloca (length);
9336 length = 0;
9337 while ((code = (enum tree_code) va_arg (args, int)))
9339 const char *prefix = length ? " or " : "expected ";
9341 strcpy (tmp + length, prefix);
9342 length += strlen (prefix);
9343 strcpy (tmp + length, get_tree_code_name (code));
9344 length += strlen (get_tree_code_name (code));
9346 va_end (args);
9348 else
9349 buffer = "unexpected node";
9351 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9352 buffer, get_tree_code_name (TREE_CODE (node)),
9353 function, trim_filename (file), line);
9356 /* Complain that the tree code of NODE does match the expected 0
9357 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
9358 the caller. */
9360 void
9361 tree_not_check_failed (const_tree node, const char *file,
9362 int line, const char *function, ...)
9364 va_list args;
9365 char *buffer;
9366 unsigned length = 0;
9367 enum tree_code code;
9369 va_start (args, function);
9370 while ((code = (enum tree_code) va_arg (args, int)))
9371 length += 4 + strlen (get_tree_code_name (code));
9372 va_end (args);
9373 va_start (args, function);
9374 buffer = (char *) alloca (length);
9375 length = 0;
9376 while ((code = (enum tree_code) va_arg (args, int)))
9378 if (length)
9380 strcpy (buffer + length, " or ");
9381 length += 4;
9383 strcpy (buffer + length, get_tree_code_name (code));
9384 length += strlen (get_tree_code_name (code));
9386 va_end (args);
9388 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
9389 buffer, get_tree_code_name (TREE_CODE (node)),
9390 function, trim_filename (file), line);
9393 /* Similar to tree_check_failed, except that we check for a class of tree
9394 code, given in CL. */
9396 void
9397 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
9398 const char *file, int line, const char *function)
9400 internal_error
9401 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
9402 TREE_CODE_CLASS_STRING (cl),
9403 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9404 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9407 /* Similar to tree_check_failed, except that instead of specifying a
9408 dozen codes, use the knowledge that they're all sequential. */
9410 void
9411 tree_range_check_failed (const_tree node, const char *file, int line,
9412 const char *function, enum tree_code c1,
9413 enum tree_code c2)
9415 char *buffer;
9416 unsigned length = 0;
9417 unsigned int c;
9419 for (c = c1; c <= c2; ++c)
9420 length += 4 + strlen (get_tree_code_name ((enum tree_code) c));
9422 length += strlen ("expected ");
9423 buffer = (char *) alloca (length);
9424 length = 0;
9426 for (c = c1; c <= c2; ++c)
9428 const char *prefix = length ? " or " : "expected ";
9430 strcpy (buffer + length, prefix);
9431 length += strlen (prefix);
9432 strcpy (buffer + length, get_tree_code_name ((enum tree_code) c));
9433 length += strlen (get_tree_code_name ((enum tree_code) c));
9436 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9437 buffer, get_tree_code_name (TREE_CODE (node)),
9438 function, trim_filename (file), line);
9442 /* Similar to tree_check_failed, except that we check that a tree does
9443 not have the specified code, given in CL. */
9445 void
9446 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
9447 const char *file, int line, const char *function)
9449 internal_error
9450 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
9451 TREE_CODE_CLASS_STRING (cl),
9452 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9453 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9457 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
9459 void
9460 omp_clause_check_failed (const_tree node, const char *file, int line,
9461 const char *function, enum omp_clause_code code)
9463 internal_error ("tree check: expected omp_clause %s, have %s in %s, at %s:%d",
9464 omp_clause_code_name[code], get_tree_code_name (TREE_CODE (node)),
9465 function, trim_filename (file), line);
9469 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
9471 void
9472 omp_clause_range_check_failed (const_tree node, const char *file, int line,
9473 const char *function, enum omp_clause_code c1,
9474 enum omp_clause_code c2)
9476 char *buffer;
9477 unsigned length = 0;
9478 unsigned int c;
9480 for (c = c1; c <= c2; ++c)
9481 length += 4 + strlen (omp_clause_code_name[c]);
9483 length += strlen ("expected ");
9484 buffer = (char *) alloca (length);
9485 length = 0;
9487 for (c = c1; c <= c2; ++c)
9489 const char *prefix = length ? " or " : "expected ";
9491 strcpy (buffer + length, prefix);
9492 length += strlen (prefix);
9493 strcpy (buffer + length, omp_clause_code_name[c]);
9494 length += strlen (omp_clause_code_name[c]);
9497 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9498 buffer, omp_clause_code_name[TREE_CODE (node)],
9499 function, trim_filename (file), line);
9503 #undef DEFTREESTRUCT
9504 #define DEFTREESTRUCT(VAL, NAME) NAME,
9506 static const char *ts_enum_names[] = {
9507 #include "treestruct.def"
9509 #undef DEFTREESTRUCT
9511 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
9513 /* Similar to tree_class_check_failed, except that we check for
9514 whether CODE contains the tree structure identified by EN. */
9516 void
9517 tree_contains_struct_check_failed (const_tree node,
9518 const enum tree_node_structure_enum en,
9519 const char *file, int line,
9520 const char *function)
9522 internal_error
9523 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
9524 TS_ENUM_NAME (en),
9525 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9529 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9530 (dynamically sized) vector. */
9532 void
9533 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
9534 const char *function)
9536 internal_error
9537 ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
9538 idx + 1, len, function, trim_filename (file), line);
9541 /* Similar to above, except that the check is for the bounds of the operand
9542 vector of an expression node EXP. */
9544 void
9545 tree_operand_check_failed (int idx, const_tree exp, const char *file,
9546 int line, const char *function)
9548 enum tree_code code = TREE_CODE (exp);
9549 internal_error
9550 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
9551 idx + 1, get_tree_code_name (code), TREE_OPERAND_LENGTH (exp),
9552 function, trim_filename (file), line);
9555 /* Similar to above, except that the check is for the number of
9556 operands of an OMP_CLAUSE node. */
9558 void
9559 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
9560 int line, const char *function)
9562 internal_error
9563 ("tree check: accessed operand %d of omp_clause %s with %d operands "
9564 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
9565 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
9566 trim_filename (file), line);
9568 #endif /* ENABLE_TREE_CHECKING */
9570 /* Create a new vector type node holding SUBPARTS units of type INNERTYPE,
9571 and mapped to the machine mode MODE. Initialize its fields and build
9572 the information necessary for debugging output. */
9574 static tree
9575 make_vector_type (tree innertype, int nunits, enum machine_mode mode)
9577 tree t;
9578 hashval_t hashcode = 0;
9580 t = make_node (VECTOR_TYPE);
9581 TREE_TYPE (t) = TYPE_MAIN_VARIANT (innertype);
9582 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
9583 SET_TYPE_MODE (t, mode);
9585 if (TYPE_STRUCTURAL_EQUALITY_P (innertype))
9586 SET_TYPE_STRUCTURAL_EQUALITY (t);
9587 else if (TYPE_CANONICAL (innertype) != innertype
9588 || mode != VOIDmode)
9589 TYPE_CANONICAL (t)
9590 = make_vector_type (TYPE_CANONICAL (innertype), nunits, VOIDmode);
9592 layout_type (t);
9594 hashcode = iterative_hash_host_wide_int (VECTOR_TYPE, hashcode);
9595 hashcode = iterative_hash_host_wide_int (nunits, hashcode);
9596 hashcode = iterative_hash_host_wide_int (mode, hashcode);
9597 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (t)), hashcode);
9598 t = type_hash_canon (hashcode, t);
9600 /* We have built a main variant, based on the main variant of the
9601 inner type. Use it to build the variant we return. */
9602 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
9603 && TREE_TYPE (t) != innertype)
9604 return build_type_attribute_qual_variant (t,
9605 TYPE_ATTRIBUTES (innertype),
9606 TYPE_QUALS (innertype));
9608 return t;
9611 static tree
9612 make_or_reuse_type (unsigned size, int unsignedp)
9614 if (size == INT_TYPE_SIZE)
9615 return unsignedp ? unsigned_type_node : integer_type_node;
9616 if (size == CHAR_TYPE_SIZE)
9617 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
9618 if (size == SHORT_TYPE_SIZE)
9619 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
9620 if (size == LONG_TYPE_SIZE)
9621 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
9622 if (size == LONG_LONG_TYPE_SIZE)
9623 return (unsignedp ? long_long_unsigned_type_node
9624 : long_long_integer_type_node);
9625 if (size == 128 && int128_integer_type_node)
9626 return (unsignedp ? int128_unsigned_type_node
9627 : int128_integer_type_node);
9629 if (unsignedp)
9630 return make_unsigned_type (size);
9631 else
9632 return make_signed_type (size);
9635 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
9637 static tree
9638 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
9640 if (satp)
9642 if (size == SHORT_FRACT_TYPE_SIZE)
9643 return unsignedp ? sat_unsigned_short_fract_type_node
9644 : sat_short_fract_type_node;
9645 if (size == FRACT_TYPE_SIZE)
9646 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
9647 if (size == LONG_FRACT_TYPE_SIZE)
9648 return unsignedp ? sat_unsigned_long_fract_type_node
9649 : sat_long_fract_type_node;
9650 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9651 return unsignedp ? sat_unsigned_long_long_fract_type_node
9652 : sat_long_long_fract_type_node;
9654 else
9656 if (size == SHORT_FRACT_TYPE_SIZE)
9657 return unsignedp ? unsigned_short_fract_type_node
9658 : short_fract_type_node;
9659 if (size == FRACT_TYPE_SIZE)
9660 return unsignedp ? unsigned_fract_type_node : fract_type_node;
9661 if (size == LONG_FRACT_TYPE_SIZE)
9662 return unsignedp ? unsigned_long_fract_type_node
9663 : long_fract_type_node;
9664 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9665 return unsignedp ? unsigned_long_long_fract_type_node
9666 : long_long_fract_type_node;
9669 return make_fract_type (size, unsignedp, satp);
9672 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
9674 static tree
9675 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
9677 if (satp)
9679 if (size == SHORT_ACCUM_TYPE_SIZE)
9680 return unsignedp ? sat_unsigned_short_accum_type_node
9681 : sat_short_accum_type_node;
9682 if (size == ACCUM_TYPE_SIZE)
9683 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
9684 if (size == LONG_ACCUM_TYPE_SIZE)
9685 return unsignedp ? sat_unsigned_long_accum_type_node
9686 : sat_long_accum_type_node;
9687 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9688 return unsignedp ? sat_unsigned_long_long_accum_type_node
9689 : sat_long_long_accum_type_node;
9691 else
9693 if (size == SHORT_ACCUM_TYPE_SIZE)
9694 return unsignedp ? unsigned_short_accum_type_node
9695 : short_accum_type_node;
9696 if (size == ACCUM_TYPE_SIZE)
9697 return unsignedp ? unsigned_accum_type_node : accum_type_node;
9698 if (size == LONG_ACCUM_TYPE_SIZE)
9699 return unsignedp ? unsigned_long_accum_type_node
9700 : long_accum_type_node;
9701 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9702 return unsignedp ? unsigned_long_long_accum_type_node
9703 : long_long_accum_type_node;
9706 return make_accum_type (size, unsignedp, satp);
9710 /* Create an atomic variant node for TYPE. This routine is called
9711 during initialization of data types to create the 5 basic atomic
9712 types. The generic build_variant_type function requires these to
9713 already be set up in order to function properly, so cannot be
9714 called from there. If ALIGN is non-zero, then ensure alignment is
9715 overridden to this value. */
9717 static tree
9718 build_atomic_base (tree type, unsigned int align)
9720 tree t;
9722 /* Make sure its not already registered. */
9723 if ((t = get_qualified_type (type, TYPE_QUAL_ATOMIC)))
9724 return t;
9726 t = build_variant_type_copy (type);
9727 set_type_quals (t, TYPE_QUAL_ATOMIC, NULL_TREE);
9729 if (align)
9730 TYPE_ALIGN (t) = align;
9732 return t;
9735 /* Create nodes for all integer types (and error_mark_node) using the sizes
9736 of C datatypes. SIGNED_CHAR specifies whether char is signed,
9737 SHORT_DOUBLE specifies whether double should be of the same precision
9738 as float. */
9740 void
9741 build_common_tree_nodes (bool signed_char, bool short_double)
9743 error_mark_node = make_node (ERROR_MARK);
9744 TREE_TYPE (error_mark_node) = error_mark_node;
9746 initialize_sizetypes ();
9748 /* Define both `signed char' and `unsigned char'. */
9749 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
9750 TYPE_STRING_FLAG (signed_char_type_node) = 1;
9751 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
9752 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
9754 /* Define `char', which is like either `signed char' or `unsigned char'
9755 but not the same as either. */
9756 char_type_node
9757 = (signed_char
9758 ? make_signed_type (CHAR_TYPE_SIZE)
9759 : make_unsigned_type (CHAR_TYPE_SIZE));
9760 TYPE_STRING_FLAG (char_type_node) = 1;
9762 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
9763 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
9764 integer_type_node = make_signed_type (INT_TYPE_SIZE);
9765 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
9766 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
9767 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
9768 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
9769 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
9770 #if HOST_BITS_PER_WIDE_INT >= 64
9771 /* TODO: This isn't correct, but as logic depends at the moment on
9772 host's instead of target's wide-integer.
9773 If there is a target not supporting TImode, but has an 128-bit
9774 integer-scalar register, this target check needs to be adjusted. */
9775 if (targetm.scalar_mode_supported_p (TImode))
9777 int128_integer_type_node = make_signed_type (128);
9778 int128_unsigned_type_node = make_unsigned_type (128);
9780 #endif
9782 /* Define a boolean type. This type only represents boolean values but
9783 may be larger than char depending on the value of BOOL_TYPE_SIZE.
9784 Front ends which want to override this size (i.e. Java) can redefine
9785 boolean_type_node before calling build_common_tree_nodes_2. */
9786 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
9787 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
9788 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
9789 TYPE_PRECISION (boolean_type_node) = 1;
9791 /* Define what type to use for size_t. */
9792 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
9793 size_type_node = unsigned_type_node;
9794 else if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
9795 size_type_node = long_unsigned_type_node;
9796 else if (strcmp (SIZE_TYPE, "long long unsigned int") == 0)
9797 size_type_node = long_long_unsigned_type_node;
9798 else if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
9799 size_type_node = short_unsigned_type_node;
9800 else
9801 gcc_unreachable ();
9803 /* Fill in the rest of the sized types. Reuse existing type nodes
9804 when possible. */
9805 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
9806 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
9807 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
9808 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
9809 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
9811 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
9812 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
9813 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
9814 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
9815 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
9817 /* Don't call build_qualified type for atomics. That routine does
9818 special processing for atomics, and until they are initialized
9819 it's better not to make that call.
9821 Check to see if there is a target override for atomic types. */
9823 atomicQI_type_node = build_atomic_base (unsigned_intQI_type_node,
9824 targetm.atomic_align_for_mode (QImode));
9825 atomicHI_type_node = build_atomic_base (unsigned_intHI_type_node,
9826 targetm.atomic_align_for_mode (HImode));
9827 atomicSI_type_node = build_atomic_base (unsigned_intSI_type_node,
9828 targetm.atomic_align_for_mode (SImode));
9829 atomicDI_type_node = build_atomic_base (unsigned_intDI_type_node,
9830 targetm.atomic_align_for_mode (DImode));
9831 atomicTI_type_node = build_atomic_base (unsigned_intTI_type_node,
9832 targetm.atomic_align_for_mode (TImode));
9834 access_public_node = get_identifier ("public");
9835 access_protected_node = get_identifier ("protected");
9836 access_private_node = get_identifier ("private");
9838 /* Define these next since types below may used them. */
9839 integer_zero_node = build_int_cst (integer_type_node, 0);
9840 integer_one_node = build_int_cst (integer_type_node, 1);
9841 integer_three_node = build_int_cst (integer_type_node, 3);
9842 integer_minus_one_node = build_int_cst (integer_type_node, -1);
9844 size_zero_node = size_int (0);
9845 size_one_node = size_int (1);
9846 bitsize_zero_node = bitsize_int (0);
9847 bitsize_one_node = bitsize_int (1);
9848 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
9850 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
9851 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
9853 void_type_node = make_node (VOID_TYPE);
9854 layout_type (void_type_node);
9856 /* We are not going to have real types in C with less than byte alignment,
9857 so we might as well not have any types that claim to have it. */
9858 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
9859 TYPE_USER_ALIGN (void_type_node) = 0;
9861 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
9862 layout_type (TREE_TYPE (null_pointer_node));
9864 ptr_type_node = build_pointer_type (void_type_node);
9865 const_ptr_type_node
9866 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
9867 fileptr_type_node = ptr_type_node;
9869 pointer_sized_int_node = build_nonstandard_integer_type (POINTER_SIZE, 1);
9871 float_type_node = make_node (REAL_TYPE);
9872 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
9873 layout_type (float_type_node);
9875 double_type_node = make_node (REAL_TYPE);
9876 if (short_double)
9877 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
9878 else
9879 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
9880 layout_type (double_type_node);
9882 long_double_type_node = make_node (REAL_TYPE);
9883 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
9884 layout_type (long_double_type_node);
9886 float_ptr_type_node = build_pointer_type (float_type_node);
9887 double_ptr_type_node = build_pointer_type (double_type_node);
9888 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
9889 integer_ptr_type_node = build_pointer_type (integer_type_node);
9891 /* Fixed size integer types. */
9892 uint16_type_node = build_nonstandard_integer_type (16, true);
9893 uint32_type_node = build_nonstandard_integer_type (32, true);
9894 uint64_type_node = build_nonstandard_integer_type (64, true);
9896 /* Decimal float types. */
9897 dfloat32_type_node = make_node (REAL_TYPE);
9898 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
9899 layout_type (dfloat32_type_node);
9900 SET_TYPE_MODE (dfloat32_type_node, SDmode);
9901 dfloat32_ptr_type_node = build_pointer_type (dfloat32_type_node);
9903 dfloat64_type_node = make_node (REAL_TYPE);
9904 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
9905 layout_type (dfloat64_type_node);
9906 SET_TYPE_MODE (dfloat64_type_node, DDmode);
9907 dfloat64_ptr_type_node = build_pointer_type (dfloat64_type_node);
9909 dfloat128_type_node = make_node (REAL_TYPE);
9910 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
9911 layout_type (dfloat128_type_node);
9912 SET_TYPE_MODE (dfloat128_type_node, TDmode);
9913 dfloat128_ptr_type_node = build_pointer_type (dfloat128_type_node);
9915 complex_integer_type_node = build_complex_type (integer_type_node);
9916 complex_float_type_node = build_complex_type (float_type_node);
9917 complex_double_type_node = build_complex_type (double_type_node);
9918 complex_long_double_type_node = build_complex_type (long_double_type_node);
9920 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
9921 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
9922 sat_ ## KIND ## _type_node = \
9923 make_sat_signed_ ## KIND ## _type (SIZE); \
9924 sat_unsigned_ ## KIND ## _type_node = \
9925 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9926 KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9927 unsigned_ ## KIND ## _type_node = \
9928 make_unsigned_ ## KIND ## _type (SIZE);
9930 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
9931 sat_ ## WIDTH ## KIND ## _type_node = \
9932 make_sat_signed_ ## KIND ## _type (SIZE); \
9933 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
9934 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9935 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9936 unsigned_ ## WIDTH ## KIND ## _type_node = \
9937 make_unsigned_ ## KIND ## _type (SIZE);
9939 /* Make fixed-point type nodes based on four different widths. */
9940 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
9941 MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
9942 MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
9943 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
9944 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
9946 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
9947 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
9948 NAME ## _type_node = \
9949 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
9950 u ## NAME ## _type_node = \
9951 make_or_reuse_unsigned_ ## KIND ## _type \
9952 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
9953 sat_ ## NAME ## _type_node = \
9954 make_or_reuse_sat_signed_ ## KIND ## _type \
9955 (GET_MODE_BITSIZE (MODE ## mode)); \
9956 sat_u ## NAME ## _type_node = \
9957 make_or_reuse_sat_unsigned_ ## KIND ## _type \
9958 (GET_MODE_BITSIZE (U ## MODE ## mode));
9960 /* Fixed-point type and mode nodes. */
9961 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
9962 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
9963 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
9964 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
9965 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
9966 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
9967 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
9968 MAKE_FIXED_MODE_NODE (accum, ha, HA)
9969 MAKE_FIXED_MODE_NODE (accum, sa, SA)
9970 MAKE_FIXED_MODE_NODE (accum, da, DA)
9971 MAKE_FIXED_MODE_NODE (accum, ta, TA)
9974 tree t = targetm.build_builtin_va_list ();
9976 /* Many back-ends define record types without setting TYPE_NAME.
9977 If we copied the record type here, we'd keep the original
9978 record type without a name. This breaks name mangling. So,
9979 don't copy record types and let c_common_nodes_and_builtins()
9980 declare the type to be __builtin_va_list. */
9981 if (TREE_CODE (t) != RECORD_TYPE)
9982 t = build_variant_type_copy (t);
9984 va_list_type_node = t;
9988 /* Modify DECL for given flags.
9989 TM_PURE attribute is set only on types, so the function will modify
9990 DECL's type when ECF_TM_PURE is used. */
9992 void
9993 set_call_expr_flags (tree decl, int flags)
9995 if (flags & ECF_NOTHROW)
9996 TREE_NOTHROW (decl) = 1;
9997 if (flags & ECF_CONST)
9998 TREE_READONLY (decl) = 1;
9999 if (flags & ECF_PURE)
10000 DECL_PURE_P (decl) = 1;
10001 if (flags & ECF_LOOPING_CONST_OR_PURE)
10002 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
10003 if (flags & ECF_NOVOPS)
10004 DECL_IS_NOVOPS (decl) = 1;
10005 if (flags & ECF_NORETURN)
10006 TREE_THIS_VOLATILE (decl) = 1;
10007 if (flags & ECF_MALLOC)
10008 DECL_IS_MALLOC (decl) = 1;
10009 if (flags & ECF_RETURNS_TWICE)
10010 DECL_IS_RETURNS_TWICE (decl) = 1;
10011 if (flags & ECF_LEAF)
10012 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("leaf"),
10013 NULL, DECL_ATTRIBUTES (decl));
10014 if ((flags & ECF_TM_PURE) && flag_tm)
10015 apply_tm_attr (decl, get_identifier ("transaction_pure"));
10016 /* Looping const or pure is implied by noreturn.
10017 There is currently no way to declare looping const or looping pure alone. */
10018 gcc_assert (!(flags & ECF_LOOPING_CONST_OR_PURE)
10019 || ((flags & ECF_NORETURN) && (flags & (ECF_CONST | ECF_PURE))));
10023 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
10025 static void
10026 local_define_builtin (const char *name, tree type, enum built_in_function code,
10027 const char *library_name, int ecf_flags)
10029 tree decl;
10031 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
10032 library_name, NULL_TREE);
10033 set_call_expr_flags (decl, ecf_flags);
10035 set_builtin_decl (code, decl, true);
10038 /* Call this function after instantiating all builtins that the language
10039 front end cares about. This will build the rest of the builtins that
10040 are relied upon by the tree optimizers and the middle-end. */
10042 void
10043 build_common_builtin_nodes (void)
10045 tree tmp, ftype;
10046 int ecf_flags;
10048 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE))
10050 ftype = build_function_type (void_type_node, void_list_node);
10051 local_define_builtin ("__builtin_unreachable", ftype, BUILT_IN_UNREACHABLE,
10052 "__builtin_unreachable",
10053 ECF_NOTHROW | ECF_LEAF | ECF_NORETURN
10054 | ECF_CONST | ECF_LEAF);
10057 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY)
10058 || !builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
10060 ftype = build_function_type_list (ptr_type_node,
10061 ptr_type_node, const_ptr_type_node,
10062 size_type_node, NULL_TREE);
10064 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY))
10065 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
10066 "memcpy", ECF_NOTHROW | ECF_LEAF);
10067 if (!builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
10068 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
10069 "memmove", ECF_NOTHROW | ECF_LEAF);
10072 if (!builtin_decl_explicit_p (BUILT_IN_MEMCMP))
10074 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
10075 const_ptr_type_node, size_type_node,
10076 NULL_TREE);
10077 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
10078 "memcmp", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10081 if (!builtin_decl_explicit_p (BUILT_IN_MEMSET))
10083 ftype = build_function_type_list (ptr_type_node,
10084 ptr_type_node, integer_type_node,
10085 size_type_node, NULL_TREE);
10086 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
10087 "memset", ECF_NOTHROW | ECF_LEAF);
10090 if (!builtin_decl_explicit_p (BUILT_IN_ALLOCA))
10092 ftype = build_function_type_list (ptr_type_node,
10093 size_type_node, NULL_TREE);
10094 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
10095 "alloca", ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
10098 ftype = build_function_type_list (ptr_type_node, size_type_node,
10099 size_type_node, NULL_TREE);
10100 local_define_builtin ("__builtin_alloca_with_align", ftype,
10101 BUILT_IN_ALLOCA_WITH_ALIGN, "alloca",
10102 ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
10104 /* If we're checking the stack, `alloca' can throw. */
10105 if (flag_stack_check)
10107 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA)) = 0;
10108 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN)) = 0;
10111 ftype = build_function_type_list (void_type_node,
10112 ptr_type_node, ptr_type_node,
10113 ptr_type_node, NULL_TREE);
10114 local_define_builtin ("__builtin_init_trampoline", ftype,
10115 BUILT_IN_INIT_TRAMPOLINE,
10116 "__builtin_init_trampoline", ECF_NOTHROW | ECF_LEAF);
10117 local_define_builtin ("__builtin_init_heap_trampoline", ftype,
10118 BUILT_IN_INIT_HEAP_TRAMPOLINE,
10119 "__builtin_init_heap_trampoline",
10120 ECF_NOTHROW | ECF_LEAF);
10122 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
10123 local_define_builtin ("__builtin_adjust_trampoline", ftype,
10124 BUILT_IN_ADJUST_TRAMPOLINE,
10125 "__builtin_adjust_trampoline",
10126 ECF_CONST | ECF_NOTHROW);
10128 ftype = build_function_type_list (void_type_node,
10129 ptr_type_node, ptr_type_node, NULL_TREE);
10130 local_define_builtin ("__builtin_nonlocal_goto", ftype,
10131 BUILT_IN_NONLOCAL_GOTO,
10132 "__builtin_nonlocal_goto",
10133 ECF_NORETURN | ECF_NOTHROW);
10135 ftype = build_function_type_list (void_type_node,
10136 ptr_type_node, ptr_type_node, NULL_TREE);
10137 local_define_builtin ("__builtin_setjmp_setup", ftype,
10138 BUILT_IN_SETJMP_SETUP,
10139 "__builtin_setjmp_setup", ECF_NOTHROW);
10141 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
10142 local_define_builtin ("__builtin_setjmp_dispatcher", ftype,
10143 BUILT_IN_SETJMP_DISPATCHER,
10144 "__builtin_setjmp_dispatcher",
10145 ECF_PURE | ECF_NOTHROW);
10147 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10148 local_define_builtin ("__builtin_setjmp_receiver", ftype,
10149 BUILT_IN_SETJMP_RECEIVER,
10150 "__builtin_setjmp_receiver", ECF_NOTHROW);
10152 ftype = build_function_type_list (ptr_type_node, NULL_TREE);
10153 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
10154 "__builtin_stack_save", ECF_NOTHROW | ECF_LEAF);
10156 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10157 local_define_builtin ("__builtin_stack_restore", ftype,
10158 BUILT_IN_STACK_RESTORE,
10159 "__builtin_stack_restore", ECF_NOTHROW | ECF_LEAF);
10161 /* If there's a possibility that we might use the ARM EABI, build the
10162 alternate __cxa_end_cleanup node used to resume from C++ and Java. */
10163 if (targetm.arm_eabi_unwinder)
10165 ftype = build_function_type_list (void_type_node, NULL_TREE);
10166 local_define_builtin ("__builtin_cxa_end_cleanup", ftype,
10167 BUILT_IN_CXA_END_CLEANUP,
10168 "__cxa_end_cleanup", ECF_NORETURN | ECF_LEAF);
10171 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10172 local_define_builtin ("__builtin_unwind_resume", ftype,
10173 BUILT_IN_UNWIND_RESUME,
10174 ((targetm_common.except_unwind_info (&global_options)
10175 == UI_SJLJ)
10176 ? "_Unwind_SjLj_Resume" : "_Unwind_Resume"),
10177 ECF_NORETURN);
10179 if (builtin_decl_explicit (BUILT_IN_RETURN_ADDRESS) == NULL_TREE)
10181 ftype = build_function_type_list (ptr_type_node, integer_type_node,
10182 NULL_TREE);
10183 local_define_builtin ("__builtin_return_address", ftype,
10184 BUILT_IN_RETURN_ADDRESS,
10185 "__builtin_return_address",
10186 ECF_NOTHROW);
10189 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER)
10190 || !builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
10192 ftype = build_function_type_list (void_type_node, ptr_type_node,
10193 ptr_type_node, NULL_TREE);
10194 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER))
10195 local_define_builtin ("__cyg_profile_func_enter", ftype,
10196 BUILT_IN_PROFILE_FUNC_ENTER,
10197 "__cyg_profile_func_enter", 0);
10198 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
10199 local_define_builtin ("__cyg_profile_func_exit", ftype,
10200 BUILT_IN_PROFILE_FUNC_EXIT,
10201 "__cyg_profile_func_exit", 0);
10204 /* The exception object and filter values from the runtime. The argument
10205 must be zero before exception lowering, i.e. from the front end. After
10206 exception lowering, it will be the region number for the exception
10207 landing pad. These functions are PURE instead of CONST to prevent
10208 them from being hoisted past the exception edge that will initialize
10209 its value in the landing pad. */
10210 ftype = build_function_type_list (ptr_type_node,
10211 integer_type_node, NULL_TREE);
10212 ecf_flags = ECF_PURE | ECF_NOTHROW | ECF_LEAF;
10213 /* Only use TM_PURE if we we have TM language support. */
10214 if (builtin_decl_explicit_p (BUILT_IN_TM_LOAD_1))
10215 ecf_flags |= ECF_TM_PURE;
10216 local_define_builtin ("__builtin_eh_pointer", ftype, BUILT_IN_EH_POINTER,
10217 "__builtin_eh_pointer", ecf_flags);
10219 tmp = lang_hooks.types.type_for_mode (targetm.eh_return_filter_mode (), 0);
10220 ftype = build_function_type_list (tmp, integer_type_node, NULL_TREE);
10221 local_define_builtin ("__builtin_eh_filter", ftype, BUILT_IN_EH_FILTER,
10222 "__builtin_eh_filter", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10224 ftype = build_function_type_list (void_type_node,
10225 integer_type_node, integer_type_node,
10226 NULL_TREE);
10227 local_define_builtin ("__builtin_eh_copy_values", ftype,
10228 BUILT_IN_EH_COPY_VALUES,
10229 "__builtin_eh_copy_values", ECF_NOTHROW);
10231 /* Complex multiplication and division. These are handled as builtins
10232 rather than optabs because emit_library_call_value doesn't support
10233 complex. Further, we can do slightly better with folding these
10234 beasties if the real and complex parts of the arguments are separate. */
10236 int mode;
10238 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
10240 char mode_name_buf[4], *q;
10241 const char *p;
10242 enum built_in_function mcode, dcode;
10243 tree type, inner_type;
10244 const char *prefix = "__";
10246 if (targetm.libfunc_gnu_prefix)
10247 prefix = "__gnu_";
10249 type = lang_hooks.types.type_for_mode ((enum machine_mode) mode, 0);
10250 if (type == NULL)
10251 continue;
10252 inner_type = TREE_TYPE (type);
10254 ftype = build_function_type_list (type, inner_type, inner_type,
10255 inner_type, inner_type, NULL_TREE);
10257 mcode = ((enum built_in_function)
10258 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10259 dcode = ((enum built_in_function)
10260 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10262 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
10263 *q = TOLOWER (*p);
10264 *q = '\0';
10266 built_in_names[mcode] = concat (prefix, "mul", mode_name_buf, "3",
10267 NULL);
10268 local_define_builtin (built_in_names[mcode], ftype, mcode,
10269 built_in_names[mcode],
10270 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
10272 built_in_names[dcode] = concat (prefix, "div", mode_name_buf, "3",
10273 NULL);
10274 local_define_builtin (built_in_names[dcode], ftype, dcode,
10275 built_in_names[dcode],
10276 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
10281 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
10282 better way.
10284 If we requested a pointer to a vector, build up the pointers that
10285 we stripped off while looking for the inner type. Similarly for
10286 return values from functions.
10288 The argument TYPE is the top of the chain, and BOTTOM is the
10289 new type which we will point to. */
10291 tree
10292 reconstruct_complex_type (tree type, tree bottom)
10294 tree inner, outer;
10296 if (TREE_CODE (type) == POINTER_TYPE)
10298 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10299 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
10300 TYPE_REF_CAN_ALIAS_ALL (type));
10302 else if (TREE_CODE (type) == REFERENCE_TYPE)
10304 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10305 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
10306 TYPE_REF_CAN_ALIAS_ALL (type));
10308 else if (TREE_CODE (type) == ARRAY_TYPE)
10310 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10311 outer = build_array_type (inner, TYPE_DOMAIN (type));
10313 else if (TREE_CODE (type) == FUNCTION_TYPE)
10315 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10316 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
10318 else if (TREE_CODE (type) == METHOD_TYPE)
10320 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10321 /* The build_method_type_directly() routine prepends 'this' to argument list,
10322 so we must compensate by getting rid of it. */
10323 outer
10324 = build_method_type_directly
10325 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
10326 inner,
10327 TREE_CHAIN (TYPE_ARG_TYPES (type)));
10329 else if (TREE_CODE (type) == OFFSET_TYPE)
10331 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10332 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
10334 else
10335 return bottom;
10337 return build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
10338 TYPE_QUALS (type));
10341 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
10342 the inner type. */
10343 tree
10344 build_vector_type_for_mode (tree innertype, enum machine_mode mode)
10346 int nunits;
10348 switch (GET_MODE_CLASS (mode))
10350 case MODE_VECTOR_INT:
10351 case MODE_VECTOR_FLOAT:
10352 case MODE_VECTOR_FRACT:
10353 case MODE_VECTOR_UFRACT:
10354 case MODE_VECTOR_ACCUM:
10355 case MODE_VECTOR_UACCUM:
10356 nunits = GET_MODE_NUNITS (mode);
10357 break;
10359 case MODE_INT:
10360 /* Check that there are no leftover bits. */
10361 gcc_assert (GET_MODE_BITSIZE (mode)
10362 % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
10364 nunits = GET_MODE_BITSIZE (mode)
10365 / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
10366 break;
10368 default:
10369 gcc_unreachable ();
10372 return make_vector_type (innertype, nunits, mode);
10375 /* Similarly, but takes the inner type and number of units, which must be
10376 a power of two. */
10378 tree
10379 build_vector_type (tree innertype, int nunits)
10381 return make_vector_type (innertype, nunits, VOIDmode);
10384 /* Similarly, but builds a variant type with TYPE_VECTOR_OPAQUE set. */
10386 tree
10387 build_opaque_vector_type (tree innertype, int nunits)
10389 tree t = make_vector_type (innertype, nunits, VOIDmode);
10390 tree cand;
10391 /* We always build the non-opaque variant before the opaque one,
10392 so if it already exists, it is TYPE_NEXT_VARIANT of this one. */
10393 cand = TYPE_NEXT_VARIANT (t);
10394 if (cand
10395 && TYPE_VECTOR_OPAQUE (cand)
10396 && check_qualified_type (cand, t, TYPE_QUALS (t), NULL_TREE))
10397 return cand;
10398 /* Othewise build a variant type and make sure to queue it after
10399 the non-opaque type. */
10400 cand = build_distinct_type_copy (t);
10401 TYPE_VECTOR_OPAQUE (cand) = true;
10402 TYPE_CANONICAL (cand) = TYPE_CANONICAL (t);
10403 TYPE_NEXT_VARIANT (cand) = TYPE_NEXT_VARIANT (t);
10404 TYPE_NEXT_VARIANT (t) = cand;
10405 TYPE_MAIN_VARIANT (cand) = TYPE_MAIN_VARIANT (t);
10406 return cand;
10410 /* Given an initializer INIT, return TRUE if INIT is zero or some
10411 aggregate of zeros. Otherwise return FALSE. */
10412 bool
10413 initializer_zerop (const_tree init)
10415 tree elt;
10417 STRIP_NOPS (init);
10419 switch (TREE_CODE (init))
10421 case INTEGER_CST:
10422 return integer_zerop (init);
10424 case REAL_CST:
10425 /* ??? Note that this is not correct for C4X float formats. There,
10426 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
10427 negative exponent. */
10428 return real_zerop (init)
10429 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
10431 case FIXED_CST:
10432 return fixed_zerop (init);
10434 case COMPLEX_CST:
10435 return integer_zerop (init)
10436 || (real_zerop (init)
10437 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
10438 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
10440 case VECTOR_CST:
10442 unsigned i;
10443 for (i = 0; i < VECTOR_CST_NELTS (init); ++i)
10444 if (!initializer_zerop (VECTOR_CST_ELT (init, i)))
10445 return false;
10446 return true;
10449 case CONSTRUCTOR:
10451 unsigned HOST_WIDE_INT idx;
10453 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
10454 if (!initializer_zerop (elt))
10455 return false;
10456 return true;
10459 case STRING_CST:
10461 int i;
10463 /* We need to loop through all elements to handle cases like
10464 "\0" and "\0foobar". */
10465 for (i = 0; i < TREE_STRING_LENGTH (init); ++i)
10466 if (TREE_STRING_POINTER (init)[i] != '\0')
10467 return false;
10469 return true;
10472 default:
10473 return false;
10477 /* Check if vector VEC consists of all the equal elements and
10478 that the number of elements corresponds to the type of VEC.
10479 The function returns first element of the vector
10480 or NULL_TREE if the vector is not uniform. */
10481 tree
10482 uniform_vector_p (const_tree vec)
10484 tree first, t;
10485 unsigned i;
10487 if (vec == NULL_TREE)
10488 return NULL_TREE;
10490 gcc_assert (VECTOR_TYPE_P (TREE_TYPE (vec)));
10492 if (TREE_CODE (vec) == VECTOR_CST)
10494 first = VECTOR_CST_ELT (vec, 0);
10495 for (i = 1; i < VECTOR_CST_NELTS (vec); ++i)
10496 if (!operand_equal_p (first, VECTOR_CST_ELT (vec, i), 0))
10497 return NULL_TREE;
10499 return first;
10502 else if (TREE_CODE (vec) == CONSTRUCTOR)
10504 first = error_mark_node;
10506 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (vec), i, t)
10508 if (i == 0)
10510 first = t;
10511 continue;
10513 if (!operand_equal_p (first, t, 0))
10514 return NULL_TREE;
10516 if (i != TYPE_VECTOR_SUBPARTS (TREE_TYPE (vec)))
10517 return NULL_TREE;
10519 return first;
10522 return NULL_TREE;
10525 /* Build an empty statement at location LOC. */
10527 tree
10528 build_empty_stmt (location_t loc)
10530 tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
10531 SET_EXPR_LOCATION (t, loc);
10532 return t;
10536 /* Build an OpenMP clause with code CODE. LOC is the location of the
10537 clause. */
10539 tree
10540 build_omp_clause (location_t loc, enum omp_clause_code code)
10542 tree t;
10543 int size, length;
10545 length = omp_clause_num_ops[code];
10546 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
10548 record_node_allocation_statistics (OMP_CLAUSE, size);
10550 t = ggc_alloc_tree_node (size);
10551 memset (t, 0, size);
10552 TREE_SET_CODE (t, OMP_CLAUSE);
10553 OMP_CLAUSE_SET_CODE (t, code);
10554 OMP_CLAUSE_LOCATION (t) = loc;
10556 return t;
10559 /* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
10560 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
10561 Except for the CODE and operand count field, other storage for the
10562 object is initialized to zeros. */
10564 tree
10565 build_vl_exp_stat (enum tree_code code, int len MEM_STAT_DECL)
10567 tree t;
10568 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
10570 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
10571 gcc_assert (len >= 1);
10573 record_node_allocation_statistics (code, length);
10575 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
10577 TREE_SET_CODE (t, code);
10579 /* Can't use TREE_OPERAND to store the length because if checking is
10580 enabled, it will try to check the length before we store it. :-P */
10581 t->exp.operands[0] = build_int_cst (sizetype, len);
10583 return t;
10586 /* Helper function for build_call_* functions; build a CALL_EXPR with
10587 indicated RETURN_TYPE, FN, and NARGS, but do not initialize any of
10588 the argument slots. */
10590 static tree
10591 build_call_1 (tree return_type, tree fn, int nargs)
10593 tree t;
10595 t = build_vl_exp (CALL_EXPR, nargs + 3);
10596 TREE_TYPE (t) = return_type;
10597 CALL_EXPR_FN (t) = fn;
10598 CALL_EXPR_STATIC_CHAIN (t) = NULL;
10600 return t;
10603 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10604 FN and a null static chain slot. NARGS is the number of call arguments
10605 which are specified as "..." arguments. */
10607 tree
10608 build_call_nary (tree return_type, tree fn, int nargs, ...)
10610 tree ret;
10611 va_list args;
10612 va_start (args, nargs);
10613 ret = build_call_valist (return_type, fn, nargs, args);
10614 va_end (args);
10615 return ret;
10618 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10619 FN and a null static chain slot. NARGS is the number of call arguments
10620 which are specified as a va_list ARGS. */
10622 tree
10623 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
10625 tree t;
10626 int i;
10628 t = build_call_1 (return_type, fn, nargs);
10629 for (i = 0; i < nargs; i++)
10630 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
10631 process_call_operands (t);
10632 return t;
10635 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10636 FN and a null static chain slot. NARGS is the number of call arguments
10637 which are specified as a tree array ARGS. */
10639 tree
10640 build_call_array_loc (location_t loc, tree return_type, tree fn,
10641 int nargs, const tree *args)
10643 tree t;
10644 int i;
10646 t = build_call_1 (return_type, fn, nargs);
10647 for (i = 0; i < nargs; i++)
10648 CALL_EXPR_ARG (t, i) = args[i];
10649 process_call_operands (t);
10650 SET_EXPR_LOCATION (t, loc);
10651 return t;
10654 /* Like build_call_array, but takes a vec. */
10656 tree
10657 build_call_vec (tree return_type, tree fn, vec<tree, va_gc> *args)
10659 tree ret, t;
10660 unsigned int ix;
10662 ret = build_call_1 (return_type, fn, vec_safe_length (args));
10663 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
10664 CALL_EXPR_ARG (ret, ix) = t;
10665 process_call_operands (ret);
10666 return ret;
10669 /* Return true if T (assumed to be a DECL) must be assigned a memory
10670 location. */
10672 bool
10673 needs_to_live_in_memory (const_tree t)
10675 return (TREE_ADDRESSABLE (t)
10676 || is_global_var (t)
10677 || (TREE_CODE (t) == RESULT_DECL
10678 && !DECL_BY_REFERENCE (t)
10679 && aggregate_value_p (t, current_function_decl)));
10682 /* Return value of a constant X and sign-extend it. */
10684 HOST_WIDE_INT
10685 int_cst_value (const_tree x)
10687 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10688 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
10690 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10691 gcc_assert (TREE_INT_CST_HIGH (x) == 0
10692 || TREE_INT_CST_HIGH (x) == -1);
10694 if (bits < HOST_BITS_PER_WIDE_INT)
10696 bool negative = ((val >> (bits - 1)) & 1) != 0;
10697 if (negative)
10698 val |= (~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1;
10699 else
10700 val &= ~((~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1);
10703 return val;
10706 /* Return value of a constant X and sign-extend it. */
10708 HOST_WIDEST_INT
10709 widest_int_cst_value (const_tree x)
10711 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10712 unsigned HOST_WIDEST_INT val = TREE_INT_CST_LOW (x);
10714 #if HOST_BITS_PER_WIDEST_INT > HOST_BITS_PER_WIDE_INT
10715 gcc_assert (HOST_BITS_PER_WIDEST_INT >= HOST_BITS_PER_DOUBLE_INT);
10716 val |= (((unsigned HOST_WIDEST_INT) TREE_INT_CST_HIGH (x))
10717 << HOST_BITS_PER_WIDE_INT);
10718 #else
10719 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10720 gcc_assert (TREE_INT_CST_HIGH (x) == 0
10721 || TREE_INT_CST_HIGH (x) == -1);
10722 #endif
10724 if (bits < HOST_BITS_PER_WIDEST_INT)
10726 bool negative = ((val >> (bits - 1)) & 1) != 0;
10727 if (negative)
10728 val |= (~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1;
10729 else
10730 val &= ~((~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1);
10733 return val;
10736 /* If TYPE is an integral or pointer type, return an integer type with
10737 the same precision which is unsigned iff UNSIGNEDP is true, or itself
10738 if TYPE is already an integer type of signedness UNSIGNEDP. */
10740 tree
10741 signed_or_unsigned_type_for (int unsignedp, tree type)
10743 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type) == unsignedp)
10744 return type;
10746 if (TREE_CODE (type) == VECTOR_TYPE)
10748 tree inner = TREE_TYPE (type);
10749 tree inner2 = signed_or_unsigned_type_for (unsignedp, inner);
10750 if (!inner2)
10751 return NULL_TREE;
10752 if (inner == inner2)
10753 return type;
10754 return build_vector_type (inner2, TYPE_VECTOR_SUBPARTS (type));
10757 if (!INTEGRAL_TYPE_P (type)
10758 && !POINTER_TYPE_P (type))
10759 return NULL_TREE;
10761 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
10764 /* If TYPE is an integral or pointer type, return an integer type with
10765 the same precision which is unsigned, or itself if TYPE is already an
10766 unsigned integer type. */
10768 tree
10769 unsigned_type_for (tree type)
10771 return signed_or_unsigned_type_for (1, type);
10774 /* If TYPE is an integral or pointer type, return an integer type with
10775 the same precision which is signed, or itself if TYPE is already a
10776 signed integer type. */
10778 tree
10779 signed_type_for (tree type)
10781 return signed_or_unsigned_type_for (0, type);
10784 /* If TYPE is a vector type, return a signed integer vector type with the
10785 same width and number of subparts. Otherwise return boolean_type_node. */
10787 tree
10788 truth_type_for (tree type)
10790 if (TREE_CODE (type) == VECTOR_TYPE)
10792 tree elem = lang_hooks.types.type_for_size
10793 (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))), 0);
10794 return build_opaque_vector_type (elem, TYPE_VECTOR_SUBPARTS (type));
10796 else
10797 return boolean_type_node;
10800 /* Returns the largest value obtainable by casting something in INNER type to
10801 OUTER type. */
10803 tree
10804 upper_bound_in_type (tree outer, tree inner)
10806 double_int high;
10807 unsigned int det = 0;
10808 unsigned oprec = TYPE_PRECISION (outer);
10809 unsigned iprec = TYPE_PRECISION (inner);
10810 unsigned prec;
10812 /* Compute a unique number for every combination. */
10813 det |= (oprec > iprec) ? 4 : 0;
10814 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
10815 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
10817 /* Determine the exponent to use. */
10818 switch (det)
10820 case 0:
10821 case 1:
10822 /* oprec <= iprec, outer: signed, inner: don't care. */
10823 prec = oprec - 1;
10824 break;
10825 case 2:
10826 case 3:
10827 /* oprec <= iprec, outer: unsigned, inner: don't care. */
10828 prec = oprec;
10829 break;
10830 case 4:
10831 /* oprec > iprec, outer: signed, inner: signed. */
10832 prec = iprec - 1;
10833 break;
10834 case 5:
10835 /* oprec > iprec, outer: signed, inner: unsigned. */
10836 prec = iprec;
10837 break;
10838 case 6:
10839 /* oprec > iprec, outer: unsigned, inner: signed. */
10840 prec = oprec;
10841 break;
10842 case 7:
10843 /* oprec > iprec, outer: unsigned, inner: unsigned. */
10844 prec = iprec;
10845 break;
10846 default:
10847 gcc_unreachable ();
10850 /* Compute 2^^prec - 1. */
10851 if (prec <= HOST_BITS_PER_WIDE_INT)
10853 high.high = 0;
10854 high.low = ((~(unsigned HOST_WIDE_INT) 0)
10855 >> (HOST_BITS_PER_WIDE_INT - prec));
10857 else
10859 high.high = ((~(unsigned HOST_WIDE_INT) 0)
10860 >> (HOST_BITS_PER_DOUBLE_INT - prec));
10861 high.low = ~(unsigned HOST_WIDE_INT) 0;
10864 return double_int_to_tree (outer, high);
10867 /* Returns the smallest value obtainable by casting something in INNER type to
10868 OUTER type. */
10870 tree
10871 lower_bound_in_type (tree outer, tree inner)
10873 double_int low;
10874 unsigned oprec = TYPE_PRECISION (outer);
10875 unsigned iprec = TYPE_PRECISION (inner);
10877 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
10878 and obtain 0. */
10879 if (TYPE_UNSIGNED (outer)
10880 /* If we are widening something of an unsigned type, OUTER type
10881 contains all values of INNER type. In particular, both INNER
10882 and OUTER types have zero in common. */
10883 || (oprec > iprec && TYPE_UNSIGNED (inner)))
10884 low.low = low.high = 0;
10885 else
10887 /* If we are widening a signed type to another signed type, we
10888 want to obtain -2^^(iprec-1). If we are keeping the
10889 precision or narrowing to a signed type, we want to obtain
10890 -2^(oprec-1). */
10891 unsigned prec = oprec > iprec ? iprec : oprec;
10893 if (prec <= HOST_BITS_PER_WIDE_INT)
10895 low.high = ~(unsigned HOST_WIDE_INT) 0;
10896 low.low = (~(unsigned HOST_WIDE_INT) 0) << (prec - 1);
10898 else
10900 low.high = ((~(unsigned HOST_WIDE_INT) 0)
10901 << (prec - HOST_BITS_PER_WIDE_INT - 1));
10902 low.low = 0;
10906 return double_int_to_tree (outer, low);
10909 /* Return nonzero if two operands that are suitable for PHI nodes are
10910 necessarily equal. Specifically, both ARG0 and ARG1 must be either
10911 SSA_NAME or invariant. Note that this is strictly an optimization.
10912 That is, callers of this function can directly call operand_equal_p
10913 and get the same result, only slower. */
10916 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
10918 if (arg0 == arg1)
10919 return 1;
10920 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
10921 return 0;
10922 return operand_equal_p (arg0, arg1, 0);
10925 /* Returns number of zeros at the end of binary representation of X.
10927 ??? Use ffs if available? */
10929 tree
10930 num_ending_zeros (const_tree x)
10932 unsigned HOST_WIDE_INT fr, nfr;
10933 unsigned num, abits;
10934 tree type = TREE_TYPE (x);
10936 if (TREE_INT_CST_LOW (x) == 0)
10938 num = HOST_BITS_PER_WIDE_INT;
10939 fr = TREE_INT_CST_HIGH (x);
10941 else
10943 num = 0;
10944 fr = TREE_INT_CST_LOW (x);
10947 for (abits = HOST_BITS_PER_WIDE_INT / 2; abits; abits /= 2)
10949 nfr = fr >> abits;
10950 if (nfr << abits == fr)
10952 num += abits;
10953 fr = nfr;
10957 if (num > TYPE_PRECISION (type))
10958 num = TYPE_PRECISION (type);
10960 return build_int_cst_type (type, num);
10964 #define WALK_SUBTREE(NODE) \
10965 do \
10967 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
10968 if (result) \
10969 return result; \
10971 while (0)
10973 /* This is a subroutine of walk_tree that walks field of TYPE that are to
10974 be walked whenever a type is seen in the tree. Rest of operands and return
10975 value are as for walk_tree. */
10977 static tree
10978 walk_type_fields (tree type, walk_tree_fn func, void *data,
10979 struct pointer_set_t *pset, walk_tree_lh lh)
10981 tree result = NULL_TREE;
10983 switch (TREE_CODE (type))
10985 case POINTER_TYPE:
10986 case REFERENCE_TYPE:
10987 /* We have to worry about mutually recursive pointers. These can't
10988 be written in C. They can in Ada. It's pathological, but
10989 there's an ACATS test (c38102a) that checks it. Deal with this
10990 by checking if we're pointing to another pointer, that one
10991 points to another pointer, that one does too, and we have no htab.
10992 If so, get a hash table. We check three levels deep to avoid
10993 the cost of the hash table if we don't need one. */
10994 if (POINTER_TYPE_P (TREE_TYPE (type))
10995 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
10996 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
10997 && !pset)
10999 result = walk_tree_without_duplicates (&TREE_TYPE (type),
11000 func, data);
11001 if (result)
11002 return result;
11004 break;
11007 /* ... fall through ... */
11009 case COMPLEX_TYPE:
11010 WALK_SUBTREE (TREE_TYPE (type));
11011 break;
11013 case METHOD_TYPE:
11014 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
11016 /* Fall through. */
11018 case FUNCTION_TYPE:
11019 WALK_SUBTREE (TREE_TYPE (type));
11021 tree arg;
11023 /* We never want to walk into default arguments. */
11024 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
11025 WALK_SUBTREE (TREE_VALUE (arg));
11027 break;
11029 case ARRAY_TYPE:
11030 /* Don't follow this nodes's type if a pointer for fear that
11031 we'll have infinite recursion. If we have a PSET, then we
11032 need not fear. */
11033 if (pset
11034 || (!POINTER_TYPE_P (TREE_TYPE (type))
11035 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
11036 WALK_SUBTREE (TREE_TYPE (type));
11037 WALK_SUBTREE (TYPE_DOMAIN (type));
11038 break;
11040 case OFFSET_TYPE:
11041 WALK_SUBTREE (TREE_TYPE (type));
11042 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
11043 break;
11045 default:
11046 break;
11049 return NULL_TREE;
11052 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
11053 called with the DATA and the address of each sub-tree. If FUNC returns a
11054 non-NULL value, the traversal is stopped, and the value returned by FUNC
11055 is returned. If PSET is non-NULL it is used to record the nodes visited,
11056 and to avoid visiting a node more than once. */
11058 tree
11059 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
11060 struct pointer_set_t *pset, walk_tree_lh lh)
11062 enum tree_code code;
11063 int walk_subtrees;
11064 tree result;
11066 #define WALK_SUBTREE_TAIL(NODE) \
11067 do \
11069 tp = & (NODE); \
11070 goto tail_recurse; \
11072 while (0)
11074 tail_recurse:
11075 /* Skip empty subtrees. */
11076 if (!*tp)
11077 return NULL_TREE;
11079 /* Don't walk the same tree twice, if the user has requested
11080 that we avoid doing so. */
11081 if (pset && pointer_set_insert (pset, *tp))
11082 return NULL_TREE;
11084 /* Call the function. */
11085 walk_subtrees = 1;
11086 result = (*func) (tp, &walk_subtrees, data);
11088 /* If we found something, return it. */
11089 if (result)
11090 return result;
11092 code = TREE_CODE (*tp);
11094 /* Even if we didn't, FUNC may have decided that there was nothing
11095 interesting below this point in the tree. */
11096 if (!walk_subtrees)
11098 /* But we still need to check our siblings. */
11099 if (code == TREE_LIST)
11100 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
11101 else if (code == OMP_CLAUSE)
11102 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11103 else
11104 return NULL_TREE;
11107 if (lh)
11109 result = (*lh) (tp, &walk_subtrees, func, data, pset);
11110 if (result || !walk_subtrees)
11111 return result;
11114 switch (code)
11116 case ERROR_MARK:
11117 case IDENTIFIER_NODE:
11118 case INTEGER_CST:
11119 case REAL_CST:
11120 case FIXED_CST:
11121 case VECTOR_CST:
11122 case STRING_CST:
11123 case BLOCK:
11124 case PLACEHOLDER_EXPR:
11125 case SSA_NAME:
11126 case FIELD_DECL:
11127 case RESULT_DECL:
11128 /* None of these have subtrees other than those already walked
11129 above. */
11130 break;
11132 case TREE_LIST:
11133 WALK_SUBTREE (TREE_VALUE (*tp));
11134 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
11135 break;
11137 case TREE_VEC:
11139 int len = TREE_VEC_LENGTH (*tp);
11141 if (len == 0)
11142 break;
11144 /* Walk all elements but the first. */
11145 while (--len)
11146 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
11148 /* Now walk the first one as a tail call. */
11149 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
11152 case COMPLEX_CST:
11153 WALK_SUBTREE (TREE_REALPART (*tp));
11154 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
11156 case CONSTRUCTOR:
11158 unsigned HOST_WIDE_INT idx;
11159 constructor_elt *ce;
11161 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (*tp), idx, &ce);
11162 idx++)
11163 WALK_SUBTREE (ce->value);
11165 break;
11167 case SAVE_EXPR:
11168 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
11170 case BIND_EXPR:
11172 tree decl;
11173 for (decl = BIND_EXPR_VARS (*tp); decl; decl = DECL_CHAIN (decl))
11175 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
11176 into declarations that are just mentioned, rather than
11177 declared; they don't really belong to this part of the tree.
11178 And, we can see cycles: the initializer for a declaration
11179 can refer to the declaration itself. */
11180 WALK_SUBTREE (DECL_INITIAL (decl));
11181 WALK_SUBTREE (DECL_SIZE (decl));
11182 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
11184 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
11187 case STATEMENT_LIST:
11189 tree_stmt_iterator i;
11190 for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
11191 WALK_SUBTREE (*tsi_stmt_ptr (i));
11193 break;
11195 case OMP_CLAUSE:
11196 switch (OMP_CLAUSE_CODE (*tp))
11198 case OMP_CLAUSE_PRIVATE:
11199 case OMP_CLAUSE_SHARED:
11200 case OMP_CLAUSE_FIRSTPRIVATE:
11201 case OMP_CLAUSE_COPYIN:
11202 case OMP_CLAUSE_COPYPRIVATE:
11203 case OMP_CLAUSE_FINAL:
11204 case OMP_CLAUSE_IF:
11205 case OMP_CLAUSE_NUM_THREADS:
11206 case OMP_CLAUSE_SCHEDULE:
11207 case OMP_CLAUSE_UNIFORM:
11208 case OMP_CLAUSE_DEPEND:
11209 case OMP_CLAUSE_NUM_TEAMS:
11210 case OMP_CLAUSE_THREAD_LIMIT:
11211 case OMP_CLAUSE_DEVICE:
11212 case OMP_CLAUSE_DIST_SCHEDULE:
11213 case OMP_CLAUSE_SAFELEN:
11214 case OMP_CLAUSE_SIMDLEN:
11215 case OMP_CLAUSE__LOOPTEMP_:
11216 case OMP_CLAUSE__SIMDUID_:
11217 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0));
11218 /* FALLTHRU */
11220 case OMP_CLAUSE_NOWAIT:
11221 case OMP_CLAUSE_ORDERED:
11222 case OMP_CLAUSE_DEFAULT:
11223 case OMP_CLAUSE_UNTIED:
11224 case OMP_CLAUSE_MERGEABLE:
11225 case OMP_CLAUSE_PROC_BIND:
11226 case OMP_CLAUSE_INBRANCH:
11227 case OMP_CLAUSE_NOTINBRANCH:
11228 case OMP_CLAUSE_FOR:
11229 case OMP_CLAUSE_PARALLEL:
11230 case OMP_CLAUSE_SECTIONS:
11231 case OMP_CLAUSE_TASKGROUP:
11232 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11234 case OMP_CLAUSE_LASTPRIVATE:
11235 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11236 WALK_SUBTREE (OMP_CLAUSE_LASTPRIVATE_STMT (*tp));
11237 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11239 case OMP_CLAUSE_COLLAPSE:
11241 int i;
11242 for (i = 0; i < 3; i++)
11243 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
11244 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11247 case OMP_CLAUSE_ALIGNED:
11248 case OMP_CLAUSE_LINEAR:
11249 case OMP_CLAUSE_FROM:
11250 case OMP_CLAUSE_TO:
11251 case OMP_CLAUSE_MAP:
11252 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11253 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 1));
11254 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11256 case OMP_CLAUSE_REDUCTION:
11258 int i;
11259 for (i = 0; i < 4; i++)
11260 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
11261 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11264 default:
11265 gcc_unreachable ();
11267 break;
11269 case TARGET_EXPR:
11271 int i, len;
11273 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
11274 But, we only want to walk once. */
11275 len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
11276 for (i = 0; i < len; ++i)
11277 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11278 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
11281 case DECL_EXPR:
11282 /* If this is a TYPE_DECL, walk into the fields of the type that it's
11283 defining. We only want to walk into these fields of a type in this
11284 case and not in the general case of a mere reference to the type.
11286 The criterion is as follows: if the field can be an expression, it
11287 must be walked only here. This should be in keeping with the fields
11288 that are directly gimplified in gimplify_type_sizes in order for the
11289 mark/copy-if-shared/unmark machinery of the gimplifier to work with
11290 variable-sized types.
11292 Note that DECLs get walked as part of processing the BIND_EXPR. */
11293 if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
11295 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
11296 if (TREE_CODE (*type_p) == ERROR_MARK)
11297 return NULL_TREE;
11299 /* Call the function for the type. See if it returns anything or
11300 doesn't want us to continue. If we are to continue, walk both
11301 the normal fields and those for the declaration case. */
11302 result = (*func) (type_p, &walk_subtrees, data);
11303 if (result || !walk_subtrees)
11304 return result;
11306 /* But do not walk a pointed-to type since it may itself need to
11307 be walked in the declaration case if it isn't anonymous. */
11308 if (!POINTER_TYPE_P (*type_p))
11310 result = walk_type_fields (*type_p, func, data, pset, lh);
11311 if (result)
11312 return result;
11315 /* If this is a record type, also walk the fields. */
11316 if (RECORD_OR_UNION_TYPE_P (*type_p))
11318 tree field;
11320 for (field = TYPE_FIELDS (*type_p); field;
11321 field = DECL_CHAIN (field))
11323 /* We'd like to look at the type of the field, but we can
11324 easily get infinite recursion. So assume it's pointed
11325 to elsewhere in the tree. Also, ignore things that
11326 aren't fields. */
11327 if (TREE_CODE (field) != FIELD_DECL)
11328 continue;
11330 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
11331 WALK_SUBTREE (DECL_SIZE (field));
11332 WALK_SUBTREE (DECL_SIZE_UNIT (field));
11333 if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
11334 WALK_SUBTREE (DECL_QUALIFIER (field));
11338 /* Same for scalar types. */
11339 else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
11340 || TREE_CODE (*type_p) == ENUMERAL_TYPE
11341 || TREE_CODE (*type_p) == INTEGER_TYPE
11342 || TREE_CODE (*type_p) == FIXED_POINT_TYPE
11343 || TREE_CODE (*type_p) == REAL_TYPE)
11345 WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
11346 WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
11349 WALK_SUBTREE (TYPE_SIZE (*type_p));
11350 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
11352 /* FALLTHRU */
11354 default:
11355 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
11357 int i, len;
11359 /* Walk over all the sub-trees of this operand. */
11360 len = TREE_OPERAND_LENGTH (*tp);
11362 /* Go through the subtrees. We need to do this in forward order so
11363 that the scope of a FOR_EXPR is handled properly. */
11364 if (len)
11366 for (i = 0; i < len - 1; ++i)
11367 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11368 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
11371 /* If this is a type, walk the needed fields in the type. */
11372 else if (TYPE_P (*tp))
11373 return walk_type_fields (*tp, func, data, pset, lh);
11374 break;
11377 /* We didn't find what we were looking for. */
11378 return NULL_TREE;
11380 #undef WALK_SUBTREE_TAIL
11382 #undef WALK_SUBTREE
11384 /* Like walk_tree, but does not walk duplicate nodes more than once. */
11386 tree
11387 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
11388 walk_tree_lh lh)
11390 tree result;
11391 struct pointer_set_t *pset;
11393 pset = pointer_set_create ();
11394 result = walk_tree_1 (tp, func, data, pset, lh);
11395 pointer_set_destroy (pset);
11396 return result;
11400 tree
11401 tree_block (tree t)
11403 char const c = TREE_CODE_CLASS (TREE_CODE (t));
11405 if (IS_EXPR_CODE_CLASS (c))
11406 return LOCATION_BLOCK (t->exp.locus);
11407 gcc_unreachable ();
11408 return NULL;
11411 void
11412 tree_set_block (tree t, tree b)
11414 char const c = TREE_CODE_CLASS (TREE_CODE (t));
11416 if (IS_EXPR_CODE_CLASS (c))
11418 if (b)
11419 t->exp.locus = COMBINE_LOCATION_DATA (line_table, t->exp.locus, b);
11420 else
11421 t->exp.locus = LOCATION_LOCUS (t->exp.locus);
11423 else
11424 gcc_unreachable ();
11427 /* Create a nameless artificial label and put it in the current
11428 function context. The label has a location of LOC. Returns the
11429 newly created label. */
11431 tree
11432 create_artificial_label (location_t loc)
11434 tree lab = build_decl (loc,
11435 LABEL_DECL, NULL_TREE, void_type_node);
11437 DECL_ARTIFICIAL (lab) = 1;
11438 DECL_IGNORED_P (lab) = 1;
11439 DECL_CONTEXT (lab) = current_function_decl;
11440 return lab;
11443 /* Given a tree, try to return a useful variable name that we can use
11444 to prefix a temporary that is being assigned the value of the tree.
11445 I.E. given <temp> = &A, return A. */
11447 const char *
11448 get_name (tree t)
11450 tree stripped_decl;
11452 stripped_decl = t;
11453 STRIP_NOPS (stripped_decl);
11454 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
11455 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
11456 else if (TREE_CODE (stripped_decl) == SSA_NAME)
11458 tree name = SSA_NAME_IDENTIFIER (stripped_decl);
11459 if (!name)
11460 return NULL;
11461 return IDENTIFIER_POINTER (name);
11463 else
11465 switch (TREE_CODE (stripped_decl))
11467 case ADDR_EXPR:
11468 return get_name (TREE_OPERAND (stripped_decl, 0));
11469 default:
11470 return NULL;
11475 /* Return true if TYPE has a variable argument list. */
11477 bool
11478 stdarg_p (const_tree fntype)
11480 function_args_iterator args_iter;
11481 tree n = NULL_TREE, t;
11483 if (!fntype)
11484 return false;
11486 FOREACH_FUNCTION_ARGS (fntype, t, args_iter)
11488 n = t;
11491 return n != NULL_TREE && n != void_type_node;
11494 /* Return true if TYPE has a prototype. */
11496 bool
11497 prototype_p (tree fntype)
11499 tree t;
11501 gcc_assert (fntype != NULL_TREE);
11503 t = TYPE_ARG_TYPES (fntype);
11504 return (t != NULL_TREE);
11507 /* If BLOCK is inlined from an __attribute__((__artificial__))
11508 routine, return pointer to location from where it has been
11509 called. */
11510 location_t *
11511 block_nonartificial_location (tree block)
11513 location_t *ret = NULL;
11515 while (block && TREE_CODE (block) == BLOCK
11516 && BLOCK_ABSTRACT_ORIGIN (block))
11518 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
11520 while (TREE_CODE (ao) == BLOCK
11521 && BLOCK_ABSTRACT_ORIGIN (ao)
11522 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
11523 ao = BLOCK_ABSTRACT_ORIGIN (ao);
11525 if (TREE_CODE (ao) == FUNCTION_DECL)
11527 /* If AO is an artificial inline, point RET to the
11528 call site locus at which it has been inlined and continue
11529 the loop, in case AO's caller is also an artificial
11530 inline. */
11531 if (DECL_DECLARED_INLINE_P (ao)
11532 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
11533 ret = &BLOCK_SOURCE_LOCATION (block);
11534 else
11535 break;
11537 else if (TREE_CODE (ao) != BLOCK)
11538 break;
11540 block = BLOCK_SUPERCONTEXT (block);
11542 return ret;
11546 /* If EXP is inlined from an __attribute__((__artificial__))
11547 function, return the location of the original call expression. */
11549 location_t
11550 tree_nonartificial_location (tree exp)
11552 location_t *loc = block_nonartificial_location (TREE_BLOCK (exp));
11554 if (loc)
11555 return *loc;
11556 else
11557 return EXPR_LOCATION (exp);
11561 /* These are the hash table functions for the hash table of OPTIMIZATION_NODEq
11562 nodes. */
11564 /* Return the hash code code X, an OPTIMIZATION_NODE or TARGET_OPTION code. */
11566 static hashval_t
11567 cl_option_hash_hash (const void *x)
11569 const_tree const t = (const_tree) x;
11570 const char *p;
11571 size_t i;
11572 size_t len = 0;
11573 hashval_t hash = 0;
11575 if (TREE_CODE (t) == OPTIMIZATION_NODE)
11577 p = (const char *)TREE_OPTIMIZATION (t);
11578 len = sizeof (struct cl_optimization);
11581 else if (TREE_CODE (t) == TARGET_OPTION_NODE)
11583 p = (const char *)TREE_TARGET_OPTION (t);
11584 len = sizeof (struct cl_target_option);
11587 else
11588 gcc_unreachable ();
11590 /* assume most opt flags are just 0/1, some are 2-3, and a few might be
11591 something else. */
11592 for (i = 0; i < len; i++)
11593 if (p[i])
11594 hash = (hash << 4) ^ ((i << 2) | p[i]);
11596 return hash;
11599 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
11600 TARGET_OPTION tree node) is the same as that given by *Y, which is the
11601 same. */
11603 static int
11604 cl_option_hash_eq (const void *x, const void *y)
11606 const_tree const xt = (const_tree) x;
11607 const_tree const yt = (const_tree) y;
11608 const char *xp;
11609 const char *yp;
11610 size_t len;
11612 if (TREE_CODE (xt) != TREE_CODE (yt))
11613 return 0;
11615 if (TREE_CODE (xt) == OPTIMIZATION_NODE)
11617 xp = (const char *)TREE_OPTIMIZATION (xt);
11618 yp = (const char *)TREE_OPTIMIZATION (yt);
11619 len = sizeof (struct cl_optimization);
11622 else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
11624 xp = (const char *)TREE_TARGET_OPTION (xt);
11625 yp = (const char *)TREE_TARGET_OPTION (yt);
11626 len = sizeof (struct cl_target_option);
11629 else
11630 gcc_unreachable ();
11632 return (memcmp (xp, yp, len) == 0);
11635 /* Build an OPTIMIZATION_NODE based on the options in OPTS. */
11637 tree
11638 build_optimization_node (struct gcc_options *opts)
11640 tree t;
11641 void **slot;
11643 /* Use the cache of optimization nodes. */
11645 cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node),
11646 opts);
11648 slot = htab_find_slot (cl_option_hash_table, cl_optimization_node, INSERT);
11649 t = (tree) *slot;
11650 if (!t)
11652 /* Insert this one into the hash table. */
11653 t = cl_optimization_node;
11654 *slot = t;
11656 /* Make a new node for next time round. */
11657 cl_optimization_node = make_node (OPTIMIZATION_NODE);
11660 return t;
11663 /* Build a TARGET_OPTION_NODE based on the options in OPTS. */
11665 tree
11666 build_target_option_node (struct gcc_options *opts)
11668 tree t;
11669 void **slot;
11671 /* Use the cache of optimization nodes. */
11673 cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node),
11674 opts);
11676 slot = htab_find_slot (cl_option_hash_table, cl_target_option_node, INSERT);
11677 t = (tree) *slot;
11678 if (!t)
11680 /* Insert this one into the hash table. */
11681 t = cl_target_option_node;
11682 *slot = t;
11684 /* Make a new node for next time round. */
11685 cl_target_option_node = make_node (TARGET_OPTION_NODE);
11688 return t;
11691 /* Reset TREE_TARGET_GLOBALS cache for TARGET_OPTION_NODE.
11692 Called through htab_traverse. */
11694 static int
11695 prepare_target_option_node_for_pch (void **slot, void *)
11697 tree node = (tree) *slot;
11698 if (TREE_CODE (node) == TARGET_OPTION_NODE)
11699 TREE_TARGET_GLOBALS (node) = NULL;
11700 return 1;
11703 /* Clear TREE_TARGET_GLOBALS of all TARGET_OPTION_NODE trees,
11704 so that they aren't saved during PCH writing. */
11706 void
11707 prepare_target_option_nodes_for_pch (void)
11709 htab_traverse (cl_option_hash_table, prepare_target_option_node_for_pch,
11710 NULL);
11713 /* Determine the "ultimate origin" of a block. The block may be an inlined
11714 instance of an inlined instance of a block which is local to an inline
11715 function, so we have to trace all of the way back through the origin chain
11716 to find out what sort of node actually served as the original seed for the
11717 given block. */
11719 tree
11720 block_ultimate_origin (const_tree block)
11722 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
11724 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
11725 nodes in the function to point to themselves; ignore that if
11726 we're trying to output the abstract instance of this function. */
11727 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
11728 return NULL_TREE;
11730 if (immediate_origin == NULL_TREE)
11731 return NULL_TREE;
11732 else
11734 tree ret_val;
11735 tree lookahead = immediate_origin;
11739 ret_val = lookahead;
11740 lookahead = (TREE_CODE (ret_val) == BLOCK
11741 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
11743 while (lookahead != NULL && lookahead != ret_val);
11745 /* The block's abstract origin chain may not be the *ultimate* origin of
11746 the block. It could lead to a DECL that has an abstract origin set.
11747 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
11748 will give us if it has one). Note that DECL's abstract origins are
11749 supposed to be the most distant ancestor (or so decl_ultimate_origin
11750 claims), so we don't need to loop following the DECL origins. */
11751 if (DECL_P (ret_val))
11752 return DECL_ORIGIN (ret_val);
11754 return ret_val;
11758 /* Return true iff conversion in EXP generates no instruction. Mark
11759 it inline so that we fully inline into the stripping functions even
11760 though we have two uses of this function. */
11762 static inline bool
11763 tree_nop_conversion (const_tree exp)
11765 tree outer_type, inner_type;
11766 int outer_is_pts_p, inner_is_pts_p;
11768 if (!CONVERT_EXPR_P (exp)
11769 && TREE_CODE (exp) != NON_LVALUE_EXPR)
11770 return false;
11771 if (TREE_OPERAND (exp, 0) == error_mark_node)
11772 return false;
11774 outer_type = TREE_TYPE (exp);
11775 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11777 if (!inner_type)
11778 return false;
11780 outer_is_pts_p = (POINTER_TYPE_P (outer_type)
11781 && upc_shared_type_p (TREE_TYPE (outer_type)));
11782 inner_is_pts_p = (POINTER_TYPE_P (inner_type)
11783 && upc_shared_type_p (TREE_TYPE (inner_type)));
11785 /* UPC pointer-to-shared types have special
11786 equivalence rules that must be checked. */
11787 if (outer_is_pts_p && inner_is_pts_p
11788 && lang_hooks.types_compatible_p)
11789 return lang_hooks.types_compatible_p (outer_type, inner_type);
11791 /* UPC pointer-to-shared types are not interchangeable
11792 with integral types. */
11793 if (outer_is_pts_p || inner_is_pts_p)
11794 return false;
11796 /* Use precision rather then machine mode when we can, which gives
11797 the correct answer even for submode (bit-field) types. */
11798 if ((INTEGRAL_TYPE_P (outer_type)
11799 || POINTER_TYPE_P (outer_type)
11800 || TREE_CODE (outer_type) == OFFSET_TYPE)
11801 && (INTEGRAL_TYPE_P (inner_type)
11802 || POINTER_TYPE_P (inner_type)
11803 || TREE_CODE (inner_type) == OFFSET_TYPE))
11804 return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
11806 /* Otherwise fall back on comparing machine modes (e.g. for
11807 aggregate types, floats). */
11808 return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
11811 /* Return true iff conversion in EXP generates no instruction. Don't
11812 consider conversions changing the signedness. */
11814 static bool
11815 tree_sign_nop_conversion (const_tree exp)
11817 tree outer_type, inner_type;
11819 if (!tree_nop_conversion (exp))
11820 return false;
11822 outer_type = TREE_TYPE (exp);
11823 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11825 return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
11826 && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
11829 /* Strip conversions from EXP according to tree_nop_conversion and
11830 return the resulting expression. */
11832 tree
11833 tree_strip_nop_conversions (tree exp)
11835 while (tree_nop_conversion (exp))
11836 exp = TREE_OPERAND (exp, 0);
11837 return exp;
11840 /* Strip conversions from EXP according to tree_sign_nop_conversion
11841 and return the resulting expression. */
11843 tree
11844 tree_strip_sign_nop_conversions (tree exp)
11846 while (tree_sign_nop_conversion (exp))
11847 exp = TREE_OPERAND (exp, 0);
11848 return exp;
11851 /* Avoid any floating point extensions from EXP. */
11852 tree
11853 strip_float_extensions (tree exp)
11855 tree sub, expt, subt;
11857 /* For floating point constant look up the narrowest type that can hold
11858 it properly and handle it like (type)(narrowest_type)constant.
11859 This way we can optimize for instance a=a*2.0 where "a" is float
11860 but 2.0 is double constant. */
11861 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
11863 REAL_VALUE_TYPE orig;
11864 tree type = NULL;
11866 orig = TREE_REAL_CST (exp);
11867 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
11868 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
11869 type = float_type_node;
11870 else if (TYPE_PRECISION (TREE_TYPE (exp))
11871 > TYPE_PRECISION (double_type_node)
11872 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
11873 type = double_type_node;
11874 if (type)
11875 return build_real (type, real_value_truncate (TYPE_MODE (type), orig));
11878 if (!CONVERT_EXPR_P (exp))
11879 return exp;
11881 sub = TREE_OPERAND (exp, 0);
11882 subt = TREE_TYPE (sub);
11883 expt = TREE_TYPE (exp);
11885 if (!FLOAT_TYPE_P (subt))
11886 return exp;
11888 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
11889 return exp;
11891 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
11892 return exp;
11894 return strip_float_extensions (sub);
11897 /* Strip out all handled components that produce invariant
11898 offsets. */
11900 const_tree
11901 strip_invariant_refs (const_tree op)
11903 while (handled_component_p (op))
11905 switch (TREE_CODE (op))
11907 case ARRAY_REF:
11908 case ARRAY_RANGE_REF:
11909 if (!is_gimple_constant (TREE_OPERAND (op, 1))
11910 || TREE_OPERAND (op, 2) != NULL_TREE
11911 || TREE_OPERAND (op, 3) != NULL_TREE)
11912 return NULL;
11913 break;
11915 case COMPONENT_REF:
11916 if (TREE_OPERAND (op, 2) != NULL_TREE)
11917 return NULL;
11918 break;
11920 default:;
11922 op = TREE_OPERAND (op, 0);
11925 return op;
11928 static GTY(()) tree gcc_eh_personality_decl;
11930 /* Return the GCC personality function decl. */
11932 tree
11933 lhd_gcc_personality (void)
11935 if (!gcc_eh_personality_decl)
11936 gcc_eh_personality_decl = build_personality_function ("gcc");
11937 return gcc_eh_personality_decl;
11940 /* For languages with One Definition Rule, work out if
11941 trees are actually the same even if the tree representation
11942 differs. This handles only decls appearing in TYPE_NAME
11943 and TYPE_CONTEXT. That is NAMESPACE_DECL, TYPE_DECL,
11944 RECORD_TYPE and IDENTIFIER_NODE. */
11946 static bool
11947 same_for_odr (tree t1, tree t2)
11949 if (t1 == t2)
11950 return true;
11951 if (!t1 || !t2)
11952 return false;
11953 /* C and C++ FEs differ by using IDENTIFIER_NODE and TYPE_DECL. */
11954 if (TREE_CODE (t1) == IDENTIFIER_NODE
11955 && TREE_CODE (t2) == TYPE_DECL
11956 && DECL_FILE_SCOPE_P (t1))
11958 t2 = DECL_NAME (t2);
11959 gcc_assert (TREE_CODE (t2) == IDENTIFIER_NODE);
11961 if (TREE_CODE (t2) == IDENTIFIER_NODE
11962 && TREE_CODE (t1) == TYPE_DECL
11963 && DECL_FILE_SCOPE_P (t2))
11965 t1 = DECL_NAME (t1);
11966 gcc_assert (TREE_CODE (t1) == IDENTIFIER_NODE);
11968 if (TREE_CODE (t1) != TREE_CODE (t2))
11969 return false;
11970 if (TYPE_P (t1))
11971 return types_same_for_odr (t1, t2);
11972 if (DECL_P (t1))
11973 return decls_same_for_odr (t1, t2);
11974 return false;
11977 /* For languages with One Definition Rule, work out if
11978 decls are actually the same even if the tree representation
11979 differs. This handles only decls appearing in TYPE_NAME
11980 and TYPE_CONTEXT. That is NAMESPACE_DECL, TYPE_DECL,
11981 RECORD_TYPE and IDENTIFIER_NODE. */
11983 static bool
11984 decls_same_for_odr (tree decl1, tree decl2)
11986 if (decl1 && TREE_CODE (decl1) == TYPE_DECL
11987 && DECL_ORIGINAL_TYPE (decl1))
11988 decl1 = DECL_ORIGINAL_TYPE (decl1);
11989 if (decl2 && TREE_CODE (decl2) == TYPE_DECL
11990 && DECL_ORIGINAL_TYPE (decl2))
11991 decl2 = DECL_ORIGINAL_TYPE (decl2);
11992 if (decl1 == decl2)
11993 return true;
11994 if (!decl1 || !decl2)
11995 return false;
11996 gcc_checking_assert (DECL_P (decl1) && DECL_P (decl2));
11997 if (TREE_CODE (decl1) != TREE_CODE (decl2))
11998 return false;
11999 if (TREE_CODE (decl1) == TRANSLATION_UNIT_DECL)
12000 return true;
12001 if (TREE_CODE (decl1) != NAMESPACE_DECL
12002 && TREE_CODE (decl1) != TYPE_DECL)
12003 return false;
12004 if (!DECL_NAME (decl1))
12005 return false;
12006 gcc_checking_assert (TREE_CODE (DECL_NAME (decl1)) == IDENTIFIER_NODE);
12007 gcc_checking_assert (!DECL_NAME (decl2)
12008 || TREE_CODE (DECL_NAME (decl2)) == IDENTIFIER_NODE);
12009 if (DECL_NAME (decl1) != DECL_NAME (decl2))
12010 return false;
12011 return same_for_odr (DECL_CONTEXT (decl1),
12012 DECL_CONTEXT (decl2));
12015 /* For languages with One Definition Rule, work out if
12016 types are same even if the tree representation differs.
12017 This is non-trivial for LTO where minnor differences in
12018 the type representation may have prevented type merging
12019 to merge two copies of otherwise equivalent type. */
12021 bool
12022 types_same_for_odr (tree type1, tree type2)
12024 gcc_checking_assert (TYPE_P (type1) && TYPE_P (type2));
12025 type1 = TYPE_MAIN_VARIANT (type1);
12026 type2 = TYPE_MAIN_VARIANT (type2);
12027 if (type1 == type2)
12028 return true;
12030 #ifndef ENABLE_CHECKING
12031 if (!in_lto_p)
12032 return false;
12033 #endif
12035 /* Check for anonymous namespaces. Those have !TREE_PUBLIC
12036 on the corresponding TYPE_STUB_DECL. */
12037 if (type_in_anonymous_namespace_p (type1)
12038 || type_in_anonymous_namespace_p (type2))
12039 return false;
12040 /* When assembler name of virtual table is available, it is
12041 easy to compare types for equivalence. */
12042 if (TYPE_BINFO (type1) && TYPE_BINFO (type2)
12043 && BINFO_VTABLE (TYPE_BINFO (type1))
12044 && BINFO_VTABLE (TYPE_BINFO (type2)))
12046 tree v1 = BINFO_VTABLE (TYPE_BINFO (type1));
12047 tree v2 = BINFO_VTABLE (TYPE_BINFO (type2));
12049 if (TREE_CODE (v1) == POINTER_PLUS_EXPR)
12051 if (TREE_CODE (v2) != POINTER_PLUS_EXPR
12052 || !operand_equal_p (TREE_OPERAND (v1, 1),
12053 TREE_OPERAND (v2, 1), 0))
12054 return false;
12055 v1 = TREE_OPERAND (TREE_OPERAND (v1, 0), 0);
12056 v2 = TREE_OPERAND (TREE_OPERAND (v2, 0), 0);
12058 v1 = DECL_ASSEMBLER_NAME (v1);
12059 v2 = DECL_ASSEMBLER_NAME (v2);
12060 return (v1 == v2);
12063 /* FIXME: the code comparing type names consider all instantiations of the
12064 same template to have same name. This is because we have no access
12065 to template parameters. For types with no virtual method tables
12066 we thus can return false positives. At the moment we do not need
12067 to compare types in other scenarios than devirtualization. */
12069 /* If types are not structuraly same, do not bother to contnue.
12070 Match in the remainder of code would mean ODR violation. */
12071 if (!types_compatible_p (type1, type2))
12072 return false;
12073 if (!TYPE_NAME (type1))
12074 return false;
12075 if (!decls_same_for_odr (TYPE_NAME (type1), TYPE_NAME (type2)))
12076 return false;
12077 if (!same_for_odr (TYPE_CONTEXT (type1), TYPE_CONTEXT (type2)))
12078 return false;
12079 /* When not in LTO the MAIN_VARIANT check should be the same. */
12080 gcc_assert (in_lto_p);
12082 return true;
12085 /* TARGET is a call target of GIMPLE call statement
12086 (obtained by gimple_call_fn). Return true if it is
12087 OBJ_TYPE_REF representing an virtual call of C++ method.
12088 (As opposed to OBJ_TYPE_REF representing objc calls
12089 through a cast where middle-end devirtualization machinery
12090 can't apply.) */
12092 bool
12093 virtual_method_call_p (tree target)
12095 if (TREE_CODE (target) != OBJ_TYPE_REF)
12096 return false;
12097 target = TREE_TYPE (target);
12098 gcc_checking_assert (TREE_CODE (target) == POINTER_TYPE);
12099 target = TREE_TYPE (target);
12100 if (TREE_CODE (target) == FUNCTION_TYPE)
12101 return false;
12102 gcc_checking_assert (TREE_CODE (target) == METHOD_TYPE);
12103 return true;
12106 /* REF is OBJ_TYPE_REF, return the class the ref corresponds to. */
12108 tree
12109 obj_type_ref_class (tree ref)
12111 gcc_checking_assert (TREE_CODE (ref) == OBJ_TYPE_REF);
12112 ref = TREE_TYPE (ref);
12113 gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
12114 ref = TREE_TYPE (ref);
12115 /* We look for type THIS points to. ObjC also builds
12116 OBJ_TYPE_REF with non-method calls, Their first parameter
12117 ID however also corresponds to class type. */
12118 gcc_checking_assert (TREE_CODE (ref) == METHOD_TYPE
12119 || TREE_CODE (ref) == FUNCTION_TYPE);
12120 ref = TREE_VALUE (TYPE_ARG_TYPES (ref));
12121 gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
12122 return TREE_TYPE (ref);
12125 /* Return true if T is in anonymous namespace. */
12127 bool
12128 type_in_anonymous_namespace_p (tree t)
12130 return (TYPE_STUB_DECL (t) && !TREE_PUBLIC (TYPE_STUB_DECL (t)));
12133 /* Try to find a base info of BINFO that would have its field decl at offset
12134 OFFSET within the BINFO type and which is of EXPECTED_TYPE. If it can be
12135 found, return, otherwise return NULL_TREE. */
12137 tree
12138 get_binfo_at_offset (tree binfo, HOST_WIDE_INT offset, tree expected_type)
12140 tree type = BINFO_TYPE (binfo);
12142 while (true)
12144 HOST_WIDE_INT pos, size;
12145 tree fld;
12146 int i;
12148 if (types_same_for_odr (type, expected_type))
12149 return binfo;
12150 if (offset < 0)
12151 return NULL_TREE;
12153 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
12155 if (TREE_CODE (fld) != FIELD_DECL)
12156 continue;
12158 pos = int_bit_position (fld);
12159 size = tree_to_uhwi (DECL_SIZE (fld));
12160 if (pos <= offset && (pos + size) > offset)
12161 break;
12163 if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE)
12164 return NULL_TREE;
12166 if (!DECL_ARTIFICIAL (fld))
12168 binfo = TYPE_BINFO (TREE_TYPE (fld));
12169 if (!binfo)
12170 return NULL_TREE;
12172 /* Offset 0 indicates the primary base, whose vtable contents are
12173 represented in the binfo for the derived class. */
12174 else if (offset != 0)
12176 tree base_binfo, binfo2 = binfo;
12178 /* Find BINFO corresponding to FLD. This is bit harder
12179 by a fact that in virtual inheritance we may need to walk down
12180 the non-virtual inheritance chain. */
12181 while (true)
12183 tree containing_binfo = NULL, found_binfo = NULL;
12184 for (i = 0; BINFO_BASE_ITERATE (binfo2, i, base_binfo); i++)
12185 if (types_same_for_odr (TREE_TYPE (base_binfo), TREE_TYPE (fld)))
12187 found_binfo = base_binfo;
12188 break;
12190 else
12191 if (BINFO_OFFSET (base_binfo) - BINFO_OFFSET (binfo) < pos
12192 && (!containing_binfo
12193 || (BINFO_OFFSET (containing_binfo)
12194 < BINFO_OFFSET (base_binfo))))
12195 containing_binfo = base_binfo;
12196 if (found_binfo)
12198 binfo = found_binfo;
12199 break;
12201 if (!containing_binfo)
12202 return NULL_TREE;
12203 binfo2 = containing_binfo;
12207 type = TREE_TYPE (fld);
12208 offset -= pos;
12212 /* Returns true if X is a typedef decl. */
12214 bool
12215 is_typedef_decl (tree x)
12217 return (x && TREE_CODE (x) == TYPE_DECL
12218 && DECL_ORIGINAL_TYPE (x) != NULL_TREE);
12221 /* Returns true iff TYPE is a type variant created for a typedef. */
12223 bool
12224 typedef_variant_p (tree type)
12226 return is_typedef_decl (TYPE_NAME (type));
12229 /* Warn about a use of an identifier which was marked deprecated. */
12230 void
12231 warn_deprecated_use (tree node, tree attr)
12233 const char *msg;
12235 if (node == 0 || !warn_deprecated_decl)
12236 return;
12238 if (!attr)
12240 if (DECL_P (node))
12241 attr = DECL_ATTRIBUTES (node);
12242 else if (TYPE_P (node))
12244 tree decl = TYPE_STUB_DECL (node);
12245 if (decl)
12246 attr = lookup_attribute ("deprecated",
12247 TYPE_ATTRIBUTES (TREE_TYPE (decl)));
12251 if (attr)
12252 attr = lookup_attribute ("deprecated", attr);
12254 if (attr)
12255 msg = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr)));
12256 else
12257 msg = NULL;
12259 if (DECL_P (node))
12261 expanded_location xloc = expand_location (DECL_SOURCE_LOCATION (node));
12262 if (msg)
12263 warning (OPT_Wdeprecated_declarations,
12264 "%qD is deprecated (declared at %r%s:%d%R): %s",
12265 node, "locus", xloc.file, xloc.line, msg);
12266 else
12267 warning (OPT_Wdeprecated_declarations,
12268 "%qD is deprecated (declared at %r%s:%d%R)",
12269 node, "locus", xloc.file, xloc.line);
12271 else if (TYPE_P (node))
12273 tree what = NULL_TREE;
12274 tree decl = TYPE_STUB_DECL (node);
12276 if (TYPE_NAME (node))
12278 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
12279 what = TYPE_NAME (node);
12280 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
12281 && DECL_NAME (TYPE_NAME (node)))
12282 what = DECL_NAME (TYPE_NAME (node));
12285 if (decl)
12287 expanded_location xloc
12288 = expand_location (DECL_SOURCE_LOCATION (decl));
12289 if (what)
12291 if (msg)
12292 warning (OPT_Wdeprecated_declarations,
12293 "%qE is deprecated (declared at %r%s:%d%R): %s",
12294 what, "locus", xloc.file, xloc.line, msg);
12295 else
12296 warning (OPT_Wdeprecated_declarations,
12297 "%qE is deprecated (declared at %r%s:%d%R)",
12298 what, "locus", xloc.file, xloc.line);
12300 else
12302 if (msg)
12303 warning (OPT_Wdeprecated_declarations,
12304 "type is deprecated (declared at %r%s:%d%R): %s",
12305 "locus", xloc.file, xloc.line, msg);
12306 else
12307 warning (OPT_Wdeprecated_declarations,
12308 "type is deprecated (declared at %r%s:%d%R)",
12309 "locus", xloc.file, xloc.line);
12312 else
12314 if (what)
12316 if (msg)
12317 warning (OPT_Wdeprecated_declarations, "%qE is deprecated: %s",
12318 what, msg);
12319 else
12320 warning (OPT_Wdeprecated_declarations, "%qE is deprecated", what);
12322 else
12324 if (msg)
12325 warning (OPT_Wdeprecated_declarations, "type is deprecated: %s",
12326 msg);
12327 else
12328 warning (OPT_Wdeprecated_declarations, "type is deprecated");
12334 /* Return true if REF has a COMPONENT_REF with a bit-field field declaration
12335 somewhere in it. */
12337 bool
12338 contains_bitfld_component_ref_p (const_tree ref)
12340 while (handled_component_p (ref))
12342 if (TREE_CODE (ref) == COMPONENT_REF
12343 && DECL_BIT_FIELD (TREE_OPERAND (ref, 1)))
12344 return true;
12345 ref = TREE_OPERAND (ref, 0);
12348 return false;
12351 /* Try to determine whether a TRY_CATCH expression can fall through.
12352 This is a subroutine of block_may_fallthru. */
12354 static bool
12355 try_catch_may_fallthru (const_tree stmt)
12357 tree_stmt_iterator i;
12359 /* If the TRY block can fall through, the whole TRY_CATCH can
12360 fall through. */
12361 if (block_may_fallthru (TREE_OPERAND (stmt, 0)))
12362 return true;
12364 i = tsi_start (TREE_OPERAND (stmt, 1));
12365 switch (TREE_CODE (tsi_stmt (i)))
12367 case CATCH_EXPR:
12368 /* We expect to see a sequence of CATCH_EXPR trees, each with a
12369 catch expression and a body. The whole TRY_CATCH may fall
12370 through iff any of the catch bodies falls through. */
12371 for (; !tsi_end_p (i); tsi_next (&i))
12373 if (block_may_fallthru (CATCH_BODY (tsi_stmt (i))))
12374 return true;
12376 return false;
12378 case EH_FILTER_EXPR:
12379 /* The exception filter expression only matters if there is an
12380 exception. If the exception does not match EH_FILTER_TYPES,
12381 we will execute EH_FILTER_FAILURE, and we will fall through
12382 if that falls through. If the exception does match
12383 EH_FILTER_TYPES, the stack unwinder will continue up the
12384 stack, so we will not fall through. We don't know whether we
12385 will throw an exception which matches EH_FILTER_TYPES or not,
12386 so we just ignore EH_FILTER_TYPES and assume that we might
12387 throw an exception which doesn't match. */
12388 return block_may_fallthru (EH_FILTER_FAILURE (tsi_stmt (i)));
12390 default:
12391 /* This case represents statements to be executed when an
12392 exception occurs. Those statements are implicitly followed
12393 by a RESX statement to resume execution after the exception.
12394 So in this case the TRY_CATCH never falls through. */
12395 return false;
12399 /* Try to determine if we can fall out of the bottom of BLOCK. This guess
12400 need not be 100% accurate; simply be conservative and return true if we
12401 don't know. This is used only to avoid stupidly generating extra code.
12402 If we're wrong, we'll just delete the extra code later. */
12404 bool
12405 block_may_fallthru (const_tree block)
12407 /* This CONST_CAST is okay because expr_last returns its argument
12408 unmodified and we assign it to a const_tree. */
12409 const_tree stmt = expr_last (CONST_CAST_TREE (block));
12411 switch (stmt ? TREE_CODE (stmt) : ERROR_MARK)
12413 case GOTO_EXPR:
12414 case RETURN_EXPR:
12415 /* Easy cases. If the last statement of the block implies
12416 control transfer, then we can't fall through. */
12417 return false;
12419 case SWITCH_EXPR:
12420 /* If SWITCH_LABELS is set, this is lowered, and represents a
12421 branch to a selected label and hence can not fall through.
12422 Otherwise SWITCH_BODY is set, and the switch can fall
12423 through. */
12424 return SWITCH_LABELS (stmt) == NULL_TREE;
12426 case COND_EXPR:
12427 if (block_may_fallthru (COND_EXPR_THEN (stmt)))
12428 return true;
12429 return block_may_fallthru (COND_EXPR_ELSE (stmt));
12431 case BIND_EXPR:
12432 return block_may_fallthru (BIND_EXPR_BODY (stmt));
12434 case TRY_CATCH_EXPR:
12435 return try_catch_may_fallthru (stmt);
12437 case TRY_FINALLY_EXPR:
12438 /* The finally clause is always executed after the try clause,
12439 so if it does not fall through, then the try-finally will not
12440 fall through. Otherwise, if the try clause does not fall
12441 through, then when the finally clause falls through it will
12442 resume execution wherever the try clause was going. So the
12443 whole try-finally will only fall through if both the try
12444 clause and the finally clause fall through. */
12445 return (block_may_fallthru (TREE_OPERAND (stmt, 0))
12446 && block_may_fallthru (TREE_OPERAND (stmt, 1)));
12448 case MODIFY_EXPR:
12449 if (TREE_CODE (TREE_OPERAND (stmt, 1)) == CALL_EXPR)
12450 stmt = TREE_OPERAND (stmt, 1);
12451 else
12452 return true;
12453 /* FALLTHRU */
12455 case CALL_EXPR:
12456 /* Functions that do not return do not fall through. */
12457 return (call_expr_flags (stmt) & ECF_NORETURN) == 0;
12459 case CLEANUP_POINT_EXPR:
12460 return block_may_fallthru (TREE_OPERAND (stmt, 0));
12462 case TARGET_EXPR:
12463 return block_may_fallthru (TREE_OPERAND (stmt, 1));
12465 case ERROR_MARK:
12466 return true;
12468 default:
12469 return lang_hooks.block_may_fallthru (stmt);
12473 /* Garbage collection support for tree_type_common. */
12475 extern void gt_ggc_mx (tree&);
12476 extern void gt_ggc_mx_die_struct (void *);
12478 void gt_ggc_mx (tree_type_common *tt)
12480 tree t = (tree) tt;
12481 tree block_factor = TYPE_BLOCK_FACTOR (t);
12483 gt_ggc_mx (tt->common.typed.type);
12484 gt_ggc_mx (tt->common.chain);
12485 gt_ggc_mx (tt->size);
12486 gt_ggc_mx (tt->size_unit);
12487 gt_ggc_mx (tt->attributes);
12488 gt_ggc_mx (tt->pointer_to);
12489 gt_ggc_mx (tt->reference_to);
12490 switch (debug_hooks->tree_type_symtab_field)
12492 case TYPE_SYMTAB_IS_ADDRESS:
12493 break;
12494 case TYPE_SYMTAB_IS_POINTER:
12495 gt_ggc_m_S (tt->symtab.pointer);
12496 break;
12497 case TYPE_SYMTAB_IS_DIE:
12498 gt_ggc_mx_die_struct (tt->symtab.die);
12499 break;
12500 default:
12501 break;
12503 gt_ggc_mx (tt->name);
12504 gt_ggc_mx (tt->next_variant);
12505 gt_ggc_mx (tt->main_variant);
12506 gt_ggc_mx (tt->context);
12507 gt_ggc_mx (tt->canonical);
12509 if (TYPE_HAS_BLOCK_FACTOR_X (t))
12510 gt_ggc_mx (block_factor);
12513 /* PCH support for tree_type_common. */
12515 extern void gt_pch_nx (tree&);
12516 extern void gt_ggc_nx_die_struct (void *);
12518 void gt_pch_nx (tree_type_common *tt)
12520 tree t = (tree) tt;
12521 tree block_factor = TYPE_BLOCK_FACTOR (t);
12523 gt_pch_nx (tt->common.typed.type);
12524 gt_pch_nx (tt->common.chain);
12525 gt_pch_nx (tt->size);
12526 gt_pch_nx (tt->size_unit);
12527 gt_pch_nx (tt->attributes);
12528 gt_pch_nx (tt->pointer_to);
12529 gt_pch_nx (tt->reference_to);
12530 switch (debug_hooks->tree_type_symtab_field)
12532 case TYPE_SYMTAB_IS_ADDRESS:
12533 break;
12534 case TYPE_SYMTAB_IS_POINTER:
12535 gt_pch_n_S (tt->symtab.pointer);
12536 break;
12537 case TYPE_SYMTAB_IS_DIE:
12538 gt_pch_nx_die_struct (tt->symtab.die);
12539 break;
12540 default:
12541 break;
12543 gt_pch_nx (tt->name);
12544 gt_pch_nx (tt->next_variant);
12545 gt_pch_nx (tt->main_variant);
12546 gt_pch_nx (tt->context);
12547 gt_pch_nx (tt->canonical);
12549 if (TYPE_HAS_BLOCK_FACTOR_X (t))
12550 gt_pch_nx (block_factor);
12553 void gt_pch_nx (tree_type_common *tt, gt_pointer_operator op, void *cookie)
12555 tree t = (tree) tt;
12556 tree block_factor = TYPE_BLOCK_FACTOR (t);
12558 op (&(tt->common.typed.type), cookie);
12559 op (&(tt->common.chain), cookie);
12560 op (&(tt->size), cookie);
12561 op (&(tt->size_unit), cookie);
12562 op (&(tt->attributes), cookie);
12563 op (&(tt->pointer_to), cookie);
12564 op (&(tt->reference_to), cookie);
12565 switch (debug_hooks->tree_type_symtab_field)
12567 case TYPE_SYMTAB_IS_ADDRESS:
12568 break;
12569 case TYPE_SYMTAB_IS_POINTER:
12570 op (&(tt->symtab.pointer), cookie);
12571 break;
12572 case TYPE_SYMTAB_IS_DIE:
12573 op (&(tt->symtab.die), cookie);
12574 break;
12575 default:
12576 break;
12578 op (&(tt->name), cookie);
12579 op (&(tt->next_variant), cookie);
12580 op (&(tt->main_variant), cookie);
12581 op (&(tt->context), cookie);
12582 op (&(tt->canonical), cookie);
12584 if (TYPE_HAS_BLOCK_FACTOR_X (t))
12585 op (&(block_factor), cookie);
12588 /* True if we are using EH to handle cleanups. */
12589 static bool using_eh_for_cleanups_flag = false;
12591 /* This routine is called from front ends to indicate eh should be used for
12592 cleanups. */
12593 void
12594 using_eh_for_cleanups (void)
12596 using_eh_for_cleanups_flag = true;
12599 /* Query whether EH is used for cleanups. */
12600 bool
12601 using_eh_for_cleanups_p (void)
12603 return using_eh_for_cleanups_flag;
12606 /* Wrapper for tree_code_name to ensure that tree code is valid */
12607 const char *
12608 get_tree_code_name (enum tree_code code)
12610 const char *invalid = "<invalid tree code>";
12612 if (code >= MAX_TREE_CODES)
12613 return invalid;
12615 return tree_code_name[code];
12618 /* Drops the TREE_OVERFLOW flag from T. */
12620 tree
12621 drop_tree_overflow (tree t)
12623 gcc_checking_assert (TREE_OVERFLOW (t));
12625 /* For tree codes with a sharing machinery re-build the result. */
12626 if (TREE_CODE (t) == INTEGER_CST)
12627 return build_int_cst_wide (TREE_TYPE (t),
12628 TREE_INT_CST_LOW (t), TREE_INT_CST_HIGH (t));
12630 /* Otherwise, as all tcc_constants are possibly shared, copy the node
12631 and drop the flag. */
12632 t = copy_node (t);
12633 TREE_OVERFLOW (t) = 0;
12634 return t;
12637 /* Given a memory reference expression T, return its base address.
12638 The base address of a memory reference expression is the main
12639 object being referenced. For instance, the base address for
12640 'array[i].fld[j]' is 'array'. You can think of this as stripping
12641 away the offset part from a memory address.
12643 This function calls handled_component_p to strip away all the inner
12644 parts of the memory reference until it reaches the base object. */
12646 tree
12647 get_base_address (tree t)
12649 while (handled_component_p (t))
12650 t = TREE_OPERAND (t, 0);
12652 if ((TREE_CODE (t) == MEM_REF
12653 || TREE_CODE (t) == TARGET_MEM_REF)
12654 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR)
12655 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
12657 /* ??? Either the alias oracle or all callers need to properly deal
12658 with WITH_SIZE_EXPRs before we can look through those. */
12659 if (TREE_CODE (t) == WITH_SIZE_EXPR)
12660 return NULL_TREE;
12662 return t;
12665 #include "gt-tree.h"