Merge trunk version 204659 into gupc branch.
[official-gcc.git] / gcc / tree.c
blob17e0743623cb2b92a20cea0e493ec63a1f53c954
1 /* Language-independent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987-2013 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 "tm_p.h"
37 #include "function.h"
38 #include "obstack.h"
39 #include "toplev.h" /* get_random_seed */
40 #include "ggc.h"
41 #include "hashtab.h"
42 #include "filenames.h"
43 #include "output.h"
44 #include "target.h"
45 #include "common/common-target.h"
46 #include "langhooks.h"
47 #include "tree-inline.h"
48 #include "tree-iterator.h"
49 #include "basic-block.h"
50 #include "bitmap.h"
51 #include "gimple.h"
52 #include "gimple-ssa.h"
53 #include "cgraph.h"
54 #include "tree-phinodes.h"
55 #include "tree-ssanames.h"
56 #include "tree-dfa.h"
57 #include "params.h"
58 #include "pointer-set.h"
59 #include "tree-pass.h"
60 #include "langhooks-def.h"
61 #include "diagnostic.h"
62 #include "tree-diagnostic.h"
63 #include "tree-pretty-print.h"
64 #include "except.h"
65 #include "debug.h"
66 #include "intl.h"
68 /* Tree code classes. */
70 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
71 #define END_OF_BASE_TREE_CODES tcc_exceptional,
73 const enum tree_code_class tree_code_type[] = {
74 #include "all-tree.def"
77 #undef DEFTREECODE
78 #undef END_OF_BASE_TREE_CODES
80 /* Table indexed by tree code giving number of expression
81 operands beyond the fixed part of the node structure.
82 Not used for types or decls. */
84 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
85 #define END_OF_BASE_TREE_CODES 0,
87 const unsigned char tree_code_length[] = {
88 #include "all-tree.def"
91 #undef DEFTREECODE
92 #undef END_OF_BASE_TREE_CODES
94 /* Names of tree components.
95 Used for printing out the tree and error messages. */
96 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
97 #define END_OF_BASE_TREE_CODES "@dummy",
99 static const char *const tree_code_name[] = {
100 #include "all-tree.def"
103 #undef DEFTREECODE
104 #undef END_OF_BASE_TREE_CODES
106 /* Each tree code class has an associated string representation.
107 These must correspond to the tree_code_class entries. */
109 const char *const tree_code_class_strings[] =
111 "exceptional",
112 "constant",
113 "type",
114 "declaration",
115 "reference",
116 "comparison",
117 "unary",
118 "binary",
119 "statement",
120 "vl_exp",
121 "expression"
124 /* obstack.[ch] explicitly declined to prototype this. */
125 extern int _obstack_allocated_p (struct obstack *h, void *obj);
127 /* Statistics-gathering stuff. */
129 static int tree_code_counts[MAX_TREE_CODES];
130 int tree_node_counts[(int) all_kinds];
131 int tree_node_sizes[(int) all_kinds];
133 /* Keep in sync with tree.h:enum tree_node_kind. */
134 static const char * const tree_node_kind_names[] = {
135 "decls",
136 "types",
137 "blocks",
138 "stmts",
139 "refs",
140 "exprs",
141 "constants",
142 "identifiers",
143 "vecs",
144 "binfos",
145 "ssa names",
146 "constructors",
147 "random kinds",
148 "lang_decl kinds",
149 "lang_type kinds",
150 "omp clauses",
153 /* Unique id for next decl created. */
154 static GTY(()) int next_decl_uid;
155 /* Unique id for next type created. */
156 static GTY(()) int next_type_uid = 1;
157 /* Unique id for next debug decl created. Use negative numbers,
158 to catch erroneous uses. */
159 static GTY(()) int next_debug_decl_uid;
161 /* Since we cannot rehash a type after it is in the table, we have to
162 keep the hash code. */
164 struct GTY(()) type_hash {
165 unsigned long hash;
166 tree type;
169 /* Initial size of the hash table (rounded to next prime). */
170 #define TYPE_HASH_INITIAL_SIZE 1000
172 /* Now here is the hash table. When recording a type, it is added to
173 the slot whose index is the hash code. Note that the hash table is
174 used for several kinds of types (function types, array types and
175 array index range types, for now). While all these live in the
176 same table, they are completely independent, and the hash code is
177 computed differently for each of these. */
179 static GTY ((if_marked ("type_hash_marked_p"), param_is (struct type_hash)))
180 htab_t type_hash_table;
182 /* Hash table and temporary node for larger integer const values. */
183 static GTY (()) tree int_cst_node;
184 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node)))
185 htab_t int_cst_hash_table;
187 /* Hash table for optimization flags and target option flags. Use the same
188 hash table for both sets of options. Nodes for building the current
189 optimization and target option nodes. The assumption is most of the time
190 the options created will already be in the hash table, so we avoid
191 allocating and freeing up a node repeatably. */
192 static GTY (()) tree cl_optimization_node;
193 static GTY (()) tree cl_target_option_node;
194 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node)))
195 htab_t cl_option_hash_table;
197 /* General tree->tree mapping structure for use in hash tables. */
200 static GTY ((if_marked ("tree_decl_map_marked_p"), param_is (struct tree_decl_map)))
201 htab_t debug_expr_for_decl;
203 static GTY ((if_marked ("tree_decl_map_marked_p"), param_is (struct tree_decl_map)))
204 htab_t value_expr_for_decl;
206 static GTY ((if_marked ("tree_vec_map_marked_p"), param_is (struct tree_vec_map)))
207 htab_t debug_args_for_decl;
209 static GTY ((if_marked ("tree_priority_map_marked_p"),
210 param_is (struct tree_priority_map)))
211 htab_t init_priority_for_decl;
213 static void set_type_quals (tree, int, tree);
214 static int type_hash_eq (const void *, const void *);
215 static hashval_t type_hash_hash (const void *);
216 static hashval_t int_cst_hash_hash (const void *);
217 static int int_cst_hash_eq (const void *, const void *);
218 static hashval_t cl_option_hash_hash (const void *);
219 static int cl_option_hash_eq (const void *, const void *);
220 static void print_type_hash_statistics (void);
221 static void print_debug_expr_statistics (void);
222 static void print_value_expr_statistics (void);
223 static int type_hash_marked_p (const void *);
224 static unsigned int type_hash_list (const_tree, hashval_t);
225 static unsigned int attribute_hash_list (const_tree, hashval_t);
226 static bool decls_same_for_odr (tree decl1, tree decl2);
228 tree global_trees[TI_MAX];
229 tree integer_types[itk_none];
231 unsigned char tree_contains_struct[MAX_TREE_CODES][64];
233 /* Number of operands for each OpenMP clause. */
234 unsigned const char omp_clause_num_ops[] =
236 0, /* OMP_CLAUSE_ERROR */
237 1, /* OMP_CLAUSE_PRIVATE */
238 1, /* OMP_CLAUSE_SHARED */
239 1, /* OMP_CLAUSE_FIRSTPRIVATE */
240 2, /* OMP_CLAUSE_LASTPRIVATE */
241 4, /* OMP_CLAUSE_REDUCTION */
242 1, /* OMP_CLAUSE_COPYIN */
243 1, /* OMP_CLAUSE_COPYPRIVATE */
244 2, /* OMP_CLAUSE_LINEAR */
245 2, /* OMP_CLAUSE_ALIGNED */
246 1, /* OMP_CLAUSE_DEPEND */
247 1, /* OMP_CLAUSE_UNIFORM */
248 2, /* OMP_CLAUSE_FROM */
249 2, /* OMP_CLAUSE_TO */
250 2, /* OMP_CLAUSE_MAP */
251 1, /* OMP_CLAUSE__LOOPTEMP_ */
252 1, /* OMP_CLAUSE_IF */
253 1, /* OMP_CLAUSE_NUM_THREADS */
254 1, /* OMP_CLAUSE_SCHEDULE */
255 0, /* OMP_CLAUSE_NOWAIT */
256 0, /* OMP_CLAUSE_ORDERED */
257 0, /* OMP_CLAUSE_DEFAULT */
258 3, /* OMP_CLAUSE_COLLAPSE */
259 0, /* OMP_CLAUSE_UNTIED */
260 1, /* OMP_CLAUSE_FINAL */
261 0, /* OMP_CLAUSE_MERGEABLE */
262 1, /* OMP_CLAUSE_DEVICE */
263 1, /* OMP_CLAUSE_DIST_SCHEDULE */
264 0, /* OMP_CLAUSE_INBRANCH */
265 0, /* OMP_CLAUSE_NOTINBRANCH */
266 1, /* OMP_CLAUSE_NUM_TEAMS */
267 1, /* OMP_CLAUSE_THREAD_LIMIT */
268 0, /* OMP_CLAUSE_PROC_BIND */
269 1, /* OMP_CLAUSE_SAFELEN */
270 1, /* OMP_CLAUSE_SIMDLEN */
271 0, /* OMP_CLAUSE_FOR */
272 0, /* OMP_CLAUSE_PARALLEL */
273 0, /* OMP_CLAUSE_SECTIONS */
274 0, /* OMP_CLAUSE_TASKGROUP */
275 1, /* OMP_CLAUSE__SIMDUID_ */
278 const char * const omp_clause_code_name[] =
280 "error_clause",
281 "private",
282 "shared",
283 "firstprivate",
284 "lastprivate",
285 "reduction",
286 "copyin",
287 "copyprivate",
288 "linear",
289 "aligned",
290 "depend",
291 "uniform",
292 "from",
293 "to",
294 "map",
295 "_looptemp_",
296 "if",
297 "num_threads",
298 "schedule",
299 "nowait",
300 "ordered",
301 "default",
302 "collapse",
303 "untied",
304 "final",
305 "mergeable",
306 "device",
307 "dist_schedule",
308 "inbranch",
309 "notinbranch",
310 "num_teams",
311 "thread_limit",
312 "proc_bind",
313 "safelen",
314 "simdlen",
315 "for",
316 "parallel",
317 "sections",
318 "taskgroup",
319 "_simduid_"
323 /* Return the tree node structure used by tree code CODE. */
325 static inline enum tree_node_structure_enum
326 tree_node_structure_for_code (enum tree_code code)
328 switch (TREE_CODE_CLASS (code))
330 case tcc_declaration:
332 switch (code)
334 case FIELD_DECL:
335 return TS_FIELD_DECL;
336 case PARM_DECL:
337 return TS_PARM_DECL;
338 case VAR_DECL:
339 return TS_VAR_DECL;
340 case LABEL_DECL:
341 return TS_LABEL_DECL;
342 case RESULT_DECL:
343 return TS_RESULT_DECL;
344 case DEBUG_EXPR_DECL:
345 return TS_DECL_WRTL;
346 case CONST_DECL:
347 return TS_CONST_DECL;
348 case TYPE_DECL:
349 return TS_TYPE_DECL;
350 case FUNCTION_DECL:
351 return TS_FUNCTION_DECL;
352 case TRANSLATION_UNIT_DECL:
353 return TS_TRANSLATION_UNIT_DECL;
354 default:
355 return TS_DECL_NON_COMMON;
358 case tcc_type:
359 return TS_TYPE_NON_COMMON;
360 case tcc_reference:
361 case tcc_comparison:
362 case tcc_unary:
363 case tcc_binary:
364 case tcc_expression:
365 case tcc_statement:
366 case tcc_vl_exp:
367 return TS_EXP;
368 default: /* tcc_constant and tcc_exceptional */
369 break;
371 switch (code)
373 /* tcc_constant cases. */
374 case INTEGER_CST: return TS_INT_CST;
375 case REAL_CST: return TS_REAL_CST;
376 case FIXED_CST: return TS_FIXED_CST;
377 case COMPLEX_CST: return TS_COMPLEX;
378 case VECTOR_CST: return TS_VECTOR;
379 case STRING_CST: return TS_STRING;
380 /* tcc_exceptional cases. */
381 case ERROR_MARK: return TS_COMMON;
382 case IDENTIFIER_NODE: return TS_IDENTIFIER;
383 case TREE_LIST: return TS_LIST;
384 case TREE_VEC: return TS_VEC;
385 case SSA_NAME: return TS_SSA_NAME;
386 case PLACEHOLDER_EXPR: return TS_COMMON;
387 case STATEMENT_LIST: return TS_STATEMENT_LIST;
388 case BLOCK: return TS_BLOCK;
389 case CONSTRUCTOR: return TS_CONSTRUCTOR;
390 case TREE_BINFO: return TS_BINFO;
391 case OMP_CLAUSE: return TS_OMP_CLAUSE;
392 case OPTIMIZATION_NODE: return TS_OPTIMIZATION;
393 case TARGET_OPTION_NODE: return TS_TARGET_OPTION;
395 default:
396 gcc_unreachable ();
401 /* Initialize tree_contains_struct to describe the hierarchy of tree
402 nodes. */
404 static void
405 initialize_tree_contains_struct (void)
407 unsigned i;
409 for (i = ERROR_MARK; i < LAST_AND_UNUSED_TREE_CODE; i++)
411 enum tree_code code;
412 enum tree_node_structure_enum ts_code;
414 code = (enum tree_code) i;
415 ts_code = tree_node_structure_for_code (code);
417 /* Mark the TS structure itself. */
418 tree_contains_struct[code][ts_code] = 1;
420 /* Mark all the structures that TS is derived from. */
421 switch (ts_code)
423 case TS_TYPED:
424 case TS_BLOCK:
425 MARK_TS_BASE (code);
426 break;
428 case TS_COMMON:
429 case TS_INT_CST:
430 case TS_REAL_CST:
431 case TS_FIXED_CST:
432 case TS_VECTOR:
433 case TS_STRING:
434 case TS_COMPLEX:
435 case TS_SSA_NAME:
436 case TS_CONSTRUCTOR:
437 case TS_EXP:
438 case TS_STATEMENT_LIST:
439 MARK_TS_TYPED (code);
440 break;
442 case TS_IDENTIFIER:
443 case TS_DECL_MINIMAL:
444 case TS_TYPE_COMMON:
445 case TS_LIST:
446 case TS_VEC:
447 case TS_BINFO:
448 case TS_OMP_CLAUSE:
449 case TS_OPTIMIZATION:
450 case TS_TARGET_OPTION:
451 MARK_TS_COMMON (code);
452 break;
454 case TS_TYPE_WITH_LANG_SPECIFIC:
455 MARK_TS_TYPE_COMMON (code);
456 break;
458 case TS_TYPE_NON_COMMON:
459 MARK_TS_TYPE_WITH_LANG_SPECIFIC (code);
460 break;
462 case TS_DECL_COMMON:
463 MARK_TS_DECL_MINIMAL (code);
464 break;
466 case TS_DECL_WRTL:
467 case TS_CONST_DECL:
468 MARK_TS_DECL_COMMON (code);
469 break;
471 case TS_DECL_NON_COMMON:
472 MARK_TS_DECL_WITH_VIS (code);
473 break;
475 case TS_DECL_WITH_VIS:
476 case TS_PARM_DECL:
477 case TS_LABEL_DECL:
478 case TS_RESULT_DECL:
479 MARK_TS_DECL_WRTL (code);
480 break;
482 case TS_FIELD_DECL:
483 MARK_TS_DECL_COMMON (code);
484 break;
486 case TS_VAR_DECL:
487 MARK_TS_DECL_WITH_VIS (code);
488 break;
490 case TS_TYPE_DECL:
491 case TS_FUNCTION_DECL:
492 MARK_TS_DECL_NON_COMMON (code);
493 break;
495 case TS_TRANSLATION_UNIT_DECL:
496 MARK_TS_DECL_COMMON (code);
497 break;
499 default:
500 gcc_unreachable ();
504 /* Basic consistency checks for attributes used in fold. */
505 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_NON_COMMON]);
506 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_NON_COMMON]);
507 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_COMMON]);
508 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_COMMON]);
509 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_COMMON]);
510 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_COMMON]);
511 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_COMMON]);
512 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_COMMON]);
513 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_COMMON]);
514 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_COMMON]);
515 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_COMMON]);
516 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WRTL]);
517 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_WRTL]);
518 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_WRTL]);
519 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WRTL]);
520 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_WRTL]);
521 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_MINIMAL]);
522 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_MINIMAL]);
523 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_MINIMAL]);
524 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_MINIMAL]);
525 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_MINIMAL]);
526 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_MINIMAL]);
527 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_MINIMAL]);
528 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_MINIMAL]);
529 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_MINIMAL]);
530 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WITH_VIS]);
531 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WITH_VIS]);
532 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_WITH_VIS]);
533 gcc_assert (tree_contains_struct[VAR_DECL][TS_VAR_DECL]);
534 gcc_assert (tree_contains_struct[FIELD_DECL][TS_FIELD_DECL]);
535 gcc_assert (tree_contains_struct[PARM_DECL][TS_PARM_DECL]);
536 gcc_assert (tree_contains_struct[LABEL_DECL][TS_LABEL_DECL]);
537 gcc_assert (tree_contains_struct[RESULT_DECL][TS_RESULT_DECL]);
538 gcc_assert (tree_contains_struct[CONST_DECL][TS_CONST_DECL]);
539 gcc_assert (tree_contains_struct[TYPE_DECL][TS_TYPE_DECL]);
540 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_FUNCTION_DECL]);
541 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_MINIMAL]);
542 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_COMMON]);
546 /* Init tree.c. */
548 void
549 init_ttree (void)
551 /* Initialize the hash table of types. */
552 type_hash_table = htab_create_ggc (TYPE_HASH_INITIAL_SIZE, type_hash_hash,
553 type_hash_eq, 0);
555 debug_expr_for_decl = htab_create_ggc (512, tree_decl_map_hash,
556 tree_decl_map_eq, 0);
558 value_expr_for_decl = htab_create_ggc (512, tree_decl_map_hash,
559 tree_decl_map_eq, 0);
560 init_priority_for_decl = htab_create_ggc (512, tree_priority_map_hash,
561 tree_priority_map_eq, 0);
563 int_cst_hash_table = htab_create_ggc (1024, int_cst_hash_hash,
564 int_cst_hash_eq, NULL);
566 int_cst_node = make_node (INTEGER_CST);
568 cl_option_hash_table = htab_create_ggc (64, cl_option_hash_hash,
569 cl_option_hash_eq, NULL);
571 cl_optimization_node = make_node (OPTIMIZATION_NODE);
572 cl_target_option_node = make_node (TARGET_OPTION_NODE);
574 /* Initialize the tree_contains_struct array. */
575 initialize_tree_contains_struct ();
576 lang_hooks.init_ts ();
580 /* The name of the object as the assembler will see it (but before any
581 translations made by ASM_OUTPUT_LABELREF). Often this is the same
582 as DECL_NAME. It is an IDENTIFIER_NODE. */
583 tree
584 decl_assembler_name (tree decl)
586 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
587 lang_hooks.set_decl_assembler_name (decl);
588 return DECL_WITH_VIS_CHECK (decl)->decl_with_vis.assembler_name;
591 /* Compute the number of bytes occupied by a tree with code CODE.
592 This function cannot be used for nodes that have variable sizes,
593 including TREE_VEC, STRING_CST, and CALL_EXPR. */
594 size_t
595 tree_code_size (enum tree_code code)
597 switch (TREE_CODE_CLASS (code))
599 case tcc_declaration: /* A decl node */
601 switch (code)
603 case FIELD_DECL:
604 return sizeof (struct tree_field_decl);
605 case PARM_DECL:
606 return sizeof (struct tree_parm_decl);
607 case VAR_DECL:
608 return sizeof (struct tree_var_decl);
609 case LABEL_DECL:
610 return sizeof (struct tree_label_decl);
611 case RESULT_DECL:
612 return sizeof (struct tree_result_decl);
613 case CONST_DECL:
614 return sizeof (struct tree_const_decl);
615 case TYPE_DECL:
616 return sizeof (struct tree_type_decl);
617 case FUNCTION_DECL:
618 return sizeof (struct tree_function_decl);
619 case DEBUG_EXPR_DECL:
620 return sizeof (struct tree_decl_with_rtl);
621 default:
622 return sizeof (struct tree_decl_non_common);
626 case tcc_type: /* a type node */
627 return sizeof (struct tree_type_non_common);
629 case tcc_reference: /* a reference */
630 case tcc_expression: /* an expression */
631 case tcc_statement: /* an expression with side effects */
632 case tcc_comparison: /* a comparison expression */
633 case tcc_unary: /* a unary arithmetic expression */
634 case tcc_binary: /* a binary arithmetic expression */
635 return (sizeof (struct tree_exp)
636 + (TREE_CODE_LENGTH (code) - 1) * sizeof (tree));
638 case tcc_constant: /* a constant */
639 switch (code)
641 case INTEGER_CST: return sizeof (struct tree_int_cst);
642 case REAL_CST: return sizeof (struct tree_real_cst);
643 case FIXED_CST: return sizeof (struct tree_fixed_cst);
644 case COMPLEX_CST: return sizeof (struct tree_complex);
645 case VECTOR_CST: return sizeof (struct tree_vector);
646 case STRING_CST: gcc_unreachable ();
647 default:
648 return lang_hooks.tree_size (code);
651 case tcc_exceptional: /* something random, like an identifier. */
652 switch (code)
654 case IDENTIFIER_NODE: return lang_hooks.identifier_size;
655 case TREE_LIST: return sizeof (struct tree_list);
657 case ERROR_MARK:
658 case PLACEHOLDER_EXPR: return sizeof (struct tree_common);
660 case TREE_VEC:
661 case OMP_CLAUSE: gcc_unreachable ();
663 case SSA_NAME: return sizeof (struct tree_ssa_name);
665 case STATEMENT_LIST: return sizeof (struct tree_statement_list);
666 case BLOCK: return sizeof (struct tree_block);
667 case CONSTRUCTOR: return sizeof (struct tree_constructor);
668 case OPTIMIZATION_NODE: return sizeof (struct tree_optimization_option);
669 case TARGET_OPTION_NODE: return sizeof (struct tree_target_option);
671 default:
672 return lang_hooks.tree_size (code);
675 default:
676 gcc_unreachable ();
680 /* Compute the number of bytes occupied by NODE. This routine only
681 looks at TREE_CODE, except for those nodes that have variable sizes. */
682 size_t
683 tree_size (const_tree node)
685 const enum tree_code code = TREE_CODE (node);
686 switch (code)
688 case TREE_BINFO:
689 return (offsetof (struct tree_binfo, base_binfos)
690 + vec<tree, va_gc>
691 ::embedded_size (BINFO_N_BASE_BINFOS (node)));
693 case TREE_VEC:
694 return (sizeof (struct tree_vec)
695 + (TREE_VEC_LENGTH (node) - 1) * sizeof (tree));
697 case VECTOR_CST:
698 return (sizeof (struct tree_vector)
699 + (TYPE_VECTOR_SUBPARTS (TREE_TYPE (node)) - 1) * sizeof (tree));
701 case STRING_CST:
702 return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
704 case OMP_CLAUSE:
705 return (sizeof (struct tree_omp_clause)
706 + (omp_clause_num_ops[OMP_CLAUSE_CODE (node)] - 1)
707 * sizeof (tree));
709 default:
710 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
711 return (sizeof (struct tree_exp)
712 + (VL_EXP_OPERAND_LENGTH (node) - 1) * sizeof (tree));
713 else
714 return tree_code_size (code);
718 /* Record interesting allocation statistics for a tree node with CODE
719 and LENGTH. */
721 static void
722 record_node_allocation_statistics (enum tree_code code ATTRIBUTE_UNUSED,
723 size_t length ATTRIBUTE_UNUSED)
725 enum tree_code_class type = TREE_CODE_CLASS (code);
726 tree_node_kind kind;
728 if (!GATHER_STATISTICS)
729 return;
731 switch (type)
733 case tcc_declaration: /* A decl node */
734 kind = d_kind;
735 break;
737 case tcc_type: /* a type node */
738 kind = t_kind;
739 break;
741 case tcc_statement: /* an expression with side effects */
742 kind = s_kind;
743 break;
745 case tcc_reference: /* a reference */
746 kind = r_kind;
747 break;
749 case tcc_expression: /* an expression */
750 case tcc_comparison: /* a comparison expression */
751 case tcc_unary: /* a unary arithmetic expression */
752 case tcc_binary: /* a binary arithmetic expression */
753 kind = e_kind;
754 break;
756 case tcc_constant: /* a constant */
757 kind = c_kind;
758 break;
760 case tcc_exceptional: /* something random, like an identifier. */
761 switch (code)
763 case IDENTIFIER_NODE:
764 kind = id_kind;
765 break;
767 case TREE_VEC:
768 kind = vec_kind;
769 break;
771 case TREE_BINFO:
772 kind = binfo_kind;
773 break;
775 case SSA_NAME:
776 kind = ssa_name_kind;
777 break;
779 case BLOCK:
780 kind = b_kind;
781 break;
783 case CONSTRUCTOR:
784 kind = constr_kind;
785 break;
787 case OMP_CLAUSE:
788 kind = omp_clause_kind;
789 break;
791 default:
792 kind = x_kind;
793 break;
795 break;
797 case tcc_vl_exp:
798 kind = e_kind;
799 break;
801 default:
802 gcc_unreachable ();
805 tree_code_counts[(int) code]++;
806 tree_node_counts[(int) kind]++;
807 tree_node_sizes[(int) kind] += length;
810 /* Allocate and return a new UID from the DECL_UID namespace. */
813 allocate_decl_uid (void)
815 return next_decl_uid++;
818 /* Return a newly allocated node of code CODE. For decl and type
819 nodes, some other fields are initialized. The rest of the node is
820 initialized to zero. This function cannot be used for TREE_VEC or
821 OMP_CLAUSE nodes, which is enforced by asserts in tree_code_size.
823 Achoo! I got a code in the node. */
825 tree
826 make_node_stat (enum tree_code code MEM_STAT_DECL)
828 tree t;
829 enum tree_code_class type = TREE_CODE_CLASS (code);
830 size_t length = tree_code_size (code);
832 record_node_allocation_statistics (code, length);
834 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
835 TREE_SET_CODE (t, code);
837 switch (type)
839 case tcc_statement:
840 TREE_SIDE_EFFECTS (t) = 1;
841 break;
843 case tcc_declaration:
844 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
846 if (code == FUNCTION_DECL)
848 DECL_ALIGN (t) = FUNCTION_BOUNDARY;
849 DECL_MODE (t) = FUNCTION_MODE;
851 else
852 DECL_ALIGN (t) = 1;
854 DECL_SOURCE_LOCATION (t) = input_location;
855 if (TREE_CODE (t) == DEBUG_EXPR_DECL)
856 DECL_UID (t) = --next_debug_decl_uid;
857 else
859 DECL_UID (t) = allocate_decl_uid ();
860 SET_DECL_PT_UID (t, -1);
862 if (TREE_CODE (t) == LABEL_DECL)
863 LABEL_DECL_UID (t) = -1;
865 break;
867 case tcc_type:
868 TYPE_UID (t) = next_type_uid++;
869 TYPE_ALIGN (t) = BITS_PER_UNIT;
870 TYPE_USER_ALIGN (t) = 0;
871 TYPE_MAIN_VARIANT (t) = t;
872 TYPE_CANONICAL (t) = t;
874 /* Default to no attributes for type, but let target change that. */
875 TYPE_ATTRIBUTES (t) = NULL_TREE;
876 targetm.set_default_type_attributes (t);
878 /* We have not yet computed the alias set for this type. */
879 TYPE_ALIAS_SET (t) = -1;
880 break;
882 case tcc_constant:
883 TREE_CONSTANT (t) = 1;
884 break;
886 case tcc_expression:
887 switch (code)
889 case INIT_EXPR:
890 case MODIFY_EXPR:
891 case VA_ARG_EXPR:
892 case PREDECREMENT_EXPR:
893 case PREINCREMENT_EXPR:
894 case POSTDECREMENT_EXPR:
895 case POSTINCREMENT_EXPR:
896 /* All of these have side-effects, no matter what their
897 operands are. */
898 TREE_SIDE_EFFECTS (t) = 1;
899 break;
901 default:
902 break;
904 break;
906 default:
907 /* Other classes need no special treatment. */
908 break;
911 return t;
914 /* Return a new node with the same contents as NODE except that its
915 TREE_CHAIN, if it has one, is zero and it has a fresh uid. */
917 tree
918 copy_node_stat (tree node MEM_STAT_DECL)
920 tree t;
921 enum tree_code code = TREE_CODE (node);
922 size_t length;
924 gcc_assert (code != STATEMENT_LIST);
926 length = tree_size (node);
927 record_node_allocation_statistics (code, length);
928 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
929 memcpy (t, node, length);
931 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
932 TREE_CHAIN (t) = 0;
933 TREE_ASM_WRITTEN (t) = 0;
934 TREE_VISITED (t) = 0;
936 if (TREE_CODE_CLASS (code) == tcc_declaration)
938 if (code == DEBUG_EXPR_DECL)
939 DECL_UID (t) = --next_debug_decl_uid;
940 else
942 DECL_UID (t) = allocate_decl_uid ();
943 if (DECL_PT_UID_SET_P (node))
944 SET_DECL_PT_UID (t, DECL_PT_UID (node));
946 if ((TREE_CODE (node) == PARM_DECL || TREE_CODE (node) == VAR_DECL)
947 && DECL_HAS_VALUE_EXPR_P (node))
949 SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (node));
950 DECL_HAS_VALUE_EXPR_P (t) = 1;
952 /* DECL_DEBUG_EXPR is copied explicitely by callers. */
953 if (TREE_CODE (node) == VAR_DECL)
954 DECL_HAS_DEBUG_EXPR_P (t) = 0;
955 if (TREE_CODE (node) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (node))
957 SET_DECL_INIT_PRIORITY (t, DECL_INIT_PRIORITY (node));
958 DECL_HAS_INIT_PRIORITY_P (t) = 1;
960 if (TREE_CODE (node) == FUNCTION_DECL)
961 DECL_STRUCT_FUNCTION (t) = NULL;
963 else if (TREE_CODE_CLASS (code) == tcc_type)
965 TYPE_UID (t) = next_type_uid++;
966 /* The following is so that the debug code for
967 the copy is different from the original type.
968 The two statements usually duplicate each other
969 (because they clear fields of the same union),
970 but the optimizer should catch that. */
971 TYPE_SYMTAB_POINTER (t) = 0;
972 TYPE_SYMTAB_ADDRESS (t) = 0;
974 /* Do not copy the values cache. */
975 if (TYPE_CACHED_VALUES_P (t))
977 TYPE_CACHED_VALUES_P (t) = 0;
978 TYPE_CACHED_VALUES (t) = NULL_TREE;
981 if (TYPE_HAS_BLOCK_FACTOR (node))
982 SET_TYPE_BLOCK_FACTOR (t, TYPE_BLOCK_FACTOR (node));
985 return t;
988 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
989 For example, this can copy a list made of TREE_LIST nodes. */
991 tree
992 copy_list (tree list)
994 tree head;
995 tree prev, next;
997 if (list == 0)
998 return 0;
1000 head = prev = copy_node (list);
1001 next = TREE_CHAIN (list);
1002 while (next)
1004 TREE_CHAIN (prev) = copy_node (next);
1005 prev = TREE_CHAIN (prev);
1006 next = TREE_CHAIN (next);
1008 return head;
1012 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1014 tree
1015 build_int_cst (tree type, HOST_WIDE_INT low)
1017 /* Support legacy code. */
1018 if (!type)
1019 type = integer_type_node;
1021 return double_int_to_tree (type, double_int::from_shwi (low));
1024 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1026 tree
1027 build_int_cst_type (tree type, HOST_WIDE_INT low)
1029 gcc_assert (type);
1031 return double_int_to_tree (type, double_int::from_shwi (low));
1034 /* Constructs tree in type TYPE from with value given by CST. Signedness
1035 of CST is assumed to be the same as the signedness of TYPE. */
1037 tree
1038 double_int_to_tree (tree type, double_int cst)
1040 bool sign_extended_type = !TYPE_UNSIGNED (type);
1042 cst = cst.ext (TYPE_PRECISION (type), !sign_extended_type);
1044 return build_int_cst_wide (type, cst.low, cst.high);
1047 /* Returns true if CST fits into range of TYPE. Signedness of CST is assumed
1048 to be the same as the signedness of TYPE. */
1050 bool
1051 double_int_fits_to_tree_p (const_tree type, double_int cst)
1053 bool sign_extended_type = !TYPE_UNSIGNED (type);
1055 double_int ext
1056 = cst.ext (TYPE_PRECISION (type), !sign_extended_type);
1058 return cst == ext;
1061 /* We force the double_int CST to the range of the type TYPE by sign or
1062 zero extending it. OVERFLOWABLE indicates if we are interested in
1063 overflow of the value, when >0 we are only interested in signed
1064 overflow, for <0 we are interested in any overflow. OVERFLOWED
1065 indicates whether overflow has already occurred. CONST_OVERFLOWED
1066 indicates whether constant overflow has already occurred. We force
1067 T's value to be within range of T's type (by setting to 0 or 1 all
1068 the bits outside the type's range). We set TREE_OVERFLOWED if,
1069 OVERFLOWED is nonzero,
1070 or OVERFLOWABLE is >0 and signed overflow occurs
1071 or OVERFLOWABLE is <0 and any overflow occurs
1072 We return a new tree node for the extended double_int. The node
1073 is shared if no overflow flags are set. */
1076 tree
1077 force_fit_type_double (tree type, double_int cst, int overflowable,
1078 bool overflowed)
1080 bool sign_extended_type = !TYPE_UNSIGNED (type);
1082 /* If we need to set overflow flags, return a new unshared node. */
1083 if (overflowed || !double_int_fits_to_tree_p (type, cst))
1085 if (overflowed
1086 || overflowable < 0
1087 || (overflowable > 0 && sign_extended_type))
1089 tree t = make_node (INTEGER_CST);
1090 TREE_INT_CST (t)
1091 = cst.ext (TYPE_PRECISION (type), !sign_extended_type);
1092 TREE_TYPE (t) = type;
1093 TREE_OVERFLOW (t) = 1;
1094 return t;
1098 /* Else build a shared node. */
1099 return double_int_to_tree (type, cst);
1102 /* These are the hash table functions for the hash table of INTEGER_CST
1103 nodes of a sizetype. */
1105 /* Return the hash code code X, an INTEGER_CST. */
1107 static hashval_t
1108 int_cst_hash_hash (const void *x)
1110 const_tree const t = (const_tree) x;
1112 return (TREE_INT_CST_HIGH (t) ^ TREE_INT_CST_LOW (t)
1113 ^ htab_hash_pointer (TREE_TYPE (t)));
1116 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
1117 is the same as that given by *Y, which is the same. */
1119 static int
1120 int_cst_hash_eq (const void *x, const void *y)
1122 const_tree const xt = (const_tree) x;
1123 const_tree const yt = (const_tree) y;
1125 return (TREE_TYPE (xt) == TREE_TYPE (yt)
1126 && TREE_INT_CST_HIGH (xt) == TREE_INT_CST_HIGH (yt)
1127 && TREE_INT_CST_LOW (xt) == TREE_INT_CST_LOW (yt));
1130 /* Create an INT_CST node of TYPE and value HI:LOW.
1131 The returned node is always shared. For small integers we use a
1132 per-type vector cache, for larger ones we use a single hash table. */
1134 tree
1135 build_int_cst_wide (tree type, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
1137 tree t;
1138 int ix = -1;
1139 int limit = 0;
1141 gcc_assert (type);
1143 switch (TREE_CODE (type))
1145 case NULLPTR_TYPE:
1146 gcc_assert (hi == 0 && low == 0);
1147 /* Fallthru. */
1149 case POINTER_TYPE:
1150 case REFERENCE_TYPE:
1151 case POINTER_BOUNDS_TYPE:
1152 /* Cache NULL pointer and zero bounds. */
1153 if (!hi && !low)
1155 limit = 1;
1156 ix = 0;
1158 break;
1160 case BOOLEAN_TYPE:
1161 /* Cache false or true. */
1162 limit = 2;
1163 if (!hi && low < 2)
1164 ix = low;
1165 break;
1167 case INTEGER_TYPE:
1168 case OFFSET_TYPE:
1169 if (TYPE_UNSIGNED (type))
1171 /* Cache 0..N */
1172 limit = INTEGER_SHARE_LIMIT;
1173 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
1174 ix = low;
1176 else
1178 /* Cache -1..N */
1179 limit = INTEGER_SHARE_LIMIT + 1;
1180 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
1181 ix = low + 1;
1182 else if (hi == -1 && low == -(unsigned HOST_WIDE_INT)1)
1183 ix = 0;
1185 break;
1187 case ENUMERAL_TYPE:
1188 break;
1190 default:
1191 gcc_unreachable ();
1194 if (ix >= 0)
1196 /* Look for it in the type's vector of small shared ints. */
1197 if (!TYPE_CACHED_VALUES_P (type))
1199 TYPE_CACHED_VALUES_P (type) = 1;
1200 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1203 t = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix);
1204 if (t)
1206 /* Make sure no one is clobbering the shared constant. */
1207 gcc_assert (TREE_TYPE (t) == type);
1208 gcc_assert (TREE_INT_CST_LOW (t) == low);
1209 gcc_assert (TREE_INT_CST_HIGH (t) == hi);
1211 else
1213 /* Create a new shared int. */
1214 t = make_node (INTEGER_CST);
1216 TREE_INT_CST_LOW (t) = low;
1217 TREE_INT_CST_HIGH (t) = hi;
1218 TREE_TYPE (t) = type;
1220 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1223 else
1225 /* Use the cache of larger shared ints. */
1226 void **slot;
1228 TREE_INT_CST_LOW (int_cst_node) = low;
1229 TREE_INT_CST_HIGH (int_cst_node) = hi;
1230 TREE_TYPE (int_cst_node) = type;
1232 slot = htab_find_slot (int_cst_hash_table, int_cst_node, INSERT);
1233 t = (tree) *slot;
1234 if (!t)
1236 /* Insert this one into the hash table. */
1237 t = int_cst_node;
1238 *slot = t;
1239 /* Make a new node for next time round. */
1240 int_cst_node = make_node (INTEGER_CST);
1244 return t;
1247 void
1248 cache_integer_cst (tree t)
1250 tree type = TREE_TYPE (t);
1251 HOST_WIDE_INT hi = TREE_INT_CST_HIGH (t);
1252 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (t);
1253 int ix = -1;
1254 int limit = 0;
1256 gcc_assert (!TREE_OVERFLOW (t));
1258 switch (TREE_CODE (type))
1260 case NULLPTR_TYPE:
1261 gcc_assert (hi == 0 && low == 0);
1262 /* Fallthru. */
1264 case POINTER_TYPE:
1265 case REFERENCE_TYPE:
1266 /* Cache NULL pointer. */
1267 if (!hi && !low)
1269 limit = 1;
1270 ix = 0;
1272 break;
1274 case BOOLEAN_TYPE:
1275 /* Cache false or true. */
1276 limit = 2;
1277 if (!hi && low < 2)
1278 ix = low;
1279 break;
1281 case INTEGER_TYPE:
1282 case OFFSET_TYPE:
1283 if (TYPE_UNSIGNED (type))
1285 /* Cache 0..N */
1286 limit = INTEGER_SHARE_LIMIT;
1287 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
1288 ix = low;
1290 else
1292 /* Cache -1..N */
1293 limit = INTEGER_SHARE_LIMIT + 1;
1294 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
1295 ix = low + 1;
1296 else if (hi == -1 && low == -(unsigned HOST_WIDE_INT)1)
1297 ix = 0;
1299 break;
1301 case ENUMERAL_TYPE:
1302 break;
1304 default:
1305 gcc_unreachable ();
1308 if (ix >= 0)
1310 /* Look for it in the type's vector of small shared ints. */
1311 if (!TYPE_CACHED_VALUES_P (type))
1313 TYPE_CACHED_VALUES_P (type) = 1;
1314 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1317 gcc_assert (TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) == NULL_TREE);
1318 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1320 else
1322 /* Use the cache of larger shared ints. */
1323 void **slot;
1325 slot = htab_find_slot (int_cst_hash_table, t, INSERT);
1326 /* If there is already an entry for the number verify it's the
1327 same. */
1328 if (*slot)
1330 gcc_assert (TREE_INT_CST_LOW ((tree)*slot) == low
1331 && TREE_INT_CST_HIGH ((tree)*slot) == hi);
1332 return;
1334 /* Otherwise insert this one into the hash table. */
1335 *slot = t;
1340 /* Builds an integer constant in TYPE such that lowest BITS bits are ones
1341 and the rest are zeros. */
1343 tree
1344 build_low_bits_mask (tree type, unsigned bits)
1346 double_int mask;
1348 gcc_assert (bits <= TYPE_PRECISION (type));
1350 if (bits == TYPE_PRECISION (type)
1351 && !TYPE_UNSIGNED (type))
1352 /* Sign extended all-ones mask. */
1353 mask = double_int_minus_one;
1354 else
1355 mask = double_int::mask (bits);
1357 return build_int_cst_wide (type, mask.low, mask.high);
1360 /* Checks that X is integer constant that can be expressed in (unsigned)
1361 HOST_WIDE_INT without loss of precision. */
1363 bool
1364 cst_and_fits_in_hwi (const_tree x)
1366 if (TREE_CODE (x) != INTEGER_CST)
1367 return false;
1369 if (TYPE_PRECISION (TREE_TYPE (x)) > HOST_BITS_PER_WIDE_INT)
1370 return false;
1372 return (TREE_INT_CST_HIGH (x) == 0
1373 || TREE_INT_CST_HIGH (x) == -1);
1376 /* Build a newly constructed TREE_VEC node of length LEN. */
1378 tree
1379 make_vector_stat (unsigned len MEM_STAT_DECL)
1381 tree t;
1382 unsigned length = (len - 1) * sizeof (tree) + sizeof (struct tree_vector);
1384 record_node_allocation_statistics (VECTOR_CST, length);
1386 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1388 TREE_SET_CODE (t, VECTOR_CST);
1389 TREE_CONSTANT (t) = 1;
1391 return t;
1394 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1395 are in a list pointed to by VALS. */
1397 tree
1398 build_vector_stat (tree type, tree *vals MEM_STAT_DECL)
1400 int over = 0;
1401 unsigned cnt = 0;
1402 tree v = make_vector (TYPE_VECTOR_SUBPARTS (type));
1403 TREE_TYPE (v) = type;
1405 /* Iterate through elements and check for overflow. */
1406 for (cnt = 0; cnt < TYPE_VECTOR_SUBPARTS (type); ++cnt)
1408 tree value = vals[cnt];
1410 VECTOR_CST_ELT (v, cnt) = value;
1412 /* Don't crash if we get an address constant. */
1413 if (!CONSTANT_CLASS_P (value))
1414 continue;
1416 over |= TREE_OVERFLOW (value);
1419 TREE_OVERFLOW (v) = over;
1420 return v;
1423 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1424 are extracted from V, a vector of CONSTRUCTOR_ELT. */
1426 tree
1427 build_vector_from_ctor (tree type, vec<constructor_elt, va_gc> *v)
1429 tree *vec = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
1430 unsigned HOST_WIDE_INT idx;
1431 tree value;
1433 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1434 vec[idx] = value;
1435 for (; idx < TYPE_VECTOR_SUBPARTS (type); ++idx)
1436 vec[idx] = build_zero_cst (TREE_TYPE (type));
1438 return build_vector (type, vec);
1441 /* Build a vector of type VECTYPE where all the elements are SCs. */
1442 tree
1443 build_vector_from_val (tree vectype, tree sc)
1445 int i, nunits = TYPE_VECTOR_SUBPARTS (vectype);
1447 if (sc == error_mark_node)
1448 return sc;
1450 /* Verify that the vector type is suitable for SC. Note that there
1451 is some inconsistency in the type-system with respect to restrict
1452 qualifications of pointers. Vector types always have a main-variant
1453 element type and the qualification is applied to the vector-type.
1454 So TREE_TYPE (vector-type) does not return a properly qualified
1455 vector element-type. */
1456 gcc_checking_assert (types_compatible_p (TYPE_MAIN_VARIANT (TREE_TYPE (sc)),
1457 TREE_TYPE (vectype)));
1459 if (CONSTANT_CLASS_P (sc))
1461 tree *v = XALLOCAVEC (tree, nunits);
1462 for (i = 0; i < nunits; ++i)
1463 v[i] = sc;
1464 return build_vector (vectype, v);
1466 else
1468 vec<constructor_elt, va_gc> *v;
1469 vec_alloc (v, nunits);
1470 for (i = 0; i < nunits; ++i)
1471 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, sc);
1472 return build_constructor (vectype, v);
1476 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1477 are in the vec pointed to by VALS. */
1478 tree
1479 build_constructor (tree type, vec<constructor_elt, va_gc> *vals)
1481 tree c = make_node (CONSTRUCTOR);
1482 unsigned int i;
1483 constructor_elt *elt;
1484 bool constant_p = true;
1485 bool side_effects_p = false;
1487 TREE_TYPE (c) = type;
1488 CONSTRUCTOR_ELTS (c) = vals;
1490 FOR_EACH_VEC_SAFE_ELT (vals, i, elt)
1492 /* Mostly ctors will have elts that don't have side-effects, so
1493 the usual case is to scan all the elements. Hence a single
1494 loop for both const and side effects, rather than one loop
1495 each (with early outs). */
1496 if (!TREE_CONSTANT (elt->value))
1497 constant_p = false;
1498 if (TREE_SIDE_EFFECTS (elt->value))
1499 side_effects_p = true;
1502 TREE_SIDE_EFFECTS (c) = side_effects_p;
1503 TREE_CONSTANT (c) = constant_p;
1505 return c;
1508 /* Build a CONSTRUCTOR node made of a single initializer, with the specified
1509 INDEX and VALUE. */
1510 tree
1511 build_constructor_single (tree type, tree index, tree value)
1513 vec<constructor_elt, va_gc> *v;
1514 constructor_elt elt = {index, value};
1516 vec_alloc (v, 1);
1517 v->quick_push (elt);
1519 return build_constructor (type, v);
1523 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1524 are in a list pointed to by VALS. */
1525 tree
1526 build_constructor_from_list (tree type, tree vals)
1528 tree t;
1529 vec<constructor_elt, va_gc> *v = NULL;
1531 if (vals)
1533 vec_alloc (v, list_length (vals));
1534 for (t = vals; t; t = TREE_CHAIN (t))
1535 CONSTRUCTOR_APPEND_ELT (v, TREE_PURPOSE (t), TREE_VALUE (t));
1538 return build_constructor (type, v);
1541 /* Return a new CONSTRUCTOR node whose type is TYPE. NELTS is the number
1542 of elements, provided as index/value pairs. */
1544 tree
1545 build_constructor_va (tree type, int nelts, ...)
1547 vec<constructor_elt, va_gc> *v = NULL;
1548 va_list p;
1550 va_start (p, nelts);
1551 vec_alloc (v, nelts);
1552 while (nelts--)
1554 tree index = va_arg (p, tree);
1555 tree value = va_arg (p, tree);
1556 CONSTRUCTOR_APPEND_ELT (v, index, value);
1558 va_end (p);
1559 return build_constructor (type, v);
1562 /* Return a new FIXED_CST node whose type is TYPE and value is F. */
1564 tree
1565 build_fixed (tree type, FIXED_VALUE_TYPE f)
1567 tree v;
1568 FIXED_VALUE_TYPE *fp;
1570 v = make_node (FIXED_CST);
1571 fp = ggc_alloc_fixed_value ();
1572 memcpy (fp, &f, sizeof (FIXED_VALUE_TYPE));
1574 TREE_TYPE (v) = type;
1575 TREE_FIXED_CST_PTR (v) = fp;
1576 return v;
1579 /* Return a new REAL_CST node whose type is TYPE and value is D. */
1581 tree
1582 build_real (tree type, REAL_VALUE_TYPE d)
1584 tree v;
1585 REAL_VALUE_TYPE *dp;
1586 int overflow = 0;
1588 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
1589 Consider doing it via real_convert now. */
1591 v = make_node (REAL_CST);
1592 dp = ggc_alloc_real_value ();
1593 memcpy (dp, &d, sizeof (REAL_VALUE_TYPE));
1595 TREE_TYPE (v) = type;
1596 TREE_REAL_CST_PTR (v) = dp;
1597 TREE_OVERFLOW (v) = overflow;
1598 return v;
1601 /* Return a new REAL_CST node whose type is TYPE
1602 and whose value is the integer value of the INTEGER_CST node I. */
1604 REAL_VALUE_TYPE
1605 real_value_from_int_cst (const_tree type, const_tree i)
1607 REAL_VALUE_TYPE d;
1609 /* Clear all bits of the real value type so that we can later do
1610 bitwise comparisons to see if two values are the same. */
1611 memset (&d, 0, sizeof d);
1613 real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode,
1614 TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i),
1615 TYPE_UNSIGNED (TREE_TYPE (i)));
1616 return d;
1619 /* Given a tree representing an integer constant I, return a tree
1620 representing the same value as a floating-point constant of type TYPE. */
1622 tree
1623 build_real_from_int_cst (tree type, const_tree i)
1625 tree v;
1626 int overflow = TREE_OVERFLOW (i);
1628 v = build_real (type, real_value_from_int_cst (type, i));
1630 TREE_OVERFLOW (v) |= overflow;
1631 return v;
1634 /* Return a newly constructed STRING_CST node whose value is
1635 the LEN characters at STR.
1636 Note that for a C string literal, LEN should include the trailing NUL.
1637 The TREE_TYPE is not initialized. */
1639 tree
1640 build_string (int len, const char *str)
1642 tree s;
1643 size_t length;
1645 /* Do not waste bytes provided by padding of struct tree_string. */
1646 length = len + offsetof (struct tree_string, str) + 1;
1648 record_node_allocation_statistics (STRING_CST, length);
1650 s = ggc_alloc_tree_node (length);
1652 memset (s, 0, sizeof (struct tree_typed));
1653 TREE_SET_CODE (s, STRING_CST);
1654 TREE_CONSTANT (s) = 1;
1655 TREE_STRING_LENGTH (s) = len;
1656 memcpy (s->string.str, str, len);
1657 s->string.str[len] = '\0';
1659 return s;
1662 /* Return a newly constructed COMPLEX_CST node whose value is
1663 specified by the real and imaginary parts REAL and IMAG.
1664 Both REAL and IMAG should be constant nodes. TYPE, if specified,
1665 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
1667 tree
1668 build_complex (tree type, tree real, tree imag)
1670 tree t = make_node (COMPLEX_CST);
1672 TREE_REALPART (t) = real;
1673 TREE_IMAGPART (t) = imag;
1674 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
1675 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
1676 return t;
1679 /* Return a constant of arithmetic type TYPE which is the
1680 multiplicative identity of the set TYPE. */
1682 tree
1683 build_one_cst (tree type)
1685 switch (TREE_CODE (type))
1687 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1688 case POINTER_TYPE: case REFERENCE_TYPE:
1689 case OFFSET_TYPE:
1690 return build_int_cst (type, 1);
1692 case REAL_TYPE:
1693 return build_real (type, dconst1);
1695 case FIXED_POINT_TYPE:
1696 /* We can only generate 1 for accum types. */
1697 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
1698 return build_fixed (type, FCONST1 (TYPE_MODE (type)));
1700 case VECTOR_TYPE:
1702 tree scalar = build_one_cst (TREE_TYPE (type));
1704 return build_vector_from_val (type, scalar);
1707 case COMPLEX_TYPE:
1708 return build_complex (type,
1709 build_one_cst (TREE_TYPE (type)),
1710 build_zero_cst (TREE_TYPE (type)));
1712 default:
1713 gcc_unreachable ();
1717 /* Return an integer of type TYPE containing all 1's in as much precision as
1718 it contains, or a complex or vector whose subparts are such integers. */
1720 tree
1721 build_all_ones_cst (tree type)
1723 if (TREE_CODE (type) == COMPLEX_TYPE)
1725 tree scalar = build_all_ones_cst (TREE_TYPE (type));
1726 return build_complex (type, scalar, scalar);
1728 else
1729 return build_minus_one_cst (type);
1732 /* Return a constant of arithmetic type TYPE which is the
1733 opposite of the multiplicative identity of the set TYPE. */
1735 tree
1736 build_minus_one_cst (tree type)
1738 switch (TREE_CODE (type))
1740 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1741 case POINTER_TYPE: case REFERENCE_TYPE:
1742 case OFFSET_TYPE:
1743 return build_int_cst (type, -1);
1745 case REAL_TYPE:
1746 return build_real (type, dconstm1);
1748 case FIXED_POINT_TYPE:
1749 /* We can only generate 1 for accum types. */
1750 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
1751 return build_fixed (type, fixed_from_double_int (double_int_minus_one,
1752 TYPE_MODE (type)));
1754 case VECTOR_TYPE:
1756 tree scalar = build_minus_one_cst (TREE_TYPE (type));
1758 return build_vector_from_val (type, scalar);
1761 case COMPLEX_TYPE:
1762 return build_complex (type,
1763 build_minus_one_cst (TREE_TYPE (type)),
1764 build_zero_cst (TREE_TYPE (type)));
1766 default:
1767 gcc_unreachable ();
1771 /* Build 0 constant of type TYPE. This is used by constructor folding
1772 and thus the constant should be represented in memory by
1773 zero(es). */
1775 tree
1776 build_zero_cst (tree type)
1778 switch (TREE_CODE (type))
1780 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1781 case POINTER_TYPE: case REFERENCE_TYPE:
1782 case OFFSET_TYPE: case NULLPTR_TYPE:
1783 return build_int_cst (type, 0);
1785 case REAL_TYPE:
1786 return build_real (type, dconst0);
1788 case FIXED_POINT_TYPE:
1789 return build_fixed (type, FCONST0 (TYPE_MODE (type)));
1791 case VECTOR_TYPE:
1793 tree scalar = build_zero_cst (TREE_TYPE (type));
1795 return build_vector_from_val (type, scalar);
1798 case COMPLEX_TYPE:
1800 tree zero = build_zero_cst (TREE_TYPE (type));
1802 return build_complex (type, zero, zero);
1805 default:
1806 if (!AGGREGATE_TYPE_P (type))
1807 return fold_convert (type, integer_zero_node);
1808 return build_constructor (type, NULL);
1813 /* Build a BINFO with LEN language slots. */
1815 tree
1816 make_tree_binfo_stat (unsigned base_binfos MEM_STAT_DECL)
1818 tree t;
1819 size_t length = (offsetof (struct tree_binfo, base_binfos)
1820 + vec<tree, va_gc>::embedded_size (base_binfos));
1822 record_node_allocation_statistics (TREE_BINFO, length);
1824 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
1826 memset (t, 0, offsetof (struct tree_binfo, base_binfos));
1828 TREE_SET_CODE (t, TREE_BINFO);
1830 BINFO_BASE_BINFOS (t)->embedded_init (base_binfos);
1832 return t;
1835 /* Create a CASE_LABEL_EXPR tree node and return it. */
1837 tree
1838 build_case_label (tree low_value, tree high_value, tree label_decl)
1840 tree t = make_node (CASE_LABEL_EXPR);
1842 TREE_TYPE (t) = void_type_node;
1843 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (label_decl));
1845 CASE_LOW (t) = low_value;
1846 CASE_HIGH (t) = high_value;
1847 CASE_LABEL (t) = label_decl;
1848 CASE_CHAIN (t) = NULL_TREE;
1850 return t;
1853 /* Build a newly constructed TREE_VEC node of length LEN. */
1855 tree
1856 make_tree_vec_stat (int len MEM_STAT_DECL)
1858 tree t;
1859 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
1861 record_node_allocation_statistics (TREE_VEC, length);
1863 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1865 TREE_SET_CODE (t, TREE_VEC);
1866 TREE_VEC_LENGTH (t) = len;
1868 return t;
1871 /* Return 1 if EXPR is the integer constant zero or a complex constant
1872 of zero. */
1875 integer_zerop (const_tree expr)
1877 STRIP_NOPS (expr);
1879 switch (TREE_CODE (expr))
1881 case INTEGER_CST:
1882 return (TREE_INT_CST_LOW (expr) == 0
1883 && TREE_INT_CST_HIGH (expr) == 0);
1884 case COMPLEX_CST:
1885 return (integer_zerop (TREE_REALPART (expr))
1886 && integer_zerop (TREE_IMAGPART (expr)));
1887 case VECTOR_CST:
1889 unsigned i;
1890 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
1891 if (!integer_zerop (VECTOR_CST_ELT (expr, i)))
1892 return false;
1893 return true;
1895 default:
1896 return false;
1900 /* Return 1 if EXPR is the integer constant one or the corresponding
1901 complex constant. */
1904 integer_onep (const_tree expr)
1906 STRIP_NOPS (expr);
1908 switch (TREE_CODE (expr))
1910 case INTEGER_CST:
1911 return (TREE_INT_CST_LOW (expr) == 1
1912 && TREE_INT_CST_HIGH (expr) == 0);
1913 case COMPLEX_CST:
1914 return (integer_onep (TREE_REALPART (expr))
1915 && integer_zerop (TREE_IMAGPART (expr)));
1916 case VECTOR_CST:
1918 unsigned i;
1919 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
1920 if (!integer_onep (VECTOR_CST_ELT (expr, i)))
1921 return false;
1922 return true;
1924 default:
1925 return false;
1929 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
1930 it contains, or a complex or vector whose subparts are such integers. */
1933 integer_all_onesp (const_tree expr)
1935 int prec;
1936 int uns;
1938 STRIP_NOPS (expr);
1940 if (TREE_CODE (expr) == COMPLEX_CST
1941 && integer_all_onesp (TREE_REALPART (expr))
1942 && integer_all_onesp (TREE_IMAGPART (expr)))
1943 return 1;
1945 else if (TREE_CODE (expr) == VECTOR_CST)
1947 unsigned i;
1948 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
1949 if (!integer_all_onesp (VECTOR_CST_ELT (expr, i)))
1950 return 0;
1951 return 1;
1954 else if (TREE_CODE (expr) != INTEGER_CST)
1955 return 0;
1957 uns = TYPE_UNSIGNED (TREE_TYPE (expr));
1958 if (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
1959 && TREE_INT_CST_HIGH (expr) == -1)
1960 return 1;
1961 if (!uns)
1962 return 0;
1964 prec = TYPE_PRECISION (TREE_TYPE (expr));
1965 if (prec >= HOST_BITS_PER_WIDE_INT)
1967 HOST_WIDE_INT high_value;
1968 int shift_amount;
1970 shift_amount = prec - HOST_BITS_PER_WIDE_INT;
1972 /* Can not handle precisions greater than twice the host int size. */
1973 gcc_assert (shift_amount <= HOST_BITS_PER_WIDE_INT);
1974 if (shift_amount == HOST_BITS_PER_WIDE_INT)
1975 /* Shifting by the host word size is undefined according to the ANSI
1976 standard, so we must handle this as a special case. */
1977 high_value = -1;
1978 else
1979 high_value = ((HOST_WIDE_INT) 1 << shift_amount) - 1;
1981 return (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
1982 && TREE_INT_CST_HIGH (expr) == high_value);
1984 else
1985 return TREE_INT_CST_LOW (expr) == ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
1988 /* Return 1 if EXPR is the integer constant minus one. */
1991 integer_minus_onep (const_tree expr)
1993 STRIP_NOPS (expr);
1995 if (TREE_CODE (expr) == COMPLEX_CST)
1996 return (integer_all_onesp (TREE_REALPART (expr))
1997 && integer_zerop (TREE_IMAGPART (expr)));
1998 else
1999 return integer_all_onesp (expr);
2002 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
2003 one bit on). */
2006 integer_pow2p (const_tree expr)
2008 int prec;
2009 unsigned HOST_WIDE_INT high, low;
2011 STRIP_NOPS (expr);
2013 if (TREE_CODE (expr) == COMPLEX_CST
2014 && integer_pow2p (TREE_REALPART (expr))
2015 && integer_zerop (TREE_IMAGPART (expr)))
2016 return 1;
2018 if (TREE_CODE (expr) != INTEGER_CST)
2019 return 0;
2021 prec = TYPE_PRECISION (TREE_TYPE (expr));
2022 high = TREE_INT_CST_HIGH (expr);
2023 low = TREE_INT_CST_LOW (expr);
2025 /* First clear all bits that are beyond the type's precision in case
2026 we've been sign extended. */
2028 if (prec == HOST_BITS_PER_DOUBLE_INT)
2030 else if (prec > HOST_BITS_PER_WIDE_INT)
2031 high &= ~(HOST_WIDE_INT_M1U << (prec - HOST_BITS_PER_WIDE_INT));
2032 else
2034 high = 0;
2035 if (prec < HOST_BITS_PER_WIDE_INT)
2036 low &= ~(HOST_WIDE_INT_M1U << prec);
2039 if (high == 0 && low == 0)
2040 return 0;
2042 return ((high == 0 && (low & (low - 1)) == 0)
2043 || (low == 0 && (high & (high - 1)) == 0));
2046 /* Return 1 if EXPR is an integer constant other than zero or a
2047 complex constant other than zero. */
2050 integer_nonzerop (const_tree expr)
2052 STRIP_NOPS (expr);
2054 return ((TREE_CODE (expr) == INTEGER_CST
2055 && (TREE_INT_CST_LOW (expr) != 0
2056 || TREE_INT_CST_HIGH (expr) != 0))
2057 || (TREE_CODE (expr) == COMPLEX_CST
2058 && (integer_nonzerop (TREE_REALPART (expr))
2059 || integer_nonzerop (TREE_IMAGPART (expr)))));
2062 /* Return 1 if EXPR is the fixed-point constant zero. */
2065 fixed_zerop (const_tree expr)
2067 return (TREE_CODE (expr) == FIXED_CST
2068 && TREE_FIXED_CST (expr).data.is_zero ());
2071 /* Return the power of two represented by a tree node known to be a
2072 power of two. */
2075 tree_log2 (const_tree expr)
2077 int prec;
2078 HOST_WIDE_INT high, low;
2080 STRIP_NOPS (expr);
2082 if (TREE_CODE (expr) == COMPLEX_CST)
2083 return tree_log2 (TREE_REALPART (expr));
2085 prec = TYPE_PRECISION (TREE_TYPE (expr));
2086 high = TREE_INT_CST_HIGH (expr);
2087 low = TREE_INT_CST_LOW (expr);
2089 /* First clear all bits that are beyond the type's precision in case
2090 we've been sign extended. */
2092 if (prec == HOST_BITS_PER_DOUBLE_INT)
2094 else if (prec > HOST_BITS_PER_WIDE_INT)
2095 high &= ~(HOST_WIDE_INT_M1U << (prec - HOST_BITS_PER_WIDE_INT));
2096 else
2098 high = 0;
2099 if (prec < HOST_BITS_PER_WIDE_INT)
2100 low &= ~(HOST_WIDE_INT_M1U << prec);
2103 return (high != 0 ? HOST_BITS_PER_WIDE_INT + exact_log2 (high)
2104 : exact_log2 (low));
2107 /* Similar, but return the largest integer Y such that 2 ** Y is less
2108 than or equal to EXPR. */
2111 tree_floor_log2 (const_tree expr)
2113 int prec;
2114 HOST_WIDE_INT high, low;
2116 STRIP_NOPS (expr);
2118 if (TREE_CODE (expr) == COMPLEX_CST)
2119 return tree_log2 (TREE_REALPART (expr));
2121 prec = TYPE_PRECISION (TREE_TYPE (expr));
2122 high = TREE_INT_CST_HIGH (expr);
2123 low = TREE_INT_CST_LOW (expr);
2125 /* First clear all bits that are beyond the type's precision in case
2126 we've been sign extended. Ignore if type's precision hasn't been set
2127 since what we are doing is setting it. */
2129 if (prec == HOST_BITS_PER_DOUBLE_INT || prec == 0)
2131 else if (prec > HOST_BITS_PER_WIDE_INT)
2132 high &= ~(HOST_WIDE_INT_M1U << (prec - HOST_BITS_PER_WIDE_INT));
2133 else
2135 high = 0;
2136 if (prec < HOST_BITS_PER_WIDE_INT)
2137 low &= ~(HOST_WIDE_INT_M1U << prec);
2140 return (high != 0 ? HOST_BITS_PER_WIDE_INT + floor_log2 (high)
2141 : floor_log2 (low));
2144 /* Return number of known trailing zero bits in EXPR, or, if the value of
2145 EXPR is known to be zero, the precision of it's type. */
2147 unsigned int
2148 tree_ctz (const_tree expr)
2150 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
2151 && !POINTER_TYPE_P (TREE_TYPE (expr)))
2152 return 0;
2154 unsigned int ret1, ret2, prec = TYPE_PRECISION (TREE_TYPE (expr));
2155 switch (TREE_CODE (expr))
2157 case INTEGER_CST:
2158 ret1 = tree_to_double_int (expr).trailing_zeros ();
2159 return MIN (ret1, prec);
2160 case SSA_NAME:
2161 ret1 = get_nonzero_bits (expr).trailing_zeros ();
2162 return MIN (ret1, prec);
2163 case PLUS_EXPR:
2164 case MINUS_EXPR:
2165 case BIT_IOR_EXPR:
2166 case BIT_XOR_EXPR:
2167 case MIN_EXPR:
2168 case MAX_EXPR:
2169 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2170 if (ret1 == 0)
2171 return ret1;
2172 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2173 return MIN (ret1, ret2);
2174 case POINTER_PLUS_EXPR:
2175 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2176 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2177 /* Second operand is sizetype, which could be in theory
2178 wider than pointer's precision. Make sure we never
2179 return more than prec. */
2180 ret2 = MIN (ret2, prec);
2181 return MIN (ret1, ret2);
2182 case BIT_AND_EXPR:
2183 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2184 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2185 return MAX (ret1, ret2);
2186 case MULT_EXPR:
2187 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2188 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2189 return MIN (ret1 + ret2, prec);
2190 case LSHIFT_EXPR:
2191 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2192 if (host_integerp (TREE_OPERAND (expr, 1), 1)
2193 && ((unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (expr, 1), 1)
2194 < (unsigned HOST_WIDE_INT) prec))
2196 ret2 = tree_low_cst (TREE_OPERAND (expr, 1), 1);
2197 return MIN (ret1 + ret2, prec);
2199 return ret1;
2200 case RSHIFT_EXPR:
2201 if (host_integerp (TREE_OPERAND (expr, 1), 1)
2202 && ((unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (expr, 1), 1)
2203 < (unsigned HOST_WIDE_INT) prec))
2205 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2206 ret2 = tree_low_cst (TREE_OPERAND (expr, 1), 1);
2207 if (ret1 > ret2)
2208 return ret1 - ret2;
2210 return 0;
2211 case TRUNC_DIV_EXPR:
2212 case CEIL_DIV_EXPR:
2213 case FLOOR_DIV_EXPR:
2214 case ROUND_DIV_EXPR:
2215 case EXACT_DIV_EXPR:
2216 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
2217 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) == 1)
2219 int l = tree_log2 (TREE_OPERAND (expr, 1));
2220 if (l >= 0)
2222 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2223 ret2 = l;
2224 if (ret1 > ret2)
2225 return ret1 - ret2;
2228 return 0;
2229 CASE_CONVERT:
2230 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2231 if (ret1 && ret1 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2232 ret1 = prec;
2233 return MIN (ret1, prec);
2234 case SAVE_EXPR:
2235 return tree_ctz (TREE_OPERAND (expr, 0));
2236 case COND_EXPR:
2237 ret1 = tree_ctz (TREE_OPERAND (expr, 1));
2238 if (ret1 == 0)
2239 return 0;
2240 ret2 = tree_ctz (TREE_OPERAND (expr, 2));
2241 return MIN (ret1, ret2);
2242 case COMPOUND_EXPR:
2243 return tree_ctz (TREE_OPERAND (expr, 1));
2244 case ADDR_EXPR:
2245 ret1 = get_pointer_alignment (CONST_CAST_TREE (expr));
2246 if (ret1 > BITS_PER_UNIT)
2248 ret1 = ctz_hwi (ret1 / BITS_PER_UNIT);
2249 return MIN (ret1, prec);
2251 return 0;
2252 default:
2253 return 0;
2257 /* Return 1 if EXPR is the real constant zero. Trailing zeroes matter for
2258 decimal float constants, so don't return 1 for them. */
2261 real_zerop (const_tree expr)
2263 STRIP_NOPS (expr);
2265 switch (TREE_CODE (expr))
2267 case REAL_CST:
2268 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0)
2269 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2270 case COMPLEX_CST:
2271 return real_zerop (TREE_REALPART (expr))
2272 && real_zerop (TREE_IMAGPART (expr));
2273 case VECTOR_CST:
2275 unsigned i;
2276 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2277 if (!real_zerop (VECTOR_CST_ELT (expr, i)))
2278 return false;
2279 return true;
2281 default:
2282 return false;
2286 /* Return 1 if EXPR is the real constant one in real or complex form.
2287 Trailing zeroes matter for decimal float constants, so don't return
2288 1 for them. */
2291 real_onep (const_tree expr)
2293 STRIP_NOPS (expr);
2295 switch (TREE_CODE (expr))
2297 case REAL_CST:
2298 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1)
2299 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2300 case COMPLEX_CST:
2301 return real_onep (TREE_REALPART (expr))
2302 && real_zerop (TREE_IMAGPART (expr));
2303 case VECTOR_CST:
2305 unsigned i;
2306 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2307 if (!real_onep (VECTOR_CST_ELT (expr, i)))
2308 return false;
2309 return true;
2311 default:
2312 return false;
2316 /* Return 1 if EXPR is the real constant minus one. Trailing zeroes
2317 matter for decimal float constants, so don't return 1 for them. */
2320 real_minus_onep (const_tree expr)
2322 STRIP_NOPS (expr);
2324 switch (TREE_CODE (expr))
2326 case REAL_CST:
2327 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconstm1)
2328 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2329 case COMPLEX_CST:
2330 return real_minus_onep (TREE_REALPART (expr))
2331 && real_zerop (TREE_IMAGPART (expr));
2332 case VECTOR_CST:
2334 unsigned i;
2335 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2336 if (!real_minus_onep (VECTOR_CST_ELT (expr, i)))
2337 return false;
2338 return true;
2340 default:
2341 return false;
2345 /* Nonzero if EXP is a constant or a cast of a constant. */
2348 really_constant_p (const_tree exp)
2350 /* This is not quite the same as STRIP_NOPS. It does more. */
2351 while (CONVERT_EXPR_P (exp)
2352 || TREE_CODE (exp) == NON_LVALUE_EXPR)
2353 exp = TREE_OPERAND (exp, 0);
2354 return TREE_CONSTANT (exp);
2357 /* Return first list element whose TREE_VALUE is ELEM.
2358 Return 0 if ELEM is not in LIST. */
2360 tree
2361 value_member (tree elem, tree list)
2363 while (list)
2365 if (elem == TREE_VALUE (list))
2366 return list;
2367 list = TREE_CHAIN (list);
2369 return NULL_TREE;
2372 /* Return first list element whose TREE_PURPOSE is ELEM.
2373 Return 0 if ELEM is not in LIST. */
2375 tree
2376 purpose_member (const_tree elem, tree list)
2378 while (list)
2380 if (elem == TREE_PURPOSE (list))
2381 return list;
2382 list = TREE_CHAIN (list);
2384 return NULL_TREE;
2387 /* Return true if ELEM is in V. */
2389 bool
2390 vec_member (const_tree elem, vec<tree, va_gc> *v)
2392 unsigned ix;
2393 tree t;
2394 FOR_EACH_VEC_SAFE_ELT (v, ix, t)
2395 if (elem == t)
2396 return true;
2397 return false;
2400 /* Returns element number IDX (zero-origin) of chain CHAIN, or
2401 NULL_TREE. */
2403 tree
2404 chain_index (int idx, tree chain)
2406 for (; chain && idx > 0; --idx)
2407 chain = TREE_CHAIN (chain);
2408 return chain;
2411 /* Return nonzero if ELEM is part of the chain CHAIN. */
2414 chain_member (const_tree elem, const_tree chain)
2416 while (chain)
2418 if (elem == chain)
2419 return 1;
2420 chain = DECL_CHAIN (chain);
2423 return 0;
2426 /* Return the length of a chain of nodes chained through TREE_CHAIN.
2427 We expect a null pointer to mark the end of the chain.
2428 This is the Lisp primitive `length'. */
2431 list_length (const_tree t)
2433 const_tree p = t;
2434 #ifdef ENABLE_TREE_CHECKING
2435 const_tree q = t;
2436 #endif
2437 int len = 0;
2439 while (p)
2441 p = TREE_CHAIN (p);
2442 #ifdef ENABLE_TREE_CHECKING
2443 if (len % 2)
2444 q = TREE_CHAIN (q);
2445 gcc_assert (p != q);
2446 #endif
2447 len++;
2450 return len;
2453 /* Returns the first FIELD_DECL in the TYPE_FIELDS of the RECORD_TYPE or
2454 UNION_TYPE TYPE, or NULL_TREE if none. */
2456 tree
2457 first_field (const_tree type)
2459 tree t = TYPE_FIELDS (type);
2460 while (t && TREE_CODE (t) != FIELD_DECL)
2461 t = TREE_CHAIN (t);
2462 return t;
2465 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
2466 by modifying the last node in chain 1 to point to chain 2.
2467 This is the Lisp primitive `nconc'. */
2469 tree
2470 chainon (tree op1, tree op2)
2472 tree t1;
2474 if (!op1)
2475 return op2;
2476 if (!op2)
2477 return op1;
2479 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
2480 continue;
2481 TREE_CHAIN (t1) = op2;
2483 #ifdef ENABLE_TREE_CHECKING
2485 tree t2;
2486 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
2487 gcc_assert (t2 != t1);
2489 #endif
2491 return op1;
2494 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
2496 tree
2497 tree_last (tree chain)
2499 tree next;
2500 if (chain)
2501 while ((next = TREE_CHAIN (chain)))
2502 chain = next;
2503 return chain;
2506 /* Reverse the order of elements in the chain T,
2507 and return the new head of the chain (old last element). */
2509 tree
2510 nreverse (tree t)
2512 tree prev = 0, decl, next;
2513 for (decl = t; decl; decl = next)
2515 /* We shouldn't be using this function to reverse BLOCK chains; we
2516 have blocks_nreverse for that. */
2517 gcc_checking_assert (TREE_CODE (decl) != BLOCK);
2518 next = TREE_CHAIN (decl);
2519 TREE_CHAIN (decl) = prev;
2520 prev = decl;
2522 return prev;
2525 /* Return a newly created TREE_LIST node whose
2526 purpose and value fields are PARM and VALUE. */
2528 tree
2529 build_tree_list_stat (tree parm, tree value MEM_STAT_DECL)
2531 tree t = make_node_stat (TREE_LIST PASS_MEM_STAT);
2532 TREE_PURPOSE (t) = parm;
2533 TREE_VALUE (t) = value;
2534 return t;
2537 /* Build a chain of TREE_LIST nodes from a vector. */
2539 tree
2540 build_tree_list_vec_stat (const vec<tree, va_gc> *vec MEM_STAT_DECL)
2542 tree ret = NULL_TREE;
2543 tree *pp = &ret;
2544 unsigned int i;
2545 tree t;
2546 FOR_EACH_VEC_SAFE_ELT (vec, i, t)
2548 *pp = build_tree_list_stat (NULL, t PASS_MEM_STAT);
2549 pp = &TREE_CHAIN (*pp);
2551 return ret;
2554 /* Return a newly created TREE_LIST node whose
2555 purpose and value fields are PURPOSE and VALUE
2556 and whose TREE_CHAIN is CHAIN. */
2558 tree
2559 tree_cons_stat (tree purpose, tree value, tree chain MEM_STAT_DECL)
2561 tree node;
2563 node = ggc_alloc_tree_node_stat (sizeof (struct tree_list) PASS_MEM_STAT);
2564 memset (node, 0, sizeof (struct tree_common));
2566 record_node_allocation_statistics (TREE_LIST, sizeof (struct tree_list));
2568 TREE_SET_CODE (node, TREE_LIST);
2569 TREE_CHAIN (node) = chain;
2570 TREE_PURPOSE (node) = purpose;
2571 TREE_VALUE (node) = value;
2572 return node;
2575 /* Return the values of the elements of a CONSTRUCTOR as a vector of
2576 trees. */
2578 vec<tree, va_gc> *
2579 ctor_to_vec (tree ctor)
2581 vec<tree, va_gc> *vec;
2582 vec_alloc (vec, CONSTRUCTOR_NELTS (ctor));
2583 unsigned int ix;
2584 tree val;
2586 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), ix, val)
2587 vec->quick_push (val);
2589 return vec;
2592 /* Return the size nominally occupied by an object of type TYPE
2593 when it resides in memory. The value is measured in units of bytes,
2594 and its data type is that normally used for type sizes
2595 (which is the first type created by make_signed_type or
2596 make_unsigned_type). */
2598 tree
2599 size_in_bytes (const_tree type)
2601 tree t;
2603 if (type == error_mark_node)
2604 return integer_zero_node;
2606 type = TYPE_MAIN_VARIANT (type);
2607 t = TYPE_SIZE_UNIT (type);
2609 if (t == 0)
2611 lang_hooks.types.incomplete_type_error (NULL_TREE, type);
2612 return size_zero_node;
2615 return t;
2618 /* Return the size of TYPE (in bytes) as a wide integer
2619 or return -1 if the size can vary or is larger than an integer. */
2621 HOST_WIDE_INT
2622 int_size_in_bytes (const_tree type)
2624 tree t;
2626 if (type == error_mark_node)
2627 return 0;
2629 type = TYPE_MAIN_VARIANT (type);
2630 t = TYPE_SIZE_UNIT (type);
2631 if (t == 0
2632 || TREE_CODE (t) != INTEGER_CST
2633 || TREE_INT_CST_HIGH (t) != 0
2634 /* If the result would appear negative, it's too big to represent. */
2635 || (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0)
2636 return -1;
2638 return TREE_INT_CST_LOW (t);
2641 /* Return the maximum size of TYPE (in bytes) as a wide integer
2642 or return -1 if the size can vary or is larger than an integer. */
2644 HOST_WIDE_INT
2645 max_int_size_in_bytes (const_tree type)
2647 HOST_WIDE_INT size = -1;
2648 tree size_tree;
2650 /* If this is an array type, check for a possible MAX_SIZE attached. */
2652 if (TREE_CODE (type) == ARRAY_TYPE)
2654 size_tree = TYPE_ARRAY_MAX_SIZE (type);
2656 if (size_tree && host_integerp (size_tree, 1))
2657 size = tree_low_cst (size_tree, 1);
2660 /* If we still haven't been able to get a size, see if the language
2661 can compute a maximum size. */
2663 if (size == -1)
2665 size_tree = lang_hooks.types.max_size (type);
2667 if (size_tree && host_integerp (size_tree, 1))
2668 size = tree_low_cst (size_tree, 1);
2671 return size;
2674 /* Return the bit position of FIELD, in bits from the start of the record.
2675 This is a tree of type bitsizetype. */
2677 tree
2678 bit_position (const_tree field)
2680 return bit_from_pos (DECL_FIELD_OFFSET (field),
2681 DECL_FIELD_BIT_OFFSET (field));
2684 /* Likewise, but return as an integer. It must be representable in
2685 that way (since it could be a signed value, we don't have the
2686 option of returning -1 like int_size_in_byte can. */
2688 HOST_WIDE_INT
2689 int_bit_position (const_tree field)
2691 return tree_low_cst (bit_position (field), 0);
2694 /* Return the byte position of FIELD, in bytes from the start of the record.
2695 This is a tree of type sizetype. */
2697 tree
2698 byte_position (const_tree field)
2700 return byte_from_pos (DECL_FIELD_OFFSET (field),
2701 DECL_FIELD_BIT_OFFSET (field));
2704 /* Likewise, but return as an integer. It must be representable in
2705 that way (since it could be a signed value, we don't have the
2706 option of returning -1 like int_size_in_byte can. */
2708 HOST_WIDE_INT
2709 int_byte_position (const_tree field)
2711 return tree_low_cst (byte_position (field), 0);
2714 /* Return the strictest alignment, in bits, that T is known to have. */
2716 unsigned int
2717 expr_align (const_tree t)
2719 unsigned int align0, align1;
2721 switch (TREE_CODE (t))
2723 CASE_CONVERT: case NON_LVALUE_EXPR:
2724 /* If we have conversions, we know that the alignment of the
2725 object must meet each of the alignments of the types. */
2726 align0 = expr_align (TREE_OPERAND (t, 0));
2727 align1 = TYPE_ALIGN (TREE_TYPE (t));
2728 return MAX (align0, align1);
2730 case SAVE_EXPR: case COMPOUND_EXPR: case MODIFY_EXPR:
2731 case INIT_EXPR: case TARGET_EXPR: case WITH_CLEANUP_EXPR:
2732 case CLEANUP_POINT_EXPR:
2733 /* These don't change the alignment of an object. */
2734 return expr_align (TREE_OPERAND (t, 0));
2736 case COND_EXPR:
2737 /* The best we can do is say that the alignment is the least aligned
2738 of the two arms. */
2739 align0 = expr_align (TREE_OPERAND (t, 1));
2740 align1 = expr_align (TREE_OPERAND (t, 2));
2741 return MIN (align0, align1);
2743 /* FIXME: LABEL_DECL and CONST_DECL never have DECL_ALIGN set
2744 meaningfully, it's always 1. */
2745 case LABEL_DECL: case CONST_DECL:
2746 case VAR_DECL: case PARM_DECL: case RESULT_DECL:
2747 case FUNCTION_DECL:
2748 gcc_assert (DECL_ALIGN (t) != 0);
2749 return DECL_ALIGN (t);
2751 default:
2752 break;
2755 /* Otherwise take the alignment from that of the type. */
2756 return TYPE_ALIGN (TREE_TYPE (t));
2759 /* Return, as a tree node, the number of elements for TYPE (which is an
2760 ARRAY_TYPE) minus one. This counts only elements of the top array. */
2762 tree
2763 array_type_nelts (const_tree type)
2765 tree index_type, min, max;
2767 /* If they did it with unspecified bounds, then we should have already
2768 given an error about it before we got here. */
2769 if (! TYPE_DOMAIN (type))
2770 return error_mark_node;
2772 index_type = TYPE_DOMAIN (type);
2773 min = TYPE_MIN_VALUE (index_type);
2774 max = TYPE_MAX_VALUE (index_type);
2776 /* TYPE_MAX_VALUE may not be set if the array has unknown length. */
2777 if (!max)
2778 return error_mark_node;
2780 return (integer_zerop (min)
2781 ? max
2782 : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
2785 /* If arg is static -- a reference to an object in static storage -- then
2786 return the object. This is not the same as the C meaning of `static'.
2787 If arg isn't static, return NULL. */
2789 tree
2790 staticp (tree arg)
2792 switch (TREE_CODE (arg))
2794 case FUNCTION_DECL:
2795 /* Nested functions are static, even though taking their address will
2796 involve a trampoline as we unnest the nested function and create
2797 the trampoline on the tree level. */
2798 return arg;
2800 case VAR_DECL:
2801 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2802 && ! DECL_THREAD_LOCAL_P (arg)
2803 && ! DECL_DLLIMPORT_P (arg)
2804 ? arg : NULL);
2806 case CONST_DECL:
2807 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2808 ? arg : NULL);
2810 case CONSTRUCTOR:
2811 return TREE_STATIC (arg) ? arg : NULL;
2813 case LABEL_DECL:
2814 case STRING_CST:
2815 return arg;
2817 case COMPONENT_REF:
2818 /* If the thing being referenced is not a field, then it is
2819 something language specific. */
2820 gcc_assert (TREE_CODE (TREE_OPERAND (arg, 1)) == FIELD_DECL);
2822 /* If we are referencing a bitfield, we can't evaluate an
2823 ADDR_EXPR at compile time and so it isn't a constant. */
2824 if (DECL_BIT_FIELD (TREE_OPERAND (arg, 1)))
2825 return NULL;
2827 return staticp (TREE_OPERAND (arg, 0));
2829 case BIT_FIELD_REF:
2830 return NULL;
2832 case INDIRECT_REF:
2833 return TREE_CONSTANT (TREE_OPERAND (arg, 0)) ? arg : NULL;
2835 case ARRAY_REF:
2836 case ARRAY_RANGE_REF:
2837 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
2838 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
2839 return staticp (TREE_OPERAND (arg, 0));
2840 else
2841 return NULL;
2843 case COMPOUND_LITERAL_EXPR:
2844 return TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (arg)) ? arg : NULL;
2846 default:
2847 return NULL;
2854 /* Return whether OP is a DECL whose address is function-invariant. */
2856 bool
2857 decl_address_invariant_p (const_tree op)
2859 /* The conditions below are slightly less strict than the one in
2860 staticp. */
2862 switch (TREE_CODE (op))
2864 case PARM_DECL:
2865 case RESULT_DECL:
2866 case LABEL_DECL:
2867 case FUNCTION_DECL:
2868 return true;
2870 case VAR_DECL:
2871 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
2872 || DECL_THREAD_LOCAL_P (op)
2873 || DECL_CONTEXT (op) == current_function_decl
2874 || decl_function_context (op) == current_function_decl)
2875 return true;
2876 break;
2878 case CONST_DECL:
2879 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
2880 || decl_function_context (op) == current_function_decl)
2881 return true;
2882 break;
2884 default:
2885 break;
2888 return false;
2891 /* Return whether OP is a DECL whose address is interprocedural-invariant. */
2893 bool
2894 decl_address_ip_invariant_p (const_tree op)
2896 /* The conditions below are slightly less strict than the one in
2897 staticp. */
2899 switch (TREE_CODE (op))
2901 case LABEL_DECL:
2902 case FUNCTION_DECL:
2903 case STRING_CST:
2904 return true;
2906 case VAR_DECL:
2907 if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
2908 && !DECL_DLLIMPORT_P (op))
2909 || DECL_THREAD_LOCAL_P (op))
2910 return true;
2911 break;
2913 case CONST_DECL:
2914 if ((TREE_STATIC (op) || DECL_EXTERNAL (op)))
2915 return true;
2916 break;
2918 default:
2919 break;
2922 return false;
2926 /* Return true if T is function-invariant (internal function, does
2927 not handle arithmetic; that's handled in skip_simple_arithmetic and
2928 tree_invariant_p). */
2930 static bool tree_invariant_p (tree t);
2932 static bool
2933 tree_invariant_p_1 (tree t)
2935 tree op;
2937 if (TREE_CONSTANT (t)
2938 || (TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t)))
2939 return true;
2941 switch (TREE_CODE (t))
2943 case SAVE_EXPR:
2944 return true;
2946 case ADDR_EXPR:
2947 op = TREE_OPERAND (t, 0);
2948 while (handled_component_p (op))
2950 switch (TREE_CODE (op))
2952 case ARRAY_REF:
2953 case ARRAY_RANGE_REF:
2954 if (!tree_invariant_p (TREE_OPERAND (op, 1))
2955 || TREE_OPERAND (op, 2) != NULL_TREE
2956 || TREE_OPERAND (op, 3) != NULL_TREE)
2957 return false;
2958 break;
2960 case COMPONENT_REF:
2961 if (TREE_OPERAND (op, 2) != NULL_TREE)
2962 return false;
2963 break;
2965 default:;
2967 op = TREE_OPERAND (op, 0);
2970 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
2972 default:
2973 break;
2976 return false;
2979 /* Return true if T is function-invariant. */
2981 static bool
2982 tree_invariant_p (tree t)
2984 tree inner = skip_simple_arithmetic (t);
2985 return tree_invariant_p_1 (inner);
2988 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
2989 Do this to any expression which may be used in more than one place,
2990 but must be evaluated only once.
2992 Normally, expand_expr would reevaluate the expression each time.
2993 Calling save_expr produces something that is evaluated and recorded
2994 the first time expand_expr is called on it. Subsequent calls to
2995 expand_expr just reuse the recorded value.
2997 The call to expand_expr that generates code that actually computes
2998 the value is the first call *at compile time*. Subsequent calls
2999 *at compile time* generate code to use the saved value.
3000 This produces correct result provided that *at run time* control
3001 always flows through the insns made by the first expand_expr
3002 before reaching the other places where the save_expr was evaluated.
3003 You, the caller of save_expr, must make sure this is so.
3005 Constants, and certain read-only nodes, are returned with no
3006 SAVE_EXPR because that is safe. Expressions containing placeholders
3007 are not touched; see tree.def for an explanation of what these
3008 are used for. */
3010 tree
3011 save_expr (tree expr)
3013 tree t = fold (expr);
3014 tree inner;
3016 /* If the tree evaluates to a constant, then we don't want to hide that
3017 fact (i.e. this allows further folding, and direct checks for constants).
3018 However, a read-only object that has side effects cannot be bypassed.
3019 Since it is no problem to reevaluate literals, we just return the
3020 literal node. */
3021 inner = skip_simple_arithmetic (t);
3022 if (TREE_CODE (inner) == ERROR_MARK)
3023 return inner;
3025 if (tree_invariant_p_1 (inner))
3026 return t;
3028 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
3029 it means that the size or offset of some field of an object depends on
3030 the value within another field.
3032 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
3033 and some variable since it would then need to be both evaluated once and
3034 evaluated more than once. Front-ends must assure this case cannot
3035 happen by surrounding any such subexpressions in their own SAVE_EXPR
3036 and forcing evaluation at the proper time. */
3037 if (contains_placeholder_p (inner))
3038 return t;
3040 t = build1 (SAVE_EXPR, TREE_TYPE (expr), t);
3041 SET_EXPR_LOCATION (t, EXPR_LOCATION (expr));
3043 /* This expression might be placed ahead of a jump to ensure that the
3044 value was computed on both sides of the jump. So make sure it isn't
3045 eliminated as dead. */
3046 TREE_SIDE_EFFECTS (t) = 1;
3047 return t;
3050 /* Look inside EXPR into any simple arithmetic operations. Return the
3051 outermost non-arithmetic or non-invariant node. */
3053 tree
3054 skip_simple_arithmetic (tree expr)
3056 /* We don't care about whether this can be used as an lvalue in this
3057 context. */
3058 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3059 expr = TREE_OPERAND (expr, 0);
3061 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
3062 a constant, it will be more efficient to not make another SAVE_EXPR since
3063 it will allow better simplification and GCSE will be able to merge the
3064 computations if they actually occur. */
3065 while (true)
3067 if (UNARY_CLASS_P (expr))
3068 expr = TREE_OPERAND (expr, 0);
3069 else if (BINARY_CLASS_P (expr))
3071 if (tree_invariant_p (TREE_OPERAND (expr, 1)))
3072 expr = TREE_OPERAND (expr, 0);
3073 else if (tree_invariant_p (TREE_OPERAND (expr, 0)))
3074 expr = TREE_OPERAND (expr, 1);
3075 else
3076 break;
3078 else
3079 break;
3082 return expr;
3085 /* Look inside EXPR into simple arithmetic operations involving constants.
3086 Return the outermost non-arithmetic or non-constant node. */
3088 tree
3089 skip_simple_constant_arithmetic (tree expr)
3091 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3092 expr = TREE_OPERAND (expr, 0);
3094 while (true)
3096 if (UNARY_CLASS_P (expr))
3097 expr = TREE_OPERAND (expr, 0);
3098 else if (BINARY_CLASS_P (expr))
3100 if (TREE_CONSTANT (TREE_OPERAND (expr, 1)))
3101 expr = TREE_OPERAND (expr, 0);
3102 else if (TREE_CONSTANT (TREE_OPERAND (expr, 0)))
3103 expr = TREE_OPERAND (expr, 1);
3104 else
3105 break;
3107 else
3108 break;
3111 return expr;
3114 /* Return which tree structure is used by T. */
3116 enum tree_node_structure_enum
3117 tree_node_structure (const_tree t)
3119 const enum tree_code code = TREE_CODE (t);
3120 return tree_node_structure_for_code (code);
3123 /* Set various status flags when building a CALL_EXPR object T. */
3125 static void
3126 process_call_operands (tree t)
3128 bool side_effects = TREE_SIDE_EFFECTS (t);
3129 bool read_only = false;
3130 int i = call_expr_flags (t);
3132 /* Calls have side-effects, except those to const or pure functions. */
3133 if ((i & ECF_LOOPING_CONST_OR_PURE) || !(i & (ECF_CONST | ECF_PURE)))
3134 side_effects = true;
3135 /* Propagate TREE_READONLY of arguments for const functions. */
3136 if (i & ECF_CONST)
3137 read_only = true;
3139 if (!side_effects || read_only)
3140 for (i = 1; i < TREE_OPERAND_LENGTH (t); i++)
3142 tree op = TREE_OPERAND (t, i);
3143 if (op && TREE_SIDE_EFFECTS (op))
3144 side_effects = true;
3145 if (op && !TREE_READONLY (op) && !CONSTANT_CLASS_P (op))
3146 read_only = false;
3149 TREE_SIDE_EFFECTS (t) = side_effects;
3150 TREE_READONLY (t) = read_only;
3153 /* Return true if EXP contains a PLACEHOLDER_EXPR, i.e. if it represents a
3154 size or offset that depends on a field within a record. */
3156 bool
3157 contains_placeholder_p (const_tree exp)
3159 enum tree_code code;
3161 if (!exp)
3162 return 0;
3164 code = TREE_CODE (exp);
3165 if (code == PLACEHOLDER_EXPR)
3166 return 1;
3168 switch (TREE_CODE_CLASS (code))
3170 case tcc_reference:
3171 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
3172 position computations since they will be converted into a
3173 WITH_RECORD_EXPR involving the reference, which will assume
3174 here will be valid. */
3175 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3177 case tcc_exceptional:
3178 if (code == TREE_LIST)
3179 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
3180 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
3181 break;
3183 case tcc_unary:
3184 case tcc_binary:
3185 case tcc_comparison:
3186 case tcc_expression:
3187 switch (code)
3189 case COMPOUND_EXPR:
3190 /* Ignoring the first operand isn't quite right, but works best. */
3191 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
3193 case COND_EXPR:
3194 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3195 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
3196 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
3198 case SAVE_EXPR:
3199 /* The save_expr function never wraps anything containing
3200 a PLACEHOLDER_EXPR. */
3201 return 0;
3203 default:
3204 break;
3207 switch (TREE_CODE_LENGTH (code))
3209 case 1:
3210 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3211 case 2:
3212 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3213 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
3214 default:
3215 return 0;
3218 case tcc_vl_exp:
3219 switch (code)
3221 case CALL_EXPR:
3223 const_tree arg;
3224 const_call_expr_arg_iterator iter;
3225 FOR_EACH_CONST_CALL_EXPR_ARG (arg, iter, exp)
3226 if (CONTAINS_PLACEHOLDER_P (arg))
3227 return 1;
3228 return 0;
3230 default:
3231 return 0;
3234 default:
3235 return 0;
3237 return 0;
3240 /* Return true if any part of the structure of TYPE involves a PLACEHOLDER_EXPR
3241 directly. This includes size, bounds, qualifiers (for QUAL_UNION_TYPE) and
3242 field positions. */
3244 static bool
3245 type_contains_placeholder_1 (const_tree type)
3247 /* If the size contains a placeholder or the parent type (component type in
3248 the case of arrays) type involves a placeholder, this type does. */
3249 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
3250 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
3251 || (!POINTER_TYPE_P (type)
3252 && TREE_TYPE (type)
3253 && type_contains_placeholder_p (TREE_TYPE (type))))
3254 return true;
3256 /* Now do type-specific checks. Note that the last part of the check above
3257 greatly limits what we have to do below. */
3258 switch (TREE_CODE (type))
3260 case VOID_TYPE:
3261 case POINTER_BOUNDS_TYPE:
3262 case COMPLEX_TYPE:
3263 case ENUMERAL_TYPE:
3264 case BOOLEAN_TYPE:
3265 case POINTER_TYPE:
3266 case OFFSET_TYPE:
3267 case REFERENCE_TYPE:
3268 case METHOD_TYPE:
3269 case FUNCTION_TYPE:
3270 case VECTOR_TYPE:
3271 case NULLPTR_TYPE:
3272 return false;
3274 case INTEGER_TYPE:
3275 case REAL_TYPE:
3276 case FIXED_POINT_TYPE:
3277 /* Here we just check the bounds. */
3278 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
3279 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
3281 case ARRAY_TYPE:
3282 /* We have already checked the component type above, so just check the
3283 domain type. */
3284 return type_contains_placeholder_p (TYPE_DOMAIN (type));
3286 case RECORD_TYPE:
3287 case UNION_TYPE:
3288 case QUAL_UNION_TYPE:
3290 tree field;
3292 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3293 if (TREE_CODE (field) == FIELD_DECL
3294 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
3295 || (TREE_CODE (type) == QUAL_UNION_TYPE
3296 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
3297 || type_contains_placeholder_p (TREE_TYPE (field))))
3298 return true;
3300 return false;
3303 default:
3304 gcc_unreachable ();
3308 /* Wrapper around above function used to cache its result. */
3310 bool
3311 type_contains_placeholder_p (tree type)
3313 bool result;
3315 /* If the contains_placeholder_bits field has been initialized,
3316 then we know the answer. */
3317 if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
3318 return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
3320 /* Indicate that we've seen this type node, and the answer is false.
3321 This is what we want to return if we run into recursion via fields. */
3322 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
3324 /* Compute the real value. */
3325 result = type_contains_placeholder_1 (type);
3327 /* Store the real value. */
3328 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
3330 return result;
3333 /* Push tree EXP onto vector QUEUE if it is not already present. */
3335 static void
3336 push_without_duplicates (tree exp, vec<tree> *queue)
3338 unsigned int i;
3339 tree iter;
3341 FOR_EACH_VEC_ELT (*queue, i, iter)
3342 if (simple_cst_equal (iter, exp) == 1)
3343 break;
3345 if (!iter)
3346 queue->safe_push (exp);
3349 /* Given a tree EXP, find all occurrences of references to fields
3350 in a PLACEHOLDER_EXPR and place them in vector REFS without
3351 duplicates. Also record VAR_DECLs and CONST_DECLs. Note that
3352 we assume here that EXP contains only arithmetic expressions
3353 or CALL_EXPRs with PLACEHOLDER_EXPRs occurring only in their
3354 argument list. */
3356 void
3357 find_placeholder_in_expr (tree exp, vec<tree> *refs)
3359 enum tree_code code = TREE_CODE (exp);
3360 tree inner;
3361 int i;
3363 /* We handle TREE_LIST and COMPONENT_REF separately. */
3364 if (code == TREE_LIST)
3366 FIND_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), refs);
3367 FIND_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), refs);
3369 else if (code == COMPONENT_REF)
3371 for (inner = TREE_OPERAND (exp, 0);
3372 REFERENCE_CLASS_P (inner);
3373 inner = TREE_OPERAND (inner, 0))
3376 if (TREE_CODE (inner) == PLACEHOLDER_EXPR)
3377 push_without_duplicates (exp, refs);
3378 else
3379 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), refs);
3381 else
3382 switch (TREE_CODE_CLASS (code))
3384 case tcc_constant:
3385 break;
3387 case tcc_declaration:
3388 /* Variables allocated to static storage can stay. */
3389 if (!TREE_STATIC (exp))
3390 push_without_duplicates (exp, refs);
3391 break;
3393 case tcc_expression:
3394 /* This is the pattern built in ada/make_aligning_type. */
3395 if (code == ADDR_EXPR
3396 && TREE_CODE (TREE_OPERAND (exp, 0)) == PLACEHOLDER_EXPR)
3398 push_without_duplicates (exp, refs);
3399 break;
3402 /* Fall through... */
3404 case tcc_exceptional:
3405 case tcc_unary:
3406 case tcc_binary:
3407 case tcc_comparison:
3408 case tcc_reference:
3409 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
3410 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3411 break;
3413 case tcc_vl_exp:
3414 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3415 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3416 break;
3418 default:
3419 gcc_unreachable ();
3423 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
3424 return a tree with all occurrences of references to F in a
3425 PLACEHOLDER_EXPR replaced by R. Also handle VAR_DECLs and
3426 CONST_DECLs. Note that we assume here that EXP contains only
3427 arithmetic expressions or CALL_EXPRs with PLACEHOLDER_EXPRs
3428 occurring only in their argument list. */
3430 tree
3431 substitute_in_expr (tree exp, tree f, tree r)
3433 enum tree_code code = TREE_CODE (exp);
3434 tree op0, op1, op2, op3;
3435 tree new_tree;
3437 /* We handle TREE_LIST and COMPONENT_REF separately. */
3438 if (code == TREE_LIST)
3440 op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
3441 op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
3442 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3443 return exp;
3445 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3447 else if (code == COMPONENT_REF)
3449 tree inner;
3451 /* If this expression is getting a value from a PLACEHOLDER_EXPR
3452 and it is the right field, replace it with R. */
3453 for (inner = TREE_OPERAND (exp, 0);
3454 REFERENCE_CLASS_P (inner);
3455 inner = TREE_OPERAND (inner, 0))
3458 /* The field. */
3459 op1 = TREE_OPERAND (exp, 1);
3461 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f)
3462 return r;
3464 /* If this expression hasn't been completed let, leave it alone. */
3465 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && !TREE_TYPE (inner))
3466 return exp;
3468 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3469 if (op0 == TREE_OPERAND (exp, 0))
3470 return exp;
3472 new_tree
3473 = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE);
3475 else
3476 switch (TREE_CODE_CLASS (code))
3478 case tcc_constant:
3479 return exp;
3481 case tcc_declaration:
3482 if (exp == f)
3483 return r;
3484 else
3485 return exp;
3487 case tcc_expression:
3488 if (exp == f)
3489 return r;
3491 /* Fall through... */
3493 case tcc_exceptional:
3494 case tcc_unary:
3495 case tcc_binary:
3496 case tcc_comparison:
3497 case tcc_reference:
3498 switch (TREE_CODE_LENGTH (code))
3500 case 0:
3501 return exp;
3503 case 1:
3504 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3505 if (op0 == TREE_OPERAND (exp, 0))
3506 return exp;
3508 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3509 break;
3511 case 2:
3512 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3513 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3515 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3516 return exp;
3518 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3519 break;
3521 case 3:
3522 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3523 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3524 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3526 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3527 && op2 == TREE_OPERAND (exp, 2))
3528 return exp;
3530 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3531 break;
3533 case 4:
3534 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3535 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3536 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3537 op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
3539 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3540 && op2 == TREE_OPERAND (exp, 2)
3541 && op3 == TREE_OPERAND (exp, 3))
3542 return exp;
3544 new_tree
3545 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3546 break;
3548 default:
3549 gcc_unreachable ();
3551 break;
3553 case tcc_vl_exp:
3555 int i;
3557 new_tree = NULL_TREE;
3559 /* If we are trying to replace F with a constant, inline back
3560 functions which do nothing else than computing a value from
3561 the arguments they are passed. This makes it possible to
3562 fold partially or entirely the replacement expression. */
3563 if (CONSTANT_CLASS_P (r) && code == CALL_EXPR)
3565 tree t = maybe_inline_call_in_expr (exp);
3566 if (t)
3567 return SUBSTITUTE_IN_EXPR (t, f, r);
3570 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3572 tree op = TREE_OPERAND (exp, i);
3573 tree new_op = SUBSTITUTE_IN_EXPR (op, f, r);
3574 if (new_op != op)
3576 if (!new_tree)
3577 new_tree = copy_node (exp);
3578 TREE_OPERAND (new_tree, i) = new_op;
3582 if (new_tree)
3584 new_tree = fold (new_tree);
3585 if (TREE_CODE (new_tree) == CALL_EXPR)
3586 process_call_operands (new_tree);
3588 else
3589 return exp;
3591 break;
3593 default:
3594 gcc_unreachable ();
3597 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3599 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3600 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3602 return new_tree;
3605 /* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
3606 for it within OBJ, a tree that is an object or a chain of references. */
3608 tree
3609 substitute_placeholder_in_expr (tree exp, tree obj)
3611 enum tree_code code = TREE_CODE (exp);
3612 tree op0, op1, op2, op3;
3613 tree new_tree;
3615 /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
3616 in the chain of OBJ. */
3617 if (code == PLACEHOLDER_EXPR)
3619 tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
3620 tree elt;
3622 for (elt = obj; elt != 0;
3623 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3624 || TREE_CODE (elt) == COND_EXPR)
3625 ? TREE_OPERAND (elt, 1)
3626 : (REFERENCE_CLASS_P (elt)
3627 || UNARY_CLASS_P (elt)
3628 || BINARY_CLASS_P (elt)
3629 || VL_EXP_CLASS_P (elt)
3630 || EXPRESSION_CLASS_P (elt))
3631 ? TREE_OPERAND (elt, 0) : 0))
3632 if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
3633 return elt;
3635 for (elt = obj; elt != 0;
3636 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3637 || TREE_CODE (elt) == COND_EXPR)
3638 ? TREE_OPERAND (elt, 1)
3639 : (REFERENCE_CLASS_P (elt)
3640 || UNARY_CLASS_P (elt)
3641 || BINARY_CLASS_P (elt)
3642 || VL_EXP_CLASS_P (elt)
3643 || EXPRESSION_CLASS_P (elt))
3644 ? TREE_OPERAND (elt, 0) : 0))
3645 if (POINTER_TYPE_P (TREE_TYPE (elt))
3646 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
3647 == need_type))
3648 return fold_build1 (INDIRECT_REF, need_type, elt);
3650 /* If we didn't find it, return the original PLACEHOLDER_EXPR. If it
3651 survives until RTL generation, there will be an error. */
3652 return exp;
3655 /* TREE_LIST is special because we need to look at TREE_VALUE
3656 and TREE_CHAIN, not TREE_OPERANDS. */
3657 else if (code == TREE_LIST)
3659 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
3660 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
3661 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3662 return exp;
3664 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3666 else
3667 switch (TREE_CODE_CLASS (code))
3669 case tcc_constant:
3670 case tcc_declaration:
3671 return exp;
3673 case tcc_exceptional:
3674 case tcc_unary:
3675 case tcc_binary:
3676 case tcc_comparison:
3677 case tcc_expression:
3678 case tcc_reference:
3679 case tcc_statement:
3680 switch (TREE_CODE_LENGTH (code))
3682 case 0:
3683 return exp;
3685 case 1:
3686 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3687 if (op0 == TREE_OPERAND (exp, 0))
3688 return exp;
3690 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3691 break;
3693 case 2:
3694 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3695 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3697 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3698 return exp;
3700 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3701 break;
3703 case 3:
3704 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3705 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3706 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3708 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3709 && op2 == TREE_OPERAND (exp, 2))
3710 return exp;
3712 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3713 break;
3715 case 4:
3716 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3717 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3718 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3719 op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
3721 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3722 && op2 == TREE_OPERAND (exp, 2)
3723 && op3 == TREE_OPERAND (exp, 3))
3724 return exp;
3726 new_tree
3727 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3728 break;
3730 default:
3731 gcc_unreachable ();
3733 break;
3735 case tcc_vl_exp:
3737 int i;
3739 new_tree = NULL_TREE;
3741 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3743 tree op = TREE_OPERAND (exp, i);
3744 tree new_op = SUBSTITUTE_PLACEHOLDER_IN_EXPR (op, obj);
3745 if (new_op != op)
3747 if (!new_tree)
3748 new_tree = copy_node (exp);
3749 TREE_OPERAND (new_tree, i) = new_op;
3753 if (new_tree)
3755 new_tree = fold (new_tree);
3756 if (TREE_CODE (new_tree) == CALL_EXPR)
3757 process_call_operands (new_tree);
3759 else
3760 return exp;
3762 break;
3764 default:
3765 gcc_unreachable ();
3768 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3770 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3771 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3773 return new_tree;
3777 /* Subroutine of stabilize_reference; this is called for subtrees of
3778 references. Any expression with side-effects must be put in a SAVE_EXPR
3779 to ensure that it is only evaluated once.
3781 We don't put SAVE_EXPR nodes around everything, because assigning very
3782 simple expressions to temporaries causes us to miss good opportunities
3783 for optimizations. Among other things, the opportunity to fold in the
3784 addition of a constant into an addressing mode often gets lost, e.g.
3785 "y[i+1] += x;". In general, we take the approach that we should not make
3786 an assignment unless we are forced into it - i.e., that any non-side effect
3787 operator should be allowed, and that cse should take care of coalescing
3788 multiple utterances of the same expression should that prove fruitful. */
3790 static tree
3791 stabilize_reference_1 (tree e)
3793 tree result;
3794 enum tree_code code = TREE_CODE (e);
3796 /* We cannot ignore const expressions because it might be a reference
3797 to a const array but whose index contains side-effects. But we can
3798 ignore things that are actual constant or that already have been
3799 handled by this function. */
3801 if (tree_invariant_p (e))
3802 return e;
3804 switch (TREE_CODE_CLASS (code))
3806 case tcc_exceptional:
3807 case tcc_type:
3808 case tcc_declaration:
3809 case tcc_comparison:
3810 case tcc_statement:
3811 case tcc_expression:
3812 case tcc_reference:
3813 case tcc_vl_exp:
3814 /* If the expression has side-effects, then encase it in a SAVE_EXPR
3815 so that it will only be evaluated once. */
3816 /* The reference (r) and comparison (<) classes could be handled as
3817 below, but it is generally faster to only evaluate them once. */
3818 if (TREE_SIDE_EFFECTS (e))
3819 return save_expr (e);
3820 return e;
3822 case tcc_constant:
3823 /* Constants need no processing. In fact, we should never reach
3824 here. */
3825 return e;
3827 case tcc_binary:
3828 /* Division is slow and tends to be compiled with jumps,
3829 especially the division by powers of 2 that is often
3830 found inside of an array reference. So do it just once. */
3831 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
3832 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
3833 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
3834 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
3835 return save_expr (e);
3836 /* Recursively stabilize each operand. */
3837 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
3838 stabilize_reference_1 (TREE_OPERAND (e, 1)));
3839 break;
3841 case tcc_unary:
3842 /* Recursively stabilize each operand. */
3843 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
3844 break;
3846 default:
3847 gcc_unreachable ();
3850 TREE_TYPE (result) = TREE_TYPE (e);
3851 TREE_READONLY (result) = TREE_READONLY (e);
3852 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
3853 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
3854 TREE_SHARED (result) = TREE_SHARED (e);
3855 TREE_STRICT (result) = TREE_STRICT (e);
3856 TREE_RELAXED (result) = TREE_RELAXED (e);
3858 return result;
3861 /* Stabilize a reference so that we can use it any number of times
3862 without causing its operands to be evaluated more than once.
3863 Returns the stabilized reference. This works by means of save_expr,
3864 so see the caveats in the comments about save_expr.
3866 Also allows conversion expressions whose operands are references.
3867 Any other kind of expression is returned unchanged. */
3869 tree
3870 stabilize_reference (tree ref)
3872 tree result;
3873 enum tree_code code = TREE_CODE (ref);
3875 switch (code)
3877 case VAR_DECL:
3878 case PARM_DECL:
3879 case RESULT_DECL:
3880 /* No action is needed in this case. */
3881 return ref;
3883 CASE_CONVERT:
3884 case FLOAT_EXPR:
3885 case FIX_TRUNC_EXPR:
3886 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
3887 break;
3889 case INDIRECT_REF:
3890 result = build_nt (INDIRECT_REF,
3891 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
3892 break;
3894 case COMPONENT_REF:
3895 result = build_nt (COMPONENT_REF,
3896 stabilize_reference (TREE_OPERAND (ref, 0)),
3897 TREE_OPERAND (ref, 1), NULL_TREE);
3898 break;
3900 case BIT_FIELD_REF:
3901 result = build_nt (BIT_FIELD_REF,
3902 stabilize_reference (TREE_OPERAND (ref, 0)),
3903 TREE_OPERAND (ref, 1), TREE_OPERAND (ref, 2));
3904 break;
3906 case ARRAY_REF:
3907 result = build_nt (ARRAY_REF,
3908 stabilize_reference (TREE_OPERAND (ref, 0)),
3909 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
3910 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
3911 break;
3913 case ARRAY_RANGE_REF:
3914 result = build_nt (ARRAY_RANGE_REF,
3915 stabilize_reference (TREE_OPERAND (ref, 0)),
3916 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
3917 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
3918 break;
3920 case COMPOUND_EXPR:
3921 /* We cannot wrap the first expression in a SAVE_EXPR, as then
3922 it wouldn't be ignored. This matters when dealing with
3923 volatiles. */
3924 return stabilize_reference_1 (ref);
3926 /* If arg isn't a kind of lvalue we recognize, make no change.
3927 Caller should recognize the error for an invalid lvalue. */
3928 default:
3929 return ref;
3931 case ERROR_MARK:
3932 return error_mark_node;
3935 TREE_TYPE (result) = TREE_TYPE (ref);
3936 TREE_READONLY (result) = TREE_READONLY (ref);
3937 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
3938 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
3939 TREE_SHARED (result) = TREE_SHARED (ref);
3940 TREE_STRICT (result) = TREE_STRICT (ref);
3941 TREE_RELAXED (result) = TREE_RELAXED (ref);
3943 return result;
3947 /* Low-level constructors for expressions. */
3949 /* A helper function for build1 and constant folders. Set TREE_CONSTANT,
3950 and TREE_SIDE_EFFECTS for an ADDR_EXPR. */
3952 void
3953 recompute_tree_invariant_for_addr_expr (tree t)
3955 tree node;
3956 bool tc = true, se = false;
3958 /* We started out assuming this address is both invariant and constant, but
3959 does not have side effects. Now go down any handled components and see if
3960 any of them involve offsets that are either non-constant or non-invariant.
3961 Also check for side-effects.
3963 ??? Note that this code makes no attempt to deal with the case where
3964 taking the address of something causes a copy due to misalignment. */
3966 #define UPDATE_FLAGS(NODE) \
3967 do { tree _node = (NODE); \
3968 if (_node && !TREE_CONSTANT (_node)) tc = false; \
3969 if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
3971 for (node = TREE_OPERAND (t, 0); handled_component_p (node);
3972 node = TREE_OPERAND (node, 0))
3974 /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
3975 array reference (probably made temporarily by the G++ front end),
3976 so ignore all the operands. */
3977 if ((TREE_CODE (node) == ARRAY_REF
3978 || TREE_CODE (node) == ARRAY_RANGE_REF)
3979 && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
3981 UPDATE_FLAGS (TREE_OPERAND (node, 1));
3982 if (TREE_OPERAND (node, 2))
3983 UPDATE_FLAGS (TREE_OPERAND (node, 2));
3984 if (TREE_OPERAND (node, 3))
3985 UPDATE_FLAGS (TREE_OPERAND (node, 3));
3987 /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
3988 FIELD_DECL, apparently. The G++ front end can put something else
3989 there, at least temporarily. */
3990 else if (TREE_CODE (node) == COMPONENT_REF
3991 && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
3993 if (TREE_OPERAND (node, 2))
3994 UPDATE_FLAGS (TREE_OPERAND (node, 2));
3998 node = lang_hooks.expr_to_decl (node, &tc, &se);
4000 /* Now see what's inside. If it's an INDIRECT_REF, copy our properties from
4001 the address, since &(*a)->b is a form of addition. If it's a constant, the
4002 address is constant too. If it's a decl, its address is constant if the
4003 decl is static. Everything else is not constant and, furthermore,
4004 taking the address of a volatile variable is not volatile. */
4005 if (TREE_CODE (node) == INDIRECT_REF
4006 || TREE_CODE (node) == MEM_REF)
4007 UPDATE_FLAGS (TREE_OPERAND (node, 0));
4008 else if (CONSTANT_CLASS_P (node))
4010 else if (DECL_P (node))
4011 tc &= (staticp (node) != NULL_TREE);
4012 else
4014 tc = false;
4015 se |= TREE_SIDE_EFFECTS (node);
4019 TREE_CONSTANT (t) = tc;
4020 TREE_SIDE_EFFECTS (t) = se;
4021 #undef UPDATE_FLAGS
4024 /* Build an expression of code CODE, data type TYPE, and operands as
4025 specified. Expressions and reference nodes can be created this way.
4026 Constants, decls, types and misc nodes cannot be.
4028 We define 5 non-variadic functions, from 0 to 4 arguments. This is
4029 enough for all extant tree codes. */
4031 tree
4032 build0_stat (enum tree_code code, tree tt MEM_STAT_DECL)
4034 tree t;
4036 gcc_assert (TREE_CODE_LENGTH (code) == 0);
4038 t = make_node_stat (code PASS_MEM_STAT);
4039 TREE_TYPE (t) = tt;
4041 return t;
4044 tree
4045 build1_stat (enum tree_code code, tree type, tree node MEM_STAT_DECL)
4047 int length = sizeof (struct tree_exp);
4048 tree t;
4050 record_node_allocation_statistics (code, length);
4052 gcc_assert (TREE_CODE_LENGTH (code) == 1);
4054 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
4056 memset (t, 0, sizeof (struct tree_common));
4058 TREE_SET_CODE (t, code);
4060 TREE_TYPE (t) = type;
4061 SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
4062 TREE_OPERAND (t, 0) = node;
4063 if (node && !TYPE_P (node))
4065 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
4066 TREE_READONLY (t) = TREE_READONLY (node);
4069 if (TREE_CODE_CLASS (code) == tcc_statement)
4070 TREE_SIDE_EFFECTS (t) = 1;
4071 else switch (code)
4073 case VA_ARG_EXPR:
4074 /* All of these have side-effects, no matter what their
4075 operands are. */
4076 TREE_SIDE_EFFECTS (t) = 1;
4077 TREE_READONLY (t) = 0;
4078 break;
4080 case INDIRECT_REF:
4081 /* Whether a dereference is readonly has nothing to do with whether
4082 its operand is readonly. */
4083 TREE_READONLY (t) = 0;
4084 TREE_SHARED (t) = upc_shared_type_p (type);
4085 break;
4087 case ADDR_EXPR:
4088 if (node)
4089 recompute_tree_invariant_for_addr_expr (t);
4090 break;
4092 default:
4093 if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
4094 && node && !TYPE_P (node)
4095 && TREE_CONSTANT (node))
4096 TREE_CONSTANT (t) = 1;
4097 if (TREE_CODE_CLASS (code) == tcc_reference
4098 && node && TREE_THIS_VOLATILE (node))
4099 TREE_THIS_VOLATILE (t) = 1;
4100 /* Drop the UPC "shared" type qualifier for
4101 expressions involving UPC shared objects. */
4102 if (TREE_CODE_CLASS (code) == tcc_unary
4103 && node && !TYPE_P (node)
4104 && upc_shared_type_p (type))
4105 TREE_TYPE (t) = build_upc_unshared_type (type);
4106 break;
4109 return t;
4112 #define PROCESS_ARG(N) \
4113 do { \
4114 TREE_OPERAND (t, N) = arg##N; \
4115 if (arg##N &&!TYPE_P (arg##N)) \
4117 if (TREE_SIDE_EFFECTS (arg##N)) \
4118 side_effects = 1; \
4119 if (!TREE_READONLY (arg##N) \
4120 && !CONSTANT_CLASS_P (arg##N)) \
4121 (void) (read_only = 0); \
4122 if (!TREE_CONSTANT (arg##N)) \
4123 (void) (constant = 0); \
4125 } while (0)
4127 tree
4128 build2_stat (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
4130 bool constant, read_only, side_effects;
4131 tree t;
4133 gcc_assert (TREE_CODE_LENGTH (code) == 2);
4135 if ((code == MINUS_EXPR || code == PLUS_EXPR || code == MULT_EXPR)
4136 && arg0 && arg1 && tt && POINTER_TYPE_P (tt)
4137 /* When sizetype precision doesn't match that of pointers
4138 we need to be able to build explicit extensions or truncations
4139 of the offset argument. */
4140 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (tt))
4141 gcc_assert (TREE_CODE (arg0) == INTEGER_CST
4142 && TREE_CODE (arg1) == INTEGER_CST);
4144 if (code == POINTER_PLUS_EXPR && arg0 && arg1 && tt)
4145 gcc_assert (POINTER_TYPE_P (tt) && POINTER_TYPE_P (TREE_TYPE (arg0))
4146 && ptrofftype_p (TREE_TYPE (arg1)));
4148 t = make_node_stat (code PASS_MEM_STAT);
4150 /* Remove UPC shared type qualifiers from the result type. */
4151 if (upc_shared_type_p (tt))
4152 tt = build_upc_unshared_type (tt);
4153 TREE_TYPE (t) = tt;
4155 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
4156 result based on those same flags for the arguments. But if the
4157 arguments aren't really even `tree' expressions, we shouldn't be trying
4158 to do this. */
4160 /* Expressions without side effects may be constant if their
4161 arguments are as well. */
4162 constant = (TREE_CODE_CLASS (code) == tcc_comparison
4163 || TREE_CODE_CLASS (code) == tcc_binary);
4164 read_only = 1;
4165 side_effects = TREE_SIDE_EFFECTS (t);
4167 PROCESS_ARG (0);
4168 PROCESS_ARG (1);
4170 TREE_READONLY (t) = read_only;
4171 TREE_CONSTANT (t) = constant;
4172 TREE_SIDE_EFFECTS (t) = side_effects;
4173 TREE_THIS_VOLATILE (t)
4174 = (TREE_CODE_CLASS (code) == tcc_reference
4175 && arg0 && TREE_THIS_VOLATILE (arg0));
4177 return t;
4181 tree
4182 build3_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4183 tree arg2 MEM_STAT_DECL)
4185 bool constant, read_only, side_effects;
4186 tree t;
4188 gcc_assert (TREE_CODE_LENGTH (code) == 3);
4189 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4191 t = make_node_stat (code PASS_MEM_STAT);
4192 TREE_TYPE (t) = tt;
4194 read_only = 1;
4196 /* As a special exception, if COND_EXPR has NULL branches, we
4197 assume that it is a gimple statement and always consider
4198 it to have side effects. */
4199 if (code == COND_EXPR
4200 && tt == void_type_node
4201 && arg1 == NULL_TREE
4202 && arg2 == NULL_TREE)
4203 side_effects = true;
4204 else
4205 side_effects = TREE_SIDE_EFFECTS (t);
4207 PROCESS_ARG (0);
4208 PROCESS_ARG (1);
4209 PROCESS_ARG (2);
4211 if (code == COND_EXPR)
4212 TREE_READONLY (t) = read_only;
4214 TREE_SIDE_EFFECTS (t) = side_effects;
4215 TREE_THIS_VOLATILE (t)
4216 = (TREE_CODE_CLASS (code) == tcc_reference
4217 && arg0 && TREE_THIS_VOLATILE (arg0));
4218 TREE_SHARED (t)
4219 = (TREE_CODE_CLASS (code) == tcc_reference
4220 && arg0 && TREE_SHARED (arg0));
4221 if (TREE_SHARED (t))
4223 TREE_STRICT (t) = TREE_STRICT (arg0);
4224 TREE_RELAXED (t) = TREE_RELAXED (arg0);
4227 return t;
4230 tree
4231 build4_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4232 tree arg2, tree arg3 MEM_STAT_DECL)
4234 bool constant, read_only, side_effects;
4235 tree t;
4237 gcc_assert (TREE_CODE_LENGTH (code) == 4);
4239 t = make_node_stat (code PASS_MEM_STAT);
4240 TREE_TYPE (t) = tt;
4242 side_effects = TREE_SIDE_EFFECTS (t);
4244 PROCESS_ARG (0);
4245 PROCESS_ARG (1);
4246 PROCESS_ARG (2);
4247 PROCESS_ARG (3);
4249 TREE_SIDE_EFFECTS (t) = side_effects;
4250 TREE_THIS_VOLATILE (t)
4251 = (TREE_CODE_CLASS (code) == tcc_reference
4252 && arg0 && TREE_THIS_VOLATILE (arg0));
4254 return t;
4257 tree
4258 build5_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4259 tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
4261 bool constant, read_only, side_effects;
4262 tree t;
4264 gcc_assert (TREE_CODE_LENGTH (code) == 5);
4266 t = make_node_stat (code PASS_MEM_STAT);
4267 TREE_TYPE (t) = tt;
4269 side_effects = TREE_SIDE_EFFECTS (t);
4271 PROCESS_ARG (0);
4272 PROCESS_ARG (1);
4273 PROCESS_ARG (2);
4274 PROCESS_ARG (3);
4275 PROCESS_ARG (4);
4277 TREE_SIDE_EFFECTS (t) = side_effects;
4278 TREE_THIS_VOLATILE (t)
4279 = (TREE_CODE_CLASS (code) == tcc_reference
4280 && arg0 && TREE_THIS_VOLATILE (arg0));
4282 return t;
4285 /* Build a simple MEM_REF tree with the sematics of a plain INDIRECT_REF
4286 on the pointer PTR. */
4288 tree
4289 build_simple_mem_ref_loc (location_t loc, tree ptr)
4291 HOST_WIDE_INT offset = 0;
4292 tree ptype = TREE_TYPE (ptr);
4293 tree tem;
4294 /* For convenience allow addresses that collapse to a simple base
4295 and offset. */
4296 if (TREE_CODE (ptr) == ADDR_EXPR
4297 && (handled_component_p (TREE_OPERAND (ptr, 0))
4298 || TREE_CODE (TREE_OPERAND (ptr, 0)) == MEM_REF))
4300 ptr = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &offset);
4301 gcc_assert (ptr);
4302 ptr = build_fold_addr_expr (ptr);
4303 gcc_assert (is_gimple_reg (ptr) || is_gimple_min_invariant (ptr));
4305 tem = build2 (MEM_REF, TREE_TYPE (ptype),
4306 ptr, build_int_cst (ptype, offset));
4307 SET_EXPR_LOCATION (tem, loc);
4308 return tem;
4311 /* Return the constant offset of a MEM_REF or TARGET_MEM_REF tree T. */
4313 double_int
4314 mem_ref_offset (const_tree t)
4316 tree toff = TREE_OPERAND (t, 1);
4317 return tree_to_double_int (toff).sext (TYPE_PRECISION (TREE_TYPE (toff)));
4320 /* Return an invariant ADDR_EXPR of type TYPE taking the address of BASE
4321 offsetted by OFFSET units. */
4323 tree
4324 build_invariant_address (tree type, tree base, HOST_WIDE_INT offset)
4326 tree ref = fold_build2 (MEM_REF, TREE_TYPE (type),
4327 build_fold_addr_expr (base),
4328 build_int_cst (ptr_type_node, offset));
4329 tree addr = build1 (ADDR_EXPR, type, ref);
4330 recompute_tree_invariant_for_addr_expr (addr);
4331 return addr;
4334 /* Similar except don't specify the TREE_TYPE
4335 and leave the TREE_SIDE_EFFECTS as 0.
4336 It is permissible for arguments to be null,
4337 or even garbage if their values do not matter. */
4339 tree
4340 build_nt (enum tree_code code, ...)
4342 tree t;
4343 int length;
4344 int i;
4345 va_list p;
4347 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4349 va_start (p, code);
4351 t = make_node (code);
4352 length = TREE_CODE_LENGTH (code);
4354 for (i = 0; i < length; i++)
4355 TREE_OPERAND (t, i) = va_arg (p, tree);
4357 va_end (p);
4358 return t;
4361 /* Similar to build_nt, but for creating a CALL_EXPR object with a
4362 tree vec. */
4364 tree
4365 build_nt_call_vec (tree fn, vec<tree, va_gc> *args)
4367 tree ret, t;
4368 unsigned int ix;
4370 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
4371 CALL_EXPR_FN (ret) = fn;
4372 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
4373 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
4374 CALL_EXPR_ARG (ret, ix) = t;
4375 return ret;
4378 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
4379 We do NOT enter this node in any sort of symbol table.
4381 LOC is the location of the decl.
4383 layout_decl is used to set up the decl's storage layout.
4384 Other slots are initialized to 0 or null pointers. */
4386 tree
4387 build_decl_stat (location_t loc, enum tree_code code, tree name,
4388 tree type MEM_STAT_DECL)
4390 tree t;
4392 t = make_node_stat (code PASS_MEM_STAT);
4393 DECL_SOURCE_LOCATION (t) = loc;
4395 /* if (type == error_mark_node)
4396 type = integer_type_node; */
4397 /* That is not done, deliberately, so that having error_mark_node
4398 as the type can suppress useless errors in the use of this variable. */
4400 DECL_NAME (t) = name;
4401 TREE_TYPE (t) = type;
4403 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
4404 layout_decl (t, 0);
4406 return t;
4409 /* Builds and returns function declaration with NAME and TYPE. */
4411 tree
4412 build_fn_decl (const char *name, tree type)
4414 tree id = get_identifier (name);
4415 tree decl = build_decl (input_location, FUNCTION_DECL, id, type);
4417 DECL_EXTERNAL (decl) = 1;
4418 TREE_PUBLIC (decl) = 1;
4419 DECL_ARTIFICIAL (decl) = 1;
4420 TREE_NOTHROW (decl) = 1;
4422 return decl;
4425 vec<tree, va_gc> *all_translation_units;
4427 /* Builds a new translation-unit decl with name NAME, queues it in the
4428 global list of translation-unit decls and returns it. */
4430 tree
4431 build_translation_unit_decl (tree name)
4433 tree tu = build_decl (UNKNOWN_LOCATION, TRANSLATION_UNIT_DECL,
4434 name, NULL_TREE);
4435 TRANSLATION_UNIT_LANGUAGE (tu) = lang_hooks.name;
4436 vec_safe_push (all_translation_units, tu);
4437 return tu;
4441 /* BLOCK nodes are used to represent the structure of binding contours
4442 and declarations, once those contours have been exited and their contents
4443 compiled. This information is used for outputting debugging info. */
4445 tree
4446 build_block (tree vars, tree subblocks, tree supercontext, tree chain)
4448 tree block = make_node (BLOCK);
4450 BLOCK_VARS (block) = vars;
4451 BLOCK_SUBBLOCKS (block) = subblocks;
4452 BLOCK_SUPERCONTEXT (block) = supercontext;
4453 BLOCK_CHAIN (block) = chain;
4454 return block;
4458 /* Like SET_EXPR_LOCATION, but make sure the tree can have a location.
4460 LOC is the location to use in tree T. */
4462 void
4463 protected_set_expr_location (tree t, location_t loc)
4465 if (t && CAN_HAVE_LOCATION_P (t))
4466 SET_EXPR_LOCATION (t, loc);
4469 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
4470 is ATTRIBUTE. */
4472 tree
4473 build_decl_attribute_variant (tree ddecl, tree attribute)
4475 DECL_ATTRIBUTES (ddecl) = attribute;
4476 return ddecl;
4479 /* Borrowed from hashtab.c iterative_hash implementation. */
4480 #define mix(a,b,c) \
4482 a -= b; a -= c; a ^= (c>>13); \
4483 b -= c; b -= a; b ^= (a<< 8); \
4484 c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \
4485 a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \
4486 b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \
4487 c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \
4488 a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \
4489 b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \
4490 c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \
4494 /* Produce good hash value combining VAL and VAL2. */
4495 hashval_t
4496 iterative_hash_hashval_t (hashval_t val, hashval_t val2)
4498 /* the golden ratio; an arbitrary value. */
4499 hashval_t a = 0x9e3779b9;
4501 mix (a, val, val2);
4502 return val2;
4505 /* Produce good hash value combining VAL and VAL2. */
4506 hashval_t
4507 iterative_hash_host_wide_int (HOST_WIDE_INT val, hashval_t val2)
4509 if (sizeof (HOST_WIDE_INT) == sizeof (hashval_t))
4510 return iterative_hash_hashval_t (val, val2);
4511 else
4513 hashval_t a = (hashval_t) val;
4514 /* Avoid warnings about shifting of more than the width of the type on
4515 hosts that won't execute this path. */
4516 int zero = 0;
4517 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 8 + zero));
4518 mix (a, b, val2);
4519 if (sizeof (HOST_WIDE_INT) > 2 * sizeof (hashval_t))
4521 hashval_t a = (hashval_t) (val >> (sizeof (hashval_t) * 16 + zero));
4522 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 24 + zero));
4523 mix (a, b, val2);
4525 return val2;
4529 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4530 is ATTRIBUTE and its qualifiers are QUALS.
4532 Record such modified types already made so we don't make duplicates. */
4534 tree
4535 build_type_attribute_qual_variant (tree ttype, tree attribute, int quals)
4537 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
4539 hashval_t hashcode = 0;
4540 tree ntype;
4541 enum tree_code code = TREE_CODE (ttype);
4543 /* Building a distinct copy of a tagged type is inappropriate; it
4544 causes breakage in code that expects there to be a one-to-one
4545 relationship between a struct and its fields.
4546 build_duplicate_type is another solution (as used in
4547 handle_transparent_union_attribute), but that doesn't play well
4548 with the stronger C++ type identity model. */
4549 if (TREE_CODE (ttype) == RECORD_TYPE
4550 || TREE_CODE (ttype) == UNION_TYPE
4551 || TREE_CODE (ttype) == QUAL_UNION_TYPE
4552 || TREE_CODE (ttype) == ENUMERAL_TYPE)
4554 warning (OPT_Wattributes,
4555 "ignoring attributes applied to %qT after definition",
4556 TYPE_MAIN_VARIANT (ttype));
4557 return build_qualified_type (ttype, quals);
4560 ttype = build_qualified_type (ttype, TYPE_UNQUALIFIED);
4561 ntype = build_distinct_type_copy (ttype);
4563 TYPE_ATTRIBUTES (ntype) = attribute;
4565 hashcode = iterative_hash_object (code, hashcode);
4566 if (TREE_TYPE (ntype))
4567 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (ntype)),
4568 hashcode);
4569 hashcode = attribute_hash_list (attribute, hashcode);
4571 switch (TREE_CODE (ntype))
4573 case FUNCTION_TYPE:
4574 hashcode = type_hash_list (TYPE_ARG_TYPES (ntype), hashcode);
4575 break;
4576 case ARRAY_TYPE:
4577 if (TYPE_DOMAIN (ntype))
4578 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (ntype)),
4579 hashcode);
4580 break;
4581 case INTEGER_TYPE:
4582 hashcode = iterative_hash_object
4583 (TREE_INT_CST_LOW (TYPE_MAX_VALUE (ntype)), hashcode);
4584 hashcode = iterative_hash_object
4585 (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (ntype)), hashcode);
4586 break;
4587 case REAL_TYPE:
4588 case FIXED_POINT_TYPE:
4590 unsigned int precision = TYPE_PRECISION (ntype);
4591 hashcode = iterative_hash_object (precision, hashcode);
4593 break;
4594 default:
4595 break;
4598 ntype = type_hash_canon (hashcode, ntype);
4600 /* If the target-dependent attributes make NTYPE different from
4601 its canonical type, we will need to use structural equality
4602 checks for this type. */
4603 if (TYPE_STRUCTURAL_EQUALITY_P (ttype)
4604 || !comp_type_attributes (ntype, ttype))
4605 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
4606 else if (TYPE_CANONICAL (ntype) == ntype)
4607 TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype);
4609 ttype = build_qualified_type (ntype, quals);
4611 else if (TYPE_QUALS (ttype) != quals)
4612 ttype = build_qualified_type (ttype, quals);
4614 return ttype;
4617 /* Check if "omp declare simd" attribute arguments, CLAUSES1 and CLAUSES2, are
4618 the same. */
4620 static bool
4621 omp_declare_simd_clauses_equal (tree clauses1, tree clauses2)
4623 tree cl1, cl2;
4624 for (cl1 = clauses1, cl2 = clauses2;
4625 cl1 && cl2;
4626 cl1 = OMP_CLAUSE_CHAIN (cl1), cl2 = OMP_CLAUSE_CHAIN (cl2))
4628 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_CODE (cl2))
4629 return false;
4630 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_SIMDLEN)
4632 if (simple_cst_equal (OMP_CLAUSE_DECL (cl1),
4633 OMP_CLAUSE_DECL (cl2)) != 1)
4634 return false;
4636 switch (OMP_CLAUSE_CODE (cl1))
4638 case OMP_CLAUSE_ALIGNED:
4639 if (simple_cst_equal (OMP_CLAUSE_ALIGNED_ALIGNMENT (cl1),
4640 OMP_CLAUSE_ALIGNED_ALIGNMENT (cl2)) != 1)
4641 return false;
4642 break;
4643 case OMP_CLAUSE_LINEAR:
4644 if (simple_cst_equal (OMP_CLAUSE_LINEAR_STEP (cl1),
4645 OMP_CLAUSE_LINEAR_STEP (cl2)) != 1)
4646 return false;
4647 break;
4648 case OMP_CLAUSE_SIMDLEN:
4649 if (simple_cst_equal (OMP_CLAUSE_SIMDLEN_EXPR (cl1),
4650 OMP_CLAUSE_SIMDLEN_EXPR (cl2)) != 1)
4651 return false;
4652 default:
4653 break;
4656 return true;
4659 /* Compare two constructor-element-type constants. Return 1 if the lists
4660 are known to be equal; otherwise return 0. */
4662 static bool
4663 simple_cst_list_equal (const_tree l1, const_tree l2)
4665 while (l1 != NULL_TREE && l2 != NULL_TREE)
4667 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
4668 return false;
4670 l1 = TREE_CHAIN (l1);
4671 l2 = TREE_CHAIN (l2);
4674 return l1 == l2;
4677 /* Compare two attributes for their value identity. Return true if the
4678 attribute values are known to be equal; otherwise return false.
4681 static bool
4682 attribute_value_equal (const_tree attr1, const_tree attr2)
4684 if (TREE_VALUE (attr1) == TREE_VALUE (attr2))
4685 return true;
4687 if (TREE_VALUE (attr1) != NULL_TREE
4688 && TREE_CODE (TREE_VALUE (attr1)) == TREE_LIST
4689 && TREE_VALUE (attr2) != NULL
4690 && TREE_CODE (TREE_VALUE (attr2)) == TREE_LIST)
4691 return (simple_cst_list_equal (TREE_VALUE (attr1),
4692 TREE_VALUE (attr2)) == 1);
4694 if ((flag_openmp || flag_openmp_simd)
4695 && TREE_VALUE (attr1) && TREE_VALUE (attr2)
4696 && TREE_CODE (TREE_VALUE (attr1)) == OMP_CLAUSE
4697 && TREE_CODE (TREE_VALUE (attr2)) == OMP_CLAUSE)
4698 return omp_declare_simd_clauses_equal (TREE_VALUE (attr1),
4699 TREE_VALUE (attr2));
4701 return (simple_cst_equal (TREE_VALUE (attr1), TREE_VALUE (attr2)) == 1);
4704 /* Return 0 if the attributes for two types are incompatible, 1 if they
4705 are compatible, and 2 if they are nearly compatible (which causes a
4706 warning to be generated). */
4708 comp_type_attributes (const_tree type1, const_tree type2)
4710 const_tree a1 = TYPE_ATTRIBUTES (type1);
4711 const_tree a2 = TYPE_ATTRIBUTES (type2);
4712 const_tree a;
4714 if (a1 == a2)
4715 return 1;
4716 for (a = a1; a != NULL_TREE; a = TREE_CHAIN (a))
4718 const struct attribute_spec *as;
4719 const_tree attr;
4721 as = lookup_attribute_spec (get_attribute_name (a));
4722 if (!as || as->affects_type_identity == false)
4723 continue;
4725 attr = lookup_attribute (as->name, CONST_CAST_TREE (a2));
4726 if (!attr || !attribute_value_equal (a, attr))
4727 break;
4729 if (!a)
4731 for (a = a2; a != NULL_TREE; a = TREE_CHAIN (a))
4733 const struct attribute_spec *as;
4735 as = lookup_attribute_spec (get_attribute_name (a));
4736 if (!as || as->affects_type_identity == false)
4737 continue;
4739 if (!lookup_attribute (as->name, CONST_CAST_TREE (a1)))
4740 break;
4741 /* We don't need to compare trees again, as we did this
4742 already in first loop. */
4744 /* All types - affecting identity - are equal, so
4745 there is no need to call target hook for comparison. */
4746 if (!a)
4747 return 1;
4749 /* As some type combinations - like default calling-convention - might
4750 be compatible, we have to call the target hook to get the final result. */
4751 return targetm.comp_type_attributes (type1, type2);
4754 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4755 is ATTRIBUTE.
4757 Record such modified types already made so we don't make duplicates. */
4759 tree
4760 build_type_attribute_variant (tree ttype, tree attribute)
4762 return build_type_attribute_qual_variant (ttype, attribute,
4763 TYPE_QUALS (ttype));
4767 /* Reset the expression *EXPR_P, a size or position.
4769 ??? We could reset all non-constant sizes or positions. But it's cheap
4770 enough to not do so and refrain from adding workarounds to dwarf2out.c.
4772 We need to reset self-referential sizes or positions because they cannot
4773 be gimplified and thus can contain a CALL_EXPR after the gimplification
4774 is finished, which will run afoul of LTO streaming. And they need to be
4775 reset to something essentially dummy but not constant, so as to preserve
4776 the properties of the object they are attached to. */
4778 static inline void
4779 free_lang_data_in_one_sizepos (tree *expr_p)
4781 tree expr = *expr_p;
4782 if (CONTAINS_PLACEHOLDER_P (expr))
4783 *expr_p = build0 (PLACEHOLDER_EXPR, TREE_TYPE (expr));
4787 /* Reset all the fields in a binfo node BINFO. We only keep
4788 BINFO_VTABLE, which is used by gimple_fold_obj_type_ref. */
4790 static void
4791 free_lang_data_in_binfo (tree binfo)
4793 unsigned i;
4794 tree t;
4796 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
4798 BINFO_VIRTUALS (binfo) = NULL_TREE;
4799 BINFO_BASE_ACCESSES (binfo) = NULL;
4800 BINFO_INHERITANCE_CHAIN (binfo) = NULL_TREE;
4801 BINFO_SUBVTT_INDEX (binfo) = NULL_TREE;
4803 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (binfo), i, t)
4804 free_lang_data_in_binfo (t);
4808 /* Reset all language specific information still present in TYPE. */
4810 static void
4811 free_lang_data_in_type (tree type)
4813 gcc_assert (TYPE_P (type));
4815 /* Give the FE a chance to remove its own data first. */
4816 lang_hooks.free_lang_data (type);
4818 TREE_LANG_FLAG_0 (type) = 0;
4819 TREE_LANG_FLAG_1 (type) = 0;
4820 TREE_LANG_FLAG_2 (type) = 0;
4821 TREE_LANG_FLAG_3 (type) = 0;
4822 TREE_LANG_FLAG_4 (type) = 0;
4823 TREE_LANG_FLAG_5 (type) = 0;
4824 TREE_LANG_FLAG_6 (type) = 0;
4826 if (TREE_CODE (type) == FUNCTION_TYPE)
4828 /* Remove the const and volatile qualifiers from arguments. The
4829 C++ front end removes them, but the C front end does not,
4830 leading to false ODR violation errors when merging two
4831 instances of the same function signature compiled by
4832 different front ends. */
4833 tree p;
4835 for (p = TYPE_ARG_TYPES (type); p; p = TREE_CHAIN (p))
4837 tree arg_type = TREE_VALUE (p);
4839 if (TYPE_READONLY (arg_type) || TYPE_VOLATILE (arg_type))
4841 int quals = TYPE_QUALS (arg_type)
4842 & ~TYPE_QUAL_CONST
4843 & ~TYPE_QUAL_VOLATILE;
4844 TREE_VALUE (p) = build_qualified_type (arg_type, quals);
4845 free_lang_data_in_type (TREE_VALUE (p));
4850 /* Remove members that are not actually FIELD_DECLs from the field
4851 list of an aggregate. These occur in C++. */
4852 if (RECORD_OR_UNION_TYPE_P (type))
4854 tree prev, member;
4856 /* Note that TYPE_FIELDS can be shared across distinct
4857 TREE_TYPEs. Therefore, if the first field of TYPE_FIELDS is
4858 to be removed, we cannot set its TREE_CHAIN to NULL.
4859 Otherwise, we would not be able to find all the other fields
4860 in the other instances of this TREE_TYPE.
4862 This was causing an ICE in testsuite/g++.dg/lto/20080915.C. */
4863 prev = NULL_TREE;
4864 member = TYPE_FIELDS (type);
4865 while (member)
4867 if (TREE_CODE (member) == FIELD_DECL
4868 || TREE_CODE (member) == TYPE_DECL)
4870 if (prev)
4871 TREE_CHAIN (prev) = member;
4872 else
4873 TYPE_FIELDS (type) = member;
4874 prev = member;
4877 member = TREE_CHAIN (member);
4880 if (prev)
4881 TREE_CHAIN (prev) = NULL_TREE;
4882 else
4883 TYPE_FIELDS (type) = NULL_TREE;
4885 TYPE_METHODS (type) = NULL_TREE;
4886 if (TYPE_BINFO (type))
4887 free_lang_data_in_binfo (TYPE_BINFO (type));
4889 else
4891 /* For non-aggregate types, clear out the language slot (which
4892 overloads TYPE_BINFO). */
4893 TYPE_LANG_SLOT_1 (type) = NULL_TREE;
4895 if (INTEGRAL_TYPE_P (type)
4896 || SCALAR_FLOAT_TYPE_P (type)
4897 || FIXED_POINT_TYPE_P (type))
4899 free_lang_data_in_one_sizepos (&TYPE_MIN_VALUE (type));
4900 free_lang_data_in_one_sizepos (&TYPE_MAX_VALUE (type));
4904 free_lang_data_in_one_sizepos (&TYPE_SIZE (type));
4905 free_lang_data_in_one_sizepos (&TYPE_SIZE_UNIT (type));
4907 if (TYPE_CONTEXT (type)
4908 && TREE_CODE (TYPE_CONTEXT (type)) == BLOCK)
4910 tree ctx = TYPE_CONTEXT (type);
4913 ctx = BLOCK_SUPERCONTEXT (ctx);
4915 while (ctx && TREE_CODE (ctx) == BLOCK);
4916 TYPE_CONTEXT (type) = ctx;
4921 /* Return true if DECL may need an assembler name to be set. */
4923 static inline bool
4924 need_assembler_name_p (tree decl)
4926 /* Only FUNCTION_DECLs and VAR_DECLs are considered. */
4927 if (TREE_CODE (decl) != FUNCTION_DECL
4928 && TREE_CODE (decl) != VAR_DECL)
4929 return false;
4931 /* If DECL already has its assembler name set, it does not need a
4932 new one. */
4933 if (!HAS_DECL_ASSEMBLER_NAME_P (decl)
4934 || DECL_ASSEMBLER_NAME_SET_P (decl))
4935 return false;
4937 /* Abstract decls do not need an assembler name. */
4938 if (DECL_ABSTRACT (decl))
4939 return false;
4941 /* For VAR_DECLs, only static, public and external symbols need an
4942 assembler name. */
4943 if (TREE_CODE (decl) == VAR_DECL
4944 && !TREE_STATIC (decl)
4945 && !TREE_PUBLIC (decl)
4946 && !DECL_EXTERNAL (decl))
4947 return false;
4949 if (TREE_CODE (decl) == FUNCTION_DECL)
4951 /* Do not set assembler name on builtins. Allow RTL expansion to
4952 decide whether to expand inline or via a regular call. */
4953 if (DECL_BUILT_IN (decl)
4954 && DECL_BUILT_IN_CLASS (decl) != BUILT_IN_FRONTEND)
4955 return false;
4957 /* Functions represented in the callgraph need an assembler name. */
4958 if (cgraph_get_node (decl) != NULL)
4959 return true;
4961 /* Unused and not public functions don't need an assembler name. */
4962 if (!TREE_USED (decl) && !TREE_PUBLIC (decl))
4963 return false;
4966 return true;
4970 /* Reset all language specific information still present in symbol
4971 DECL. */
4973 static void
4974 free_lang_data_in_decl (tree decl)
4976 gcc_assert (DECL_P (decl));
4978 /* Give the FE a chance to remove its own data first. */
4979 lang_hooks.free_lang_data (decl);
4981 TREE_LANG_FLAG_0 (decl) = 0;
4982 TREE_LANG_FLAG_1 (decl) = 0;
4983 TREE_LANG_FLAG_2 (decl) = 0;
4984 TREE_LANG_FLAG_3 (decl) = 0;
4985 TREE_LANG_FLAG_4 (decl) = 0;
4986 TREE_LANG_FLAG_5 (decl) = 0;
4987 TREE_LANG_FLAG_6 (decl) = 0;
4989 free_lang_data_in_one_sizepos (&DECL_SIZE (decl));
4990 free_lang_data_in_one_sizepos (&DECL_SIZE_UNIT (decl));
4991 if (TREE_CODE (decl) == FIELD_DECL)
4993 free_lang_data_in_one_sizepos (&DECL_FIELD_OFFSET (decl));
4994 if (TREE_CODE (DECL_CONTEXT (decl)) == QUAL_UNION_TYPE)
4995 DECL_QUALIFIER (decl) = NULL_TREE;
4998 if (TREE_CODE (decl) == FUNCTION_DECL)
5000 struct cgraph_node *node;
5001 if (!(node = cgraph_get_node (decl))
5002 || (!node->definition && !node->clones))
5004 if (node)
5005 cgraph_release_function_body (node);
5006 else
5008 release_function_body (decl);
5009 DECL_ARGUMENTS (decl) = NULL;
5010 DECL_RESULT (decl) = NULL;
5011 DECL_INITIAL (decl) = error_mark_node;
5014 if (gimple_has_body_p (decl))
5016 tree t;
5018 /* If DECL has a gimple body, then the context for its
5019 arguments must be DECL. Otherwise, it doesn't really
5020 matter, as we will not be emitting any code for DECL. In
5021 general, there may be other instances of DECL created by
5022 the front end and since PARM_DECLs are generally shared,
5023 their DECL_CONTEXT changes as the replicas of DECL are
5024 created. The only time where DECL_CONTEXT is important
5025 is for the FUNCTION_DECLs that have a gimple body (since
5026 the PARM_DECL will be used in the function's body). */
5027 for (t = DECL_ARGUMENTS (decl); t; t = TREE_CHAIN (t))
5028 DECL_CONTEXT (t) = decl;
5031 /* DECL_SAVED_TREE holds the GENERIC representation for DECL.
5032 At this point, it is not needed anymore. */
5033 DECL_SAVED_TREE (decl) = NULL_TREE;
5035 /* Clear the abstract origin if it refers to a method. Otherwise
5036 dwarf2out.c will ICE as we clear TYPE_METHODS and thus the
5037 origin will not be output correctly. */
5038 if (DECL_ABSTRACT_ORIGIN (decl)
5039 && DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))
5040 && RECORD_OR_UNION_TYPE_P
5041 (DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))))
5042 DECL_ABSTRACT_ORIGIN (decl) = NULL_TREE;
5044 /* Sometimes the C++ frontend doesn't manage to transform a temporary
5045 DECL_VINDEX referring to itself into a vtable slot number as it
5046 should. Happens with functions that are copied and then forgotten
5047 about. Just clear it, it won't matter anymore. */
5048 if (DECL_VINDEX (decl) && !host_integerp (DECL_VINDEX (decl), 0))
5049 DECL_VINDEX (decl) = NULL_TREE;
5051 else if (TREE_CODE (decl) == VAR_DECL)
5053 if ((DECL_EXTERNAL (decl)
5054 && (!TREE_STATIC (decl) || !TREE_READONLY (decl)))
5055 || (decl_function_context (decl) && !TREE_STATIC (decl)))
5056 DECL_INITIAL (decl) = NULL_TREE;
5058 else if (TREE_CODE (decl) == TYPE_DECL
5059 || TREE_CODE (decl) == FIELD_DECL)
5060 DECL_INITIAL (decl) = NULL_TREE;
5061 else if (TREE_CODE (decl) == TRANSLATION_UNIT_DECL
5062 && DECL_INITIAL (decl)
5063 && TREE_CODE (DECL_INITIAL (decl)) == BLOCK)
5065 /* Strip builtins from the translation-unit BLOCK. We still have targets
5066 without builtin_decl_explicit support and also builtins are shared
5067 nodes and thus we can't use TREE_CHAIN in multiple lists. */
5068 tree *nextp = &BLOCK_VARS (DECL_INITIAL (decl));
5069 while (*nextp)
5071 tree var = *nextp;
5072 if (TREE_CODE (var) == FUNCTION_DECL
5073 && DECL_BUILT_IN (var))
5074 *nextp = TREE_CHAIN (var);
5075 else
5076 nextp = &TREE_CHAIN (var);
5082 /* Data used when collecting DECLs and TYPEs for language data removal. */
5084 struct free_lang_data_d
5086 /* Worklist to avoid excessive recursion. */
5087 vec<tree> worklist;
5089 /* Set of traversed objects. Used to avoid duplicate visits. */
5090 struct pointer_set_t *pset;
5092 /* Array of symbols to process with free_lang_data_in_decl. */
5093 vec<tree> decls;
5095 /* Array of types to process with free_lang_data_in_type. */
5096 vec<tree> types;
5100 /* Save all language fields needed to generate proper debug information
5101 for DECL. This saves most fields cleared out by free_lang_data_in_decl. */
5103 static void
5104 save_debug_info_for_decl (tree t)
5106 /*struct saved_debug_info_d *sdi;*/
5108 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && DECL_P (t));
5110 /* FIXME. Partial implementation for saving debug info removed. */
5114 /* Save all language fields needed to generate proper debug information
5115 for TYPE. This saves most fields cleared out by free_lang_data_in_type. */
5117 static void
5118 save_debug_info_for_type (tree t)
5120 /*struct saved_debug_info_d *sdi;*/
5122 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && TYPE_P (t));
5124 /* FIXME. Partial implementation for saving debug info removed. */
5128 /* Add type or decl T to one of the list of tree nodes that need their
5129 language data removed. The lists are held inside FLD. */
5131 static void
5132 add_tree_to_fld_list (tree t, struct free_lang_data_d *fld)
5134 if (DECL_P (t))
5136 fld->decls.safe_push (t);
5137 if (debug_info_level > DINFO_LEVEL_TERSE)
5138 save_debug_info_for_decl (t);
5140 else if (TYPE_P (t))
5142 fld->types.safe_push (t);
5143 if (debug_info_level > DINFO_LEVEL_TERSE)
5144 save_debug_info_for_type (t);
5146 else
5147 gcc_unreachable ();
5150 /* Push tree node T into FLD->WORKLIST. */
5152 static inline void
5153 fld_worklist_push (tree t, struct free_lang_data_d *fld)
5155 if (t && !is_lang_specific (t) && !pointer_set_contains (fld->pset, t))
5156 fld->worklist.safe_push ((t));
5160 /* Operand callback helper for free_lang_data_in_node. *TP is the
5161 subtree operand being considered. */
5163 static tree
5164 find_decls_types_r (tree *tp, int *ws, void *data)
5166 tree t = *tp;
5167 struct free_lang_data_d *fld = (struct free_lang_data_d *) data;
5169 if (TREE_CODE (t) == TREE_LIST)
5170 return NULL_TREE;
5172 /* Language specific nodes will be removed, so there is no need
5173 to gather anything under them. */
5174 if (is_lang_specific (t))
5176 *ws = 0;
5177 return NULL_TREE;
5180 if (DECL_P (t))
5182 /* Note that walk_tree does not traverse every possible field in
5183 decls, so we have to do our own traversals here. */
5184 add_tree_to_fld_list (t, fld);
5186 fld_worklist_push (DECL_NAME (t), fld);
5187 fld_worklist_push (DECL_CONTEXT (t), fld);
5188 fld_worklist_push (DECL_SIZE (t), fld);
5189 fld_worklist_push (DECL_SIZE_UNIT (t), fld);
5191 /* We are going to remove everything under DECL_INITIAL for
5192 TYPE_DECLs. No point walking them. */
5193 if (TREE_CODE (t) != TYPE_DECL)
5194 fld_worklist_push (DECL_INITIAL (t), fld);
5196 fld_worklist_push (DECL_ATTRIBUTES (t), fld);
5197 fld_worklist_push (DECL_ABSTRACT_ORIGIN (t), fld);
5199 if (TREE_CODE (t) == FUNCTION_DECL)
5201 fld_worklist_push (DECL_ARGUMENTS (t), fld);
5202 fld_worklist_push (DECL_RESULT (t), fld);
5204 else if (TREE_CODE (t) == TYPE_DECL)
5206 fld_worklist_push (DECL_ARGUMENT_FLD (t), fld);
5207 fld_worklist_push (DECL_VINDEX (t), fld);
5208 fld_worklist_push (DECL_ORIGINAL_TYPE (t), fld);
5210 else if (TREE_CODE (t) == FIELD_DECL)
5212 fld_worklist_push (DECL_FIELD_OFFSET (t), fld);
5213 fld_worklist_push (DECL_BIT_FIELD_TYPE (t), fld);
5214 fld_worklist_push (DECL_FIELD_BIT_OFFSET (t), fld);
5215 fld_worklist_push (DECL_FCONTEXT (t), fld);
5217 else if (TREE_CODE (t) == VAR_DECL)
5219 fld_worklist_push (DECL_SECTION_NAME (t), fld);
5220 fld_worklist_push (DECL_COMDAT_GROUP (t), fld);
5223 if ((TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
5224 && DECL_HAS_VALUE_EXPR_P (t))
5225 fld_worklist_push (DECL_VALUE_EXPR (t), fld);
5227 if (TREE_CODE (t) != FIELD_DECL
5228 && TREE_CODE (t) != TYPE_DECL)
5229 fld_worklist_push (TREE_CHAIN (t), fld);
5230 *ws = 0;
5232 else if (TYPE_P (t))
5234 /* Note that walk_tree does not traverse every possible field in
5235 types, so we have to do our own traversals here. */
5236 add_tree_to_fld_list (t, fld);
5238 if (!RECORD_OR_UNION_TYPE_P (t))
5239 fld_worklist_push (TYPE_CACHED_VALUES (t), fld);
5240 fld_worklist_push (TYPE_SIZE (t), fld);
5241 fld_worklist_push (TYPE_SIZE_UNIT (t), fld);
5242 fld_worklist_push (TYPE_ATTRIBUTES (t), fld);
5243 fld_worklist_push (TYPE_POINTER_TO (t), fld);
5244 fld_worklist_push (TYPE_REFERENCE_TO (t), fld);
5245 fld_worklist_push (TYPE_NAME (t), fld);
5246 /* Do not walk TYPE_NEXT_PTR_TO or TYPE_NEXT_REF_TO. We do not stream
5247 them and thus do not and want not to reach unused pointer types
5248 this way. */
5249 if (!POINTER_TYPE_P (t))
5250 fld_worklist_push (TYPE_MINVAL (t), fld);
5251 if (!RECORD_OR_UNION_TYPE_P (t))
5252 fld_worklist_push (TYPE_MAXVAL (t), fld);
5253 fld_worklist_push (TYPE_MAIN_VARIANT (t), fld);
5254 /* Do not walk TYPE_NEXT_VARIANT. We do not stream it and thus
5255 do not and want not to reach unused variants this way. */
5256 if (TYPE_CONTEXT (t))
5258 tree ctx = TYPE_CONTEXT (t);
5259 /* We adjust BLOCK TYPE_CONTEXTs to the innermost non-BLOCK one.
5260 So push that instead. */
5261 while (ctx && TREE_CODE (ctx) == BLOCK)
5262 ctx = BLOCK_SUPERCONTEXT (ctx);
5263 fld_worklist_push (ctx, fld);
5265 /* Do not walk TYPE_CANONICAL. We do not stream it and thus do not
5266 and want not to reach unused types this way. */
5268 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t))
5270 unsigned i;
5271 tree tem;
5272 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (TYPE_BINFO (t)), i, tem)
5273 fld_worklist_push (TREE_TYPE (tem), fld);
5274 tem = BINFO_VIRTUALS (TYPE_BINFO (t));
5275 if (tem
5276 /* The Java FE overloads BINFO_VIRTUALS for its own purpose. */
5277 && TREE_CODE (tem) == TREE_LIST)
5280 fld_worklist_push (TREE_VALUE (tem), fld);
5281 tem = TREE_CHAIN (tem);
5283 while (tem);
5285 if (RECORD_OR_UNION_TYPE_P (t))
5287 tree tem;
5288 /* Push all TYPE_FIELDS - there can be interleaving interesting
5289 and non-interesting things. */
5290 tem = TYPE_FIELDS (t);
5291 while (tem)
5293 if (TREE_CODE (tem) == FIELD_DECL
5294 || TREE_CODE (tem) == TYPE_DECL)
5295 fld_worklist_push (tem, fld);
5296 tem = TREE_CHAIN (tem);
5300 fld_worklist_push (TYPE_STUB_DECL (t), fld);
5301 *ws = 0;
5303 else if (TREE_CODE (t) == BLOCK)
5305 tree tem;
5306 for (tem = BLOCK_VARS (t); tem; tem = TREE_CHAIN (tem))
5307 fld_worklist_push (tem, fld);
5308 for (tem = BLOCK_SUBBLOCKS (t); tem; tem = BLOCK_CHAIN (tem))
5309 fld_worklist_push (tem, fld);
5310 fld_worklist_push (BLOCK_ABSTRACT_ORIGIN (t), fld);
5313 if (TREE_CODE (t) != IDENTIFIER_NODE
5314 && CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED))
5315 fld_worklist_push (TREE_TYPE (t), fld);
5317 return NULL_TREE;
5321 /* Find decls and types in T. */
5323 static void
5324 find_decls_types (tree t, struct free_lang_data_d *fld)
5326 while (1)
5328 if (!pointer_set_contains (fld->pset, t))
5329 walk_tree (&t, find_decls_types_r, fld, fld->pset);
5330 if (fld->worklist.is_empty ())
5331 break;
5332 t = fld->worklist.pop ();
5336 /* Translate all the types in LIST with the corresponding runtime
5337 types. */
5339 static tree
5340 get_eh_types_for_runtime (tree list)
5342 tree head, prev;
5344 if (list == NULL_TREE)
5345 return NULL_TREE;
5347 head = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5348 prev = head;
5349 list = TREE_CHAIN (list);
5350 while (list)
5352 tree n = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5353 TREE_CHAIN (prev) = n;
5354 prev = TREE_CHAIN (prev);
5355 list = TREE_CHAIN (list);
5358 return head;
5362 /* Find decls and types referenced in EH region R and store them in
5363 FLD->DECLS and FLD->TYPES. */
5365 static void
5366 find_decls_types_in_eh_region (eh_region r, struct free_lang_data_d *fld)
5368 switch (r->type)
5370 case ERT_CLEANUP:
5371 break;
5373 case ERT_TRY:
5375 eh_catch c;
5377 /* The types referenced in each catch must first be changed to the
5378 EH types used at runtime. This removes references to FE types
5379 in the region. */
5380 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
5382 c->type_list = get_eh_types_for_runtime (c->type_list);
5383 walk_tree (&c->type_list, find_decls_types_r, fld, fld->pset);
5386 break;
5388 case ERT_ALLOWED_EXCEPTIONS:
5389 r->u.allowed.type_list
5390 = get_eh_types_for_runtime (r->u.allowed.type_list);
5391 walk_tree (&r->u.allowed.type_list, find_decls_types_r, fld, fld->pset);
5392 break;
5394 case ERT_MUST_NOT_THROW:
5395 walk_tree (&r->u.must_not_throw.failure_decl,
5396 find_decls_types_r, fld, fld->pset);
5397 break;
5402 /* Find decls and types referenced in cgraph node N and store them in
5403 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5404 look for *every* kind of DECL and TYPE node reachable from N,
5405 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5406 NAMESPACE_DECLs, etc). */
5408 static void
5409 find_decls_types_in_node (struct cgraph_node *n, struct free_lang_data_d *fld)
5411 basic_block bb;
5412 struct function *fn;
5413 unsigned ix;
5414 tree t;
5416 find_decls_types (n->decl, fld);
5418 if (!gimple_has_body_p (n->decl))
5419 return;
5421 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
5423 fn = DECL_STRUCT_FUNCTION (n->decl);
5425 /* Traverse locals. */
5426 FOR_EACH_LOCAL_DECL (fn, ix, t)
5427 find_decls_types (t, fld);
5429 /* Traverse EH regions in FN. */
5431 eh_region r;
5432 FOR_ALL_EH_REGION_FN (r, fn)
5433 find_decls_types_in_eh_region (r, fld);
5436 /* Traverse every statement in FN. */
5437 FOR_EACH_BB_FN (bb, fn)
5439 gimple_stmt_iterator si;
5440 unsigned i;
5442 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
5444 gimple phi = gsi_stmt (si);
5446 for (i = 0; i < gimple_phi_num_args (phi); i++)
5448 tree *arg_p = gimple_phi_arg_def_ptr (phi, i);
5449 find_decls_types (*arg_p, fld);
5453 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
5455 gimple stmt = gsi_stmt (si);
5457 if (is_gimple_call (stmt))
5458 find_decls_types (gimple_call_fntype (stmt), fld);
5460 for (i = 0; i < gimple_num_ops (stmt); i++)
5462 tree arg = gimple_op (stmt, i);
5463 find_decls_types (arg, fld);
5470 /* Find decls and types referenced in varpool node N and store them in
5471 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5472 look for *every* kind of DECL and TYPE node reachable from N,
5473 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5474 NAMESPACE_DECLs, etc). */
5476 static void
5477 find_decls_types_in_var (struct varpool_node *v, struct free_lang_data_d *fld)
5479 find_decls_types (v->decl, fld);
5482 /* If T needs an assembler name, have one created for it. */
5484 void
5485 assign_assembler_name_if_neeeded (tree t)
5487 if (need_assembler_name_p (t))
5489 /* When setting DECL_ASSEMBLER_NAME, the C++ mangler may emit
5490 diagnostics that use input_location to show locus
5491 information. The problem here is that, at this point,
5492 input_location is generally anchored to the end of the file
5493 (since the parser is long gone), so we don't have a good
5494 position to pin it to.
5496 To alleviate this problem, this uses the location of T's
5497 declaration. Examples of this are
5498 testsuite/g++.dg/template/cond2.C and
5499 testsuite/g++.dg/template/pr35240.C. */
5500 location_t saved_location = input_location;
5501 input_location = DECL_SOURCE_LOCATION (t);
5503 decl_assembler_name (t);
5505 input_location = saved_location;
5510 /* Free language specific information for every operand and expression
5511 in every node of the call graph. This process operates in three stages:
5513 1- Every callgraph node and varpool node is traversed looking for
5514 decls and types embedded in them. This is a more exhaustive
5515 search than that done by find_referenced_vars, because it will
5516 also collect individual fields, decls embedded in types, etc.
5518 2- All the decls found are sent to free_lang_data_in_decl.
5520 3- All the types found are sent to free_lang_data_in_type.
5522 The ordering between decls and types is important because
5523 free_lang_data_in_decl sets assembler names, which includes
5524 mangling. So types cannot be freed up until assembler names have
5525 been set up. */
5527 static void
5528 free_lang_data_in_cgraph (void)
5530 struct cgraph_node *n;
5531 struct varpool_node *v;
5532 struct free_lang_data_d fld;
5533 tree t;
5534 unsigned i;
5535 alias_pair *p;
5537 /* Initialize sets and arrays to store referenced decls and types. */
5538 fld.pset = pointer_set_create ();
5539 fld.worklist.create (0);
5540 fld.decls.create (100);
5541 fld.types.create (100);
5543 /* Find decls and types in the body of every function in the callgraph. */
5544 FOR_EACH_FUNCTION (n)
5545 find_decls_types_in_node (n, &fld);
5547 FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
5548 find_decls_types (p->decl, &fld);
5550 /* Find decls and types in every varpool symbol. */
5551 FOR_EACH_VARIABLE (v)
5552 find_decls_types_in_var (v, &fld);
5554 /* Set the assembler name on every decl found. We need to do this
5555 now because free_lang_data_in_decl will invalidate data needed
5556 for mangling. This breaks mangling on interdependent decls. */
5557 FOR_EACH_VEC_ELT (fld.decls, i, t)
5558 assign_assembler_name_if_neeeded (t);
5560 /* Traverse every decl found freeing its language data. */
5561 FOR_EACH_VEC_ELT (fld.decls, i, t)
5562 free_lang_data_in_decl (t);
5564 /* Traverse every type found freeing its language data. */
5565 FOR_EACH_VEC_ELT (fld.types, i, t)
5566 free_lang_data_in_type (t);
5568 pointer_set_destroy (fld.pset);
5569 fld.worklist.release ();
5570 fld.decls.release ();
5571 fld.types.release ();
5575 /* Free resources that are used by FE but are not needed once they are done. */
5577 static unsigned
5578 free_lang_data (void)
5580 unsigned i;
5582 /* If we are the LTO frontend we have freed lang-specific data already. */
5583 if (in_lto_p
5584 || !flag_generate_lto)
5585 return 0;
5587 /* Allocate and assign alias sets to the standard integer types
5588 while the slots are still in the way the frontends generated them. */
5589 for (i = 0; i < itk_none; ++i)
5590 if (integer_types[i])
5591 TYPE_ALIAS_SET (integer_types[i]) = get_alias_set (integer_types[i]);
5593 /* Traverse the IL resetting language specific information for
5594 operands, expressions, etc. */
5595 free_lang_data_in_cgraph ();
5597 /* Create gimple variants for common types. */
5598 ptrdiff_type_node = integer_type_node;
5599 fileptr_type_node = ptr_type_node;
5601 /* Reset some langhooks. Do not reset types_compatible_p, it may
5602 still be used indirectly via the get_alias_set langhook. */
5603 lang_hooks.dwarf_name = lhd_dwarf_name;
5604 lang_hooks.decl_printable_name = gimple_decl_printable_name;
5605 /* We do not want the default decl_assembler_name implementation,
5606 rather if we have fixed everything we want a wrapper around it
5607 asserting that all non-local symbols already got their assembler
5608 name and only produce assembler names for local symbols. Or rather
5609 make sure we never call decl_assembler_name on local symbols and
5610 devise a separate, middle-end private scheme for it. */
5612 /* Reset diagnostic machinery. */
5613 tree_diagnostics_defaults (global_dc);
5615 return 0;
5619 namespace {
5621 const pass_data pass_data_ipa_free_lang_data =
5623 SIMPLE_IPA_PASS, /* type */
5624 "*free_lang_data", /* name */
5625 OPTGROUP_NONE, /* optinfo_flags */
5626 false, /* has_gate */
5627 true, /* has_execute */
5628 TV_IPA_FREE_LANG_DATA, /* tv_id */
5629 0, /* properties_required */
5630 0, /* properties_provided */
5631 0, /* properties_destroyed */
5632 0, /* todo_flags_start */
5633 0, /* todo_flags_finish */
5636 class pass_ipa_free_lang_data : public simple_ipa_opt_pass
5638 public:
5639 pass_ipa_free_lang_data (gcc::context *ctxt)
5640 : simple_ipa_opt_pass (pass_data_ipa_free_lang_data, ctxt)
5643 /* opt_pass methods: */
5644 unsigned int execute () { return free_lang_data (); }
5646 }; // class pass_ipa_free_lang_data
5648 } // anon namespace
5650 simple_ipa_opt_pass *
5651 make_pass_ipa_free_lang_data (gcc::context *ctxt)
5653 return new pass_ipa_free_lang_data (ctxt);
5656 /* The backbone of is_attribute_p(). ATTR_LEN is the string length of
5657 ATTR_NAME. Also used internally by remove_attribute(). */
5658 bool
5659 private_is_attribute_p (const char *attr_name, size_t attr_len, const_tree ident)
5661 size_t ident_len = IDENTIFIER_LENGTH (ident);
5663 if (ident_len == attr_len)
5665 if (strcmp (attr_name, IDENTIFIER_POINTER (ident)) == 0)
5666 return true;
5668 else if (ident_len == attr_len + 4)
5670 /* There is the possibility that ATTR is 'text' and IDENT is
5671 '__text__'. */
5672 const char *p = IDENTIFIER_POINTER (ident);
5673 if (p[0] == '_' && p[1] == '_'
5674 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5675 && strncmp (attr_name, p + 2, attr_len) == 0)
5676 return true;
5679 return false;
5682 /* The backbone of lookup_attribute(). ATTR_LEN is the string length
5683 of ATTR_NAME, and LIST is not NULL_TREE. */
5684 tree
5685 private_lookup_attribute (const char *attr_name, size_t attr_len, tree list)
5687 while (list)
5689 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5691 if (ident_len == attr_len)
5693 if (!strcmp (attr_name,
5694 IDENTIFIER_POINTER (get_attribute_name (list))))
5695 break;
5697 /* TODO: If we made sure that attributes were stored in the
5698 canonical form without '__...__' (ie, as in 'text' as opposed
5699 to '__text__') then we could avoid the following case. */
5700 else if (ident_len == attr_len + 4)
5702 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5703 if (p[0] == '_' && p[1] == '_'
5704 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5705 && strncmp (attr_name, p + 2, attr_len) == 0)
5706 break;
5708 list = TREE_CHAIN (list);
5711 return list;
5714 /* A variant of lookup_attribute() that can be used with an identifier
5715 as the first argument, and where the identifier can be either
5716 'text' or '__text__'.
5718 Given an attribute ATTR_IDENTIFIER, and a list of attributes LIST,
5719 return a pointer to the attribute's list element if the attribute
5720 is part of the list, or NULL_TREE if not found. If the attribute
5721 appears more than once, this only returns the first occurrence; the
5722 TREE_CHAIN of the return value should be passed back in if further
5723 occurrences are wanted. ATTR_IDENTIFIER must be an identifier but
5724 can be in the form 'text' or '__text__'. */
5725 static tree
5726 lookup_ident_attribute (tree attr_identifier, tree list)
5728 gcc_checking_assert (TREE_CODE (attr_identifier) == IDENTIFIER_NODE);
5730 while (list)
5732 gcc_checking_assert (TREE_CODE (get_attribute_name (list))
5733 == IDENTIFIER_NODE);
5735 /* Identifiers can be compared directly for equality. */
5736 if (attr_identifier == get_attribute_name (list))
5737 break;
5739 /* If they are not equal, they may still be one in the form
5740 'text' while the other one is in the form '__text__'. TODO:
5741 If we were storing attributes in normalized 'text' form, then
5742 this could all go away and we could take full advantage of
5743 the fact that we're comparing identifiers. :-) */
5745 size_t attr_len = IDENTIFIER_LENGTH (attr_identifier);
5746 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5748 if (ident_len == attr_len + 4)
5750 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5751 const char *q = IDENTIFIER_POINTER (attr_identifier);
5752 if (p[0] == '_' && p[1] == '_'
5753 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5754 && strncmp (q, p + 2, attr_len) == 0)
5755 break;
5757 else if (ident_len + 4 == attr_len)
5759 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5760 const char *q = IDENTIFIER_POINTER (attr_identifier);
5761 if (q[0] == '_' && q[1] == '_'
5762 && q[attr_len - 2] == '_' && q[attr_len - 1] == '_'
5763 && strncmp (q + 2, p, ident_len) == 0)
5764 break;
5767 list = TREE_CHAIN (list);
5770 return list;
5773 /* Remove any instances of attribute ATTR_NAME in LIST and return the
5774 modified list. */
5776 tree
5777 remove_attribute (const char *attr_name, tree list)
5779 tree *p;
5780 size_t attr_len = strlen (attr_name);
5782 gcc_checking_assert (attr_name[0] != '_');
5784 for (p = &list; *p; )
5786 tree l = *p;
5787 /* TODO: If we were storing attributes in normalized form, here
5788 we could use a simple strcmp(). */
5789 if (private_is_attribute_p (attr_name, attr_len, get_attribute_name (l)))
5790 *p = TREE_CHAIN (l);
5791 else
5792 p = &TREE_CHAIN (l);
5795 return list;
5798 /* Return an attribute list that is the union of a1 and a2. */
5800 tree
5801 merge_attributes (tree a1, tree a2)
5803 tree attributes;
5805 /* Either one unset? Take the set one. */
5807 if ((attributes = a1) == 0)
5808 attributes = a2;
5810 /* One that completely contains the other? Take it. */
5812 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
5814 if (attribute_list_contained (a2, a1))
5815 attributes = a2;
5816 else
5818 /* Pick the longest list, and hang on the other list. */
5820 if (list_length (a1) < list_length (a2))
5821 attributes = a2, a2 = a1;
5823 for (; a2 != 0; a2 = TREE_CHAIN (a2))
5825 tree a;
5826 for (a = lookup_ident_attribute (get_attribute_name (a2),
5827 attributes);
5828 a != NULL_TREE && !attribute_value_equal (a, a2);
5829 a = lookup_ident_attribute (get_attribute_name (a2),
5830 TREE_CHAIN (a)))
5832 if (a == NULL_TREE)
5834 a1 = copy_node (a2);
5835 TREE_CHAIN (a1) = attributes;
5836 attributes = a1;
5841 return attributes;
5844 /* Given types T1 and T2, merge their attributes and return
5845 the result. */
5847 tree
5848 merge_type_attributes (tree t1, tree t2)
5850 return merge_attributes (TYPE_ATTRIBUTES (t1),
5851 TYPE_ATTRIBUTES (t2));
5854 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
5855 the result. */
5857 tree
5858 merge_decl_attributes (tree olddecl, tree newdecl)
5860 return merge_attributes (DECL_ATTRIBUTES (olddecl),
5861 DECL_ATTRIBUTES (newdecl));
5864 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
5866 /* Specialization of merge_decl_attributes for various Windows targets.
5868 This handles the following situation:
5870 __declspec (dllimport) int foo;
5871 int foo;
5873 The second instance of `foo' nullifies the dllimport. */
5875 tree
5876 merge_dllimport_decl_attributes (tree old, tree new_tree)
5878 tree a;
5879 int delete_dllimport_p = 1;
5881 /* What we need to do here is remove from `old' dllimport if it doesn't
5882 appear in `new'. dllimport behaves like extern: if a declaration is
5883 marked dllimport and a definition appears later, then the object
5884 is not dllimport'd. We also remove a `new' dllimport if the old list
5885 contains dllexport: dllexport always overrides dllimport, regardless
5886 of the order of declaration. */
5887 if (!VAR_OR_FUNCTION_DECL_P (new_tree))
5888 delete_dllimport_p = 0;
5889 else if (DECL_DLLIMPORT_P (new_tree)
5890 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
5892 DECL_DLLIMPORT_P (new_tree) = 0;
5893 warning (OPT_Wattributes, "%q+D already declared with dllexport attribute: "
5894 "dllimport ignored", new_tree);
5896 else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new_tree))
5898 /* Warn about overriding a symbol that has already been used, e.g.:
5899 extern int __attribute__ ((dllimport)) foo;
5900 int* bar () {return &foo;}
5901 int foo;
5903 if (TREE_USED (old))
5905 warning (0, "%q+D redeclared without dllimport attribute "
5906 "after being referenced with dll linkage", new_tree);
5907 /* If we have used a variable's address with dllimport linkage,
5908 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
5909 decl may already have had TREE_CONSTANT computed.
5910 We still remove the attribute so that assembler code refers
5911 to '&foo rather than '_imp__foo'. */
5912 if (TREE_CODE (old) == VAR_DECL && TREE_ADDRESSABLE (old))
5913 DECL_DLLIMPORT_P (new_tree) = 1;
5916 /* Let an inline definition silently override the external reference,
5917 but otherwise warn about attribute inconsistency. */
5918 else if (TREE_CODE (new_tree) == VAR_DECL
5919 || !DECL_DECLARED_INLINE_P (new_tree))
5920 warning (OPT_Wattributes, "%q+D redeclared without dllimport attribute: "
5921 "previous dllimport ignored", new_tree);
5923 else
5924 delete_dllimport_p = 0;
5926 a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new_tree));
5928 if (delete_dllimport_p)
5929 a = remove_attribute ("dllimport", a);
5931 return a;
5934 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
5935 struct attribute_spec.handler. */
5937 tree
5938 handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
5939 bool *no_add_attrs)
5941 tree node = *pnode;
5942 bool is_dllimport;
5944 /* These attributes may apply to structure and union types being created,
5945 but otherwise should pass to the declaration involved. */
5946 if (!DECL_P (node))
5948 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
5949 | (int) ATTR_FLAG_ARRAY_NEXT))
5951 *no_add_attrs = true;
5952 return tree_cons (name, args, NULL_TREE);
5954 if (TREE_CODE (node) == RECORD_TYPE
5955 || TREE_CODE (node) == UNION_TYPE)
5957 node = TYPE_NAME (node);
5958 if (!node)
5959 return NULL_TREE;
5961 else
5963 warning (OPT_Wattributes, "%qE attribute ignored",
5964 name);
5965 *no_add_attrs = true;
5966 return NULL_TREE;
5970 if (TREE_CODE (node) != FUNCTION_DECL
5971 && TREE_CODE (node) != VAR_DECL
5972 && TREE_CODE (node) != TYPE_DECL)
5974 *no_add_attrs = true;
5975 warning (OPT_Wattributes, "%qE attribute ignored",
5976 name);
5977 return NULL_TREE;
5980 if (TREE_CODE (node) == TYPE_DECL
5981 && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE
5982 && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE)
5984 *no_add_attrs = true;
5985 warning (OPT_Wattributes, "%qE attribute ignored",
5986 name);
5987 return NULL_TREE;
5990 is_dllimport = is_attribute_p ("dllimport", name);
5992 /* Report error on dllimport ambiguities seen now before they cause
5993 any damage. */
5994 if (is_dllimport)
5996 /* Honor any target-specific overrides. */
5997 if (!targetm.valid_dllimport_attribute_p (node))
5998 *no_add_attrs = true;
6000 else if (TREE_CODE (node) == FUNCTION_DECL
6001 && DECL_DECLARED_INLINE_P (node))
6003 warning (OPT_Wattributes, "inline function %q+D declared as "
6004 " dllimport: attribute ignored", node);
6005 *no_add_attrs = true;
6007 /* Like MS, treat definition of dllimported variables and
6008 non-inlined functions on declaration as syntax errors. */
6009 else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
6011 error ("function %q+D definition is marked dllimport", node);
6012 *no_add_attrs = true;
6015 else if (TREE_CODE (node) == VAR_DECL)
6017 if (DECL_INITIAL (node))
6019 error ("variable %q+D definition is marked dllimport",
6020 node);
6021 *no_add_attrs = true;
6024 /* `extern' needn't be specified with dllimport.
6025 Specify `extern' now and hope for the best. Sigh. */
6026 DECL_EXTERNAL (node) = 1;
6027 /* Also, implicitly give dllimport'd variables declared within
6028 a function global scope, unless declared static. */
6029 if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
6030 TREE_PUBLIC (node) = 1;
6033 if (*no_add_attrs == false)
6034 DECL_DLLIMPORT_P (node) = 1;
6036 else if (TREE_CODE (node) == FUNCTION_DECL
6037 && DECL_DECLARED_INLINE_P (node)
6038 && flag_keep_inline_dllexport)
6039 /* An exported function, even if inline, must be emitted. */
6040 DECL_EXTERNAL (node) = 0;
6042 /* Report error if symbol is not accessible at global scope. */
6043 if (!TREE_PUBLIC (node)
6044 && (TREE_CODE (node) == VAR_DECL
6045 || TREE_CODE (node) == FUNCTION_DECL))
6047 error ("external linkage required for symbol %q+D because of "
6048 "%qE attribute", node, name);
6049 *no_add_attrs = true;
6052 /* A dllexport'd entity must have default visibility so that other
6053 program units (shared libraries or the main executable) can see
6054 it. A dllimport'd entity must have default visibility so that
6055 the linker knows that undefined references within this program
6056 unit can be resolved by the dynamic linker. */
6057 if (!*no_add_attrs)
6059 if (DECL_VISIBILITY_SPECIFIED (node)
6060 && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT)
6061 error ("%qE implies default visibility, but %qD has already "
6062 "been declared with a different visibility",
6063 name, node);
6064 DECL_VISIBILITY (node) = VISIBILITY_DEFAULT;
6065 DECL_VISIBILITY_SPECIFIED (node) = 1;
6068 return NULL_TREE;
6071 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
6073 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
6074 of the various TYPE_QUAL values. Also, set the UPC layout qualifier,
6075 which is either null or a reference to an integral constant. */
6077 static void
6078 set_type_quals (tree type, int type_quals, tree layout_qualifier)
6080 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
6081 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
6082 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
6083 TYPE_ATOMIC (type) = (type_quals & TYPE_QUAL_ATOMIC) != 0;
6084 TYPE_ADDR_SPACE (type) = DECODE_QUAL_ADDR_SPACE (type_quals);
6085 TYPE_SHARED (type) = (type_quals & TYPE_QUAL_SHARED) != 0;
6086 TYPE_STRICT (type) = (type_quals & TYPE_QUAL_STRICT) != 0;
6087 TYPE_RELAXED (type) = (type_quals & TYPE_QUAL_RELAXED) != 0;
6088 if (TYPE_SHARED (type))
6089 SET_TYPE_BLOCK_FACTOR (type, layout_qualifier);
6092 /* Returns true iff CAND is equivalent to BASE with
6093 TYPE_QUALS and LAYOUT_QUALIFIER. */
6095 bool
6096 check_qualified_type (const_tree cand, const_tree base,
6097 int type_quals, tree layout_qualifier)
6099 return (TYPE_QUALS (cand) == type_quals
6100 && (TYPE_BLOCK_FACTOR (cand) == layout_qualifier
6101 || tree_int_cst_equal (TYPE_BLOCK_FACTOR (cand),
6102 layout_qualifier))
6103 && TYPE_NAME (cand) == TYPE_NAME (base)
6104 /* Apparently this is needed for Objective-C. */
6105 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
6106 /* Check alignment. */
6107 && TYPE_ALIGN (cand) == TYPE_ALIGN (base)
6108 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
6109 TYPE_ATTRIBUTES (base)));
6112 /* Returns true iff CAND is equivalent to BASE with ALIGN. */
6114 static bool
6115 check_aligned_type (const_tree cand, const_tree base, unsigned int align)
6117 return (TYPE_QUALS (cand) == TYPE_QUALS (base)
6118 && (TYPE_BLOCK_FACTOR (cand) == TYPE_BLOCK_FACTOR (base)
6119 || tree_int_cst_equal (TYPE_BLOCK_FACTOR (cand),
6120 TYPE_BLOCK_FACTOR (base)))
6121 && TYPE_NAME (cand) == TYPE_NAME (base)
6122 /* Apparently this is needed for Objective-C. */
6123 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
6124 /* Check alignment. */
6125 && TYPE_ALIGN (cand) == align
6126 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
6127 TYPE_ATTRIBUTES (base)));
6130 /* This function checks to see if TYPE matches the size one of the built-in
6131 atomic types, and returns that core atomic type. */
6133 static tree
6134 find_atomic_core_type (tree type)
6136 tree base_atomic_type;
6138 /* Only handle complete types. */
6139 if (TYPE_SIZE (type) == NULL_TREE)
6140 return NULL_TREE;
6142 HOST_WIDE_INT type_size = tree_low_cst (TYPE_SIZE (type), 1);
6143 switch (type_size)
6145 case 8:
6146 base_atomic_type = atomicQI_type_node;
6147 break;
6149 case 16:
6150 base_atomic_type = atomicHI_type_node;
6151 break;
6153 case 32:
6154 base_atomic_type = atomicSI_type_node;
6155 break;
6157 case 64:
6158 base_atomic_type = atomicDI_type_node;
6159 break;
6161 case 128:
6162 base_atomic_type = atomicTI_type_node;
6163 break;
6165 default:
6166 base_atomic_type = NULL_TREE;
6169 return base_atomic_type;
6172 /* Return a version of the TYPE, qualified as indicated by the
6173 TYPE_QUALS, if one exists. If no qualified version exists yet,
6174 return NULL_TREE. */
6176 tree
6177 get_qualified_type_1 (tree type, int type_quals, tree layout_qualifier)
6179 tree t;
6181 if (TYPE_QUALS (type) == type_quals)
6182 return type;
6184 /* Search the chain of variants to see if there is already one there just
6185 like the one we need to have. If so, use that existing one. We must
6186 preserve the TYPE_NAME, since there is code that depends on this. */
6187 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6188 if (check_qualified_type (t, type, type_quals, layout_qualifier))
6189 return t;
6191 return NULL_TREE;
6194 /* Like get_qualified_type_1, but creates the type if it does not
6195 exist. This function never returns NULL_TREE. */
6197 tree
6198 build_qualified_type_1 (tree type, int type_quals, tree layout_qualifier)
6200 tree t;
6202 /* See if we already have the appropriate qualified variant. */
6203 t = get_qualified_type_1 (type, type_quals, layout_qualifier);
6205 /* If not, build it. */
6206 if (!t)
6208 t = build_variant_type_copy (type);
6209 set_type_quals (t, type_quals, layout_qualifier);
6211 if (((type_quals & TYPE_QUAL_ATOMIC) == TYPE_QUAL_ATOMIC))
6213 /* See if this object can map to a basic atomic type. */
6214 tree atomic_type = find_atomic_core_type (type);
6215 if (atomic_type)
6217 /* Ensure the alignment of this type is compatible with
6218 the required alignment of the atomic type. */
6219 if (TYPE_ALIGN (atomic_type) > TYPE_ALIGN (t))
6220 TYPE_ALIGN (t) = TYPE_ALIGN (atomic_type);
6224 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6225 /* Propagate structural equality. */
6226 SET_TYPE_STRUCTURAL_EQUALITY (t);
6227 else if (TYPE_CANONICAL (type) != type)
6228 /* Build the underlying canonical type, since it is different
6229 from TYPE. */
6230 TYPE_CANONICAL (t) = build_qualified_type (TYPE_CANONICAL (type),
6231 type_quals);
6232 else
6233 /* T is its own canonical type. */
6234 TYPE_CANONICAL (t) = t;
6238 return t;
6241 /* Return a variant of TYPE, where all UPC qualifiers
6242 have been removed. */
6244 tree
6245 build_upc_unshared_type (tree type)
6247 tree u_type = type;
6248 if (TREE_CODE (type) == ARRAY_TYPE)
6250 const tree elem_type = TREE_TYPE(type);
6251 const tree u_elem_type = build_upc_unshared_type (elem_type);
6252 if (u_elem_type != elem_type)
6254 for (u_type = TYPE_MAIN_VARIANT (type);
6255 u_type && TREE_TYPE(u_type) != u_elem_type;
6256 u_type = TYPE_NEXT_VARIANT (u_type)) /* loop */;
6257 if (!u_type)
6259 u_type = build_variant_type_copy (type);
6260 TREE_TYPE (u_type) = u_elem_type;
6264 else
6266 const int quals = TYPE_QUALS (type);
6267 const int u_quals = quals & ~(TYPE_QUAL_SHARED
6268 | TYPE_QUAL_RELAXED
6269 | TYPE_QUAL_STRICT);
6270 u_type = build_qualified_type (type, u_quals);
6272 return u_type;
6275 /* Create a variant of type T with alignment ALIGN. */
6277 tree
6278 build_aligned_type (tree type, unsigned int align)
6280 tree t;
6282 if (TYPE_PACKED (type)
6283 || TYPE_ALIGN (type) == align)
6284 return type;
6286 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6287 if (check_aligned_type (t, type, align))
6288 return t;
6290 t = build_variant_type_copy (type);
6291 TYPE_ALIGN (t) = align;
6293 return t;
6296 /* Create a new distinct copy of TYPE. The new type is made its own
6297 MAIN_VARIANT. If TYPE requires structural equality checks, the
6298 resulting type requires structural equality checks; otherwise, its
6299 TYPE_CANONICAL points to itself. */
6301 tree
6302 build_distinct_type_copy (tree type)
6304 tree t = copy_node (type);
6306 TYPE_POINTER_TO (t) = 0;
6307 TYPE_REFERENCE_TO (t) = 0;
6309 /* Set the canonical type either to a new equivalence class, or
6310 propagate the need for structural equality checks. */
6311 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6312 SET_TYPE_STRUCTURAL_EQUALITY (t);
6313 else
6314 TYPE_CANONICAL (t) = t;
6316 /* Make it its own variant. */
6317 TYPE_MAIN_VARIANT (t) = t;
6318 TYPE_NEXT_VARIANT (t) = 0;
6320 /* Note that it is now possible for TYPE_MIN_VALUE to be a value
6321 whose TREE_TYPE is not t. This can also happen in the Ada
6322 frontend when using subtypes. */
6324 return t;
6327 /* Create a new variant of TYPE, equivalent but distinct. This is so
6328 the caller can modify it. TYPE_CANONICAL for the return type will
6329 be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
6330 are considered equal by the language itself (or that both types
6331 require structural equality checks). */
6333 tree
6334 build_variant_type_copy (tree type)
6336 tree t, m = TYPE_MAIN_VARIANT (type);
6338 t = build_distinct_type_copy (type);
6340 /* Since we're building a variant, assume that it is a non-semantic
6341 variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
6342 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
6344 /* Add the new type to the chain of variants of TYPE. */
6345 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
6346 TYPE_NEXT_VARIANT (m) = t;
6347 TYPE_MAIN_VARIANT (t) = m;
6349 return t;
6352 /* Return true if the from tree in both tree maps are equal. */
6355 tree_map_base_eq (const void *va, const void *vb)
6357 const struct tree_map_base *const a = (const struct tree_map_base *) va,
6358 *const b = (const struct tree_map_base *) vb;
6359 return (a->from == b->from);
6362 /* Hash a from tree in a tree_base_map. */
6364 unsigned int
6365 tree_map_base_hash (const void *item)
6367 return htab_hash_pointer (((const struct tree_map_base *)item)->from);
6370 /* Return true if this tree map structure is marked for garbage collection
6371 purposes. We simply return true if the from tree is marked, so that this
6372 structure goes away when the from tree goes away. */
6375 tree_map_base_marked_p (const void *p)
6377 return ggc_marked_p (((const struct tree_map_base *) p)->from);
6380 /* Hash a from tree in a tree_map. */
6382 unsigned int
6383 tree_map_hash (const void *item)
6385 return (((const struct tree_map *) item)->hash);
6388 /* Hash a from tree in a tree_decl_map. */
6390 unsigned int
6391 tree_decl_map_hash (const void *item)
6393 return DECL_UID (((const struct tree_decl_map *) item)->base.from);
6396 /* Return the initialization priority for DECL. */
6398 priority_type
6399 decl_init_priority_lookup (tree decl)
6401 struct tree_priority_map *h;
6402 struct tree_map_base in;
6404 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
6405 in.from = decl;
6406 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
6407 return h ? h->init : DEFAULT_INIT_PRIORITY;
6410 /* Return the finalization priority for DECL. */
6412 priority_type
6413 decl_fini_priority_lookup (tree decl)
6415 struct tree_priority_map *h;
6416 struct tree_map_base in;
6418 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
6419 in.from = decl;
6420 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
6421 return h ? h->fini : DEFAULT_INIT_PRIORITY;
6424 /* Return the initialization and finalization priority information for
6425 DECL. If there is no previous priority information, a freshly
6426 allocated structure is returned. */
6428 static struct tree_priority_map *
6429 decl_priority_info (tree decl)
6431 struct tree_priority_map in;
6432 struct tree_priority_map *h;
6433 void **loc;
6435 in.base.from = decl;
6436 loc = htab_find_slot (init_priority_for_decl, &in, INSERT);
6437 h = (struct tree_priority_map *) *loc;
6438 if (!h)
6440 h = ggc_alloc_cleared_tree_priority_map ();
6441 *loc = h;
6442 h->base.from = decl;
6443 h->init = DEFAULT_INIT_PRIORITY;
6444 h->fini = DEFAULT_INIT_PRIORITY;
6447 return h;
6450 /* Set the initialization priority for DECL to PRIORITY. */
6452 void
6453 decl_init_priority_insert (tree decl, priority_type priority)
6455 struct tree_priority_map *h;
6457 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
6458 if (priority == DEFAULT_INIT_PRIORITY)
6459 return;
6460 h = decl_priority_info (decl);
6461 h->init = priority;
6464 /* Set the finalization priority for DECL to PRIORITY. */
6466 void
6467 decl_fini_priority_insert (tree decl, priority_type priority)
6469 struct tree_priority_map *h;
6471 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
6472 if (priority == DEFAULT_INIT_PRIORITY)
6473 return;
6474 h = decl_priority_info (decl);
6475 h->fini = priority;
6478 /* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
6480 static void
6481 print_debug_expr_statistics (void)
6483 fprintf (stderr, "DECL_DEBUG_EXPR hash: size %ld, %ld elements, %f collisions\n",
6484 (long) htab_size (debug_expr_for_decl),
6485 (long) htab_elements (debug_expr_for_decl),
6486 htab_collisions (debug_expr_for_decl));
6489 /* Print out the statistics for the DECL_VALUE_EXPR hash table. */
6491 static void
6492 print_value_expr_statistics (void)
6494 fprintf (stderr, "DECL_VALUE_EXPR hash: size %ld, %ld elements, %f collisions\n",
6495 (long) htab_size (value_expr_for_decl),
6496 (long) htab_elements (value_expr_for_decl),
6497 htab_collisions (value_expr_for_decl));
6500 /* Lookup a debug expression for FROM, and return it if we find one. */
6502 tree
6503 decl_debug_expr_lookup (tree from)
6505 struct tree_decl_map *h, in;
6506 in.base.from = from;
6508 h = (struct tree_decl_map *)
6509 htab_find_with_hash (debug_expr_for_decl, &in, DECL_UID (from));
6510 if (h)
6511 return h->to;
6512 return NULL_TREE;
6515 /* Insert a mapping FROM->TO in the debug expression hashtable. */
6517 void
6518 decl_debug_expr_insert (tree from, tree to)
6520 struct tree_decl_map *h;
6521 void **loc;
6523 h = ggc_alloc_tree_decl_map ();
6524 h->base.from = from;
6525 h->to = to;
6526 loc = htab_find_slot_with_hash (debug_expr_for_decl, h, DECL_UID (from),
6527 INSERT);
6528 *(struct tree_decl_map **) loc = h;
6531 /* Lookup a value expression for FROM, and return it if we find one. */
6533 tree
6534 decl_value_expr_lookup (tree from)
6536 struct tree_decl_map *h, in;
6537 in.base.from = from;
6539 h = (struct tree_decl_map *)
6540 htab_find_with_hash (value_expr_for_decl, &in, DECL_UID (from));
6541 if (h)
6542 return h->to;
6543 return NULL_TREE;
6546 /* Insert a mapping FROM->TO in the value expression hashtable. */
6548 void
6549 decl_value_expr_insert (tree from, tree to)
6551 struct tree_decl_map *h;
6552 void **loc;
6554 h = ggc_alloc_tree_decl_map ();
6555 h->base.from = from;
6556 h->to = to;
6557 loc = htab_find_slot_with_hash (value_expr_for_decl, h, DECL_UID (from),
6558 INSERT);
6559 *(struct tree_decl_map **) loc = h;
6562 /* Lookup a vector of debug arguments for FROM, and return it if we
6563 find one. */
6565 vec<tree, va_gc> **
6566 decl_debug_args_lookup (tree from)
6568 struct tree_vec_map *h, in;
6570 if (!DECL_HAS_DEBUG_ARGS_P (from))
6571 return NULL;
6572 gcc_checking_assert (debug_args_for_decl != NULL);
6573 in.base.from = from;
6574 h = (struct tree_vec_map *)
6575 htab_find_with_hash (debug_args_for_decl, &in, DECL_UID (from));
6576 if (h)
6577 return &h->to;
6578 return NULL;
6581 /* Insert a mapping FROM->empty vector of debug arguments in the value
6582 expression hashtable. */
6584 vec<tree, va_gc> **
6585 decl_debug_args_insert (tree from)
6587 struct tree_vec_map *h;
6588 void **loc;
6590 if (DECL_HAS_DEBUG_ARGS_P (from))
6591 return decl_debug_args_lookup (from);
6592 if (debug_args_for_decl == NULL)
6593 debug_args_for_decl = htab_create_ggc (64, tree_vec_map_hash,
6594 tree_vec_map_eq, 0);
6595 h = ggc_alloc_tree_vec_map ();
6596 h->base.from = from;
6597 h->to = NULL;
6598 loc = htab_find_slot_with_hash (debug_args_for_decl, h, DECL_UID (from),
6599 INSERT);
6600 *(struct tree_vec_map **) loc = h;
6601 DECL_HAS_DEBUG_ARGS_P (from) = 1;
6602 return &h->to;
6605 /* Hashing of types so that we don't make duplicates.
6606 The entry point is `type_hash_canon'. */
6608 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
6609 with types in the TREE_VALUE slots), by adding the hash codes
6610 of the individual types. */
6612 static unsigned int
6613 type_hash_list (const_tree list, hashval_t hashcode)
6615 const_tree tail;
6617 for (tail = list; tail; tail = TREE_CHAIN (tail))
6618 if (TREE_VALUE (tail) != error_mark_node)
6619 hashcode = iterative_hash_object (TYPE_HASH (TREE_VALUE (tail)),
6620 hashcode);
6622 return hashcode;
6625 /* These are the Hashtable callback functions. */
6627 /* Returns true iff the types are equivalent. */
6629 static int
6630 type_hash_eq (const void *va, const void *vb)
6632 const struct type_hash *const a = (const struct type_hash *) va,
6633 *const b = (const struct type_hash *) vb;
6635 /* First test the things that are the same for all types. */
6636 if (a->hash != b->hash
6637 || TREE_CODE (a->type) != TREE_CODE (b->type)
6638 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
6639 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
6640 TYPE_ATTRIBUTES (b->type))
6641 || (TREE_CODE (a->type) != COMPLEX_TYPE
6642 && TYPE_NAME (a->type) != TYPE_NAME (b->type)))
6643 return 0;
6645 /* Be careful about comparing arrays before and after the element type
6646 has been completed; don't compare TYPE_ALIGN unless both types are
6647 complete. */
6648 if (COMPLETE_TYPE_P (a->type) && COMPLETE_TYPE_P (b->type)
6649 && (TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
6650 || TYPE_MODE (a->type) != TYPE_MODE (b->type)))
6651 return 0;
6653 switch (TREE_CODE (a->type))
6655 case VOID_TYPE:
6656 case COMPLEX_TYPE:
6657 case POINTER_TYPE:
6658 case REFERENCE_TYPE:
6659 case NULLPTR_TYPE:
6660 return 1;
6662 case VECTOR_TYPE:
6663 return TYPE_VECTOR_SUBPARTS (a->type) == TYPE_VECTOR_SUBPARTS (b->type);
6665 case ENUMERAL_TYPE:
6666 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
6667 && !(TYPE_VALUES (a->type)
6668 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
6669 && TYPE_VALUES (b->type)
6670 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
6671 && type_list_equal (TYPE_VALUES (a->type),
6672 TYPE_VALUES (b->type))))
6673 return 0;
6675 /* ... fall through ... */
6677 case INTEGER_TYPE:
6678 case REAL_TYPE:
6679 case BOOLEAN_TYPE:
6680 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
6681 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
6682 TYPE_MAX_VALUE (b->type)))
6683 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
6684 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
6685 TYPE_MIN_VALUE (b->type))));
6687 case FIXED_POINT_TYPE:
6688 return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
6690 case OFFSET_TYPE:
6691 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
6693 case METHOD_TYPE:
6694 if (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
6695 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6696 || (TYPE_ARG_TYPES (a->type)
6697 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6698 && TYPE_ARG_TYPES (b->type)
6699 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6700 && type_list_equal (TYPE_ARG_TYPES (a->type),
6701 TYPE_ARG_TYPES (b->type)))))
6702 break;
6703 return 0;
6704 case ARRAY_TYPE:
6705 return TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type);
6707 case RECORD_TYPE:
6708 case UNION_TYPE:
6709 case QUAL_UNION_TYPE:
6710 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
6711 || (TYPE_FIELDS (a->type)
6712 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
6713 && TYPE_FIELDS (b->type)
6714 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
6715 && type_list_equal (TYPE_FIELDS (a->type),
6716 TYPE_FIELDS (b->type))));
6718 case FUNCTION_TYPE:
6719 if (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6720 || (TYPE_ARG_TYPES (a->type)
6721 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6722 && TYPE_ARG_TYPES (b->type)
6723 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6724 && type_list_equal (TYPE_ARG_TYPES (a->type),
6725 TYPE_ARG_TYPES (b->type))))
6726 break;
6727 return 0;
6729 default:
6730 return 0;
6733 if (lang_hooks.types.type_hash_eq != NULL)
6734 return lang_hooks.types.type_hash_eq (a->type, b->type);
6736 return 1;
6739 /* Return the cached hash value. */
6741 static hashval_t
6742 type_hash_hash (const void *item)
6744 return ((const struct type_hash *) item)->hash;
6747 /* Look in the type hash table for a type isomorphic to TYPE.
6748 If one is found, return it. Otherwise return 0. */
6750 static tree
6751 type_hash_lookup (hashval_t hashcode, tree type)
6753 struct type_hash *h, in;
6755 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
6756 must call that routine before comparing TYPE_ALIGNs. */
6757 layout_type (type);
6759 in.hash = hashcode;
6760 in.type = type;
6762 h = (struct type_hash *) htab_find_with_hash (type_hash_table, &in,
6763 hashcode);
6764 if (h)
6765 return h->type;
6766 return NULL_TREE;
6769 /* Add an entry to the type-hash-table
6770 for a type TYPE whose hash code is HASHCODE. */
6772 static void
6773 type_hash_add (hashval_t hashcode, tree type)
6775 struct type_hash *h;
6776 void **loc;
6778 h = ggc_alloc_type_hash ();
6779 h->hash = hashcode;
6780 h->type = type;
6781 loc = htab_find_slot_with_hash (type_hash_table, h, hashcode, INSERT);
6782 *loc = (void *)h;
6785 /* Given TYPE, and HASHCODE its hash code, return the canonical
6786 object for an identical type if one already exists.
6787 Otherwise, return TYPE, and record it as the canonical object.
6789 To use this function, first create a type of the sort you want.
6790 Then compute its hash code from the fields of the type that
6791 make it different from other similar types.
6792 Then call this function and use the value. */
6794 tree
6795 type_hash_canon (unsigned int hashcode, tree type)
6797 tree t1;
6799 /* The hash table only contains main variants, so ensure that's what we're
6800 being passed. */
6801 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
6803 /* See if the type is in the hash table already. If so, return it.
6804 Otherwise, add the type. */
6805 t1 = type_hash_lookup (hashcode, type);
6806 if (t1 != 0)
6808 if (GATHER_STATISTICS)
6810 tree_code_counts[(int) TREE_CODE (type)]--;
6811 tree_node_counts[(int) t_kind]--;
6812 tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type_non_common);
6814 return t1;
6816 else
6818 type_hash_add (hashcode, type);
6819 return type;
6823 /* See if the data pointed to by the type hash table is marked. We consider
6824 it marked if the type is marked or if a debug type number or symbol
6825 table entry has been made for the type. */
6827 static int
6828 type_hash_marked_p (const void *p)
6830 const_tree const type = ((const struct type_hash *) p)->type;
6832 return ggc_marked_p (type);
6835 static void
6836 print_type_hash_statistics (void)
6838 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
6839 (long) htab_size (type_hash_table),
6840 (long) htab_elements (type_hash_table),
6841 htab_collisions (type_hash_table));
6844 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
6845 with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
6846 by adding the hash codes of the individual attributes. */
6848 static unsigned int
6849 attribute_hash_list (const_tree list, hashval_t hashcode)
6851 const_tree tail;
6853 for (tail = list; tail; tail = TREE_CHAIN (tail))
6854 /* ??? Do we want to add in TREE_VALUE too? */
6855 hashcode = iterative_hash_object
6856 (IDENTIFIER_HASH_VALUE (get_attribute_name (tail)), hashcode);
6857 return hashcode;
6860 /* Given two lists of attributes, return true if list l2 is
6861 equivalent to l1. */
6864 attribute_list_equal (const_tree l1, const_tree l2)
6866 if (l1 == l2)
6867 return 1;
6869 return attribute_list_contained (l1, l2)
6870 && attribute_list_contained (l2, l1);
6873 /* Given two lists of attributes, return true if list L2 is
6874 completely contained within L1. */
6875 /* ??? This would be faster if attribute names were stored in a canonicalized
6876 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
6877 must be used to show these elements are equivalent (which they are). */
6878 /* ??? It's not clear that attributes with arguments will always be handled
6879 correctly. */
6882 attribute_list_contained (const_tree l1, const_tree l2)
6884 const_tree t1, t2;
6886 /* First check the obvious, maybe the lists are identical. */
6887 if (l1 == l2)
6888 return 1;
6890 /* Maybe the lists are similar. */
6891 for (t1 = l1, t2 = l2;
6892 t1 != 0 && t2 != 0
6893 && get_attribute_name (t1) == get_attribute_name (t2)
6894 && TREE_VALUE (t1) == TREE_VALUE (t2);
6895 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6898 /* Maybe the lists are equal. */
6899 if (t1 == 0 && t2 == 0)
6900 return 1;
6902 for (; t2 != 0; t2 = TREE_CHAIN (t2))
6904 const_tree attr;
6905 /* This CONST_CAST is okay because lookup_attribute does not
6906 modify its argument and the return value is assigned to a
6907 const_tree. */
6908 for (attr = lookup_ident_attribute (get_attribute_name (t2),
6909 CONST_CAST_TREE (l1));
6910 attr != NULL_TREE && !attribute_value_equal (t2, attr);
6911 attr = lookup_ident_attribute (get_attribute_name (t2),
6912 TREE_CHAIN (attr)))
6915 if (attr == NULL_TREE)
6916 return 0;
6919 return 1;
6922 /* Given two lists of types
6923 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
6924 return 1 if the lists contain the same types in the same order.
6925 Also, the TREE_PURPOSEs must match. */
6928 type_list_equal (const_tree l1, const_tree l2)
6930 const_tree t1, t2;
6932 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6933 if (TREE_VALUE (t1) != TREE_VALUE (t2)
6934 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
6935 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
6936 && (TREE_TYPE (TREE_PURPOSE (t1))
6937 == TREE_TYPE (TREE_PURPOSE (t2))))))
6938 return 0;
6940 return t1 == t2;
6943 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
6944 given by TYPE. If the argument list accepts variable arguments,
6945 then this function counts only the ordinary arguments. */
6948 type_num_arguments (const_tree type)
6950 int i = 0;
6951 tree t;
6953 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
6954 /* If the function does not take a variable number of arguments,
6955 the last element in the list will have type `void'. */
6956 if (VOID_TYPE_P (TREE_VALUE (t)))
6957 break;
6958 else
6959 ++i;
6961 return i;
6964 /* Nonzero if integer constants T1 and T2
6965 represent the same constant value. */
6968 tree_int_cst_equal (const_tree t1, const_tree t2)
6970 if (t1 == t2)
6971 return 1;
6973 if (t1 == 0 || t2 == 0)
6974 return 0;
6976 if (TREE_CODE (t1) == INTEGER_CST
6977 && TREE_CODE (t2) == INTEGER_CST
6978 && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
6979 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
6980 return 1;
6982 return 0;
6985 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
6986 The precise way of comparison depends on their data type. */
6989 tree_int_cst_lt (const_tree t1, const_tree t2)
6991 if (t1 == t2)
6992 return 0;
6994 if (TYPE_UNSIGNED (TREE_TYPE (t1)) != TYPE_UNSIGNED (TREE_TYPE (t2)))
6996 int t1_sgn = tree_int_cst_sgn (t1);
6997 int t2_sgn = tree_int_cst_sgn (t2);
6999 if (t1_sgn < t2_sgn)
7000 return 1;
7001 else if (t1_sgn > t2_sgn)
7002 return 0;
7003 /* Otherwise, both are non-negative, so we compare them as
7004 unsigned just in case one of them would overflow a signed
7005 type. */
7007 else if (!TYPE_UNSIGNED (TREE_TYPE (t1)))
7008 return INT_CST_LT (t1, t2);
7010 return INT_CST_LT_UNSIGNED (t1, t2);
7013 /* Returns -1 if T1 < T2, 0 if T1 == T2, and 1 if T1 > T2. */
7016 tree_int_cst_compare (const_tree t1, const_tree t2)
7018 if (tree_int_cst_lt (t1, t2))
7019 return -1;
7020 else if (tree_int_cst_lt (t2, t1))
7021 return 1;
7022 else
7023 return 0;
7026 /* Return 1 if T is an INTEGER_CST that can be manipulated efficiently on
7027 the host. If POS is zero, the value can be represented in a single
7028 HOST_WIDE_INT. If POS is nonzero, the value must be non-negative and can
7029 be represented in a single unsigned HOST_WIDE_INT. */
7032 host_integerp (const_tree t, int pos)
7034 if (t == NULL_TREE)
7035 return 0;
7037 return (TREE_CODE (t) == INTEGER_CST
7038 && ((TREE_INT_CST_HIGH (t) == 0
7039 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) >= 0)
7040 || (! pos && TREE_INT_CST_HIGH (t) == -1
7041 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0
7042 && !TYPE_UNSIGNED (TREE_TYPE (t)))
7043 || (pos && TREE_INT_CST_HIGH (t) == 0)));
7046 /* Return the HOST_WIDE_INT least significant bits of T if it is an
7047 INTEGER_CST and there is no overflow. POS is nonzero if the result must
7048 be non-negative. We must be able to satisfy the above conditions. */
7050 HOST_WIDE_INT
7051 tree_low_cst (const_tree t, int pos)
7053 gcc_assert (host_integerp (t, pos));
7054 return TREE_INT_CST_LOW (t);
7057 /* Return the most significant (sign) bit of T. */
7060 tree_int_cst_sign_bit (const_tree t)
7062 unsigned bitno = TYPE_PRECISION (TREE_TYPE (t)) - 1;
7063 unsigned HOST_WIDE_INT w;
7065 if (bitno < HOST_BITS_PER_WIDE_INT)
7066 w = TREE_INT_CST_LOW (t);
7067 else
7069 w = TREE_INT_CST_HIGH (t);
7070 bitno -= HOST_BITS_PER_WIDE_INT;
7073 return (w >> bitno) & 1;
7076 /* Return an indication of the sign of the integer constant T.
7077 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
7078 Note that -1 will never be returned if T's type is unsigned. */
7081 tree_int_cst_sgn (const_tree t)
7083 if (TREE_INT_CST_LOW (t) == 0 && TREE_INT_CST_HIGH (t) == 0)
7084 return 0;
7085 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
7086 return 1;
7087 else if (TREE_INT_CST_HIGH (t) < 0)
7088 return -1;
7089 else
7090 return 1;
7093 /* Return the minimum number of bits needed to represent VALUE in a
7094 signed or unsigned type, UNSIGNEDP says which. */
7096 unsigned int
7097 tree_int_cst_min_precision (tree value, bool unsignedp)
7099 /* If the value is negative, compute its negative minus 1. The latter
7100 adjustment is because the absolute value of the largest negative value
7101 is one larger than the largest positive value. This is equivalent to
7102 a bit-wise negation, so use that operation instead. */
7104 if (tree_int_cst_sgn (value) < 0)
7105 value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
7107 /* Return the number of bits needed, taking into account the fact
7108 that we need one more bit for a signed than unsigned type.
7109 If value is 0 or -1, the minimum precision is 1 no matter
7110 whether unsignedp is true or false. */
7112 if (integer_zerop (value))
7113 return 1;
7114 else
7115 return tree_floor_log2 (value) + 1 + !unsignedp;
7118 /* Return truthvalue of whether T1 is the same tree structure as T2.
7119 Return 1 if they are the same.
7120 Return 0 if they are understandably different.
7121 Return -1 if either contains tree structure not understood by
7122 this function. */
7125 simple_cst_equal (const_tree t1, const_tree t2)
7127 enum tree_code code1, code2;
7128 int cmp;
7129 int i;
7131 if (t1 == t2)
7132 return 1;
7133 if (t1 == 0 || t2 == 0)
7134 return 0;
7136 code1 = TREE_CODE (t1);
7137 code2 = TREE_CODE (t2);
7139 if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
7141 if (CONVERT_EXPR_CODE_P (code2)
7142 || code2 == NON_LVALUE_EXPR)
7143 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7144 else
7145 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
7148 else if (CONVERT_EXPR_CODE_P (code2)
7149 || code2 == NON_LVALUE_EXPR)
7150 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
7152 if (code1 != code2)
7153 return 0;
7155 switch (code1)
7157 case INTEGER_CST:
7158 return (TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
7159 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2));
7161 case REAL_CST:
7162 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
7164 case FIXED_CST:
7165 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
7167 case STRING_CST:
7168 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
7169 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
7170 TREE_STRING_LENGTH (t1)));
7172 case CONSTRUCTOR:
7174 unsigned HOST_WIDE_INT idx;
7175 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (t1);
7176 vec<constructor_elt, va_gc> *v2 = CONSTRUCTOR_ELTS (t2);
7178 if (vec_safe_length (v1) != vec_safe_length (v2))
7179 return false;
7181 for (idx = 0; idx < vec_safe_length (v1); ++idx)
7182 /* ??? Should we handle also fields here? */
7183 if (!simple_cst_equal ((*v1)[idx].value, (*v2)[idx].value))
7184 return false;
7185 return true;
7188 case SAVE_EXPR:
7189 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7191 case CALL_EXPR:
7192 cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
7193 if (cmp <= 0)
7194 return cmp;
7195 if (call_expr_nargs (t1) != call_expr_nargs (t2))
7196 return 0;
7198 const_tree arg1, arg2;
7199 const_call_expr_arg_iterator iter1, iter2;
7200 for (arg1 = first_const_call_expr_arg (t1, &iter1),
7201 arg2 = first_const_call_expr_arg (t2, &iter2);
7202 arg1 && arg2;
7203 arg1 = next_const_call_expr_arg (&iter1),
7204 arg2 = next_const_call_expr_arg (&iter2))
7206 cmp = simple_cst_equal (arg1, arg2);
7207 if (cmp <= 0)
7208 return cmp;
7210 return arg1 == arg2;
7213 case TARGET_EXPR:
7214 /* Special case: if either target is an unallocated VAR_DECL,
7215 it means that it's going to be unified with whatever the
7216 TARGET_EXPR is really supposed to initialize, so treat it
7217 as being equivalent to anything. */
7218 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
7219 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
7220 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
7221 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
7222 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
7223 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
7224 cmp = 1;
7225 else
7226 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7228 if (cmp <= 0)
7229 return cmp;
7231 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
7233 case WITH_CLEANUP_EXPR:
7234 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7235 if (cmp <= 0)
7236 return cmp;
7238 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
7240 case COMPONENT_REF:
7241 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
7242 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7244 return 0;
7246 case VAR_DECL:
7247 case PARM_DECL:
7248 case CONST_DECL:
7249 case FUNCTION_DECL:
7250 return 0;
7252 default:
7253 break;
7256 /* This general rule works for most tree codes. All exceptions should be
7257 handled above. If this is a language-specific tree code, we can't
7258 trust what might be in the operand, so say we don't know
7259 the situation. */
7260 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
7261 return -1;
7263 switch (TREE_CODE_CLASS (code1))
7265 case tcc_unary:
7266 case tcc_binary:
7267 case tcc_comparison:
7268 case tcc_expression:
7269 case tcc_reference:
7270 case tcc_statement:
7271 cmp = 1;
7272 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
7274 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
7275 if (cmp <= 0)
7276 return cmp;
7279 return cmp;
7281 default:
7282 return -1;
7286 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
7287 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
7288 than U, respectively. */
7291 compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
7293 if (tree_int_cst_sgn (t) < 0)
7294 return -1;
7295 else if (TREE_INT_CST_HIGH (t) != 0)
7296 return 1;
7297 else if (TREE_INT_CST_LOW (t) == u)
7298 return 0;
7299 else if (TREE_INT_CST_LOW (t) < u)
7300 return -1;
7301 else
7302 return 1;
7305 /* Return true if SIZE represents a constant size that is in bounds of
7306 what the middle-end and the backend accepts (covering not more than
7307 half of the address-space). */
7309 bool
7310 valid_constant_size_p (const_tree size)
7312 if (! host_integerp (size, 1)
7313 || TREE_OVERFLOW (size)
7314 || tree_int_cst_sign_bit (size) != 0)
7315 return false;
7316 return true;
7319 /* Return the precision of the type, or for a complex or vector type the
7320 precision of the type of its elements. */
7322 unsigned int
7323 element_precision (const_tree type)
7325 enum tree_code code = TREE_CODE (type);
7326 if (code == COMPLEX_TYPE || code == VECTOR_TYPE)
7327 type = TREE_TYPE (type);
7329 return TYPE_PRECISION (type);
7332 /* Return true if CODE represents an associative tree code. Otherwise
7333 return false. */
7334 bool
7335 associative_tree_code (enum tree_code code)
7337 switch (code)
7339 case BIT_IOR_EXPR:
7340 case BIT_AND_EXPR:
7341 case BIT_XOR_EXPR:
7342 case PLUS_EXPR:
7343 case MULT_EXPR:
7344 case MIN_EXPR:
7345 case MAX_EXPR:
7346 return true;
7348 default:
7349 break;
7351 return false;
7354 /* Return true if CODE represents a commutative tree code. Otherwise
7355 return false. */
7356 bool
7357 commutative_tree_code (enum tree_code code)
7359 switch (code)
7361 case PLUS_EXPR:
7362 case MULT_EXPR:
7363 case MULT_HIGHPART_EXPR:
7364 case MIN_EXPR:
7365 case MAX_EXPR:
7366 case BIT_IOR_EXPR:
7367 case BIT_XOR_EXPR:
7368 case BIT_AND_EXPR:
7369 case NE_EXPR:
7370 case EQ_EXPR:
7371 case UNORDERED_EXPR:
7372 case ORDERED_EXPR:
7373 case UNEQ_EXPR:
7374 case LTGT_EXPR:
7375 case TRUTH_AND_EXPR:
7376 case TRUTH_XOR_EXPR:
7377 case TRUTH_OR_EXPR:
7378 case WIDEN_MULT_EXPR:
7379 case VEC_WIDEN_MULT_HI_EXPR:
7380 case VEC_WIDEN_MULT_LO_EXPR:
7381 case VEC_WIDEN_MULT_EVEN_EXPR:
7382 case VEC_WIDEN_MULT_ODD_EXPR:
7383 return true;
7385 default:
7386 break;
7388 return false;
7391 /* Return true if CODE represents a ternary tree code for which the
7392 first two operands are commutative. Otherwise return false. */
7393 bool
7394 commutative_ternary_tree_code (enum tree_code code)
7396 switch (code)
7398 case WIDEN_MULT_PLUS_EXPR:
7399 case WIDEN_MULT_MINUS_EXPR:
7400 return true;
7402 default:
7403 break;
7405 return false;
7408 /* Generate a hash value for an expression. This can be used iteratively
7409 by passing a previous result as the VAL argument.
7411 This function is intended to produce the same hash for expressions which
7412 would compare equal using operand_equal_p. */
7414 hashval_t
7415 iterative_hash_expr (const_tree t, hashval_t val)
7417 int i;
7418 enum tree_code code;
7419 char tclass;
7421 if (t == NULL_TREE)
7422 return iterative_hash_hashval_t (0, val);
7424 code = TREE_CODE (t);
7426 switch (code)
7428 /* Alas, constants aren't shared, so we can't rely on pointer
7429 identity. */
7430 case INTEGER_CST:
7431 val = iterative_hash_host_wide_int (TREE_INT_CST_LOW (t), val);
7432 return iterative_hash_host_wide_int (TREE_INT_CST_HIGH (t), val);
7433 case REAL_CST:
7435 unsigned int val2 = real_hash (TREE_REAL_CST_PTR (t));
7437 return iterative_hash_hashval_t (val2, val);
7439 case FIXED_CST:
7441 unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
7443 return iterative_hash_hashval_t (val2, val);
7445 case STRING_CST:
7446 return iterative_hash (TREE_STRING_POINTER (t),
7447 TREE_STRING_LENGTH (t), val);
7448 case COMPLEX_CST:
7449 val = iterative_hash_expr (TREE_REALPART (t), val);
7450 return iterative_hash_expr (TREE_IMAGPART (t), val);
7451 case VECTOR_CST:
7453 unsigned i;
7454 for (i = 0; i < VECTOR_CST_NELTS (t); ++i)
7455 val = iterative_hash_expr (VECTOR_CST_ELT (t, i), val);
7456 return val;
7458 case SSA_NAME:
7459 /* We can just compare by pointer. */
7460 return iterative_hash_host_wide_int (SSA_NAME_VERSION (t), val);
7461 case PLACEHOLDER_EXPR:
7462 /* The node itself doesn't matter. */
7463 return val;
7464 case TREE_LIST:
7465 /* A list of expressions, for a CALL_EXPR or as the elements of a
7466 VECTOR_CST. */
7467 for (; t; t = TREE_CHAIN (t))
7468 val = iterative_hash_expr (TREE_VALUE (t), val);
7469 return val;
7470 case CONSTRUCTOR:
7472 unsigned HOST_WIDE_INT idx;
7473 tree field, value;
7474 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
7476 val = iterative_hash_expr (field, val);
7477 val = iterative_hash_expr (value, val);
7479 return val;
7481 case FUNCTION_DECL:
7482 /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
7483 Otherwise nodes that compare equal according to operand_equal_p might
7484 get different hash codes. However, don't do this for machine specific
7485 or front end builtins, since the function code is overloaded in those
7486 cases. */
7487 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
7488 && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
7490 t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
7491 code = TREE_CODE (t);
7493 /* FALL THROUGH */
7494 default:
7495 tclass = TREE_CODE_CLASS (code);
7497 if (tclass == tcc_declaration)
7499 /* DECL's have a unique ID */
7500 val = iterative_hash_host_wide_int (DECL_UID (t), val);
7502 else
7504 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
7506 val = iterative_hash_object (code, val);
7508 /* Don't hash the type, that can lead to having nodes which
7509 compare equal according to operand_equal_p, but which
7510 have different hash codes. */
7511 if (CONVERT_EXPR_CODE_P (code)
7512 || code == NON_LVALUE_EXPR)
7514 /* Make sure to include signness in the hash computation. */
7515 val += TYPE_UNSIGNED (TREE_TYPE (t));
7516 val = iterative_hash_expr (TREE_OPERAND (t, 0), val);
7519 else if (commutative_tree_code (code))
7521 /* It's a commutative expression. We want to hash it the same
7522 however it appears. We do this by first hashing both operands
7523 and then rehashing based on the order of their independent
7524 hashes. */
7525 hashval_t one = iterative_hash_expr (TREE_OPERAND (t, 0), 0);
7526 hashval_t two = iterative_hash_expr (TREE_OPERAND (t, 1), 0);
7527 hashval_t t;
7529 if (one > two)
7530 t = one, one = two, two = t;
7532 val = iterative_hash_hashval_t (one, val);
7533 val = iterative_hash_hashval_t (two, val);
7535 else
7536 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
7537 val = iterative_hash_expr (TREE_OPERAND (t, i), val);
7539 return val;
7543 /* Constructors for pointer, array and function types.
7544 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
7545 constructed by language-dependent code, not here.) */
7547 /* Construct, lay out and return the type of pointers to TO_TYPE with
7548 mode MODE. If CAN_ALIAS_ALL is TRUE, indicate this type can
7549 reference all of memory. If such a type has already been
7550 constructed, reuse it. */
7552 tree
7553 build_pointer_type_for_mode (tree to_type, enum machine_mode mode,
7554 bool can_alias_all)
7556 tree t;
7558 if (to_type == error_mark_node)
7559 return error_mark_node;
7561 /* If the pointed-to type has the may_alias attribute set, force
7562 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7563 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7564 can_alias_all = true;
7566 /* In some cases, languages will have things that aren't a POINTER_TYPE
7567 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
7568 In that case, return that type without regard to the rest of our
7569 operands.
7571 ??? This is a kludge, but consistent with the way this function has
7572 always operated and there doesn't seem to be a good way to avoid this
7573 at the moment. */
7574 if (TYPE_POINTER_TO (to_type) != 0
7575 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
7576 return TYPE_POINTER_TO (to_type);
7578 /* First, if we already have a type for pointers to TO_TYPE and it's
7579 the proper mode, use it. */
7580 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
7581 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7582 return t;
7584 t = make_node (POINTER_TYPE);
7586 TREE_TYPE (t) = to_type;
7587 SET_TYPE_MODE (t, mode);
7588 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7589 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
7590 TYPE_POINTER_TO (to_type) = t;
7592 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7593 SET_TYPE_STRUCTURAL_EQUALITY (t);
7594 else if (TYPE_CANONICAL (to_type) != to_type)
7595 TYPE_CANONICAL (t)
7596 = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
7597 mode, can_alias_all);
7599 /* Lay out the type. This function has many callers that are concerned
7600 with expression-construction, and this simplifies them all. */
7601 layout_type (t);
7603 return t;
7606 /* By default build pointers in ptr_mode. */
7608 tree
7609 build_pointer_type (tree to_type)
7611 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7612 : TYPE_ADDR_SPACE (to_type);
7613 enum machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7614 if (upc_shared_type_p (to_type))
7616 tree upc_pts_type;
7617 pointer_mode = TYPE_MODE (upc_pts_rep_type_node);
7618 upc_pts_type = build_pointer_type_for_mode (to_type, pointer_mode,
7619 false);
7620 TYPE_USER_ALIGN (upc_pts_type) = TYPE_USER_ALIGN (upc_pts_rep_type_node);
7621 TYPE_ALIGN (upc_pts_type) = TYPE_ALIGN (upc_pts_rep_type_node);
7622 return upc_pts_type;
7624 return build_pointer_type_for_mode (to_type, pointer_mode, false);
7627 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
7629 tree
7630 build_reference_type_for_mode (tree to_type, enum machine_mode mode,
7631 bool can_alias_all)
7633 tree t;
7635 if (to_type == error_mark_node)
7636 return error_mark_node;
7638 /* If the pointed-to type has the may_alias attribute set, force
7639 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7640 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7641 can_alias_all = true;
7643 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
7644 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
7645 In that case, return that type without regard to the rest of our
7646 operands.
7648 ??? This is a kludge, but consistent with the way this function has
7649 always operated and there doesn't seem to be a good way to avoid this
7650 at the moment. */
7651 if (TYPE_REFERENCE_TO (to_type) != 0
7652 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
7653 return TYPE_REFERENCE_TO (to_type);
7655 /* First, if we already have a type for pointers to TO_TYPE and it's
7656 the proper mode, use it. */
7657 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
7658 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7659 return t;
7661 t = make_node (REFERENCE_TYPE);
7663 TREE_TYPE (t) = to_type;
7664 SET_TYPE_MODE (t, mode);
7665 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7666 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
7667 TYPE_REFERENCE_TO (to_type) = t;
7669 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7670 SET_TYPE_STRUCTURAL_EQUALITY (t);
7671 else if (TYPE_CANONICAL (to_type) != to_type)
7672 TYPE_CANONICAL (t)
7673 = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
7674 mode, can_alias_all);
7676 layout_type (t);
7678 return t;
7682 /* Build the node for the type of references-to-TO_TYPE by default
7683 in ptr_mode. */
7685 tree
7686 build_reference_type (tree to_type)
7688 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7689 : TYPE_ADDR_SPACE (to_type);
7690 enum machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7691 return build_reference_type_for_mode (to_type, pointer_mode, false);
7694 #define MAX_INT_CACHED_PREC \
7695 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
7696 static GTY(()) tree nonstandard_integer_type_cache[2 * MAX_INT_CACHED_PREC + 2];
7698 /* Builds a signed or unsigned integer type of precision PRECISION.
7699 Used for C bitfields whose precision does not match that of
7700 built-in target types. */
7701 tree
7702 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
7703 int unsignedp)
7705 tree itype, ret;
7707 if (unsignedp)
7708 unsignedp = MAX_INT_CACHED_PREC + 1;
7710 if (precision <= MAX_INT_CACHED_PREC)
7712 itype = nonstandard_integer_type_cache[precision + unsignedp];
7713 if (itype)
7714 return itype;
7717 itype = make_node (INTEGER_TYPE);
7718 TYPE_PRECISION (itype) = precision;
7720 if (unsignedp)
7721 fixup_unsigned_type (itype);
7722 else
7723 fixup_signed_type (itype);
7725 ret = itype;
7726 if (host_integerp (TYPE_MAX_VALUE (itype), 1))
7727 ret = type_hash_canon (tree_low_cst (TYPE_MAX_VALUE (itype), 1), itype);
7728 if (precision <= MAX_INT_CACHED_PREC)
7729 nonstandard_integer_type_cache[precision + unsignedp] = ret;
7731 return ret;
7734 /* Create a range of some discrete type TYPE (an INTEGER_TYPE, ENUMERAL_TYPE
7735 or BOOLEAN_TYPE) with low bound LOWVAL and high bound HIGHVAL. If SHARED
7736 is true, reuse such a type that has already been constructed. */
7738 static tree
7739 build_range_type_1 (tree type, tree lowval, tree highval, bool shared)
7741 tree itype = make_node (INTEGER_TYPE);
7742 hashval_t hashcode = 0;
7744 TREE_TYPE (itype) = type;
7746 TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
7747 TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
7749 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
7750 SET_TYPE_MODE (itype, TYPE_MODE (type));
7751 TYPE_SIZE (itype) = TYPE_SIZE (type);
7752 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
7753 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
7754 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
7756 if (!shared)
7757 return itype;
7759 if ((TYPE_MIN_VALUE (itype)
7760 && TREE_CODE (TYPE_MIN_VALUE (itype)) != INTEGER_CST)
7761 || (TYPE_MAX_VALUE (itype)
7762 && TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST))
7764 /* Since we cannot reliably merge this type, we need to compare it using
7765 structural equality checks. */
7766 SET_TYPE_STRUCTURAL_EQUALITY (itype);
7767 return itype;
7770 hashcode = iterative_hash_expr (TYPE_MIN_VALUE (itype), hashcode);
7771 hashcode = iterative_hash_expr (TYPE_MAX_VALUE (itype), hashcode);
7772 hashcode = iterative_hash_hashval_t (TYPE_HASH (type), hashcode);
7773 itype = type_hash_canon (hashcode, itype);
7775 return itype;
7778 /* Wrapper around build_range_type_1 with SHARED set to true. */
7780 tree
7781 build_range_type (tree type, tree lowval, tree highval)
7783 return build_range_type_1 (type, lowval, highval, true);
7786 /* Wrapper around build_range_type_1 with SHARED set to false. */
7788 tree
7789 build_nonshared_range_type (tree type, tree lowval, tree highval)
7791 return build_range_type_1 (type, lowval, highval, false);
7794 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
7795 MAXVAL should be the maximum value in the domain
7796 (one less than the length of the array).
7798 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
7799 We don't enforce this limit, that is up to caller (e.g. language front end).
7800 The limit exists because the result is a signed type and we don't handle
7801 sizes that use more than one HOST_WIDE_INT. */
7803 tree
7804 build_index_type (tree maxval)
7806 return build_range_type (sizetype, size_zero_node, maxval);
7809 /* Return true if the debug information for TYPE, a subtype, should be emitted
7810 as a subrange type. If so, set LOWVAL to the low bound and HIGHVAL to the
7811 high bound, respectively. Sometimes doing so unnecessarily obfuscates the
7812 debug info and doesn't reflect the source code. */
7814 bool
7815 subrange_type_for_debug_p (const_tree type, tree *lowval, tree *highval)
7817 tree base_type = TREE_TYPE (type), low, high;
7819 /* Subrange types have a base type which is an integral type. */
7820 if (!INTEGRAL_TYPE_P (base_type))
7821 return false;
7823 /* Get the real bounds of the subtype. */
7824 if (lang_hooks.types.get_subrange_bounds)
7825 lang_hooks.types.get_subrange_bounds (type, &low, &high);
7826 else
7828 low = TYPE_MIN_VALUE (type);
7829 high = TYPE_MAX_VALUE (type);
7832 /* If the type and its base type have the same representation and the same
7833 name, then the type is not a subrange but a copy of the base type. */
7834 if ((TREE_CODE (base_type) == INTEGER_TYPE
7835 || TREE_CODE (base_type) == BOOLEAN_TYPE)
7836 && int_size_in_bytes (type) == int_size_in_bytes (base_type)
7837 && tree_int_cst_equal (low, TYPE_MIN_VALUE (base_type))
7838 && tree_int_cst_equal (high, TYPE_MAX_VALUE (base_type)))
7840 tree type_name = TYPE_NAME (type);
7841 tree base_type_name = TYPE_NAME (base_type);
7843 if (type_name && TREE_CODE (type_name) == TYPE_DECL)
7844 type_name = DECL_NAME (type_name);
7846 if (base_type_name && TREE_CODE (base_type_name) == TYPE_DECL)
7847 base_type_name = DECL_NAME (base_type_name);
7849 if (type_name == base_type_name)
7850 return false;
7853 if (lowval)
7854 *lowval = low;
7855 if (highval)
7856 *highval = high;
7857 return true;
7860 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
7861 and number of elements specified by the range of values of INDEX_TYPE.
7862 If SHARED is true, reuse such a type that has already been constructed. */
7864 static tree
7865 build_array_type_1 (tree elt_type, tree index_type, bool shared)
7867 tree t;
7869 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
7871 error ("arrays of functions are not meaningful");
7872 elt_type = integer_type_node;
7875 t = make_node (ARRAY_TYPE);
7876 TREE_TYPE (t) = elt_type;
7877 TYPE_DOMAIN (t) = index_type;
7878 TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (elt_type);
7879 layout_type (t);
7881 /* If the element type is incomplete at this point we get marked for
7882 structural equality. Do not record these types in the canonical
7883 type hashtable. */
7884 if (TYPE_STRUCTURAL_EQUALITY_P (t))
7885 return t;
7887 if (shared)
7889 hashval_t hashcode = iterative_hash_object (TYPE_HASH (elt_type), 0);
7890 if (index_type)
7891 hashcode = iterative_hash_object (TYPE_HASH (index_type), hashcode);
7892 t = type_hash_canon (hashcode, t);
7895 if (TYPE_CANONICAL (t) == t)
7897 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
7898 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
7899 SET_TYPE_STRUCTURAL_EQUALITY (t);
7900 else if (TYPE_CANONICAL (elt_type) != elt_type
7901 || (index_type && TYPE_CANONICAL (index_type) != index_type))
7902 TYPE_CANONICAL (t)
7903 = build_array_type_1 (TYPE_CANONICAL (elt_type),
7904 index_type
7905 ? TYPE_CANONICAL (index_type) : NULL_TREE,
7906 shared);
7909 return t;
7912 /* Wrapper around build_array_type_1 with SHARED set to true. */
7914 tree
7915 build_array_type (tree elt_type, tree index_type)
7917 return build_array_type_1 (elt_type, index_type, true);
7920 /* Wrapper around build_array_type_1 with SHARED set to false. */
7922 tree
7923 build_nonshared_array_type (tree elt_type, tree index_type)
7925 return build_array_type_1 (elt_type, index_type, false);
7928 /* Return a representation of ELT_TYPE[NELTS], using indices of type
7929 sizetype. */
7931 tree
7932 build_array_type_nelts (tree elt_type, unsigned HOST_WIDE_INT nelts)
7934 return build_array_type (elt_type, build_index_type (size_int (nelts - 1)));
7937 /* Recursively examines the array elements of TYPE, until a non-array
7938 element type is found. */
7940 tree
7941 strip_array_types (tree type)
7943 while (TREE_CODE (type) == ARRAY_TYPE)
7944 type = TREE_TYPE (type);
7946 return type;
7949 /* Computes the canonical argument types from the argument type list
7950 ARGTYPES.
7952 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
7953 on entry to this function, or if any of the ARGTYPES are
7954 structural.
7956 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
7957 true on entry to this function, or if any of the ARGTYPES are
7958 non-canonical.
7960 Returns a canonical argument list, which may be ARGTYPES when the
7961 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
7962 true) or would not differ from ARGTYPES. */
7964 static tree
7965 maybe_canonicalize_argtypes (tree argtypes,
7966 bool *any_structural_p,
7967 bool *any_noncanonical_p)
7969 tree arg;
7970 bool any_noncanonical_argtypes_p = false;
7972 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
7974 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
7975 /* Fail gracefully by stating that the type is structural. */
7976 *any_structural_p = true;
7977 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
7978 *any_structural_p = true;
7979 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
7980 || TREE_PURPOSE (arg))
7981 /* If the argument has a default argument, we consider it
7982 non-canonical even though the type itself is canonical.
7983 That way, different variants of function and method types
7984 with default arguments will all point to the variant with
7985 no defaults as their canonical type. */
7986 any_noncanonical_argtypes_p = true;
7989 if (*any_structural_p)
7990 return argtypes;
7992 if (any_noncanonical_argtypes_p)
7994 /* Build the canonical list of argument types. */
7995 tree canon_argtypes = NULL_TREE;
7996 bool is_void = false;
7998 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
8000 if (arg == void_list_node)
8001 is_void = true;
8002 else
8003 canon_argtypes = tree_cons (NULL_TREE,
8004 TYPE_CANONICAL (TREE_VALUE (arg)),
8005 canon_argtypes);
8008 canon_argtypes = nreverse (canon_argtypes);
8009 if (is_void)
8010 canon_argtypes = chainon (canon_argtypes, void_list_node);
8012 /* There is a non-canonical type. */
8013 *any_noncanonical_p = true;
8014 return canon_argtypes;
8017 /* The canonical argument types are the same as ARGTYPES. */
8018 return argtypes;
8021 /* Construct, lay out and return
8022 the type of functions returning type VALUE_TYPE
8023 given arguments of types ARG_TYPES.
8024 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
8025 are data type nodes for the arguments of the function.
8026 If such a type has already been constructed, reuse it. */
8028 tree
8029 build_function_type (tree value_type, tree arg_types)
8031 tree t;
8032 hashval_t hashcode = 0;
8033 bool any_structural_p, any_noncanonical_p;
8034 tree canon_argtypes;
8036 if (TREE_CODE (value_type) == FUNCTION_TYPE)
8038 error ("function return type cannot be function");
8039 value_type = integer_type_node;
8042 /* Make a node of the sort we want. */
8043 t = make_node (FUNCTION_TYPE);
8044 TREE_TYPE (t) = value_type;
8045 TYPE_ARG_TYPES (t) = arg_types;
8047 /* If we already have such a type, use the old one. */
8048 hashcode = iterative_hash_object (TYPE_HASH (value_type), hashcode);
8049 hashcode = type_hash_list (arg_types, hashcode);
8050 t = type_hash_canon (hashcode, t);
8052 /* Set up the canonical type. */
8053 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
8054 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
8055 canon_argtypes = maybe_canonicalize_argtypes (arg_types,
8056 &any_structural_p,
8057 &any_noncanonical_p);
8058 if (any_structural_p)
8059 SET_TYPE_STRUCTURAL_EQUALITY (t);
8060 else if (any_noncanonical_p)
8061 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
8062 canon_argtypes);
8064 if (!COMPLETE_TYPE_P (t))
8065 layout_type (t);
8066 return t;
8069 /* Build a function type. The RETURN_TYPE is the type returned by the
8070 function. If VAARGS is set, no void_type_node is appended to the
8071 the list. ARGP must be always be terminated be a NULL_TREE. */
8073 static tree
8074 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
8076 tree t, args, last;
8078 t = va_arg (argp, tree);
8079 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
8080 args = tree_cons (NULL_TREE, t, args);
8082 if (vaargs)
8084 last = args;
8085 if (args != NULL_TREE)
8086 args = nreverse (args);
8087 gcc_assert (last != void_list_node);
8089 else if (args == NULL_TREE)
8090 args = void_list_node;
8091 else
8093 last = args;
8094 args = nreverse (args);
8095 TREE_CHAIN (last) = void_list_node;
8097 args = build_function_type (return_type, args);
8099 return args;
8102 /* Build a function type. The RETURN_TYPE is the type returned by the
8103 function. If additional arguments are provided, they are
8104 additional argument types. The list of argument types must always
8105 be terminated by NULL_TREE. */
8107 tree
8108 build_function_type_list (tree return_type, ...)
8110 tree args;
8111 va_list p;
8113 va_start (p, return_type);
8114 args = build_function_type_list_1 (false, return_type, p);
8115 va_end (p);
8116 return args;
8119 /* Build a variable argument function type. The RETURN_TYPE is the
8120 type returned by the function. If additional arguments are provided,
8121 they are additional argument types. The list of argument types must
8122 always be terminated by NULL_TREE. */
8124 tree
8125 build_varargs_function_type_list (tree return_type, ...)
8127 tree args;
8128 va_list p;
8130 va_start (p, return_type);
8131 args = build_function_type_list_1 (true, return_type, p);
8132 va_end (p);
8134 return args;
8137 /* Build a function type. RETURN_TYPE is the type returned by the
8138 function; VAARGS indicates whether the function takes varargs. The
8139 function takes N named arguments, the types of which are provided in
8140 ARG_TYPES. */
8142 static tree
8143 build_function_type_array_1 (bool vaargs, tree return_type, int n,
8144 tree *arg_types)
8146 int i;
8147 tree t = vaargs ? NULL_TREE : void_list_node;
8149 for (i = n - 1; i >= 0; i--)
8150 t = tree_cons (NULL_TREE, arg_types[i], t);
8152 return build_function_type (return_type, t);
8155 /* Build a function type. RETURN_TYPE is the type returned by the
8156 function. The function takes N named arguments, the types of which
8157 are provided in ARG_TYPES. */
8159 tree
8160 build_function_type_array (tree return_type, int n, tree *arg_types)
8162 return build_function_type_array_1 (false, return_type, n, arg_types);
8165 /* Build a variable argument function type. RETURN_TYPE is the type
8166 returned by the function. The function takes N named arguments, the
8167 types of which are provided in ARG_TYPES. */
8169 tree
8170 build_varargs_function_type_array (tree return_type, int n, tree *arg_types)
8172 return build_function_type_array_1 (true, return_type, n, arg_types);
8175 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
8176 and ARGTYPES (a TREE_LIST) are the return type and arguments types
8177 for the method. An implicit additional parameter (of type
8178 pointer-to-BASETYPE) is added to the ARGTYPES. */
8180 tree
8181 build_method_type_directly (tree basetype,
8182 tree rettype,
8183 tree argtypes)
8185 tree t;
8186 tree ptype;
8187 int hashcode = 0;
8188 bool any_structural_p, any_noncanonical_p;
8189 tree canon_argtypes;
8191 /* Make a node of the sort we want. */
8192 t = make_node (METHOD_TYPE);
8194 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8195 TREE_TYPE (t) = rettype;
8196 ptype = build_pointer_type (basetype);
8198 /* The actual arglist for this function includes a "hidden" argument
8199 which is "this". Put it into the list of argument types. */
8200 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
8201 TYPE_ARG_TYPES (t) = argtypes;
8203 /* If we already have such a type, use the old one. */
8204 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
8205 hashcode = iterative_hash_object (TYPE_HASH (rettype), hashcode);
8206 hashcode = type_hash_list (argtypes, hashcode);
8207 t = type_hash_canon (hashcode, t);
8209 /* Set up the canonical type. */
8210 any_structural_p
8211 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8212 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
8213 any_noncanonical_p
8214 = (TYPE_CANONICAL (basetype) != basetype
8215 || TYPE_CANONICAL (rettype) != rettype);
8216 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
8217 &any_structural_p,
8218 &any_noncanonical_p);
8219 if (any_structural_p)
8220 SET_TYPE_STRUCTURAL_EQUALITY (t);
8221 else if (any_noncanonical_p)
8222 TYPE_CANONICAL (t)
8223 = build_method_type_directly (TYPE_CANONICAL (basetype),
8224 TYPE_CANONICAL (rettype),
8225 canon_argtypes);
8226 if (!COMPLETE_TYPE_P (t))
8227 layout_type (t);
8229 return t;
8232 /* Construct, lay out and return the type of methods belonging to class
8233 BASETYPE and whose arguments and values are described by TYPE.
8234 If that type exists already, reuse it.
8235 TYPE must be a FUNCTION_TYPE node. */
8237 tree
8238 build_method_type (tree basetype, tree type)
8240 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8242 return build_method_type_directly (basetype,
8243 TREE_TYPE (type),
8244 TYPE_ARG_TYPES (type));
8247 /* Construct, lay out and return the type of offsets to a value
8248 of type TYPE, within an object of type BASETYPE.
8249 If a suitable offset type exists already, reuse it. */
8251 tree
8252 build_offset_type (tree basetype, tree type)
8254 tree t;
8255 hashval_t hashcode = 0;
8257 /* Make a node of the sort we want. */
8258 t = make_node (OFFSET_TYPE);
8260 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8261 TREE_TYPE (t) = type;
8263 /* If we already have such a type, use the old one. */
8264 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
8265 hashcode = iterative_hash_object (TYPE_HASH (type), hashcode);
8266 t = type_hash_canon (hashcode, t);
8268 if (!COMPLETE_TYPE_P (t))
8269 layout_type (t);
8271 if (TYPE_CANONICAL (t) == t)
8273 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8274 || TYPE_STRUCTURAL_EQUALITY_P (type))
8275 SET_TYPE_STRUCTURAL_EQUALITY (t);
8276 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
8277 || TYPE_CANONICAL (type) != type)
8278 TYPE_CANONICAL (t)
8279 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
8280 TYPE_CANONICAL (type));
8283 return t;
8286 /* Create a complex type whose components are COMPONENT_TYPE. */
8288 tree
8289 build_complex_type (tree component_type)
8291 tree t;
8292 hashval_t hashcode;
8294 gcc_assert (INTEGRAL_TYPE_P (component_type)
8295 || SCALAR_FLOAT_TYPE_P (component_type)
8296 || FIXED_POINT_TYPE_P (component_type));
8298 /* Make a node of the sort we want. */
8299 t = make_node (COMPLEX_TYPE);
8301 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
8303 /* If we already have such a type, use the old one. */
8304 hashcode = iterative_hash_object (TYPE_HASH (component_type), 0);
8305 t = type_hash_canon (hashcode, t);
8307 if (!COMPLETE_TYPE_P (t))
8308 layout_type (t);
8310 if (TYPE_CANONICAL (t) == t)
8312 if (TYPE_STRUCTURAL_EQUALITY_P (component_type))
8313 SET_TYPE_STRUCTURAL_EQUALITY (t);
8314 else if (TYPE_CANONICAL (component_type) != component_type)
8315 TYPE_CANONICAL (t)
8316 = build_complex_type (TYPE_CANONICAL (component_type));
8319 /* We need to create a name, since complex is a fundamental type. */
8320 if (! TYPE_NAME (t))
8322 const char *name;
8323 if (component_type == char_type_node)
8324 name = "complex char";
8325 else if (component_type == signed_char_type_node)
8326 name = "complex signed char";
8327 else if (component_type == unsigned_char_type_node)
8328 name = "complex unsigned char";
8329 else if (component_type == short_integer_type_node)
8330 name = "complex short int";
8331 else if (component_type == short_unsigned_type_node)
8332 name = "complex short unsigned int";
8333 else if (component_type == integer_type_node)
8334 name = "complex int";
8335 else if (component_type == unsigned_type_node)
8336 name = "complex unsigned int";
8337 else if (component_type == long_integer_type_node)
8338 name = "complex long int";
8339 else if (component_type == long_unsigned_type_node)
8340 name = "complex long unsigned int";
8341 else if (component_type == long_long_integer_type_node)
8342 name = "complex long long int";
8343 else if (component_type == long_long_unsigned_type_node)
8344 name = "complex long long unsigned int";
8345 else
8346 name = 0;
8348 if (name != 0)
8349 TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
8350 get_identifier (name), t);
8353 return build_qualified_type (t, TYPE_QUALS (component_type));
8356 /* If TYPE is a real or complex floating-point type and the target
8357 does not directly support arithmetic on TYPE then return the wider
8358 type to be used for arithmetic on TYPE. Otherwise, return
8359 NULL_TREE. */
8361 tree
8362 excess_precision_type (tree type)
8364 if (flag_excess_precision != EXCESS_PRECISION_FAST)
8366 int flt_eval_method = TARGET_FLT_EVAL_METHOD;
8367 switch (TREE_CODE (type))
8369 case REAL_TYPE:
8370 switch (flt_eval_method)
8372 case 1:
8373 if (TYPE_MODE (type) == TYPE_MODE (float_type_node))
8374 return double_type_node;
8375 break;
8376 case 2:
8377 if (TYPE_MODE (type) == TYPE_MODE (float_type_node)
8378 || TYPE_MODE (type) == TYPE_MODE (double_type_node))
8379 return long_double_type_node;
8380 break;
8381 default:
8382 gcc_unreachable ();
8384 break;
8385 case COMPLEX_TYPE:
8386 if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
8387 return NULL_TREE;
8388 switch (flt_eval_method)
8390 case 1:
8391 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node))
8392 return complex_double_type_node;
8393 break;
8394 case 2:
8395 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node)
8396 || (TYPE_MODE (TREE_TYPE (type))
8397 == TYPE_MODE (double_type_node)))
8398 return complex_long_double_type_node;
8399 break;
8400 default:
8401 gcc_unreachable ();
8403 break;
8404 default:
8405 break;
8408 return NULL_TREE;
8411 /* Return OP, stripped of any conversions to wider types as much as is safe.
8412 Converting the value back to OP's type makes a value equivalent to OP.
8414 If FOR_TYPE is nonzero, we return a value which, if converted to
8415 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
8417 OP must have integer, real or enumeral type. Pointers are not allowed!
8419 There are some cases where the obvious value we could return
8420 would regenerate to OP if converted to OP's type,
8421 but would not extend like OP to wider types.
8422 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
8423 For example, if OP is (unsigned short)(signed char)-1,
8424 we avoid returning (signed char)-1 if FOR_TYPE is int,
8425 even though extending that to an unsigned short would regenerate OP,
8426 since the result of extending (signed char)-1 to (int)
8427 is different from (int) OP. */
8429 tree
8430 get_unwidened (tree op, tree for_type)
8432 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
8433 tree type = TREE_TYPE (op);
8434 unsigned final_prec
8435 = TYPE_PRECISION (for_type != 0 ? for_type : type);
8436 int uns
8437 = (for_type != 0 && for_type != type
8438 && final_prec > TYPE_PRECISION (type)
8439 && TYPE_UNSIGNED (type));
8440 tree win = op;
8442 while (CONVERT_EXPR_P (op))
8444 int bitschange;
8446 /* TYPE_PRECISION on vector types has different meaning
8447 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
8448 so avoid them here. */
8449 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
8450 break;
8452 bitschange = TYPE_PRECISION (TREE_TYPE (op))
8453 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
8455 /* Truncations are many-one so cannot be removed.
8456 Unless we are later going to truncate down even farther. */
8457 if (bitschange < 0
8458 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
8459 break;
8461 /* See what's inside this conversion. If we decide to strip it,
8462 we will set WIN. */
8463 op = TREE_OPERAND (op, 0);
8465 /* If we have not stripped any zero-extensions (uns is 0),
8466 we can strip any kind of extension.
8467 If we have previously stripped a zero-extension,
8468 only zero-extensions can safely be stripped.
8469 Any extension can be stripped if the bits it would produce
8470 are all going to be discarded later by truncating to FOR_TYPE. */
8472 if (bitschange > 0)
8474 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
8475 win = op;
8476 /* TYPE_UNSIGNED says whether this is a zero-extension.
8477 Let's avoid computing it if it does not affect WIN
8478 and if UNS will not be needed again. */
8479 if ((uns
8480 || CONVERT_EXPR_P (op))
8481 && TYPE_UNSIGNED (TREE_TYPE (op)))
8483 uns = 1;
8484 win = op;
8489 /* If we finally reach a constant see if it fits in for_type and
8490 in that case convert it. */
8491 if (for_type
8492 && TREE_CODE (win) == INTEGER_CST
8493 && TREE_TYPE (win) != for_type
8494 && int_fits_type_p (win, for_type))
8495 win = fold_convert (for_type, win);
8497 return win;
8500 /* Return OP or a simpler expression for a narrower value
8501 which can be sign-extended or zero-extended to give back OP.
8502 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
8503 or 0 if the value should be sign-extended. */
8505 tree
8506 get_narrower (tree op, int *unsignedp_ptr)
8508 int uns = 0;
8509 int first = 1;
8510 tree win = op;
8511 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
8513 while (TREE_CODE (op) == NOP_EXPR)
8515 int bitschange
8516 = (TYPE_PRECISION (TREE_TYPE (op))
8517 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
8519 /* Truncations are many-one so cannot be removed. */
8520 if (bitschange < 0)
8521 break;
8523 /* See what's inside this conversion. If we decide to strip it,
8524 we will set WIN. */
8526 if (bitschange > 0)
8528 op = TREE_OPERAND (op, 0);
8529 /* An extension: the outermost one can be stripped,
8530 but remember whether it is zero or sign extension. */
8531 if (first)
8532 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8533 /* Otherwise, if a sign extension has been stripped,
8534 only sign extensions can now be stripped;
8535 if a zero extension has been stripped, only zero-extensions. */
8536 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
8537 break;
8538 first = 0;
8540 else /* bitschange == 0 */
8542 /* A change in nominal type can always be stripped, but we must
8543 preserve the unsignedness. */
8544 if (first)
8545 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8546 first = 0;
8547 op = TREE_OPERAND (op, 0);
8548 /* Keep trying to narrow, but don't assign op to win if it
8549 would turn an integral type into something else. */
8550 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
8551 continue;
8554 win = op;
8557 if (TREE_CODE (op) == COMPONENT_REF
8558 /* Since type_for_size always gives an integer type. */
8559 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
8560 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
8561 /* Ensure field is laid out already. */
8562 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
8563 && host_integerp (DECL_SIZE (TREE_OPERAND (op, 1)), 1))
8565 unsigned HOST_WIDE_INT innerprec
8566 = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
8567 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
8568 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
8569 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
8571 /* We can get this structure field in a narrower type that fits it,
8572 but the resulting extension to its nominal type (a fullword type)
8573 must satisfy the same conditions as for other extensions.
8575 Do this only for fields that are aligned (not bit-fields),
8576 because when bit-field insns will be used there is no
8577 advantage in doing this. */
8579 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
8580 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
8581 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
8582 && type != 0)
8584 if (first)
8585 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
8586 win = fold_convert (type, op);
8590 *unsignedp_ptr = uns;
8591 return win;
8594 /* Returns true if integer constant C has a value that is permissible
8595 for type TYPE (an INTEGER_TYPE). */
8597 bool
8598 int_fits_type_p (const_tree c, const_tree type)
8600 tree type_low_bound, type_high_bound;
8601 bool ok_for_low_bound, ok_for_high_bound, unsc;
8602 double_int dc, dd;
8604 dc = tree_to_double_int (c);
8605 unsc = TYPE_UNSIGNED (TREE_TYPE (c));
8607 retry:
8608 type_low_bound = TYPE_MIN_VALUE (type);
8609 type_high_bound = TYPE_MAX_VALUE (type);
8611 /* If at least one bound of the type is a constant integer, we can check
8612 ourselves and maybe make a decision. If no such decision is possible, but
8613 this type is a subtype, try checking against that. Otherwise, use
8614 double_int_fits_to_tree_p, which checks against the precision.
8616 Compute the status for each possibly constant bound, and return if we see
8617 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
8618 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
8619 for "constant known to fit". */
8621 /* Check if c >= type_low_bound. */
8622 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
8624 dd = tree_to_double_int (type_low_bound);
8625 if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_low_bound)))
8627 int c_neg = (!unsc && dc.is_negative ());
8628 int t_neg = (unsc && dd.is_negative ());
8630 if (c_neg && !t_neg)
8631 return false;
8632 if ((c_neg || !t_neg) && dc.ult (dd))
8633 return false;
8635 else if (dc.cmp (dd, unsc) < 0)
8636 return false;
8637 ok_for_low_bound = true;
8639 else
8640 ok_for_low_bound = false;
8642 /* Check if c <= type_high_bound. */
8643 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
8645 dd = tree_to_double_int (type_high_bound);
8646 if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_high_bound)))
8648 int c_neg = (!unsc && dc.is_negative ());
8649 int t_neg = (unsc && dd.is_negative ());
8651 if (t_neg && !c_neg)
8652 return false;
8653 if ((t_neg || !c_neg) && dc.ugt (dd))
8654 return false;
8656 else if (dc.cmp (dd, unsc) > 0)
8657 return false;
8658 ok_for_high_bound = true;
8660 else
8661 ok_for_high_bound = false;
8663 /* If the constant fits both bounds, the result is known. */
8664 if (ok_for_low_bound && ok_for_high_bound)
8665 return true;
8667 /* Perform some generic filtering which may allow making a decision
8668 even if the bounds are not constant. First, negative integers
8669 never fit in unsigned types, */
8670 if (TYPE_UNSIGNED (type) && !unsc && dc.is_negative ())
8671 return false;
8673 /* Second, narrower types always fit in wider ones. */
8674 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
8675 return true;
8677 /* Third, unsigned integers with top bit set never fit signed types. */
8678 if (! TYPE_UNSIGNED (type) && unsc)
8680 int prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (c))) - 1;
8681 if (prec < HOST_BITS_PER_WIDE_INT)
8683 if (((((unsigned HOST_WIDE_INT) 1) << prec) & dc.low) != 0)
8684 return false;
8686 else if (((((unsigned HOST_WIDE_INT) 1)
8687 << (prec - HOST_BITS_PER_WIDE_INT)) & dc.high) != 0)
8688 return false;
8691 /* If we haven't been able to decide at this point, there nothing more we
8692 can check ourselves here. Look at the base type if we have one and it
8693 has the same precision. */
8694 if (TREE_CODE (type) == INTEGER_TYPE
8695 && TREE_TYPE (type) != 0
8696 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
8698 type = TREE_TYPE (type);
8699 goto retry;
8702 /* Or to double_int_fits_to_tree_p, if nothing else. */
8703 return double_int_fits_to_tree_p (type, dc);
8706 /* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
8707 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
8708 represented (assuming two's-complement arithmetic) within the bit
8709 precision of the type are returned instead. */
8711 void
8712 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
8714 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
8715 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
8716 mpz_set_double_int (min, tree_to_double_int (TYPE_MIN_VALUE (type)),
8717 TYPE_UNSIGNED (type));
8718 else
8720 if (TYPE_UNSIGNED (type))
8721 mpz_set_ui (min, 0);
8722 else
8724 double_int mn;
8725 mn = double_int::mask (TYPE_PRECISION (type) - 1);
8726 mn = (mn + double_int_one).sext (TYPE_PRECISION (type));
8727 mpz_set_double_int (min, mn, false);
8731 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
8732 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
8733 mpz_set_double_int (max, tree_to_double_int (TYPE_MAX_VALUE (type)),
8734 TYPE_UNSIGNED (type));
8735 else
8737 if (TYPE_UNSIGNED (type))
8738 mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type)),
8739 true);
8740 else
8741 mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type) - 1),
8742 true);
8746 /* Return true if VAR is an automatic variable defined in function FN. */
8748 bool
8749 auto_var_in_fn_p (const_tree var, const_tree fn)
8751 return (DECL_P (var) && DECL_CONTEXT (var) == fn
8752 && ((((TREE_CODE (var) == VAR_DECL && ! DECL_EXTERNAL (var))
8753 || TREE_CODE (var) == PARM_DECL)
8754 && ! TREE_STATIC (var))
8755 || TREE_CODE (var) == LABEL_DECL
8756 || TREE_CODE (var) == RESULT_DECL));
8759 /* Subprogram of following function. Called by walk_tree.
8761 Return *TP if it is an automatic variable or parameter of the
8762 function passed in as DATA. */
8764 static tree
8765 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
8767 tree fn = (tree) data;
8769 if (TYPE_P (*tp))
8770 *walk_subtrees = 0;
8772 else if (DECL_P (*tp)
8773 && auto_var_in_fn_p (*tp, fn))
8774 return *tp;
8776 return NULL_TREE;
8779 /* Returns true if T is, contains, or refers to a type with variable
8780 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
8781 arguments, but not the return type. If FN is nonzero, only return
8782 true if a modifier of the type or position of FN is a variable or
8783 parameter inside FN.
8785 This concept is more general than that of C99 'variably modified types':
8786 in C99, a struct type is never variably modified because a VLA may not
8787 appear as a structure member. However, in GNU C code like:
8789 struct S { int i[f()]; };
8791 is valid, and other languages may define similar constructs. */
8793 bool
8794 variably_modified_type_p (tree type, tree fn)
8796 tree t;
8798 /* Test if T is either variable (if FN is zero) or an expression containing
8799 a variable in FN. If TYPE isn't gimplified, return true also if
8800 gimplify_one_sizepos would gimplify the expression into a local
8801 variable. */
8802 #define RETURN_TRUE_IF_VAR(T) \
8803 do { tree _t = (T); \
8804 if (_t != NULL_TREE \
8805 && _t != error_mark_node \
8806 && TREE_CODE (_t) != INTEGER_CST \
8807 && TREE_CODE (_t) != PLACEHOLDER_EXPR \
8808 && (!fn \
8809 || (!TYPE_SIZES_GIMPLIFIED (type) \
8810 && !is_gimple_sizepos (_t)) \
8811 || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
8812 return true; } while (0)
8814 if (type == error_mark_node)
8815 return false;
8817 /* If TYPE itself has variable size, it is variably modified. */
8818 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
8819 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
8821 switch (TREE_CODE (type))
8823 case POINTER_TYPE:
8824 case REFERENCE_TYPE:
8825 case VECTOR_TYPE:
8826 if (variably_modified_type_p (TREE_TYPE (type), fn))
8827 return true;
8828 break;
8830 case FUNCTION_TYPE:
8831 case METHOD_TYPE:
8832 /* If TYPE is a function type, it is variably modified if the
8833 return type is variably modified. */
8834 if (variably_modified_type_p (TREE_TYPE (type), fn))
8835 return true;
8836 break;
8838 case INTEGER_TYPE:
8839 case REAL_TYPE:
8840 case FIXED_POINT_TYPE:
8841 case ENUMERAL_TYPE:
8842 case BOOLEAN_TYPE:
8843 /* Scalar types are variably modified if their end points
8844 aren't constant. */
8845 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
8846 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
8847 break;
8849 case RECORD_TYPE:
8850 case UNION_TYPE:
8851 case QUAL_UNION_TYPE:
8852 /* We can't see if any of the fields are variably-modified by the
8853 definition we normally use, since that would produce infinite
8854 recursion via pointers. */
8855 /* This is variably modified if some field's type is. */
8856 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
8857 if (TREE_CODE (t) == FIELD_DECL)
8859 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
8860 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
8861 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
8863 if (TREE_CODE (type) == QUAL_UNION_TYPE)
8864 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
8866 break;
8868 case ARRAY_TYPE:
8869 /* Do not call ourselves to avoid infinite recursion. This is
8870 variably modified if the element type is. */
8871 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
8872 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
8873 break;
8875 default:
8876 break;
8879 /* The current language may have other cases to check, but in general,
8880 all other types are not variably modified. */
8881 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
8883 #undef RETURN_TRUE_IF_VAR
8886 /* Given a DECL or TYPE, return the scope in which it was declared, or
8887 NULL_TREE if there is no containing scope. */
8889 tree
8890 get_containing_scope (const_tree t)
8892 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
8895 /* Return the innermost context enclosing DECL that is
8896 a FUNCTION_DECL, or zero if none. */
8898 tree
8899 decl_function_context (const_tree decl)
8901 tree context;
8903 if (TREE_CODE (decl) == ERROR_MARK)
8904 return 0;
8906 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
8907 where we look up the function at runtime. Such functions always take
8908 a first argument of type 'pointer to real context'.
8910 C++ should really be fixed to use DECL_CONTEXT for the real context,
8911 and use something else for the "virtual context". */
8912 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
8913 context
8914 = TYPE_MAIN_VARIANT
8915 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
8916 else
8917 context = DECL_CONTEXT (decl);
8919 while (context && TREE_CODE (context) != FUNCTION_DECL)
8921 if (TREE_CODE (context) == BLOCK)
8922 context = BLOCK_SUPERCONTEXT (context);
8923 else
8924 context = get_containing_scope (context);
8927 return context;
8930 /* Return the innermost context enclosing DECL that is
8931 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
8932 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
8934 tree
8935 decl_type_context (const_tree decl)
8937 tree context = DECL_CONTEXT (decl);
8939 while (context)
8940 switch (TREE_CODE (context))
8942 case NAMESPACE_DECL:
8943 case TRANSLATION_UNIT_DECL:
8944 return NULL_TREE;
8946 case RECORD_TYPE:
8947 case UNION_TYPE:
8948 case QUAL_UNION_TYPE:
8949 return context;
8951 case TYPE_DECL:
8952 case FUNCTION_DECL:
8953 context = DECL_CONTEXT (context);
8954 break;
8956 case BLOCK:
8957 context = BLOCK_SUPERCONTEXT (context);
8958 break;
8960 default:
8961 gcc_unreachable ();
8964 return NULL_TREE;
8967 /* CALL is a CALL_EXPR. Return the declaration for the function
8968 called, or NULL_TREE if the called function cannot be
8969 determined. */
8971 tree
8972 get_callee_fndecl (const_tree call)
8974 tree addr;
8976 if (call == error_mark_node)
8977 return error_mark_node;
8979 /* It's invalid to call this function with anything but a
8980 CALL_EXPR. */
8981 gcc_assert (TREE_CODE (call) == CALL_EXPR);
8983 /* The first operand to the CALL is the address of the function
8984 called. */
8985 addr = CALL_EXPR_FN (call);
8987 STRIP_NOPS (addr);
8989 /* If this is a readonly function pointer, extract its initial value. */
8990 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
8991 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
8992 && DECL_INITIAL (addr))
8993 addr = DECL_INITIAL (addr);
8995 /* If the address is just `&f' for some function `f', then we know
8996 that `f' is being called. */
8997 if (TREE_CODE (addr) == ADDR_EXPR
8998 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
8999 return TREE_OPERAND (addr, 0);
9001 /* We couldn't figure out what was being called. */
9002 return NULL_TREE;
9005 /* Print debugging information about tree nodes generated during the compile,
9006 and any language-specific information. */
9008 void
9009 dump_tree_statistics (void)
9011 if (GATHER_STATISTICS)
9013 int i;
9014 int total_nodes, total_bytes;
9015 fprintf (stderr, "Kind Nodes Bytes\n");
9016 fprintf (stderr, "---------------------------------------\n");
9017 total_nodes = total_bytes = 0;
9018 for (i = 0; i < (int) all_kinds; i++)
9020 fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
9021 tree_node_counts[i], tree_node_sizes[i]);
9022 total_nodes += tree_node_counts[i];
9023 total_bytes += tree_node_sizes[i];
9025 fprintf (stderr, "---------------------------------------\n");
9026 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
9027 fprintf (stderr, "---------------------------------------\n");
9028 fprintf (stderr, "Code Nodes\n");
9029 fprintf (stderr, "----------------------------\n");
9030 for (i = 0; i < (int) MAX_TREE_CODES; i++)
9031 fprintf (stderr, "%-20s %7d\n", get_tree_code_name ((enum tree_code) i),
9032 tree_code_counts[i]);
9033 fprintf (stderr, "----------------------------\n");
9034 ssanames_print_statistics ();
9035 phinodes_print_statistics ();
9037 else
9038 fprintf (stderr, "(No per-node statistics)\n");
9040 print_type_hash_statistics ();
9041 print_debug_expr_statistics ();
9042 print_value_expr_statistics ();
9043 lang_hooks.print_statistics ();
9046 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
9048 /* Generate a crc32 of a byte. */
9050 static unsigned
9051 crc32_unsigned_bits (unsigned chksum, unsigned value, unsigned bits)
9053 unsigned ix;
9055 for (ix = bits; ix--; value <<= 1)
9057 unsigned feedback;
9059 feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
9060 chksum <<= 1;
9061 chksum ^= feedback;
9063 return chksum;
9066 /* Generate a crc32 of a 32-bit unsigned. */
9068 unsigned
9069 crc32_unsigned (unsigned chksum, unsigned value)
9071 return crc32_unsigned_bits (chksum, value, 32);
9074 /* Generate a crc32 of a byte. */
9076 unsigned
9077 crc32_byte (unsigned chksum, char byte)
9079 return crc32_unsigned_bits (chksum, (unsigned) byte << 24, 8);
9082 /* Generate a crc32 of a string. */
9084 unsigned
9085 crc32_string (unsigned chksum, const char *string)
9089 chksum = crc32_byte (chksum, *string);
9091 while (*string++);
9092 return chksum;
9095 /* P is a string that will be used in a symbol. Mask out any characters
9096 that are not valid in that context. */
9098 void
9099 clean_symbol_name (char *p)
9101 for (; *p; p++)
9102 if (! (ISALNUM (*p)
9103 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
9104 || *p == '$'
9105 #endif
9106 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
9107 || *p == '.'
9108 #endif
9110 *p = '_';
9113 /* Generate a name for a special-purpose function.
9114 The generated name may need to be unique across the whole link.
9115 Changes to this function may also require corresponding changes to
9116 xstrdup_mask_random.
9117 TYPE is some string to identify the purpose of this function to the
9118 linker or collect2; it must start with an uppercase letter,
9119 one of:
9120 I - for constructors
9121 D - for destructors
9122 N - for C++ anonymous namespaces
9123 F - for DWARF unwind frame information. */
9125 tree
9126 get_file_function_name (const char *type)
9128 char *buf;
9129 const char *p;
9130 char *q;
9132 /* If we already have a name we know to be unique, just use that. */
9133 if (first_global_object_name)
9134 p = q = ASTRDUP (first_global_object_name);
9135 /* If the target is handling the constructors/destructors, they
9136 will be local to this file and the name is only necessary for
9137 debugging purposes.
9138 We also assign sub_I and sub_D sufixes to constructors called from
9139 the global static constructors. These are always local. */
9140 else if (((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
9141 || (strncmp (type, "sub_", 4) == 0
9142 && (type[4] == 'I' || type[4] == 'D')))
9144 const char *file = main_input_filename;
9145 if (! file)
9146 file = input_filename;
9147 /* Just use the file's basename, because the full pathname
9148 might be quite long. */
9149 p = q = ASTRDUP (lbasename (file));
9151 else
9153 /* Otherwise, the name must be unique across the entire link.
9154 We don't have anything that we know to be unique to this translation
9155 unit, so use what we do have and throw in some randomness. */
9156 unsigned len;
9157 const char *name = weak_global_object_name;
9158 const char *file = main_input_filename;
9160 if (! name)
9161 name = "";
9162 if (! file)
9163 file = input_filename;
9165 len = strlen (file);
9166 q = (char *) alloca (9 + 17 + len + 1);
9167 memcpy (q, file, len + 1);
9169 snprintf (q + len, 9 + 17 + 1, "_%08X_" HOST_WIDE_INT_PRINT_HEX,
9170 crc32_string (0, name), get_random_seed (false));
9172 p = q;
9175 clean_symbol_name (q);
9176 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
9177 + strlen (type));
9179 /* Set up the name of the file-level functions we may need.
9180 Use a global object (which is already required to be unique over
9181 the program) rather than the file name (which imposes extra
9182 constraints). */
9183 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
9185 return get_identifier (buf);
9188 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
9190 /* Complain that the tree code of NODE does not match the expected 0
9191 terminated list of trailing codes. The trailing code list can be
9192 empty, for a more vague error message. FILE, LINE, and FUNCTION
9193 are of the caller. */
9195 void
9196 tree_check_failed (const_tree node, const char *file,
9197 int line, const char *function, ...)
9199 va_list args;
9200 const char *buffer;
9201 unsigned length = 0;
9202 enum tree_code code;
9204 va_start (args, function);
9205 while ((code = (enum tree_code) va_arg (args, int)))
9206 length += 4 + strlen (get_tree_code_name (code));
9207 va_end (args);
9208 if (length)
9210 char *tmp;
9211 va_start (args, function);
9212 length += strlen ("expected ");
9213 buffer = tmp = (char *) alloca (length);
9214 length = 0;
9215 while ((code = (enum tree_code) va_arg (args, int)))
9217 const char *prefix = length ? " or " : "expected ";
9219 strcpy (tmp + length, prefix);
9220 length += strlen (prefix);
9221 strcpy (tmp + length, get_tree_code_name (code));
9222 length += strlen (get_tree_code_name (code));
9224 va_end (args);
9226 else
9227 buffer = "unexpected node";
9229 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9230 buffer, get_tree_code_name (TREE_CODE (node)),
9231 function, trim_filename (file), line);
9234 /* Complain that the tree code of NODE does match the expected 0
9235 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
9236 the caller. */
9238 void
9239 tree_not_check_failed (const_tree node, const char *file,
9240 int line, const char *function, ...)
9242 va_list args;
9243 char *buffer;
9244 unsigned length = 0;
9245 enum tree_code code;
9247 va_start (args, function);
9248 while ((code = (enum tree_code) va_arg (args, int)))
9249 length += 4 + strlen (get_tree_code_name (code));
9250 va_end (args);
9251 va_start (args, function);
9252 buffer = (char *) alloca (length);
9253 length = 0;
9254 while ((code = (enum tree_code) va_arg (args, int)))
9256 if (length)
9258 strcpy (buffer + length, " or ");
9259 length += 4;
9261 strcpy (buffer + length, get_tree_code_name (code));
9262 length += strlen (get_tree_code_name (code));
9264 va_end (args);
9266 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
9267 buffer, get_tree_code_name (TREE_CODE (node)),
9268 function, trim_filename (file), line);
9271 /* Similar to tree_check_failed, except that we check for a class of tree
9272 code, given in CL. */
9274 void
9275 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
9276 const char *file, int line, const char *function)
9278 internal_error
9279 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
9280 TREE_CODE_CLASS_STRING (cl),
9281 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9282 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9285 /* Similar to tree_check_failed, except that instead of specifying a
9286 dozen codes, use the knowledge that they're all sequential. */
9288 void
9289 tree_range_check_failed (const_tree node, const char *file, int line,
9290 const char *function, enum tree_code c1,
9291 enum tree_code c2)
9293 char *buffer;
9294 unsigned length = 0;
9295 unsigned int c;
9297 for (c = c1; c <= c2; ++c)
9298 length += 4 + strlen (get_tree_code_name ((enum tree_code) c));
9300 length += strlen ("expected ");
9301 buffer = (char *) alloca (length);
9302 length = 0;
9304 for (c = c1; c <= c2; ++c)
9306 const char *prefix = length ? " or " : "expected ";
9308 strcpy (buffer + length, prefix);
9309 length += strlen (prefix);
9310 strcpy (buffer + length, get_tree_code_name ((enum tree_code) c));
9311 length += strlen (get_tree_code_name ((enum tree_code) c));
9314 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9315 buffer, get_tree_code_name (TREE_CODE (node)),
9316 function, trim_filename (file), line);
9320 /* Similar to tree_check_failed, except that we check that a tree does
9321 not have the specified code, given in CL. */
9323 void
9324 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
9325 const char *file, int line, const char *function)
9327 internal_error
9328 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
9329 TREE_CODE_CLASS_STRING (cl),
9330 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9331 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9335 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
9337 void
9338 omp_clause_check_failed (const_tree node, const char *file, int line,
9339 const char *function, enum omp_clause_code code)
9341 internal_error ("tree check: expected omp_clause %s, have %s in %s, at %s:%d",
9342 omp_clause_code_name[code], get_tree_code_name (TREE_CODE (node)),
9343 function, trim_filename (file), line);
9347 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
9349 void
9350 omp_clause_range_check_failed (const_tree node, const char *file, int line,
9351 const char *function, enum omp_clause_code c1,
9352 enum omp_clause_code c2)
9354 char *buffer;
9355 unsigned length = 0;
9356 unsigned int c;
9358 for (c = c1; c <= c2; ++c)
9359 length += 4 + strlen (omp_clause_code_name[c]);
9361 length += strlen ("expected ");
9362 buffer = (char *) alloca (length);
9363 length = 0;
9365 for (c = c1; c <= c2; ++c)
9367 const char *prefix = length ? " or " : "expected ";
9369 strcpy (buffer + length, prefix);
9370 length += strlen (prefix);
9371 strcpy (buffer + length, omp_clause_code_name[c]);
9372 length += strlen (omp_clause_code_name[c]);
9375 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9376 buffer, omp_clause_code_name[TREE_CODE (node)],
9377 function, trim_filename (file), line);
9381 #undef DEFTREESTRUCT
9382 #define DEFTREESTRUCT(VAL, NAME) NAME,
9384 static const char *ts_enum_names[] = {
9385 #include "treestruct.def"
9387 #undef DEFTREESTRUCT
9389 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
9391 /* Similar to tree_class_check_failed, except that we check for
9392 whether CODE contains the tree structure identified by EN. */
9394 void
9395 tree_contains_struct_check_failed (const_tree node,
9396 const enum tree_node_structure_enum en,
9397 const char *file, int line,
9398 const char *function)
9400 internal_error
9401 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
9402 TS_ENUM_NAME (en),
9403 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9407 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9408 (dynamically sized) vector. */
9410 void
9411 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
9412 const char *function)
9414 internal_error
9415 ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
9416 idx + 1, len, function, trim_filename (file), line);
9419 /* Similar to above, except that the check is for the bounds of the operand
9420 vector of an expression node EXP. */
9422 void
9423 tree_operand_check_failed (int idx, const_tree exp, const char *file,
9424 int line, const char *function)
9426 enum tree_code code = TREE_CODE (exp);
9427 internal_error
9428 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
9429 idx + 1, get_tree_code_name (code), TREE_OPERAND_LENGTH (exp),
9430 function, trim_filename (file), line);
9433 /* Similar to above, except that the check is for the number of
9434 operands of an OMP_CLAUSE node. */
9436 void
9437 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
9438 int line, const char *function)
9440 internal_error
9441 ("tree check: accessed operand %d of omp_clause %s with %d operands "
9442 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
9443 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
9444 trim_filename (file), line);
9446 #endif /* ENABLE_TREE_CHECKING */
9448 /* Create a new vector type node holding SUBPARTS units of type INNERTYPE,
9449 and mapped to the machine mode MODE. Initialize its fields and build
9450 the information necessary for debugging output. */
9452 static tree
9453 make_vector_type (tree innertype, int nunits, enum machine_mode mode)
9455 tree t;
9456 hashval_t hashcode = 0;
9458 t = make_node (VECTOR_TYPE);
9459 TREE_TYPE (t) = TYPE_MAIN_VARIANT (innertype);
9460 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
9461 SET_TYPE_MODE (t, mode);
9463 if (TYPE_STRUCTURAL_EQUALITY_P (innertype))
9464 SET_TYPE_STRUCTURAL_EQUALITY (t);
9465 else if (TYPE_CANONICAL (innertype) != innertype
9466 || mode != VOIDmode)
9467 TYPE_CANONICAL (t)
9468 = make_vector_type (TYPE_CANONICAL (innertype), nunits, VOIDmode);
9470 layout_type (t);
9472 hashcode = iterative_hash_host_wide_int (VECTOR_TYPE, hashcode);
9473 hashcode = iterative_hash_host_wide_int (nunits, hashcode);
9474 hashcode = iterative_hash_host_wide_int (mode, hashcode);
9475 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (t)), hashcode);
9476 t = type_hash_canon (hashcode, t);
9478 /* We have built a main variant, based on the main variant of the
9479 inner type. Use it to build the variant we return. */
9480 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
9481 && TREE_TYPE (t) != innertype)
9482 return build_type_attribute_qual_variant (t,
9483 TYPE_ATTRIBUTES (innertype),
9484 TYPE_QUALS (innertype));
9486 return t;
9489 static tree
9490 make_or_reuse_type (unsigned size, int unsignedp)
9492 if (size == INT_TYPE_SIZE)
9493 return unsignedp ? unsigned_type_node : integer_type_node;
9494 if (size == CHAR_TYPE_SIZE)
9495 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
9496 if (size == SHORT_TYPE_SIZE)
9497 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
9498 if (size == LONG_TYPE_SIZE)
9499 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
9500 if (size == LONG_LONG_TYPE_SIZE)
9501 return (unsignedp ? long_long_unsigned_type_node
9502 : long_long_integer_type_node);
9503 if (size == 128 && int128_integer_type_node)
9504 return (unsignedp ? int128_unsigned_type_node
9505 : int128_integer_type_node);
9507 if (unsignedp)
9508 return make_unsigned_type (size);
9509 else
9510 return make_signed_type (size);
9513 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
9515 static tree
9516 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
9518 if (satp)
9520 if (size == SHORT_FRACT_TYPE_SIZE)
9521 return unsignedp ? sat_unsigned_short_fract_type_node
9522 : sat_short_fract_type_node;
9523 if (size == FRACT_TYPE_SIZE)
9524 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
9525 if (size == LONG_FRACT_TYPE_SIZE)
9526 return unsignedp ? sat_unsigned_long_fract_type_node
9527 : sat_long_fract_type_node;
9528 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9529 return unsignedp ? sat_unsigned_long_long_fract_type_node
9530 : sat_long_long_fract_type_node;
9532 else
9534 if (size == SHORT_FRACT_TYPE_SIZE)
9535 return unsignedp ? unsigned_short_fract_type_node
9536 : short_fract_type_node;
9537 if (size == FRACT_TYPE_SIZE)
9538 return unsignedp ? unsigned_fract_type_node : fract_type_node;
9539 if (size == LONG_FRACT_TYPE_SIZE)
9540 return unsignedp ? unsigned_long_fract_type_node
9541 : long_fract_type_node;
9542 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9543 return unsignedp ? unsigned_long_long_fract_type_node
9544 : long_long_fract_type_node;
9547 return make_fract_type (size, unsignedp, satp);
9550 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
9552 static tree
9553 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
9555 if (satp)
9557 if (size == SHORT_ACCUM_TYPE_SIZE)
9558 return unsignedp ? sat_unsigned_short_accum_type_node
9559 : sat_short_accum_type_node;
9560 if (size == ACCUM_TYPE_SIZE)
9561 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
9562 if (size == LONG_ACCUM_TYPE_SIZE)
9563 return unsignedp ? sat_unsigned_long_accum_type_node
9564 : sat_long_accum_type_node;
9565 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9566 return unsignedp ? sat_unsigned_long_long_accum_type_node
9567 : sat_long_long_accum_type_node;
9569 else
9571 if (size == SHORT_ACCUM_TYPE_SIZE)
9572 return unsignedp ? unsigned_short_accum_type_node
9573 : short_accum_type_node;
9574 if (size == ACCUM_TYPE_SIZE)
9575 return unsignedp ? unsigned_accum_type_node : accum_type_node;
9576 if (size == LONG_ACCUM_TYPE_SIZE)
9577 return unsignedp ? unsigned_long_accum_type_node
9578 : long_accum_type_node;
9579 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9580 return unsignedp ? unsigned_long_long_accum_type_node
9581 : long_long_accum_type_node;
9584 return make_accum_type (size, unsignedp, satp);
9588 /* Create an atomic variant node for TYPE. This routine is called
9589 during initialization of data types to create the 5 basic atomic
9590 types. The generic build_variant_type function requires these to
9591 already be set up in order to function properly, so cannot be
9592 called from there. */
9594 static tree
9595 build_atomic_base (tree type)
9597 tree t;
9599 /* Make sure its not already registered. */
9600 if ((t = get_qualified_type (type, TYPE_QUAL_ATOMIC)))
9601 return t;
9603 t = build_variant_type_copy (type);
9604 set_type_quals (t, TYPE_QUAL_ATOMIC, NULL_TREE);
9606 return t;
9609 /* Create nodes for all integer types (and error_mark_node) using the sizes
9610 of C datatypes. SIGNED_CHAR specifies whether char is signed,
9611 SHORT_DOUBLE specifies whether double should be of the same precision
9612 as float. */
9614 void
9615 build_common_tree_nodes (bool signed_char, bool short_double)
9617 error_mark_node = make_node (ERROR_MARK);
9618 TREE_TYPE (error_mark_node) = error_mark_node;
9620 initialize_sizetypes ();
9622 /* Define both `signed char' and `unsigned char'. */
9623 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
9624 TYPE_STRING_FLAG (signed_char_type_node) = 1;
9625 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
9626 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
9628 /* Define `char', which is like either `signed char' or `unsigned char'
9629 but not the same as either. */
9630 char_type_node
9631 = (signed_char
9632 ? make_signed_type (CHAR_TYPE_SIZE)
9633 : make_unsigned_type (CHAR_TYPE_SIZE));
9634 TYPE_STRING_FLAG (char_type_node) = 1;
9636 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
9637 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
9638 integer_type_node = make_signed_type (INT_TYPE_SIZE);
9639 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
9640 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
9641 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
9642 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
9643 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
9644 #if HOST_BITS_PER_WIDE_INT >= 64
9645 /* TODO: This isn't correct, but as logic depends at the moment on
9646 host's instead of target's wide-integer.
9647 If there is a target not supporting TImode, but has an 128-bit
9648 integer-scalar register, this target check needs to be adjusted. */
9649 if (targetm.scalar_mode_supported_p (TImode))
9651 int128_integer_type_node = make_signed_type (128);
9652 int128_unsigned_type_node = make_unsigned_type (128);
9654 #endif
9656 /* Define a boolean type. This type only represents boolean values but
9657 may be larger than char depending on the value of BOOL_TYPE_SIZE.
9658 Front ends which want to override this size (i.e. Java) can redefine
9659 boolean_type_node before calling build_common_tree_nodes_2. */
9660 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
9661 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
9662 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
9663 TYPE_PRECISION (boolean_type_node) = 1;
9665 /* Define what type to use for size_t. */
9666 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
9667 size_type_node = unsigned_type_node;
9668 else if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
9669 size_type_node = long_unsigned_type_node;
9670 else if (strcmp (SIZE_TYPE, "long long unsigned int") == 0)
9671 size_type_node = long_long_unsigned_type_node;
9672 else if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
9673 size_type_node = short_unsigned_type_node;
9674 else
9675 gcc_unreachable ();
9677 /* Fill in the rest of the sized types. Reuse existing type nodes
9678 when possible. */
9679 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
9680 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
9681 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
9682 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
9683 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
9685 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
9686 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
9687 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
9688 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
9689 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
9691 /* Don't call build_qualified type for atomics. That routine does
9692 special processing for atomics, and until they are initialized
9693 it's better not to make that call. */
9695 atomicQI_type_node = build_atomic_base (unsigned_intQI_type_node);
9696 atomicHI_type_node = build_atomic_base (unsigned_intHI_type_node);
9697 atomicSI_type_node = build_atomic_base (unsigned_intSI_type_node);
9698 atomicDI_type_node = build_atomic_base (unsigned_intDI_type_node);
9699 atomicTI_type_node = build_atomic_base (unsigned_intTI_type_node);
9701 access_public_node = get_identifier ("public");
9702 access_protected_node = get_identifier ("protected");
9703 access_private_node = get_identifier ("private");
9705 /* Define these next since types below may used them. */
9706 integer_zero_node = build_int_cst (integer_type_node, 0);
9707 integer_one_node = build_int_cst (integer_type_node, 1);
9708 integer_three_node = build_int_cst (integer_type_node, 3);
9709 integer_minus_one_node = build_int_cst (integer_type_node, -1);
9711 size_zero_node = size_int (0);
9712 size_one_node = size_int (1);
9713 bitsize_zero_node = bitsize_int (0);
9714 bitsize_one_node = bitsize_int (1);
9715 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
9717 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
9718 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
9720 void_type_node = make_node (VOID_TYPE);
9721 layout_type (void_type_node);
9723 pointer_bounds_type_node = targetm.chkp_bound_type ();
9725 /* We are not going to have real types in C with less than byte alignment,
9726 so we might as well not have any types that claim to have it. */
9727 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
9728 TYPE_USER_ALIGN (void_type_node) = 0;
9730 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
9731 layout_type (TREE_TYPE (null_pointer_node));
9733 ptr_type_node = build_pointer_type (void_type_node);
9734 const_ptr_type_node
9735 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
9736 fileptr_type_node = ptr_type_node;
9738 pointer_sized_int_node = build_nonstandard_integer_type (POINTER_SIZE, 1);
9740 float_type_node = make_node (REAL_TYPE);
9741 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
9742 layout_type (float_type_node);
9744 double_type_node = make_node (REAL_TYPE);
9745 if (short_double)
9746 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
9747 else
9748 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
9749 layout_type (double_type_node);
9751 long_double_type_node = make_node (REAL_TYPE);
9752 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
9753 layout_type (long_double_type_node);
9755 float_ptr_type_node = build_pointer_type (float_type_node);
9756 double_ptr_type_node = build_pointer_type (double_type_node);
9757 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
9758 integer_ptr_type_node = build_pointer_type (integer_type_node);
9760 /* Fixed size integer types. */
9761 uint16_type_node = build_nonstandard_integer_type (16, true);
9762 uint32_type_node = build_nonstandard_integer_type (32, true);
9763 uint64_type_node = build_nonstandard_integer_type (64, true);
9765 /* Decimal float types. */
9766 dfloat32_type_node = make_node (REAL_TYPE);
9767 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
9768 layout_type (dfloat32_type_node);
9769 SET_TYPE_MODE (dfloat32_type_node, SDmode);
9770 dfloat32_ptr_type_node = build_pointer_type (dfloat32_type_node);
9772 dfloat64_type_node = make_node (REAL_TYPE);
9773 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
9774 layout_type (dfloat64_type_node);
9775 SET_TYPE_MODE (dfloat64_type_node, DDmode);
9776 dfloat64_ptr_type_node = build_pointer_type (dfloat64_type_node);
9778 dfloat128_type_node = make_node (REAL_TYPE);
9779 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
9780 layout_type (dfloat128_type_node);
9781 SET_TYPE_MODE (dfloat128_type_node, TDmode);
9782 dfloat128_ptr_type_node = build_pointer_type (dfloat128_type_node);
9784 complex_integer_type_node = build_complex_type (integer_type_node);
9785 complex_float_type_node = build_complex_type (float_type_node);
9786 complex_double_type_node = build_complex_type (double_type_node);
9787 complex_long_double_type_node = build_complex_type (long_double_type_node);
9789 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
9790 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
9791 sat_ ## KIND ## _type_node = \
9792 make_sat_signed_ ## KIND ## _type (SIZE); \
9793 sat_unsigned_ ## KIND ## _type_node = \
9794 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9795 KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9796 unsigned_ ## KIND ## _type_node = \
9797 make_unsigned_ ## KIND ## _type (SIZE);
9799 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
9800 sat_ ## WIDTH ## KIND ## _type_node = \
9801 make_sat_signed_ ## KIND ## _type (SIZE); \
9802 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
9803 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9804 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9805 unsigned_ ## WIDTH ## KIND ## _type_node = \
9806 make_unsigned_ ## KIND ## _type (SIZE);
9808 /* Make fixed-point type nodes based on four different widths. */
9809 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
9810 MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
9811 MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
9812 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
9813 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
9815 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
9816 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
9817 NAME ## _type_node = \
9818 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
9819 u ## NAME ## _type_node = \
9820 make_or_reuse_unsigned_ ## KIND ## _type \
9821 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
9822 sat_ ## NAME ## _type_node = \
9823 make_or_reuse_sat_signed_ ## KIND ## _type \
9824 (GET_MODE_BITSIZE (MODE ## mode)); \
9825 sat_u ## NAME ## _type_node = \
9826 make_or_reuse_sat_unsigned_ ## KIND ## _type \
9827 (GET_MODE_BITSIZE (U ## MODE ## mode));
9829 /* Fixed-point type and mode nodes. */
9830 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
9831 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
9832 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
9833 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
9834 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
9835 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
9836 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
9837 MAKE_FIXED_MODE_NODE (accum, ha, HA)
9838 MAKE_FIXED_MODE_NODE (accum, sa, SA)
9839 MAKE_FIXED_MODE_NODE (accum, da, DA)
9840 MAKE_FIXED_MODE_NODE (accum, ta, TA)
9843 tree t = targetm.build_builtin_va_list ();
9845 /* Many back-ends define record types without setting TYPE_NAME.
9846 If we copied the record type here, we'd keep the original
9847 record type without a name. This breaks name mangling. So,
9848 don't copy record types and let c_common_nodes_and_builtins()
9849 declare the type to be __builtin_va_list. */
9850 if (TREE_CODE (t) != RECORD_TYPE)
9851 t = build_variant_type_copy (t);
9853 va_list_type_node = t;
9857 /* Modify DECL for given flags.
9858 TM_PURE attribute is set only on types, so the function will modify
9859 DECL's type when ECF_TM_PURE is used. */
9861 void
9862 set_call_expr_flags (tree decl, int flags)
9864 if (flags & ECF_NOTHROW)
9865 TREE_NOTHROW (decl) = 1;
9866 if (flags & ECF_CONST)
9867 TREE_READONLY (decl) = 1;
9868 if (flags & ECF_PURE)
9869 DECL_PURE_P (decl) = 1;
9870 if (flags & ECF_LOOPING_CONST_OR_PURE)
9871 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
9872 if (flags & ECF_NOVOPS)
9873 DECL_IS_NOVOPS (decl) = 1;
9874 if (flags & ECF_NORETURN)
9875 TREE_THIS_VOLATILE (decl) = 1;
9876 if (flags & ECF_MALLOC)
9877 DECL_IS_MALLOC (decl) = 1;
9878 if (flags & ECF_RETURNS_TWICE)
9879 DECL_IS_RETURNS_TWICE (decl) = 1;
9880 if (flags & ECF_LEAF)
9881 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("leaf"),
9882 NULL, DECL_ATTRIBUTES (decl));
9883 if ((flags & ECF_TM_PURE) && flag_tm)
9884 apply_tm_attr (decl, get_identifier ("transaction_pure"));
9885 /* Looping const or pure is implied by noreturn.
9886 There is currently no way to declare looping const or looping pure alone. */
9887 gcc_assert (!(flags & ECF_LOOPING_CONST_OR_PURE)
9888 || ((flags & ECF_NORETURN) && (flags & (ECF_CONST | ECF_PURE))));
9892 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
9894 static void
9895 local_define_builtin (const char *name, tree type, enum built_in_function code,
9896 const char *library_name, int ecf_flags)
9898 tree decl;
9900 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
9901 library_name, NULL_TREE);
9902 set_call_expr_flags (decl, ecf_flags);
9904 set_builtin_decl (code, decl, true);
9907 /* Call this function after instantiating all builtins that the language
9908 front end cares about. This will build the rest of the builtins that
9909 are relied upon by the tree optimizers and the middle-end. */
9911 void
9912 build_common_builtin_nodes (void)
9914 tree tmp, ftype;
9915 int ecf_flags;
9917 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE))
9919 ftype = build_function_type (void_type_node, void_list_node);
9920 local_define_builtin ("__builtin_unreachable", ftype, BUILT_IN_UNREACHABLE,
9921 "__builtin_unreachable",
9922 ECF_NOTHROW | ECF_LEAF | ECF_NORETURN
9923 | ECF_CONST | ECF_LEAF);
9926 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY)
9927 || !builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9929 ftype = build_function_type_list (ptr_type_node,
9930 ptr_type_node, const_ptr_type_node,
9931 size_type_node, NULL_TREE);
9933 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY))
9934 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
9935 "memcpy", ECF_NOTHROW | ECF_LEAF);
9936 if (!builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9937 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
9938 "memmove", ECF_NOTHROW | ECF_LEAF);
9941 if (!builtin_decl_explicit_p (BUILT_IN_MEMCMP))
9943 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
9944 const_ptr_type_node, size_type_node,
9945 NULL_TREE);
9946 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
9947 "memcmp", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9950 if (!builtin_decl_explicit_p (BUILT_IN_MEMSET))
9952 ftype = build_function_type_list (ptr_type_node,
9953 ptr_type_node, integer_type_node,
9954 size_type_node, NULL_TREE);
9955 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
9956 "memset", ECF_NOTHROW | ECF_LEAF);
9959 if (!builtin_decl_explicit_p (BUILT_IN_ALLOCA))
9961 ftype = build_function_type_list (ptr_type_node,
9962 size_type_node, NULL_TREE);
9963 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
9964 "alloca", ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
9967 ftype = build_function_type_list (ptr_type_node, size_type_node,
9968 size_type_node, NULL_TREE);
9969 local_define_builtin ("__builtin_alloca_with_align", ftype,
9970 BUILT_IN_ALLOCA_WITH_ALIGN, "alloca",
9971 ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
9973 /* If we're checking the stack, `alloca' can throw. */
9974 if (flag_stack_check)
9976 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA)) = 0;
9977 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN)) = 0;
9980 ftype = build_function_type_list (void_type_node,
9981 ptr_type_node, ptr_type_node,
9982 ptr_type_node, NULL_TREE);
9983 local_define_builtin ("__builtin_init_trampoline", ftype,
9984 BUILT_IN_INIT_TRAMPOLINE,
9985 "__builtin_init_trampoline", ECF_NOTHROW | ECF_LEAF);
9986 local_define_builtin ("__builtin_init_heap_trampoline", ftype,
9987 BUILT_IN_INIT_HEAP_TRAMPOLINE,
9988 "__builtin_init_heap_trampoline",
9989 ECF_NOTHROW | ECF_LEAF);
9991 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
9992 local_define_builtin ("__builtin_adjust_trampoline", ftype,
9993 BUILT_IN_ADJUST_TRAMPOLINE,
9994 "__builtin_adjust_trampoline",
9995 ECF_CONST | ECF_NOTHROW);
9997 ftype = build_function_type_list (void_type_node,
9998 ptr_type_node, ptr_type_node, NULL_TREE);
9999 local_define_builtin ("__builtin_nonlocal_goto", ftype,
10000 BUILT_IN_NONLOCAL_GOTO,
10001 "__builtin_nonlocal_goto",
10002 ECF_NORETURN | ECF_NOTHROW);
10004 ftype = build_function_type_list (void_type_node,
10005 ptr_type_node, ptr_type_node, NULL_TREE);
10006 local_define_builtin ("__builtin_setjmp_setup", ftype,
10007 BUILT_IN_SETJMP_SETUP,
10008 "__builtin_setjmp_setup", ECF_NOTHROW);
10010 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
10011 local_define_builtin ("__builtin_setjmp_dispatcher", ftype,
10012 BUILT_IN_SETJMP_DISPATCHER,
10013 "__builtin_setjmp_dispatcher",
10014 ECF_PURE | ECF_NOTHROW);
10016 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10017 local_define_builtin ("__builtin_setjmp_receiver", ftype,
10018 BUILT_IN_SETJMP_RECEIVER,
10019 "__builtin_setjmp_receiver", ECF_NOTHROW);
10021 ftype = build_function_type_list (ptr_type_node, NULL_TREE);
10022 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
10023 "__builtin_stack_save", ECF_NOTHROW | ECF_LEAF);
10025 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10026 local_define_builtin ("__builtin_stack_restore", ftype,
10027 BUILT_IN_STACK_RESTORE,
10028 "__builtin_stack_restore", ECF_NOTHROW | ECF_LEAF);
10030 /* If there's a possibility that we might use the ARM EABI, build the
10031 alternate __cxa_end_cleanup node used to resume from C++ and Java. */
10032 if (targetm.arm_eabi_unwinder)
10034 ftype = build_function_type_list (void_type_node, NULL_TREE);
10035 local_define_builtin ("__builtin_cxa_end_cleanup", ftype,
10036 BUILT_IN_CXA_END_CLEANUP,
10037 "__cxa_end_cleanup", ECF_NORETURN | ECF_LEAF);
10040 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10041 local_define_builtin ("__builtin_unwind_resume", ftype,
10042 BUILT_IN_UNWIND_RESUME,
10043 ((targetm_common.except_unwind_info (&global_options)
10044 == UI_SJLJ)
10045 ? "_Unwind_SjLj_Resume" : "_Unwind_Resume"),
10046 ECF_NORETURN);
10048 if (builtin_decl_explicit (BUILT_IN_RETURN_ADDRESS) == NULL_TREE)
10050 ftype = build_function_type_list (ptr_type_node, integer_type_node,
10051 NULL_TREE);
10052 local_define_builtin ("__builtin_return_address", ftype,
10053 BUILT_IN_RETURN_ADDRESS,
10054 "__builtin_return_address",
10055 ECF_NOTHROW);
10058 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER)
10059 || !builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
10061 ftype = build_function_type_list (void_type_node, ptr_type_node,
10062 ptr_type_node, NULL_TREE);
10063 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER))
10064 local_define_builtin ("__cyg_profile_func_enter", ftype,
10065 BUILT_IN_PROFILE_FUNC_ENTER,
10066 "__cyg_profile_func_enter", 0);
10067 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
10068 local_define_builtin ("__cyg_profile_func_exit", ftype,
10069 BUILT_IN_PROFILE_FUNC_EXIT,
10070 "__cyg_profile_func_exit", 0);
10073 /* The exception object and filter values from the runtime. The argument
10074 must be zero before exception lowering, i.e. from the front end. After
10075 exception lowering, it will be the region number for the exception
10076 landing pad. These functions are PURE instead of CONST to prevent
10077 them from being hoisted past the exception edge that will initialize
10078 its value in the landing pad. */
10079 ftype = build_function_type_list (ptr_type_node,
10080 integer_type_node, NULL_TREE);
10081 ecf_flags = ECF_PURE | ECF_NOTHROW | ECF_LEAF;
10082 /* Only use TM_PURE if we we have TM language support. */
10083 if (builtin_decl_explicit_p (BUILT_IN_TM_LOAD_1))
10084 ecf_flags |= ECF_TM_PURE;
10085 local_define_builtin ("__builtin_eh_pointer", ftype, BUILT_IN_EH_POINTER,
10086 "__builtin_eh_pointer", ecf_flags);
10088 tmp = lang_hooks.types.type_for_mode (targetm.eh_return_filter_mode (), 0);
10089 ftype = build_function_type_list (tmp, integer_type_node, NULL_TREE);
10090 local_define_builtin ("__builtin_eh_filter", ftype, BUILT_IN_EH_FILTER,
10091 "__builtin_eh_filter", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10093 ftype = build_function_type_list (void_type_node,
10094 integer_type_node, integer_type_node,
10095 NULL_TREE);
10096 local_define_builtin ("__builtin_eh_copy_values", ftype,
10097 BUILT_IN_EH_COPY_VALUES,
10098 "__builtin_eh_copy_values", ECF_NOTHROW);
10100 /* Complex multiplication and division. These are handled as builtins
10101 rather than optabs because emit_library_call_value doesn't support
10102 complex. Further, we can do slightly better with folding these
10103 beasties if the real and complex parts of the arguments are separate. */
10105 int mode;
10107 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
10109 char mode_name_buf[4], *q;
10110 const char *p;
10111 enum built_in_function mcode, dcode;
10112 tree type, inner_type;
10113 const char *prefix = "__";
10115 if (targetm.libfunc_gnu_prefix)
10116 prefix = "__gnu_";
10118 type = lang_hooks.types.type_for_mode ((enum machine_mode) mode, 0);
10119 if (type == NULL)
10120 continue;
10121 inner_type = TREE_TYPE (type);
10123 ftype = build_function_type_list (type, inner_type, inner_type,
10124 inner_type, inner_type, NULL_TREE);
10126 mcode = ((enum built_in_function)
10127 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10128 dcode = ((enum built_in_function)
10129 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10131 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
10132 *q = TOLOWER (*p);
10133 *q = '\0';
10135 built_in_names[mcode] = concat (prefix, "mul", mode_name_buf, "3",
10136 NULL);
10137 local_define_builtin (built_in_names[mcode], ftype, mcode,
10138 built_in_names[mcode],
10139 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
10141 built_in_names[dcode] = concat (prefix, "div", mode_name_buf, "3",
10142 NULL);
10143 local_define_builtin (built_in_names[dcode], ftype, dcode,
10144 built_in_names[dcode],
10145 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
10150 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
10151 better way.
10153 If we requested a pointer to a vector, build up the pointers that
10154 we stripped off while looking for the inner type. Similarly for
10155 return values from functions.
10157 The argument TYPE is the top of the chain, and BOTTOM is the
10158 new type which we will point to. */
10160 tree
10161 reconstruct_complex_type (tree type, tree bottom)
10163 tree inner, outer;
10165 if (TREE_CODE (type) == POINTER_TYPE)
10167 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10168 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
10169 TYPE_REF_CAN_ALIAS_ALL (type));
10171 else if (TREE_CODE (type) == REFERENCE_TYPE)
10173 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10174 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
10175 TYPE_REF_CAN_ALIAS_ALL (type));
10177 else if (TREE_CODE (type) == ARRAY_TYPE)
10179 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10180 outer = build_array_type (inner, TYPE_DOMAIN (type));
10182 else if (TREE_CODE (type) == FUNCTION_TYPE)
10184 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10185 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
10187 else if (TREE_CODE (type) == METHOD_TYPE)
10189 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10190 /* The build_method_type_directly() routine prepends 'this' to argument list,
10191 so we must compensate by getting rid of it. */
10192 outer
10193 = build_method_type_directly
10194 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
10195 inner,
10196 TREE_CHAIN (TYPE_ARG_TYPES (type)));
10198 else if (TREE_CODE (type) == OFFSET_TYPE)
10200 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10201 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
10203 else
10204 return bottom;
10206 return build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
10207 TYPE_QUALS (type));
10210 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
10211 the inner type. */
10212 tree
10213 build_vector_type_for_mode (tree innertype, enum machine_mode mode)
10215 int nunits;
10217 switch (GET_MODE_CLASS (mode))
10219 case MODE_VECTOR_INT:
10220 case MODE_VECTOR_FLOAT:
10221 case MODE_VECTOR_FRACT:
10222 case MODE_VECTOR_UFRACT:
10223 case MODE_VECTOR_ACCUM:
10224 case MODE_VECTOR_UACCUM:
10225 nunits = GET_MODE_NUNITS (mode);
10226 break;
10228 case MODE_INT:
10229 /* Check that there are no leftover bits. */
10230 gcc_assert (GET_MODE_BITSIZE (mode)
10231 % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
10233 nunits = GET_MODE_BITSIZE (mode)
10234 / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
10235 break;
10237 default:
10238 gcc_unreachable ();
10241 return make_vector_type (innertype, nunits, mode);
10244 /* Similarly, but takes the inner type and number of units, which must be
10245 a power of two. */
10247 tree
10248 build_vector_type (tree innertype, int nunits)
10250 return make_vector_type (innertype, nunits, VOIDmode);
10253 /* Similarly, but builds a variant type with TYPE_VECTOR_OPAQUE set. */
10255 tree
10256 build_opaque_vector_type (tree innertype, int nunits)
10258 tree t = make_vector_type (innertype, nunits, VOIDmode);
10259 tree cand;
10260 /* We always build the non-opaque variant before the opaque one,
10261 so if it already exists, it is TYPE_NEXT_VARIANT of this one. */
10262 cand = TYPE_NEXT_VARIANT (t);
10263 if (cand
10264 && TYPE_VECTOR_OPAQUE (cand)
10265 && check_qualified_type (cand, t, TYPE_QUALS (t), NULL_TREE))
10266 return cand;
10267 /* Othewise build a variant type and make sure to queue it after
10268 the non-opaque type. */
10269 cand = build_distinct_type_copy (t);
10270 TYPE_VECTOR_OPAQUE (cand) = true;
10271 TYPE_CANONICAL (cand) = TYPE_CANONICAL (t);
10272 TYPE_NEXT_VARIANT (cand) = TYPE_NEXT_VARIANT (t);
10273 TYPE_NEXT_VARIANT (t) = cand;
10274 TYPE_MAIN_VARIANT (cand) = TYPE_MAIN_VARIANT (t);
10275 return cand;
10279 /* Given an initializer INIT, return TRUE if INIT is zero or some
10280 aggregate of zeros. Otherwise return FALSE. */
10281 bool
10282 initializer_zerop (const_tree init)
10284 tree elt;
10286 STRIP_NOPS (init);
10288 switch (TREE_CODE (init))
10290 case INTEGER_CST:
10291 return integer_zerop (init);
10293 case REAL_CST:
10294 /* ??? Note that this is not correct for C4X float formats. There,
10295 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
10296 negative exponent. */
10297 return real_zerop (init)
10298 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
10300 case FIXED_CST:
10301 return fixed_zerop (init);
10303 case COMPLEX_CST:
10304 return integer_zerop (init)
10305 || (real_zerop (init)
10306 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
10307 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
10309 case VECTOR_CST:
10311 unsigned i;
10312 for (i = 0; i < VECTOR_CST_NELTS (init); ++i)
10313 if (!initializer_zerop (VECTOR_CST_ELT (init, i)))
10314 return false;
10315 return true;
10318 case CONSTRUCTOR:
10320 unsigned HOST_WIDE_INT idx;
10322 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
10323 if (!initializer_zerop (elt))
10324 return false;
10325 return true;
10328 case STRING_CST:
10330 int i;
10332 /* We need to loop through all elements to handle cases like
10333 "\0" and "\0foobar". */
10334 for (i = 0; i < TREE_STRING_LENGTH (init); ++i)
10335 if (TREE_STRING_POINTER (init)[i] != '\0')
10336 return false;
10338 return true;
10341 default:
10342 return false;
10346 /* Check if vector VEC consists of all the equal elements and
10347 that the number of elements corresponds to the type of VEC.
10348 The function returns first element of the vector
10349 or NULL_TREE if the vector is not uniform. */
10350 tree
10351 uniform_vector_p (const_tree vec)
10353 tree first, t;
10354 unsigned i;
10356 if (vec == NULL_TREE)
10357 return NULL_TREE;
10359 gcc_assert (VECTOR_TYPE_P (TREE_TYPE (vec)));
10361 if (TREE_CODE (vec) == VECTOR_CST)
10363 first = VECTOR_CST_ELT (vec, 0);
10364 for (i = 1; i < VECTOR_CST_NELTS (vec); ++i)
10365 if (!operand_equal_p (first, VECTOR_CST_ELT (vec, i), 0))
10366 return NULL_TREE;
10368 return first;
10371 else if (TREE_CODE (vec) == CONSTRUCTOR)
10373 first = error_mark_node;
10375 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (vec), i, t)
10377 if (i == 0)
10379 first = t;
10380 continue;
10382 if (!operand_equal_p (first, t, 0))
10383 return NULL_TREE;
10385 if (i != TYPE_VECTOR_SUBPARTS (TREE_TYPE (vec)))
10386 return NULL_TREE;
10388 return first;
10391 return NULL_TREE;
10394 /* Build an empty statement at location LOC. */
10396 tree
10397 build_empty_stmt (location_t loc)
10399 tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
10400 SET_EXPR_LOCATION (t, loc);
10401 return t;
10405 /* Build an OpenMP clause with code CODE. LOC is the location of the
10406 clause. */
10408 tree
10409 build_omp_clause (location_t loc, enum omp_clause_code code)
10411 tree t;
10412 int size, length;
10414 length = omp_clause_num_ops[code];
10415 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
10417 record_node_allocation_statistics (OMP_CLAUSE, size);
10419 t = ggc_alloc_tree_node (size);
10420 memset (t, 0, size);
10421 TREE_SET_CODE (t, OMP_CLAUSE);
10422 OMP_CLAUSE_SET_CODE (t, code);
10423 OMP_CLAUSE_LOCATION (t) = loc;
10425 return t;
10428 /* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
10429 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
10430 Except for the CODE and operand count field, other storage for the
10431 object is initialized to zeros. */
10433 tree
10434 build_vl_exp_stat (enum tree_code code, int len MEM_STAT_DECL)
10436 tree t;
10437 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
10439 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
10440 gcc_assert (len >= 1);
10442 record_node_allocation_statistics (code, length);
10444 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
10446 TREE_SET_CODE (t, code);
10448 /* Can't use TREE_OPERAND to store the length because if checking is
10449 enabled, it will try to check the length before we store it. :-P */
10450 t->exp.operands[0] = build_int_cst (sizetype, len);
10452 return t;
10455 /* Helper function for build_call_* functions; build a CALL_EXPR with
10456 indicated RETURN_TYPE, FN, and NARGS, but do not initialize any of
10457 the argument slots. */
10459 static tree
10460 build_call_1 (tree return_type, tree fn, int nargs)
10462 tree t;
10464 t = build_vl_exp (CALL_EXPR, nargs + 3);
10465 TREE_TYPE (t) = return_type;
10466 CALL_EXPR_FN (t) = fn;
10467 CALL_EXPR_STATIC_CHAIN (t) = NULL;
10469 return t;
10472 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10473 FN and a null static chain slot. NARGS is the number of call arguments
10474 which are specified as "..." arguments. */
10476 tree
10477 build_call_nary (tree return_type, tree fn, int nargs, ...)
10479 tree ret;
10480 va_list args;
10481 va_start (args, nargs);
10482 ret = build_call_valist (return_type, fn, nargs, args);
10483 va_end (args);
10484 return ret;
10487 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10488 FN and a null static chain slot. NARGS is the number of call arguments
10489 which are specified as a va_list ARGS. */
10491 tree
10492 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
10494 tree t;
10495 int i;
10497 t = build_call_1 (return_type, fn, nargs);
10498 for (i = 0; i < nargs; i++)
10499 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
10500 process_call_operands (t);
10501 return t;
10504 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10505 FN and a null static chain slot. NARGS is the number of call arguments
10506 which are specified as a tree array ARGS. */
10508 tree
10509 build_call_array_loc (location_t loc, tree return_type, tree fn,
10510 int nargs, const tree *args)
10512 tree t;
10513 int i;
10515 t = build_call_1 (return_type, fn, nargs);
10516 for (i = 0; i < nargs; i++)
10517 CALL_EXPR_ARG (t, i) = args[i];
10518 process_call_operands (t);
10519 SET_EXPR_LOCATION (t, loc);
10520 return t;
10523 /* Like build_call_array, but takes a vec. */
10525 tree
10526 build_call_vec (tree return_type, tree fn, vec<tree, va_gc> *args)
10528 tree ret, t;
10529 unsigned int ix;
10531 ret = build_call_1 (return_type, fn, vec_safe_length (args));
10532 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
10533 CALL_EXPR_ARG (ret, ix) = t;
10534 process_call_operands (ret);
10535 return ret;
10538 /* Return true if T (assumed to be a DECL) must be assigned a memory
10539 location. */
10541 bool
10542 needs_to_live_in_memory (const_tree t)
10544 return (TREE_ADDRESSABLE (t)
10545 || is_global_var (t)
10546 || (TREE_CODE (t) == RESULT_DECL
10547 && !DECL_BY_REFERENCE (t)
10548 && aggregate_value_p (t, current_function_decl)));
10551 /* Return value of a constant X and sign-extend it. */
10553 HOST_WIDE_INT
10554 int_cst_value (const_tree x)
10556 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10557 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
10559 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10560 gcc_assert (TREE_INT_CST_HIGH (x) == 0
10561 || TREE_INT_CST_HIGH (x) == -1);
10563 if (bits < HOST_BITS_PER_WIDE_INT)
10565 bool negative = ((val >> (bits - 1)) & 1) != 0;
10566 if (negative)
10567 val |= (~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1;
10568 else
10569 val &= ~((~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1);
10572 return val;
10575 /* Return value of a constant X and sign-extend it. */
10577 HOST_WIDEST_INT
10578 widest_int_cst_value (const_tree x)
10580 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10581 unsigned HOST_WIDEST_INT val = TREE_INT_CST_LOW (x);
10583 #if HOST_BITS_PER_WIDEST_INT > HOST_BITS_PER_WIDE_INT
10584 gcc_assert (HOST_BITS_PER_WIDEST_INT >= HOST_BITS_PER_DOUBLE_INT);
10585 val |= (((unsigned HOST_WIDEST_INT) TREE_INT_CST_HIGH (x))
10586 << HOST_BITS_PER_WIDE_INT);
10587 #else
10588 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10589 gcc_assert (TREE_INT_CST_HIGH (x) == 0
10590 || TREE_INT_CST_HIGH (x) == -1);
10591 #endif
10593 if (bits < HOST_BITS_PER_WIDEST_INT)
10595 bool negative = ((val >> (bits - 1)) & 1) != 0;
10596 if (negative)
10597 val |= (~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1;
10598 else
10599 val &= ~((~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1);
10602 return val;
10605 /* If TYPE is an integral or pointer type, return an integer type with
10606 the same precision which is unsigned iff UNSIGNEDP is true, or itself
10607 if TYPE is already an integer type of signedness UNSIGNEDP. */
10609 tree
10610 signed_or_unsigned_type_for (int unsignedp, tree type)
10612 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type) == unsignedp)
10613 return type;
10615 if (TREE_CODE (type) == VECTOR_TYPE)
10617 tree inner = TREE_TYPE (type);
10618 tree inner2 = signed_or_unsigned_type_for (unsignedp, inner);
10619 if (!inner2)
10620 return NULL_TREE;
10621 if (inner == inner2)
10622 return type;
10623 return build_vector_type (inner2, TYPE_VECTOR_SUBPARTS (type));
10626 if (!INTEGRAL_TYPE_P (type)
10627 && !POINTER_TYPE_P (type))
10628 return NULL_TREE;
10630 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
10633 /* If TYPE is an integral or pointer type, return an integer type with
10634 the same precision which is unsigned, or itself if TYPE is already an
10635 unsigned integer type. */
10637 tree
10638 unsigned_type_for (tree type)
10640 return signed_or_unsigned_type_for (1, type);
10643 /* If TYPE is an integral or pointer type, return an integer type with
10644 the same precision which is signed, or itself if TYPE is already a
10645 signed integer type. */
10647 tree
10648 signed_type_for (tree type)
10650 return signed_or_unsigned_type_for (0, type);
10653 /* If TYPE is a vector type, return a signed integer vector type with the
10654 same width and number of subparts. Otherwise return boolean_type_node. */
10656 tree
10657 truth_type_for (tree type)
10659 if (TREE_CODE (type) == VECTOR_TYPE)
10661 tree elem = lang_hooks.types.type_for_size
10662 (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))), 0);
10663 return build_opaque_vector_type (elem, TYPE_VECTOR_SUBPARTS (type));
10665 else
10666 return boolean_type_node;
10669 /* Returns the largest value obtainable by casting something in INNER type to
10670 OUTER type. */
10672 tree
10673 upper_bound_in_type (tree outer, tree inner)
10675 double_int high;
10676 unsigned int det = 0;
10677 unsigned oprec = TYPE_PRECISION (outer);
10678 unsigned iprec = TYPE_PRECISION (inner);
10679 unsigned prec;
10681 /* Compute a unique number for every combination. */
10682 det |= (oprec > iprec) ? 4 : 0;
10683 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
10684 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
10686 /* Determine the exponent to use. */
10687 switch (det)
10689 case 0:
10690 case 1:
10691 /* oprec <= iprec, outer: signed, inner: don't care. */
10692 prec = oprec - 1;
10693 break;
10694 case 2:
10695 case 3:
10696 /* oprec <= iprec, outer: unsigned, inner: don't care. */
10697 prec = oprec;
10698 break;
10699 case 4:
10700 /* oprec > iprec, outer: signed, inner: signed. */
10701 prec = iprec - 1;
10702 break;
10703 case 5:
10704 /* oprec > iprec, outer: signed, inner: unsigned. */
10705 prec = iprec;
10706 break;
10707 case 6:
10708 /* oprec > iprec, outer: unsigned, inner: signed. */
10709 prec = oprec;
10710 break;
10711 case 7:
10712 /* oprec > iprec, outer: unsigned, inner: unsigned. */
10713 prec = iprec;
10714 break;
10715 default:
10716 gcc_unreachable ();
10719 /* Compute 2^^prec - 1. */
10720 if (prec <= HOST_BITS_PER_WIDE_INT)
10722 high.high = 0;
10723 high.low = ((~(unsigned HOST_WIDE_INT) 0)
10724 >> (HOST_BITS_PER_WIDE_INT - prec));
10726 else
10728 high.high = ((~(unsigned HOST_WIDE_INT) 0)
10729 >> (HOST_BITS_PER_DOUBLE_INT - prec));
10730 high.low = ~(unsigned HOST_WIDE_INT) 0;
10733 return double_int_to_tree (outer, high);
10736 /* Returns the smallest value obtainable by casting something in INNER type to
10737 OUTER type. */
10739 tree
10740 lower_bound_in_type (tree outer, tree inner)
10742 double_int low;
10743 unsigned oprec = TYPE_PRECISION (outer);
10744 unsigned iprec = TYPE_PRECISION (inner);
10746 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
10747 and obtain 0. */
10748 if (TYPE_UNSIGNED (outer)
10749 /* If we are widening something of an unsigned type, OUTER type
10750 contains all values of INNER type. In particular, both INNER
10751 and OUTER types have zero in common. */
10752 || (oprec > iprec && TYPE_UNSIGNED (inner)))
10753 low.low = low.high = 0;
10754 else
10756 /* If we are widening a signed type to another signed type, we
10757 want to obtain -2^^(iprec-1). If we are keeping the
10758 precision or narrowing to a signed type, we want to obtain
10759 -2^(oprec-1). */
10760 unsigned prec = oprec > iprec ? iprec : oprec;
10762 if (prec <= HOST_BITS_PER_WIDE_INT)
10764 low.high = ~(unsigned HOST_WIDE_INT) 0;
10765 low.low = (~(unsigned HOST_WIDE_INT) 0) << (prec - 1);
10767 else
10769 low.high = ((~(unsigned HOST_WIDE_INT) 0)
10770 << (prec - HOST_BITS_PER_WIDE_INT - 1));
10771 low.low = 0;
10775 return double_int_to_tree (outer, low);
10778 /* Return nonzero if two operands that are suitable for PHI nodes are
10779 necessarily equal. Specifically, both ARG0 and ARG1 must be either
10780 SSA_NAME or invariant. Note that this is strictly an optimization.
10781 That is, callers of this function can directly call operand_equal_p
10782 and get the same result, only slower. */
10785 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
10787 if (arg0 == arg1)
10788 return 1;
10789 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
10790 return 0;
10791 return operand_equal_p (arg0, arg1, 0);
10794 /* Returns number of zeros at the end of binary representation of X.
10796 ??? Use ffs if available? */
10798 tree
10799 num_ending_zeros (const_tree x)
10801 unsigned HOST_WIDE_INT fr, nfr;
10802 unsigned num, abits;
10803 tree type = TREE_TYPE (x);
10805 if (TREE_INT_CST_LOW (x) == 0)
10807 num = HOST_BITS_PER_WIDE_INT;
10808 fr = TREE_INT_CST_HIGH (x);
10810 else
10812 num = 0;
10813 fr = TREE_INT_CST_LOW (x);
10816 for (abits = HOST_BITS_PER_WIDE_INT / 2; abits; abits /= 2)
10818 nfr = fr >> abits;
10819 if (nfr << abits == fr)
10821 num += abits;
10822 fr = nfr;
10826 if (num > TYPE_PRECISION (type))
10827 num = TYPE_PRECISION (type);
10829 return build_int_cst_type (type, num);
10833 #define WALK_SUBTREE(NODE) \
10834 do \
10836 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
10837 if (result) \
10838 return result; \
10840 while (0)
10842 /* This is a subroutine of walk_tree that walks field of TYPE that are to
10843 be walked whenever a type is seen in the tree. Rest of operands and return
10844 value are as for walk_tree. */
10846 static tree
10847 walk_type_fields (tree type, walk_tree_fn func, void *data,
10848 struct pointer_set_t *pset, walk_tree_lh lh)
10850 tree result = NULL_TREE;
10852 switch (TREE_CODE (type))
10854 case POINTER_TYPE:
10855 case REFERENCE_TYPE:
10856 /* We have to worry about mutually recursive pointers. These can't
10857 be written in C. They can in Ada. It's pathological, but
10858 there's an ACATS test (c38102a) that checks it. Deal with this
10859 by checking if we're pointing to another pointer, that one
10860 points to another pointer, that one does too, and we have no htab.
10861 If so, get a hash table. We check three levels deep to avoid
10862 the cost of the hash table if we don't need one. */
10863 if (POINTER_TYPE_P (TREE_TYPE (type))
10864 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
10865 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
10866 && !pset)
10868 result = walk_tree_without_duplicates (&TREE_TYPE (type),
10869 func, data);
10870 if (result)
10871 return result;
10873 break;
10876 /* ... fall through ... */
10878 case COMPLEX_TYPE:
10879 WALK_SUBTREE (TREE_TYPE (type));
10880 break;
10882 case METHOD_TYPE:
10883 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
10885 /* Fall through. */
10887 case FUNCTION_TYPE:
10888 WALK_SUBTREE (TREE_TYPE (type));
10890 tree arg;
10892 /* We never want to walk into default arguments. */
10893 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
10894 WALK_SUBTREE (TREE_VALUE (arg));
10896 break;
10898 case ARRAY_TYPE:
10899 /* Don't follow this nodes's type if a pointer for fear that
10900 we'll have infinite recursion. If we have a PSET, then we
10901 need not fear. */
10902 if (pset
10903 || (!POINTER_TYPE_P (TREE_TYPE (type))
10904 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
10905 WALK_SUBTREE (TREE_TYPE (type));
10906 WALK_SUBTREE (TYPE_DOMAIN (type));
10907 break;
10909 case OFFSET_TYPE:
10910 WALK_SUBTREE (TREE_TYPE (type));
10911 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
10912 break;
10914 default:
10915 break;
10918 return NULL_TREE;
10921 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
10922 called with the DATA and the address of each sub-tree. If FUNC returns a
10923 non-NULL value, the traversal is stopped, and the value returned by FUNC
10924 is returned. If PSET is non-NULL it is used to record the nodes visited,
10925 and to avoid visiting a node more than once. */
10927 tree
10928 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
10929 struct pointer_set_t *pset, walk_tree_lh lh)
10931 enum tree_code code;
10932 int walk_subtrees;
10933 tree result;
10935 #define WALK_SUBTREE_TAIL(NODE) \
10936 do \
10938 tp = & (NODE); \
10939 goto tail_recurse; \
10941 while (0)
10943 tail_recurse:
10944 /* Skip empty subtrees. */
10945 if (!*tp)
10946 return NULL_TREE;
10948 /* Don't walk the same tree twice, if the user has requested
10949 that we avoid doing so. */
10950 if (pset && pointer_set_insert (pset, *tp))
10951 return NULL_TREE;
10953 /* Call the function. */
10954 walk_subtrees = 1;
10955 result = (*func) (tp, &walk_subtrees, data);
10957 /* If we found something, return it. */
10958 if (result)
10959 return result;
10961 code = TREE_CODE (*tp);
10963 /* Even if we didn't, FUNC may have decided that there was nothing
10964 interesting below this point in the tree. */
10965 if (!walk_subtrees)
10967 /* But we still need to check our siblings. */
10968 if (code == TREE_LIST)
10969 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
10970 else if (code == OMP_CLAUSE)
10971 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10972 else
10973 return NULL_TREE;
10976 if (lh)
10978 result = (*lh) (tp, &walk_subtrees, func, data, pset);
10979 if (result || !walk_subtrees)
10980 return result;
10983 switch (code)
10985 case ERROR_MARK:
10986 case IDENTIFIER_NODE:
10987 case INTEGER_CST:
10988 case REAL_CST:
10989 case FIXED_CST:
10990 case VECTOR_CST:
10991 case STRING_CST:
10992 case BLOCK:
10993 case PLACEHOLDER_EXPR:
10994 case SSA_NAME:
10995 case FIELD_DECL:
10996 case RESULT_DECL:
10997 /* None of these have subtrees other than those already walked
10998 above. */
10999 break;
11001 case TREE_LIST:
11002 WALK_SUBTREE (TREE_VALUE (*tp));
11003 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
11004 break;
11006 case TREE_VEC:
11008 int len = TREE_VEC_LENGTH (*tp);
11010 if (len == 0)
11011 break;
11013 /* Walk all elements but the first. */
11014 while (--len)
11015 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
11017 /* Now walk the first one as a tail call. */
11018 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
11021 case COMPLEX_CST:
11022 WALK_SUBTREE (TREE_REALPART (*tp));
11023 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
11025 case CONSTRUCTOR:
11027 unsigned HOST_WIDE_INT idx;
11028 constructor_elt *ce;
11030 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (*tp), idx, &ce);
11031 idx++)
11032 WALK_SUBTREE (ce->value);
11034 break;
11036 case SAVE_EXPR:
11037 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
11039 case BIND_EXPR:
11041 tree decl;
11042 for (decl = BIND_EXPR_VARS (*tp); decl; decl = DECL_CHAIN (decl))
11044 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
11045 into declarations that are just mentioned, rather than
11046 declared; they don't really belong to this part of the tree.
11047 And, we can see cycles: the initializer for a declaration
11048 can refer to the declaration itself. */
11049 WALK_SUBTREE (DECL_INITIAL (decl));
11050 WALK_SUBTREE (DECL_SIZE (decl));
11051 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
11053 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
11056 case STATEMENT_LIST:
11058 tree_stmt_iterator i;
11059 for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
11060 WALK_SUBTREE (*tsi_stmt_ptr (i));
11062 break;
11064 case OMP_CLAUSE:
11065 switch (OMP_CLAUSE_CODE (*tp))
11067 case OMP_CLAUSE_PRIVATE:
11068 case OMP_CLAUSE_SHARED:
11069 case OMP_CLAUSE_FIRSTPRIVATE:
11070 case OMP_CLAUSE_COPYIN:
11071 case OMP_CLAUSE_COPYPRIVATE:
11072 case OMP_CLAUSE_FINAL:
11073 case OMP_CLAUSE_IF:
11074 case OMP_CLAUSE_NUM_THREADS:
11075 case OMP_CLAUSE_SCHEDULE:
11076 case OMP_CLAUSE_UNIFORM:
11077 case OMP_CLAUSE_DEPEND:
11078 case OMP_CLAUSE_NUM_TEAMS:
11079 case OMP_CLAUSE_THREAD_LIMIT:
11080 case OMP_CLAUSE_DEVICE:
11081 case OMP_CLAUSE_DIST_SCHEDULE:
11082 case OMP_CLAUSE_SAFELEN:
11083 case OMP_CLAUSE_SIMDLEN:
11084 case OMP_CLAUSE__LOOPTEMP_:
11085 case OMP_CLAUSE__SIMDUID_:
11086 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0));
11087 /* FALLTHRU */
11089 case OMP_CLAUSE_NOWAIT:
11090 case OMP_CLAUSE_ORDERED:
11091 case OMP_CLAUSE_DEFAULT:
11092 case OMP_CLAUSE_UNTIED:
11093 case OMP_CLAUSE_MERGEABLE:
11094 case OMP_CLAUSE_PROC_BIND:
11095 case OMP_CLAUSE_INBRANCH:
11096 case OMP_CLAUSE_NOTINBRANCH:
11097 case OMP_CLAUSE_FOR:
11098 case OMP_CLAUSE_PARALLEL:
11099 case OMP_CLAUSE_SECTIONS:
11100 case OMP_CLAUSE_TASKGROUP:
11101 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11103 case OMP_CLAUSE_LASTPRIVATE:
11104 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11105 WALK_SUBTREE (OMP_CLAUSE_LASTPRIVATE_STMT (*tp));
11106 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11108 case OMP_CLAUSE_COLLAPSE:
11110 int i;
11111 for (i = 0; i < 3; i++)
11112 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
11113 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11116 case OMP_CLAUSE_ALIGNED:
11117 case OMP_CLAUSE_LINEAR:
11118 case OMP_CLAUSE_FROM:
11119 case OMP_CLAUSE_TO:
11120 case OMP_CLAUSE_MAP:
11121 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11122 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 1));
11123 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11125 case OMP_CLAUSE_REDUCTION:
11127 int i;
11128 for (i = 0; i < 4; i++)
11129 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
11130 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11133 default:
11134 gcc_unreachable ();
11136 break;
11138 case TARGET_EXPR:
11140 int i, len;
11142 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
11143 But, we only want to walk once. */
11144 len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
11145 for (i = 0; i < len; ++i)
11146 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11147 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
11150 case DECL_EXPR:
11151 /* If this is a TYPE_DECL, walk into the fields of the type that it's
11152 defining. We only want to walk into these fields of a type in this
11153 case and not in the general case of a mere reference to the type.
11155 The criterion is as follows: if the field can be an expression, it
11156 must be walked only here. This should be in keeping with the fields
11157 that are directly gimplified in gimplify_type_sizes in order for the
11158 mark/copy-if-shared/unmark machinery of the gimplifier to work with
11159 variable-sized types.
11161 Note that DECLs get walked as part of processing the BIND_EXPR. */
11162 if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
11164 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
11165 if (TREE_CODE (*type_p) == ERROR_MARK)
11166 return NULL_TREE;
11168 /* Call the function for the type. See if it returns anything or
11169 doesn't want us to continue. If we are to continue, walk both
11170 the normal fields and those for the declaration case. */
11171 result = (*func) (type_p, &walk_subtrees, data);
11172 if (result || !walk_subtrees)
11173 return result;
11175 /* But do not walk a pointed-to type since it may itself need to
11176 be walked in the declaration case if it isn't anonymous. */
11177 if (!POINTER_TYPE_P (*type_p))
11179 result = walk_type_fields (*type_p, func, data, pset, lh);
11180 if (result)
11181 return result;
11184 /* If this is a record type, also walk the fields. */
11185 if (RECORD_OR_UNION_TYPE_P (*type_p))
11187 tree field;
11189 for (field = TYPE_FIELDS (*type_p); field;
11190 field = DECL_CHAIN (field))
11192 /* We'd like to look at the type of the field, but we can
11193 easily get infinite recursion. So assume it's pointed
11194 to elsewhere in the tree. Also, ignore things that
11195 aren't fields. */
11196 if (TREE_CODE (field) != FIELD_DECL)
11197 continue;
11199 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
11200 WALK_SUBTREE (DECL_SIZE (field));
11201 WALK_SUBTREE (DECL_SIZE_UNIT (field));
11202 if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
11203 WALK_SUBTREE (DECL_QUALIFIER (field));
11207 /* Same for scalar types. */
11208 else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
11209 || TREE_CODE (*type_p) == ENUMERAL_TYPE
11210 || TREE_CODE (*type_p) == INTEGER_TYPE
11211 || TREE_CODE (*type_p) == FIXED_POINT_TYPE
11212 || TREE_CODE (*type_p) == REAL_TYPE)
11214 WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
11215 WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
11218 WALK_SUBTREE (TYPE_SIZE (*type_p));
11219 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
11221 /* FALLTHRU */
11223 default:
11224 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
11226 int i, len;
11228 /* Walk over all the sub-trees of this operand. */
11229 len = TREE_OPERAND_LENGTH (*tp);
11231 /* Go through the subtrees. We need to do this in forward order so
11232 that the scope of a FOR_EXPR is handled properly. */
11233 if (len)
11235 for (i = 0; i < len - 1; ++i)
11236 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11237 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
11240 /* If this is a type, walk the needed fields in the type. */
11241 else if (TYPE_P (*tp))
11242 return walk_type_fields (*tp, func, data, pset, lh);
11243 break;
11246 /* We didn't find what we were looking for. */
11247 return NULL_TREE;
11249 #undef WALK_SUBTREE_TAIL
11251 #undef WALK_SUBTREE
11253 /* Like walk_tree, but does not walk duplicate nodes more than once. */
11255 tree
11256 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
11257 walk_tree_lh lh)
11259 tree result;
11260 struct pointer_set_t *pset;
11262 pset = pointer_set_create ();
11263 result = walk_tree_1 (tp, func, data, pset, lh);
11264 pointer_set_destroy (pset);
11265 return result;
11269 tree
11270 tree_block (tree t)
11272 char const c = TREE_CODE_CLASS (TREE_CODE (t));
11274 if (IS_EXPR_CODE_CLASS (c))
11275 return LOCATION_BLOCK (t->exp.locus);
11276 gcc_unreachable ();
11277 return NULL;
11280 void
11281 tree_set_block (tree t, tree b)
11283 char const c = TREE_CODE_CLASS (TREE_CODE (t));
11285 if (IS_EXPR_CODE_CLASS (c))
11287 if (b)
11288 t->exp.locus = COMBINE_LOCATION_DATA (line_table, t->exp.locus, b);
11289 else
11290 t->exp.locus = LOCATION_LOCUS (t->exp.locus);
11292 else
11293 gcc_unreachable ();
11296 /* Create a nameless artificial label and put it in the current
11297 function context. The label has a location of LOC. Returns the
11298 newly created label. */
11300 tree
11301 create_artificial_label (location_t loc)
11303 tree lab = build_decl (loc,
11304 LABEL_DECL, NULL_TREE, void_type_node);
11306 DECL_ARTIFICIAL (lab) = 1;
11307 DECL_IGNORED_P (lab) = 1;
11308 DECL_CONTEXT (lab) = current_function_decl;
11309 return lab;
11312 /* Given a tree, try to return a useful variable name that we can use
11313 to prefix a temporary that is being assigned the value of the tree.
11314 I.E. given <temp> = &A, return A. */
11316 const char *
11317 get_name (tree t)
11319 tree stripped_decl;
11321 stripped_decl = t;
11322 STRIP_NOPS (stripped_decl);
11323 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
11324 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
11325 else if (TREE_CODE (stripped_decl) == SSA_NAME)
11327 tree name = SSA_NAME_IDENTIFIER (stripped_decl);
11328 if (!name)
11329 return NULL;
11330 return IDENTIFIER_POINTER (name);
11332 else
11334 switch (TREE_CODE (stripped_decl))
11336 case ADDR_EXPR:
11337 return get_name (TREE_OPERAND (stripped_decl, 0));
11338 default:
11339 return NULL;
11344 /* Return true if TYPE has a variable argument list. */
11346 bool
11347 stdarg_p (const_tree fntype)
11349 function_args_iterator args_iter;
11350 tree n = NULL_TREE, t;
11352 if (!fntype)
11353 return false;
11355 FOREACH_FUNCTION_ARGS (fntype, t, args_iter)
11357 n = t;
11360 return n != NULL_TREE && n != void_type_node;
11363 /* Return true if TYPE has a prototype. */
11365 bool
11366 prototype_p (tree fntype)
11368 tree t;
11370 gcc_assert (fntype != NULL_TREE);
11372 t = TYPE_ARG_TYPES (fntype);
11373 return (t != NULL_TREE);
11376 /* If BLOCK is inlined from an __attribute__((__artificial__))
11377 routine, return pointer to location from where it has been
11378 called. */
11379 location_t *
11380 block_nonartificial_location (tree block)
11382 location_t *ret = NULL;
11384 while (block && TREE_CODE (block) == BLOCK
11385 && BLOCK_ABSTRACT_ORIGIN (block))
11387 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
11389 while (TREE_CODE (ao) == BLOCK
11390 && BLOCK_ABSTRACT_ORIGIN (ao)
11391 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
11392 ao = BLOCK_ABSTRACT_ORIGIN (ao);
11394 if (TREE_CODE (ao) == FUNCTION_DECL)
11396 /* If AO is an artificial inline, point RET to the
11397 call site locus at which it has been inlined and continue
11398 the loop, in case AO's caller is also an artificial
11399 inline. */
11400 if (DECL_DECLARED_INLINE_P (ao)
11401 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
11402 ret = &BLOCK_SOURCE_LOCATION (block);
11403 else
11404 break;
11406 else if (TREE_CODE (ao) != BLOCK)
11407 break;
11409 block = BLOCK_SUPERCONTEXT (block);
11411 return ret;
11415 /* If EXP is inlined from an __attribute__((__artificial__))
11416 function, return the location of the original call expression. */
11418 location_t
11419 tree_nonartificial_location (tree exp)
11421 location_t *loc = block_nonartificial_location (TREE_BLOCK (exp));
11423 if (loc)
11424 return *loc;
11425 else
11426 return EXPR_LOCATION (exp);
11430 /* These are the hash table functions for the hash table of OPTIMIZATION_NODEq
11431 nodes. */
11433 /* Return the hash code code X, an OPTIMIZATION_NODE or TARGET_OPTION code. */
11435 static hashval_t
11436 cl_option_hash_hash (const void *x)
11438 const_tree const t = (const_tree) x;
11439 const char *p;
11440 size_t i;
11441 size_t len = 0;
11442 hashval_t hash = 0;
11444 if (TREE_CODE (t) == OPTIMIZATION_NODE)
11446 p = (const char *)TREE_OPTIMIZATION (t);
11447 len = sizeof (struct cl_optimization);
11450 else if (TREE_CODE (t) == TARGET_OPTION_NODE)
11452 p = (const char *)TREE_TARGET_OPTION (t);
11453 len = sizeof (struct cl_target_option);
11456 else
11457 gcc_unreachable ();
11459 /* assume most opt flags are just 0/1, some are 2-3, and a few might be
11460 something else. */
11461 for (i = 0; i < len; i++)
11462 if (p[i])
11463 hash = (hash << 4) ^ ((i << 2) | p[i]);
11465 return hash;
11468 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
11469 TARGET_OPTION tree node) is the same as that given by *Y, which is the
11470 same. */
11472 static int
11473 cl_option_hash_eq (const void *x, const void *y)
11475 const_tree const xt = (const_tree) x;
11476 const_tree const yt = (const_tree) y;
11477 const char *xp;
11478 const char *yp;
11479 size_t len;
11481 if (TREE_CODE (xt) != TREE_CODE (yt))
11482 return 0;
11484 if (TREE_CODE (xt) == OPTIMIZATION_NODE)
11486 xp = (const char *)TREE_OPTIMIZATION (xt);
11487 yp = (const char *)TREE_OPTIMIZATION (yt);
11488 len = sizeof (struct cl_optimization);
11491 else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
11493 xp = (const char *)TREE_TARGET_OPTION (xt);
11494 yp = (const char *)TREE_TARGET_OPTION (yt);
11495 len = sizeof (struct cl_target_option);
11498 else
11499 gcc_unreachable ();
11501 return (memcmp (xp, yp, len) == 0);
11504 /* Build an OPTIMIZATION_NODE based on the options in OPTS. */
11506 tree
11507 build_optimization_node (struct gcc_options *opts)
11509 tree t;
11510 void **slot;
11512 /* Use the cache of optimization nodes. */
11514 cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node),
11515 opts);
11517 slot = htab_find_slot (cl_option_hash_table, cl_optimization_node, INSERT);
11518 t = (tree) *slot;
11519 if (!t)
11521 /* Insert this one into the hash table. */
11522 t = cl_optimization_node;
11523 *slot = t;
11525 /* Make a new node for next time round. */
11526 cl_optimization_node = make_node (OPTIMIZATION_NODE);
11529 return t;
11532 /* Build a TARGET_OPTION_NODE based on the options in OPTS. */
11534 tree
11535 build_target_option_node (struct gcc_options *opts)
11537 tree t;
11538 void **slot;
11540 /* Use the cache of optimization nodes. */
11542 cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node),
11543 opts);
11545 slot = htab_find_slot (cl_option_hash_table, cl_target_option_node, INSERT);
11546 t = (tree) *slot;
11547 if (!t)
11549 /* Insert this one into the hash table. */
11550 t = cl_target_option_node;
11551 *slot = t;
11553 /* Make a new node for next time round. */
11554 cl_target_option_node = make_node (TARGET_OPTION_NODE);
11557 return t;
11560 /* Determine the "ultimate origin" of a block. The block may be an inlined
11561 instance of an inlined instance of a block which is local to an inline
11562 function, so we have to trace all of the way back through the origin chain
11563 to find out what sort of node actually served as the original seed for the
11564 given block. */
11566 tree
11567 block_ultimate_origin (const_tree block)
11569 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
11571 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
11572 nodes in the function to point to themselves; ignore that if
11573 we're trying to output the abstract instance of this function. */
11574 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
11575 return NULL_TREE;
11577 if (immediate_origin == NULL_TREE)
11578 return NULL_TREE;
11579 else
11581 tree ret_val;
11582 tree lookahead = immediate_origin;
11586 ret_val = lookahead;
11587 lookahead = (TREE_CODE (ret_val) == BLOCK
11588 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
11590 while (lookahead != NULL && lookahead != ret_val);
11592 /* The block's abstract origin chain may not be the *ultimate* origin of
11593 the block. It could lead to a DECL that has an abstract origin set.
11594 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
11595 will give us if it has one). Note that DECL's abstract origins are
11596 supposed to be the most distant ancestor (or so decl_ultimate_origin
11597 claims), so we don't need to loop following the DECL origins. */
11598 if (DECL_P (ret_val))
11599 return DECL_ORIGIN (ret_val);
11601 return ret_val;
11605 /* Return true iff conversion in EXP generates no instruction. Mark
11606 it inline so that we fully inline into the stripping functions even
11607 though we have two uses of this function. */
11609 static inline bool
11610 tree_nop_conversion (const_tree exp)
11612 tree outer_type, inner_type;
11613 int outer_is_pts_p, inner_is_pts_p;
11615 if (!CONVERT_EXPR_P (exp)
11616 && TREE_CODE (exp) != NON_LVALUE_EXPR)
11617 return false;
11618 if (TREE_OPERAND (exp, 0) == error_mark_node)
11619 return false;
11621 outer_type = TREE_TYPE (exp);
11622 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11624 if (!inner_type)
11625 return false;
11627 outer_is_pts_p = (POINTER_TYPE_P (outer_type)
11628 && upc_shared_type_p (TREE_TYPE (outer_type)));
11629 inner_is_pts_p = (POINTER_TYPE_P (inner_type)
11630 && upc_shared_type_p (TREE_TYPE (inner_type)));
11632 /* UPC pointer-to-shared types have special
11633 equivalence rules that must be checked. */
11634 if (outer_is_pts_p && inner_is_pts_p
11635 && lang_hooks.types_compatible_p)
11636 return lang_hooks.types_compatible_p (outer_type, inner_type);
11638 /* UPC pointer-to-shared types are not interchangeable
11639 with integral types. */
11640 if (outer_is_pts_p || inner_is_pts_p)
11641 return false;
11643 /* Use precision rather then machine mode when we can, which gives
11644 the correct answer even for submode (bit-field) types. */
11645 if ((INTEGRAL_TYPE_P (outer_type)
11646 || POINTER_TYPE_P (outer_type)
11647 || TREE_CODE (outer_type) == OFFSET_TYPE)
11648 && (INTEGRAL_TYPE_P (inner_type)
11649 || POINTER_TYPE_P (inner_type)
11650 || TREE_CODE (inner_type) == OFFSET_TYPE))
11651 return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
11653 /* Otherwise fall back on comparing machine modes (e.g. for
11654 aggregate types, floats). */
11655 return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
11658 /* Return true iff conversion in EXP generates no instruction. Don't
11659 consider conversions changing the signedness. */
11661 static bool
11662 tree_sign_nop_conversion (const_tree exp)
11664 tree outer_type, inner_type;
11666 if (!tree_nop_conversion (exp))
11667 return false;
11669 outer_type = TREE_TYPE (exp);
11670 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11672 return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
11673 && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
11676 /* Strip conversions from EXP according to tree_nop_conversion and
11677 return the resulting expression. */
11679 tree
11680 tree_strip_nop_conversions (tree exp)
11682 while (tree_nop_conversion (exp))
11683 exp = TREE_OPERAND (exp, 0);
11684 return exp;
11687 /* Strip conversions from EXP according to tree_sign_nop_conversion
11688 and return the resulting expression. */
11690 tree
11691 tree_strip_sign_nop_conversions (tree exp)
11693 while (tree_sign_nop_conversion (exp))
11694 exp = TREE_OPERAND (exp, 0);
11695 return exp;
11698 /* Avoid any floating point extensions from EXP. */
11699 tree
11700 strip_float_extensions (tree exp)
11702 tree sub, expt, subt;
11704 /* For floating point constant look up the narrowest type that can hold
11705 it properly and handle it like (type)(narrowest_type)constant.
11706 This way we can optimize for instance a=a*2.0 where "a" is float
11707 but 2.0 is double constant. */
11708 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
11710 REAL_VALUE_TYPE orig;
11711 tree type = NULL;
11713 orig = TREE_REAL_CST (exp);
11714 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
11715 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
11716 type = float_type_node;
11717 else if (TYPE_PRECISION (TREE_TYPE (exp))
11718 > TYPE_PRECISION (double_type_node)
11719 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
11720 type = double_type_node;
11721 if (type)
11722 return build_real (type, real_value_truncate (TYPE_MODE (type), orig));
11725 if (!CONVERT_EXPR_P (exp))
11726 return exp;
11728 sub = TREE_OPERAND (exp, 0);
11729 subt = TREE_TYPE (sub);
11730 expt = TREE_TYPE (exp);
11732 if (!FLOAT_TYPE_P (subt))
11733 return exp;
11735 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
11736 return exp;
11738 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
11739 return exp;
11741 return strip_float_extensions (sub);
11744 /* Strip out all handled components that produce invariant
11745 offsets. */
11747 const_tree
11748 strip_invariant_refs (const_tree op)
11750 while (handled_component_p (op))
11752 switch (TREE_CODE (op))
11754 case ARRAY_REF:
11755 case ARRAY_RANGE_REF:
11756 if (!is_gimple_constant (TREE_OPERAND (op, 1))
11757 || TREE_OPERAND (op, 2) != NULL_TREE
11758 || TREE_OPERAND (op, 3) != NULL_TREE)
11759 return NULL;
11760 break;
11762 case COMPONENT_REF:
11763 if (TREE_OPERAND (op, 2) != NULL_TREE)
11764 return NULL;
11765 break;
11767 default:;
11769 op = TREE_OPERAND (op, 0);
11772 return op;
11775 static GTY(()) tree gcc_eh_personality_decl;
11777 /* Return the GCC personality function decl. */
11779 tree
11780 lhd_gcc_personality (void)
11782 if (!gcc_eh_personality_decl)
11783 gcc_eh_personality_decl = build_personality_function ("gcc");
11784 return gcc_eh_personality_decl;
11787 /* For languages with One Definition Rule, work out if
11788 trees are actually the same even if the tree representation
11789 differs. This handles only decls appearing in TYPE_NAME
11790 and TYPE_CONTEXT. That is NAMESPACE_DECL, TYPE_DECL,
11791 RECORD_TYPE and IDENTIFIER_NODE. */
11793 static bool
11794 same_for_odr (tree t1, tree t2)
11796 if (t1 == t2)
11797 return true;
11798 if (!t1 || !t2)
11799 return false;
11800 /* C and C++ FEs differ by using IDENTIFIER_NODE and TYPE_DECL. */
11801 if (TREE_CODE (t1) == IDENTIFIER_NODE
11802 && TREE_CODE (t2) == TYPE_DECL
11803 && DECL_FILE_SCOPE_P (t1))
11805 t2 = DECL_NAME (t2);
11806 gcc_assert (TREE_CODE (t2) == IDENTIFIER_NODE);
11808 if (TREE_CODE (t2) == IDENTIFIER_NODE
11809 && TREE_CODE (t1) == TYPE_DECL
11810 && DECL_FILE_SCOPE_P (t2))
11812 t1 = DECL_NAME (t1);
11813 gcc_assert (TREE_CODE (t1) == IDENTIFIER_NODE);
11815 if (TREE_CODE (t1) != TREE_CODE (t2))
11816 return false;
11817 if (TYPE_P (t1))
11818 return types_same_for_odr (t1, t2);
11819 if (DECL_P (t1))
11820 return decls_same_for_odr (t1, t2);
11821 return false;
11824 /* For languages with One Definition Rule, work out if
11825 decls are actually the same even if the tree representation
11826 differs. This handles only decls appearing in TYPE_NAME
11827 and TYPE_CONTEXT. That is NAMESPACE_DECL, TYPE_DECL,
11828 RECORD_TYPE and IDENTIFIER_NODE. */
11830 static bool
11831 decls_same_for_odr (tree decl1, tree decl2)
11833 if (decl1 && TREE_CODE (decl1) == TYPE_DECL
11834 && DECL_ORIGINAL_TYPE (decl1))
11835 decl1 = DECL_ORIGINAL_TYPE (decl1);
11836 if (decl2 && TREE_CODE (decl2) == TYPE_DECL
11837 && DECL_ORIGINAL_TYPE (decl2))
11838 decl2 = DECL_ORIGINAL_TYPE (decl2);
11839 if (decl1 == decl2)
11840 return true;
11841 if (!decl1 || !decl2)
11842 return false;
11843 gcc_checking_assert (DECL_P (decl1) && DECL_P (decl2));
11844 if (TREE_CODE (decl1) != TREE_CODE (decl2))
11845 return false;
11846 if (TREE_CODE (decl1) == TRANSLATION_UNIT_DECL)
11847 return true;
11848 if (TREE_CODE (decl1) != NAMESPACE_DECL
11849 && TREE_CODE (decl1) != TYPE_DECL)
11850 return false;
11851 if (!DECL_NAME (decl1))
11852 return false;
11853 gcc_checking_assert (TREE_CODE (DECL_NAME (decl1)) == IDENTIFIER_NODE);
11854 gcc_checking_assert (!DECL_NAME (decl2)
11855 || TREE_CODE (DECL_NAME (decl2)) == IDENTIFIER_NODE);
11856 if (DECL_NAME (decl1) != DECL_NAME (decl2))
11857 return false;
11858 return same_for_odr (DECL_CONTEXT (decl1),
11859 DECL_CONTEXT (decl2));
11862 /* For languages with One Definition Rule, work out if
11863 types are same even if the tree representation differs.
11864 This is non-trivial for LTO where minnor differences in
11865 the type representation may have prevented type merging
11866 to merge two copies of otherwise equivalent type. */
11868 bool
11869 types_same_for_odr (tree type1, tree type2)
11871 gcc_checking_assert (TYPE_P (type1) && TYPE_P (type2));
11872 type1 = TYPE_MAIN_VARIANT (type1);
11873 type2 = TYPE_MAIN_VARIANT (type2);
11874 if (type1 == type2)
11875 return true;
11877 #ifndef ENABLE_CHECKING
11878 if (!in_lto_p)
11879 return false;
11880 #endif
11882 /* Check for anonymous namespaces. Those have !TREE_PUBLIC
11883 on the corresponding TYPE_STUB_DECL. */
11884 if (type_in_anonymous_namespace_p (type1)
11885 || type_in_anonymous_namespace_p (type2))
11886 return false;
11887 /* When assembler name of virtual table is available, it is
11888 easy to compare types for equivalence. */
11889 if (TYPE_BINFO (type1) && TYPE_BINFO (type2)
11890 && BINFO_VTABLE (TYPE_BINFO (type1))
11891 && BINFO_VTABLE (TYPE_BINFO (type2)))
11893 tree v1 = BINFO_VTABLE (TYPE_BINFO (type1));
11894 tree v2 = BINFO_VTABLE (TYPE_BINFO (type2));
11896 if (TREE_CODE (v1) == POINTER_PLUS_EXPR)
11898 if (TREE_CODE (v2) != POINTER_PLUS_EXPR
11899 || !operand_equal_p (TREE_OPERAND (v1, 1),
11900 TREE_OPERAND (v2, 1), 0))
11901 return false;
11902 v1 = TREE_OPERAND (TREE_OPERAND (v1, 0), 0);
11903 v2 = TREE_OPERAND (TREE_OPERAND (v2, 0), 0);
11905 v1 = DECL_ASSEMBLER_NAME (v1);
11906 v2 = DECL_ASSEMBLER_NAME (v2);
11907 return (v1 == v2);
11910 /* FIXME: the code comparing type names consider all instantiations of the
11911 same template to have same name. This is because we have no access
11912 to template parameters. For types with no virtual method tables
11913 we thus can return false positives. At the moment we do not need
11914 to compare types in other scenarios than devirtualization. */
11916 /* If types are not structuraly same, do not bother to contnue.
11917 Match in the remainder of code would mean ODR violation. */
11918 if (!types_compatible_p (type1, type2))
11919 return false;
11920 if (!TYPE_NAME (type1))
11921 return false;
11922 if (!decls_same_for_odr (TYPE_NAME (type1), TYPE_NAME (type2)))
11923 return false;
11924 if (!same_for_odr (TYPE_CONTEXT (type1), TYPE_CONTEXT (type2)))
11925 return false;
11926 /* When not in LTO the MAIN_VARIANT check should be the same. */
11927 gcc_assert (in_lto_p);
11929 return true;
11932 /* TARGET is a call target of GIMPLE call statement
11933 (obtained by gimple_call_fn). Return true if it is
11934 OBJ_TYPE_REF representing an virtual call of C++ method.
11935 (As opposed to OBJ_TYPE_REF representing objc calls
11936 through a cast where middle-end devirtualization machinery
11937 can't apply.) */
11939 bool
11940 virtual_method_call_p (tree target)
11942 if (TREE_CODE (target) != OBJ_TYPE_REF)
11943 return false;
11944 target = TREE_TYPE (target);
11945 gcc_checking_assert (TREE_CODE (target) == POINTER_TYPE);
11946 target = TREE_TYPE (target);
11947 if (TREE_CODE (target) == FUNCTION_TYPE)
11948 return false;
11949 gcc_checking_assert (TREE_CODE (target) == METHOD_TYPE);
11950 return true;
11953 /* REF is OBJ_TYPE_REF, return the class the ref corresponds to. */
11955 tree
11956 obj_type_ref_class (tree ref)
11958 gcc_checking_assert (TREE_CODE (ref) == OBJ_TYPE_REF);
11959 ref = TREE_TYPE (ref);
11960 gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
11961 ref = TREE_TYPE (ref);
11962 /* We look for type THIS points to. ObjC also builds
11963 OBJ_TYPE_REF with non-method calls, Their first parameter
11964 ID however also corresponds to class type. */
11965 gcc_checking_assert (TREE_CODE (ref) == METHOD_TYPE
11966 || TREE_CODE (ref) == FUNCTION_TYPE);
11967 ref = TREE_VALUE (TYPE_ARG_TYPES (ref));
11968 gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
11969 return TREE_TYPE (ref);
11972 /* Return true if T is in anonymous namespace. */
11974 bool
11975 type_in_anonymous_namespace_p (tree t)
11977 return (TYPE_STUB_DECL (t) && !TREE_PUBLIC (TYPE_STUB_DECL (t)));
11980 /* Try to find a base info of BINFO that would have its field decl at offset
11981 OFFSET within the BINFO type and which is of EXPECTED_TYPE. If it can be
11982 found, return, otherwise return NULL_TREE. */
11984 tree
11985 get_binfo_at_offset (tree binfo, HOST_WIDE_INT offset, tree expected_type)
11987 tree type = BINFO_TYPE (binfo);
11989 while (true)
11991 HOST_WIDE_INT pos, size;
11992 tree fld;
11993 int i;
11995 if (types_same_for_odr (type, expected_type))
11996 return binfo;
11997 if (offset < 0)
11998 return NULL_TREE;
12000 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
12002 if (TREE_CODE (fld) != FIELD_DECL)
12003 continue;
12005 pos = int_bit_position (fld);
12006 size = tree_low_cst (DECL_SIZE (fld), 1);
12007 if (pos <= offset && (pos + size) > offset)
12008 break;
12010 if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE)
12011 return NULL_TREE;
12013 if (!DECL_ARTIFICIAL (fld))
12015 binfo = TYPE_BINFO (TREE_TYPE (fld));
12016 if (!binfo)
12017 return NULL_TREE;
12019 /* Offset 0 indicates the primary base, whose vtable contents are
12020 represented in the binfo for the derived class. */
12021 else if (offset != 0)
12023 tree base_binfo, found_binfo = NULL_TREE;
12024 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
12025 if (types_same_for_odr (TREE_TYPE (base_binfo), TREE_TYPE (fld)))
12027 found_binfo = base_binfo;
12028 break;
12030 if (!found_binfo)
12031 return NULL_TREE;
12032 binfo = found_binfo;
12035 type = TREE_TYPE (fld);
12036 offset -= pos;
12040 /* Returns true if X is a typedef decl. */
12042 bool
12043 is_typedef_decl (tree x)
12045 return (x && TREE_CODE (x) == TYPE_DECL
12046 && DECL_ORIGINAL_TYPE (x) != NULL_TREE);
12049 /* Returns true iff TYPE is a type variant created for a typedef. */
12051 bool
12052 typedef_variant_p (tree type)
12054 return is_typedef_decl (TYPE_NAME (type));
12057 /* Warn about a use of an identifier which was marked deprecated. */
12058 void
12059 warn_deprecated_use (tree node, tree attr)
12061 const char *msg;
12063 if (node == 0 || !warn_deprecated_decl)
12064 return;
12066 if (!attr)
12068 if (DECL_P (node))
12069 attr = DECL_ATTRIBUTES (node);
12070 else if (TYPE_P (node))
12072 tree decl = TYPE_STUB_DECL (node);
12073 if (decl)
12074 attr = lookup_attribute ("deprecated",
12075 TYPE_ATTRIBUTES (TREE_TYPE (decl)));
12079 if (attr)
12080 attr = lookup_attribute ("deprecated", attr);
12082 if (attr)
12083 msg = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr)));
12084 else
12085 msg = NULL;
12087 if (DECL_P (node))
12089 expanded_location xloc = expand_location (DECL_SOURCE_LOCATION (node));
12090 if (msg)
12091 warning (OPT_Wdeprecated_declarations,
12092 "%qD is deprecated (declared at %r%s:%d%R): %s",
12093 node, "locus", xloc.file, xloc.line, msg);
12094 else
12095 warning (OPT_Wdeprecated_declarations,
12096 "%qD is deprecated (declared at %r%s:%d%R)",
12097 node, "locus", xloc.file, xloc.line);
12099 else if (TYPE_P (node))
12101 tree what = NULL_TREE;
12102 tree decl = TYPE_STUB_DECL (node);
12104 if (TYPE_NAME (node))
12106 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
12107 what = TYPE_NAME (node);
12108 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
12109 && DECL_NAME (TYPE_NAME (node)))
12110 what = DECL_NAME (TYPE_NAME (node));
12113 if (decl)
12115 expanded_location xloc
12116 = expand_location (DECL_SOURCE_LOCATION (decl));
12117 if (what)
12119 if (msg)
12120 warning (OPT_Wdeprecated_declarations,
12121 "%qE is deprecated (declared at %r%s:%d%R): %s",
12122 what, "locus", xloc.file, xloc.line, msg);
12123 else
12124 warning (OPT_Wdeprecated_declarations,
12125 "%qE is deprecated (declared at %r%s:%d%R)",
12126 what, "locus", xloc.file, xloc.line);
12128 else
12130 if (msg)
12131 warning (OPT_Wdeprecated_declarations,
12132 "type is deprecated (declared at %r%s:%d%R): %s",
12133 "locus", xloc.file, xloc.line, msg);
12134 else
12135 warning (OPT_Wdeprecated_declarations,
12136 "type is deprecated (declared at %r%s:%d%R)",
12137 "locus", xloc.file, xloc.line);
12140 else
12142 if (what)
12144 if (msg)
12145 warning (OPT_Wdeprecated_declarations, "%qE is deprecated: %s",
12146 what, msg);
12147 else
12148 warning (OPT_Wdeprecated_declarations, "%qE is deprecated", what);
12150 else
12152 if (msg)
12153 warning (OPT_Wdeprecated_declarations, "type is deprecated: %s",
12154 msg);
12155 else
12156 warning (OPT_Wdeprecated_declarations, "type is deprecated");
12162 /* Return true if REF has a COMPONENT_REF with a bit-field field declaration
12163 somewhere in it. */
12165 bool
12166 contains_bitfld_component_ref_p (const_tree ref)
12168 while (handled_component_p (ref))
12170 if (TREE_CODE (ref) == COMPONENT_REF
12171 && DECL_BIT_FIELD (TREE_OPERAND (ref, 1)))
12172 return true;
12173 ref = TREE_OPERAND (ref, 0);
12176 return false;
12179 /* Try to determine whether a TRY_CATCH expression can fall through.
12180 This is a subroutine of block_may_fallthru. */
12182 static bool
12183 try_catch_may_fallthru (const_tree stmt)
12185 tree_stmt_iterator i;
12187 /* If the TRY block can fall through, the whole TRY_CATCH can
12188 fall through. */
12189 if (block_may_fallthru (TREE_OPERAND (stmt, 0)))
12190 return true;
12192 i = tsi_start (TREE_OPERAND (stmt, 1));
12193 switch (TREE_CODE (tsi_stmt (i)))
12195 case CATCH_EXPR:
12196 /* We expect to see a sequence of CATCH_EXPR trees, each with a
12197 catch expression and a body. The whole TRY_CATCH may fall
12198 through iff any of the catch bodies falls through. */
12199 for (; !tsi_end_p (i); tsi_next (&i))
12201 if (block_may_fallthru (CATCH_BODY (tsi_stmt (i))))
12202 return true;
12204 return false;
12206 case EH_FILTER_EXPR:
12207 /* The exception filter expression only matters if there is an
12208 exception. If the exception does not match EH_FILTER_TYPES,
12209 we will execute EH_FILTER_FAILURE, and we will fall through
12210 if that falls through. If the exception does match
12211 EH_FILTER_TYPES, the stack unwinder will continue up the
12212 stack, so we will not fall through. We don't know whether we
12213 will throw an exception which matches EH_FILTER_TYPES or not,
12214 so we just ignore EH_FILTER_TYPES and assume that we might
12215 throw an exception which doesn't match. */
12216 return block_may_fallthru (EH_FILTER_FAILURE (tsi_stmt (i)));
12218 default:
12219 /* This case represents statements to be executed when an
12220 exception occurs. Those statements are implicitly followed
12221 by a RESX statement to resume execution after the exception.
12222 So in this case the TRY_CATCH never falls through. */
12223 return false;
12227 /* Try to determine if we can fall out of the bottom of BLOCK. This guess
12228 need not be 100% accurate; simply be conservative and return true if we
12229 don't know. This is used only to avoid stupidly generating extra code.
12230 If we're wrong, we'll just delete the extra code later. */
12232 bool
12233 block_may_fallthru (const_tree block)
12235 /* This CONST_CAST is okay because expr_last returns its argument
12236 unmodified and we assign it to a const_tree. */
12237 const_tree stmt = expr_last (CONST_CAST_TREE (block));
12239 switch (stmt ? TREE_CODE (stmt) : ERROR_MARK)
12241 case GOTO_EXPR:
12242 case RETURN_EXPR:
12243 /* Easy cases. If the last statement of the block implies
12244 control transfer, then we can't fall through. */
12245 return false;
12247 case SWITCH_EXPR:
12248 /* If SWITCH_LABELS is set, this is lowered, and represents a
12249 branch to a selected label and hence can not fall through.
12250 Otherwise SWITCH_BODY is set, and the switch can fall
12251 through. */
12252 return SWITCH_LABELS (stmt) == NULL_TREE;
12254 case COND_EXPR:
12255 if (block_may_fallthru (COND_EXPR_THEN (stmt)))
12256 return true;
12257 return block_may_fallthru (COND_EXPR_ELSE (stmt));
12259 case BIND_EXPR:
12260 return block_may_fallthru (BIND_EXPR_BODY (stmt));
12262 case TRY_CATCH_EXPR:
12263 return try_catch_may_fallthru (stmt);
12265 case TRY_FINALLY_EXPR:
12266 /* The finally clause is always executed after the try clause,
12267 so if it does not fall through, then the try-finally will not
12268 fall through. Otherwise, if the try clause does not fall
12269 through, then when the finally clause falls through it will
12270 resume execution wherever the try clause was going. So the
12271 whole try-finally will only fall through if both the try
12272 clause and the finally clause fall through. */
12273 return (block_may_fallthru (TREE_OPERAND (stmt, 0))
12274 && block_may_fallthru (TREE_OPERAND (stmt, 1)));
12276 case MODIFY_EXPR:
12277 if (TREE_CODE (TREE_OPERAND (stmt, 1)) == CALL_EXPR)
12278 stmt = TREE_OPERAND (stmt, 1);
12279 else
12280 return true;
12281 /* FALLTHRU */
12283 case CALL_EXPR:
12284 /* Functions that do not return do not fall through. */
12285 return (call_expr_flags (stmt) & ECF_NORETURN) == 0;
12287 case CLEANUP_POINT_EXPR:
12288 return block_may_fallthru (TREE_OPERAND (stmt, 0));
12290 case TARGET_EXPR:
12291 return block_may_fallthru (TREE_OPERAND (stmt, 1));
12293 case ERROR_MARK:
12294 return true;
12296 default:
12297 return lang_hooks.block_may_fallthru (stmt);
12301 /* True if we are using EH to handle cleanups. */
12302 static bool using_eh_for_cleanups_flag = false;
12304 /* This routine is called from front ends to indicate eh should be used for
12305 cleanups. */
12306 void
12307 using_eh_for_cleanups (void)
12309 using_eh_for_cleanups_flag = true;
12312 /* Query whether EH is used for cleanups. */
12313 bool
12314 using_eh_for_cleanups_p (void)
12316 return using_eh_for_cleanups_flag;
12319 /* Wrapper for tree_code_name to ensure that tree code is valid */
12320 const char *
12321 get_tree_code_name (enum tree_code code)
12323 const char *invalid = "<invalid tree code>";
12325 if (code >= MAX_TREE_CODES)
12326 return invalid;
12328 return tree_code_name[code];
12331 /* Drops the TREE_OVERFLOW flag from T. */
12333 tree
12334 drop_tree_overflow (tree t)
12336 gcc_checking_assert (TREE_OVERFLOW (t));
12338 /* For tree codes with a sharing machinery re-build the result. */
12339 if (TREE_CODE (t) == INTEGER_CST)
12340 return build_int_cst_wide (TREE_TYPE (t),
12341 TREE_INT_CST_LOW (t), TREE_INT_CST_HIGH (t));
12343 /* Otherwise, as all tcc_constants are possibly shared, copy the node
12344 and drop the flag. */
12345 t = copy_node (t);
12346 TREE_OVERFLOW (t) = 0;
12347 return t;
12350 #include "gt-tree.h"