OpenACC loop construct.
[official-gcc.git] / gcc / tree.c
blob96e856c4346417a41b0a16882121744e9fb9b9a2
1 /* Language-independent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This file contains the low level primitives for operating on tree nodes,
21 including allocation, list operations, interning of identifiers,
22 construction of data type nodes and statement nodes,
23 and construction of type conversion nodes. It also contains
24 tables index by tree code that describe how to take apart
25 nodes of that code.
27 It is intended to be language-independent, but occasionally
28 calls language-dependent routines defined (for C) in typecheck.c. */
30 #include "config.h"
31 #include "system.h"
32 #include "coretypes.h"
33 #include "tm.h"
34 #include "flags.h"
35 #include "tree.h"
36 #include "stor-layout.h"
37 #include "calls.h"
38 #include "attribs.h"
39 #include "varasm.h"
40 #include "tm_p.h"
41 #include "function.h"
42 #include "obstack.h"
43 #include "toplev.h" /* get_random_seed */
44 #include "hashtab.h"
45 #include "filenames.h"
46 #include "output.h"
47 #include "target.h"
48 #include "common/common-target.h"
49 #include "langhooks.h"
50 #include "tree-inline.h"
51 #include "tree-iterator.h"
52 #include "basic-block.h"
53 #include "bitmap.h"
54 #include "pointer-set.h"
55 #include "tree-ssa-alias.h"
56 #include "internal-fn.h"
57 #include "gimple-expr.h"
58 #include "is-a.h"
59 #include "gimple.h"
60 #include "gimple-iterator.h"
61 #include "gimplify.h"
62 #include "gimple-ssa.h"
63 #include "cgraph.h"
64 #include "tree-phinodes.h"
65 #include "stringpool.h"
66 #include "tree-ssanames.h"
67 #include "expr.h"
68 #include "tree-dfa.h"
69 #include "params.h"
70 #include "tree-pass.h"
71 #include "langhooks-def.h"
72 #include "diagnostic.h"
73 #include "tree-diagnostic.h"
74 #include "tree-pretty-print.h"
75 #include "except.h"
76 #include "debug.h"
77 #include "intl.h"
78 #include "wide-int.h"
80 /* Tree code classes. */
82 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
83 #define END_OF_BASE_TREE_CODES tcc_exceptional,
85 const enum tree_code_class tree_code_type[] = {
86 #include "all-tree.def"
89 #undef DEFTREECODE
90 #undef END_OF_BASE_TREE_CODES
92 /* Table indexed by tree code giving number of expression
93 operands beyond the fixed part of the node structure.
94 Not used for types or decls. */
96 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
97 #define END_OF_BASE_TREE_CODES 0,
99 const unsigned char tree_code_length[] = {
100 #include "all-tree.def"
103 #undef DEFTREECODE
104 #undef END_OF_BASE_TREE_CODES
106 /* Names of tree components.
107 Used for printing out the tree and error messages. */
108 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
109 #define END_OF_BASE_TREE_CODES "@dummy",
111 static const char *const tree_code_name[] = {
112 #include "all-tree.def"
115 #undef DEFTREECODE
116 #undef END_OF_BASE_TREE_CODES
118 /* Each tree code class has an associated string representation.
119 These must correspond to the tree_code_class entries. */
121 const char *const tree_code_class_strings[] =
123 "exceptional",
124 "constant",
125 "type",
126 "declaration",
127 "reference",
128 "comparison",
129 "unary",
130 "binary",
131 "statement",
132 "vl_exp",
133 "expression"
136 /* obstack.[ch] explicitly declined to prototype this. */
137 extern int _obstack_allocated_p (struct obstack *h, void *obj);
139 /* Statistics-gathering stuff. */
141 static int tree_code_counts[MAX_TREE_CODES];
142 int tree_node_counts[(int) all_kinds];
143 int tree_node_sizes[(int) all_kinds];
145 /* Keep in sync with tree.h:enum tree_node_kind. */
146 static const char * const tree_node_kind_names[] = {
147 "decls",
148 "types",
149 "blocks",
150 "stmts",
151 "refs",
152 "exprs",
153 "constants",
154 "identifiers",
155 "vecs",
156 "binfos",
157 "ssa names",
158 "constructors",
159 "random kinds",
160 "lang_decl kinds",
161 "lang_type kinds",
162 "omp clauses",
165 /* Unique id for next decl created. */
166 static GTY(()) int next_decl_uid;
167 /* Unique id for next type created. */
168 static GTY(()) int next_type_uid = 1;
169 /* Unique id for next debug decl created. Use negative numbers,
170 to catch erroneous uses. */
171 static GTY(()) int next_debug_decl_uid;
173 /* Since we cannot rehash a type after it is in the table, we have to
174 keep the hash code. */
176 struct GTY(()) type_hash {
177 unsigned long hash;
178 tree type;
181 /* Initial size of the hash table (rounded to next prime). */
182 #define TYPE_HASH_INITIAL_SIZE 1000
184 /* Now here is the hash table. When recording a type, it is added to
185 the slot whose index is the hash code. Note that the hash table is
186 used for several kinds of types (function types, array types and
187 array index range types, for now). While all these live in the
188 same table, they are completely independent, and the hash code is
189 computed differently for each of these. */
191 static GTY ((if_marked ("type_hash_marked_p"), param_is (struct type_hash)))
192 htab_t type_hash_table;
194 /* Hash table and temporary node for larger integer const values. */
195 static GTY (()) tree int_cst_node;
196 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node)))
197 htab_t int_cst_hash_table;
199 /* Hash table for optimization flags and target option flags. Use the same
200 hash table for both sets of options. Nodes for building the current
201 optimization and target option nodes. The assumption is most of the time
202 the options created will already be in the hash table, so we avoid
203 allocating and freeing up a node repeatably. */
204 static GTY (()) tree cl_optimization_node;
205 static GTY (()) tree cl_target_option_node;
206 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node)))
207 htab_t cl_option_hash_table;
209 /* General tree->tree mapping structure for use in hash tables. */
212 static GTY ((if_marked ("tree_decl_map_marked_p"), param_is (struct tree_decl_map)))
213 htab_t debug_expr_for_decl;
215 static GTY ((if_marked ("tree_decl_map_marked_p"), param_is (struct tree_decl_map)))
216 htab_t value_expr_for_decl;
218 static GTY ((if_marked ("tree_vec_map_marked_p"), param_is (struct tree_vec_map)))
219 htab_t debug_args_for_decl;
221 static GTY ((if_marked ("tree_priority_map_marked_p"),
222 param_is (struct tree_priority_map)))
223 htab_t init_priority_for_decl;
225 static void set_type_quals (tree, int);
226 static int type_hash_eq (const void *, const void *);
227 static hashval_t type_hash_hash (const void *);
228 static hashval_t int_cst_hash_hash (const void *);
229 static int int_cst_hash_eq (const void *, const void *);
230 static hashval_t cl_option_hash_hash (const void *);
231 static int cl_option_hash_eq (const void *, const void *);
232 static void print_type_hash_statistics (void);
233 static void print_debug_expr_statistics (void);
234 static void print_value_expr_statistics (void);
235 static int type_hash_marked_p (const void *);
236 static unsigned int type_hash_list (const_tree, hashval_t);
237 static unsigned int attribute_hash_list (const_tree, hashval_t);
238 static bool decls_same_for_odr (tree decl1, tree decl2);
240 tree global_trees[TI_MAX];
241 tree integer_types[itk_none];
243 unsigned char tree_contains_struct[MAX_TREE_CODES][64];
245 /* Number of operands for each OpenMP clause. */
246 unsigned const char omp_clause_num_ops[] =
248 0, /* OMP_CLAUSE_ERROR */
249 1, /* OMP_CLAUSE_PRIVATE */
250 1, /* OMP_CLAUSE_SHARED */
251 1, /* OMP_CLAUSE_FIRSTPRIVATE */
252 2, /* OMP_CLAUSE_LASTPRIVATE */
253 4, /* OMP_CLAUSE_REDUCTION */
254 1, /* OMP_CLAUSE_COPYIN */
255 1, /* OMP_CLAUSE_COPYPRIVATE */
256 3, /* OMP_CLAUSE_LINEAR */
257 2, /* OMP_CLAUSE_ALIGNED */
258 1, /* OMP_CLAUSE_DEPEND */
259 1, /* OMP_CLAUSE_UNIFORM */
260 2, /* OMP_CLAUSE_FROM */
261 2, /* OMP_CLAUSE_TO */
262 2, /* OMP_CLAUSE_MAP */
263 1, /* OMP_CLAUSE_HOST */
264 1, /* OMP_CLAUSE_OACC_DEVICE */
265 1, /* OMP_CLAUSE_DEVICE_RESIDENT */
266 1, /* OMP_CLAUSE_USE_DEVICE */
267 1, /* OMP_CLAUSE_GANG */
268 1, /* OMP_CLAUSE_ASYNC */
269 1, /* OMP_CLAUSE_WAIT */
270 1, /* OMP_NO_CLAUSE_CACHE */
271 1, /* OMP_CLAUSE__LOOPTEMP_ */
272 1, /* OMP_CLAUSE_IF */
273 1, /* OMP_CLAUSE_NUM_THREADS */
274 1, /* OMP_CLAUSE_SCHEDULE */
275 0, /* OMP_CLAUSE_NOWAIT */
276 0, /* OMP_CLAUSE_ORDERED */
277 0, /* OMP_CLAUSE_DEFAULT */
278 3, /* OMP_CLAUSE_COLLAPSE */
279 0, /* OMP_CLAUSE_UNTIED */
280 1, /* OMP_CLAUSE_FINAL */
281 0, /* OMP_CLAUSE_MERGEABLE */
282 1, /* OMP_CLAUSE_DEVICE */
283 1, /* OMP_CLAUSE_DIST_SCHEDULE */
284 0, /* OMP_CLAUSE_INBRANCH */
285 0, /* OMP_CLAUSE_NOTINBRANCH */
286 1, /* OMP_CLAUSE_NUM_TEAMS */
287 1, /* OMP_CLAUSE_THREAD_LIMIT */
288 0, /* OMP_CLAUSE_PROC_BIND */
289 1, /* OMP_CLAUSE_SAFELEN */
290 1, /* OMP_CLAUSE_SIMDLEN */
291 0, /* OMP_CLAUSE_FOR */
292 0, /* OMP_CLAUSE_PARALLEL */
293 0, /* OMP_CLAUSE_SECTIONS */
294 0, /* OMP_CLAUSE_TASKGROUP */
295 1, /* OMP_CLAUSE__SIMDUID_ */
296 0, /* OMP_CLAUSE_INDEPENDENT */
297 1, /* OMP_CLAUSE_WORKER */
298 1, /* OMP_CLAUSE_VECTOR */
299 1, /* OMP_CLAUSE_NUM_GANGS */
300 1, /* OMP_CLAUSE_NUM_WORKERS */
301 1, /* OMP_CLAUSE_VECTOR_LENGTH */
304 const char * const omp_clause_code_name[] =
306 "error_clause",
307 "private",
308 "shared",
309 "firstprivate",
310 "lastprivate",
311 "reduction",
312 "copyin",
313 "copyprivate",
314 "linear",
315 "aligned",
316 "depend",
317 "uniform",
318 "from",
319 "to",
320 "map",
321 "host",
322 "device",
323 "device_resident",
324 "use_device",
325 "gang",
326 "async",
327 "wait",
328 "_cache_",
329 "_looptemp_",
330 "if",
331 "num_threads",
332 "schedule",
333 "nowait",
334 "ordered",
335 "default",
336 "collapse",
337 "untied",
338 "final",
339 "mergeable",
340 "device",
341 "dist_schedule",
342 "inbranch",
343 "notinbranch",
344 "num_teams",
345 "thread_limit",
346 "proc_bind",
347 "safelen",
348 "simdlen",
349 "for",
350 "parallel",
351 "sections",
352 "taskgroup",
353 "_simduid_",
354 "independent",
355 "worker",
356 "vector",
357 "num_gangs",
358 "num_workers",
359 "vector_length"
363 /* Return the tree node structure used by tree code CODE. */
365 static inline enum tree_node_structure_enum
366 tree_node_structure_for_code (enum tree_code code)
368 switch (TREE_CODE_CLASS (code))
370 case tcc_declaration:
372 switch (code)
374 case FIELD_DECL:
375 return TS_FIELD_DECL;
376 case PARM_DECL:
377 return TS_PARM_DECL;
378 case VAR_DECL:
379 return TS_VAR_DECL;
380 case LABEL_DECL:
381 return TS_LABEL_DECL;
382 case RESULT_DECL:
383 return TS_RESULT_DECL;
384 case DEBUG_EXPR_DECL:
385 return TS_DECL_WRTL;
386 case CONST_DECL:
387 return TS_CONST_DECL;
388 case TYPE_DECL:
389 return TS_TYPE_DECL;
390 case FUNCTION_DECL:
391 return TS_FUNCTION_DECL;
392 case TRANSLATION_UNIT_DECL:
393 return TS_TRANSLATION_UNIT_DECL;
394 default:
395 return TS_DECL_NON_COMMON;
398 case tcc_type:
399 return TS_TYPE_NON_COMMON;
400 case tcc_reference:
401 case tcc_comparison:
402 case tcc_unary:
403 case tcc_binary:
404 case tcc_expression:
405 case tcc_statement:
406 case tcc_vl_exp:
407 return TS_EXP;
408 default: /* tcc_constant and tcc_exceptional */
409 break;
411 switch (code)
413 /* tcc_constant cases. */
414 case INTEGER_CST: return TS_INT_CST;
415 case REAL_CST: return TS_REAL_CST;
416 case FIXED_CST: return TS_FIXED_CST;
417 case COMPLEX_CST: return TS_COMPLEX;
418 case VECTOR_CST: return TS_VECTOR;
419 case STRING_CST: return TS_STRING;
420 /* tcc_exceptional cases. */
421 case ERROR_MARK: return TS_COMMON;
422 case IDENTIFIER_NODE: return TS_IDENTIFIER;
423 case TREE_LIST: return TS_LIST;
424 case TREE_VEC: return TS_VEC;
425 case SSA_NAME: return TS_SSA_NAME;
426 case PLACEHOLDER_EXPR: return TS_COMMON;
427 case STATEMENT_LIST: return TS_STATEMENT_LIST;
428 case BLOCK: return TS_BLOCK;
429 case CONSTRUCTOR: return TS_CONSTRUCTOR;
430 case TREE_BINFO: return TS_BINFO;
431 case OMP_CLAUSE: return TS_OMP_CLAUSE;
432 case OPTIMIZATION_NODE: return TS_OPTIMIZATION;
433 case TARGET_OPTION_NODE: return TS_TARGET_OPTION;
435 default:
436 gcc_unreachable ();
441 /* Initialize tree_contains_struct to describe the hierarchy of tree
442 nodes. */
444 static void
445 initialize_tree_contains_struct (void)
447 unsigned i;
449 for (i = ERROR_MARK; i < LAST_AND_UNUSED_TREE_CODE; i++)
451 enum tree_code code;
452 enum tree_node_structure_enum ts_code;
454 code = (enum tree_code) i;
455 ts_code = tree_node_structure_for_code (code);
457 /* Mark the TS structure itself. */
458 tree_contains_struct[code][ts_code] = 1;
460 /* Mark all the structures that TS is derived from. */
461 switch (ts_code)
463 case TS_TYPED:
464 case TS_BLOCK:
465 MARK_TS_BASE (code);
466 break;
468 case TS_COMMON:
469 case TS_INT_CST:
470 case TS_REAL_CST:
471 case TS_FIXED_CST:
472 case TS_VECTOR:
473 case TS_STRING:
474 case TS_COMPLEX:
475 case TS_SSA_NAME:
476 case TS_CONSTRUCTOR:
477 case TS_EXP:
478 case TS_STATEMENT_LIST:
479 MARK_TS_TYPED (code);
480 break;
482 case TS_IDENTIFIER:
483 case TS_DECL_MINIMAL:
484 case TS_TYPE_COMMON:
485 case TS_LIST:
486 case TS_VEC:
487 case TS_BINFO:
488 case TS_OMP_CLAUSE:
489 case TS_OPTIMIZATION:
490 case TS_TARGET_OPTION:
491 MARK_TS_COMMON (code);
492 break;
494 case TS_TYPE_WITH_LANG_SPECIFIC:
495 MARK_TS_TYPE_COMMON (code);
496 break;
498 case TS_TYPE_NON_COMMON:
499 MARK_TS_TYPE_WITH_LANG_SPECIFIC (code);
500 break;
502 case TS_DECL_COMMON:
503 MARK_TS_DECL_MINIMAL (code);
504 break;
506 case TS_DECL_WRTL:
507 case TS_CONST_DECL:
508 MARK_TS_DECL_COMMON (code);
509 break;
511 case TS_DECL_NON_COMMON:
512 MARK_TS_DECL_WITH_VIS (code);
513 break;
515 case TS_DECL_WITH_VIS:
516 case TS_PARM_DECL:
517 case TS_LABEL_DECL:
518 case TS_RESULT_DECL:
519 MARK_TS_DECL_WRTL (code);
520 break;
522 case TS_FIELD_DECL:
523 MARK_TS_DECL_COMMON (code);
524 break;
526 case TS_VAR_DECL:
527 MARK_TS_DECL_WITH_VIS (code);
528 break;
530 case TS_TYPE_DECL:
531 case TS_FUNCTION_DECL:
532 MARK_TS_DECL_NON_COMMON (code);
533 break;
535 case TS_TRANSLATION_UNIT_DECL:
536 MARK_TS_DECL_COMMON (code);
537 break;
539 default:
540 gcc_unreachable ();
544 /* Basic consistency checks for attributes used in fold. */
545 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_NON_COMMON]);
546 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_NON_COMMON]);
547 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_COMMON]);
548 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_COMMON]);
549 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_COMMON]);
550 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_COMMON]);
551 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_COMMON]);
552 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_COMMON]);
553 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_COMMON]);
554 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_COMMON]);
555 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_COMMON]);
556 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WRTL]);
557 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_WRTL]);
558 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_WRTL]);
559 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WRTL]);
560 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_WRTL]);
561 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_MINIMAL]);
562 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_MINIMAL]);
563 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_MINIMAL]);
564 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_MINIMAL]);
565 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_MINIMAL]);
566 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_MINIMAL]);
567 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_MINIMAL]);
568 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_MINIMAL]);
569 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_MINIMAL]);
570 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WITH_VIS]);
571 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WITH_VIS]);
572 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_WITH_VIS]);
573 gcc_assert (tree_contains_struct[VAR_DECL][TS_VAR_DECL]);
574 gcc_assert (tree_contains_struct[FIELD_DECL][TS_FIELD_DECL]);
575 gcc_assert (tree_contains_struct[PARM_DECL][TS_PARM_DECL]);
576 gcc_assert (tree_contains_struct[LABEL_DECL][TS_LABEL_DECL]);
577 gcc_assert (tree_contains_struct[RESULT_DECL][TS_RESULT_DECL]);
578 gcc_assert (tree_contains_struct[CONST_DECL][TS_CONST_DECL]);
579 gcc_assert (tree_contains_struct[TYPE_DECL][TS_TYPE_DECL]);
580 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_FUNCTION_DECL]);
581 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_MINIMAL]);
582 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_COMMON]);
583 gcc_assert (tree_contains_struct[NAMELIST_DECL][TS_DECL_MINIMAL]);
584 gcc_assert (tree_contains_struct[NAMELIST_DECL][TS_DECL_COMMON]);
588 /* Init tree.c. */
590 void
591 init_ttree (void)
593 /* Initialize the hash table of types. */
594 type_hash_table = htab_create_ggc (TYPE_HASH_INITIAL_SIZE, type_hash_hash,
595 type_hash_eq, 0);
597 debug_expr_for_decl = htab_create_ggc (512, tree_decl_map_hash,
598 tree_decl_map_eq, 0);
600 value_expr_for_decl = htab_create_ggc (512, tree_decl_map_hash,
601 tree_decl_map_eq, 0);
602 init_priority_for_decl = htab_create_ggc (512, tree_priority_map_hash,
603 tree_priority_map_eq, 0);
605 int_cst_hash_table = htab_create_ggc (1024, int_cst_hash_hash,
606 int_cst_hash_eq, NULL);
608 int_cst_node = make_int_cst (1, 1);
610 cl_option_hash_table = htab_create_ggc (64, cl_option_hash_hash,
611 cl_option_hash_eq, NULL);
613 cl_optimization_node = make_node (OPTIMIZATION_NODE);
614 cl_target_option_node = make_node (TARGET_OPTION_NODE);
616 /* Initialize the tree_contains_struct array. */
617 initialize_tree_contains_struct ();
618 lang_hooks.init_ts ();
622 /* The name of the object as the assembler will see it (but before any
623 translations made by ASM_OUTPUT_LABELREF). Often this is the same
624 as DECL_NAME. It is an IDENTIFIER_NODE. */
625 tree
626 decl_assembler_name (tree decl)
628 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
629 lang_hooks.set_decl_assembler_name (decl);
630 return DECL_WITH_VIS_CHECK (decl)->decl_with_vis.assembler_name;
633 /* Compute the number of bytes occupied by a tree with code CODE.
634 This function cannot be used for nodes that have variable sizes,
635 including TREE_VEC, INTEGER_CST, STRING_CST, and CALL_EXPR. */
636 size_t
637 tree_code_size (enum tree_code code)
639 switch (TREE_CODE_CLASS (code))
641 case tcc_declaration: /* A decl node */
643 switch (code)
645 case FIELD_DECL:
646 return sizeof (struct tree_field_decl);
647 case PARM_DECL:
648 return sizeof (struct tree_parm_decl);
649 case VAR_DECL:
650 return sizeof (struct tree_var_decl);
651 case LABEL_DECL:
652 return sizeof (struct tree_label_decl);
653 case RESULT_DECL:
654 return sizeof (struct tree_result_decl);
655 case CONST_DECL:
656 return sizeof (struct tree_const_decl);
657 case TYPE_DECL:
658 return sizeof (struct tree_type_decl);
659 case FUNCTION_DECL:
660 return sizeof (struct tree_function_decl);
661 case DEBUG_EXPR_DECL:
662 return sizeof (struct tree_decl_with_rtl);
663 default:
664 return sizeof (struct tree_decl_non_common);
668 case tcc_type: /* a type node */
669 return sizeof (struct tree_type_non_common);
671 case tcc_reference: /* a reference */
672 case tcc_expression: /* an expression */
673 case tcc_statement: /* an expression with side effects */
674 case tcc_comparison: /* a comparison expression */
675 case tcc_unary: /* a unary arithmetic expression */
676 case tcc_binary: /* a binary arithmetic expression */
677 return (sizeof (struct tree_exp)
678 + (TREE_CODE_LENGTH (code) - 1) * sizeof (tree));
680 case tcc_constant: /* a constant */
681 switch (code)
683 case INTEGER_CST: gcc_unreachable ();
684 case REAL_CST: return sizeof (struct tree_real_cst);
685 case FIXED_CST: return sizeof (struct tree_fixed_cst);
686 case COMPLEX_CST: return sizeof (struct tree_complex);
687 case VECTOR_CST: return sizeof (struct tree_vector);
688 case STRING_CST: gcc_unreachable ();
689 default:
690 return lang_hooks.tree_size (code);
693 case tcc_exceptional: /* something random, like an identifier. */
694 switch (code)
696 case IDENTIFIER_NODE: return lang_hooks.identifier_size;
697 case TREE_LIST: return sizeof (struct tree_list);
699 case ERROR_MARK:
700 case PLACEHOLDER_EXPR: return sizeof (struct tree_common);
702 case TREE_VEC:
703 case OMP_CLAUSE: gcc_unreachable ();
705 case SSA_NAME: return sizeof (struct tree_ssa_name);
707 case STATEMENT_LIST: return sizeof (struct tree_statement_list);
708 case BLOCK: return sizeof (struct tree_block);
709 case CONSTRUCTOR: return sizeof (struct tree_constructor);
710 case OPTIMIZATION_NODE: return sizeof (struct tree_optimization_option);
711 case TARGET_OPTION_NODE: return sizeof (struct tree_target_option);
713 default:
714 return lang_hooks.tree_size (code);
717 default:
718 gcc_unreachable ();
722 /* Compute the number of bytes occupied by NODE. This routine only
723 looks at TREE_CODE, except for those nodes that have variable sizes. */
724 size_t
725 tree_size (const_tree node)
727 const enum tree_code code = TREE_CODE (node);
728 switch (code)
730 case INTEGER_CST:
731 return (sizeof (struct tree_int_cst)
732 + (TREE_INT_CST_EXT_NUNITS (node) - 1) * sizeof (HOST_WIDE_INT));
734 case TREE_BINFO:
735 return (offsetof (struct tree_binfo, base_binfos)
736 + vec<tree, va_gc>
737 ::embedded_size (BINFO_N_BASE_BINFOS (node)));
739 case TREE_VEC:
740 return (sizeof (struct tree_vec)
741 + (TREE_VEC_LENGTH (node) - 1) * sizeof (tree));
743 case VECTOR_CST:
744 return (sizeof (struct tree_vector)
745 + (TYPE_VECTOR_SUBPARTS (TREE_TYPE (node)) - 1) * sizeof (tree));
747 case STRING_CST:
748 return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
750 case OMP_CLAUSE:
751 return (sizeof (struct tree_omp_clause)
752 + (omp_clause_num_ops[OMP_CLAUSE_CODE (node)] - 1)
753 * sizeof (tree));
755 default:
756 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
757 return (sizeof (struct tree_exp)
758 + (VL_EXP_OPERAND_LENGTH (node) - 1) * sizeof (tree));
759 else
760 return tree_code_size (code);
764 /* Record interesting allocation statistics for a tree node with CODE
765 and LENGTH. */
767 static void
768 record_node_allocation_statistics (enum tree_code code ATTRIBUTE_UNUSED,
769 size_t length ATTRIBUTE_UNUSED)
771 enum tree_code_class type = TREE_CODE_CLASS (code);
772 tree_node_kind kind;
774 if (!GATHER_STATISTICS)
775 return;
777 switch (type)
779 case tcc_declaration: /* A decl node */
780 kind = d_kind;
781 break;
783 case tcc_type: /* a type node */
784 kind = t_kind;
785 break;
787 case tcc_statement: /* an expression with side effects */
788 kind = s_kind;
789 break;
791 case tcc_reference: /* a reference */
792 kind = r_kind;
793 break;
795 case tcc_expression: /* an expression */
796 case tcc_comparison: /* a comparison expression */
797 case tcc_unary: /* a unary arithmetic expression */
798 case tcc_binary: /* a binary arithmetic expression */
799 kind = e_kind;
800 break;
802 case tcc_constant: /* a constant */
803 kind = c_kind;
804 break;
806 case tcc_exceptional: /* something random, like an identifier. */
807 switch (code)
809 case IDENTIFIER_NODE:
810 kind = id_kind;
811 break;
813 case TREE_VEC:
814 kind = vec_kind;
815 break;
817 case TREE_BINFO:
818 kind = binfo_kind;
819 break;
821 case SSA_NAME:
822 kind = ssa_name_kind;
823 break;
825 case BLOCK:
826 kind = b_kind;
827 break;
829 case CONSTRUCTOR:
830 kind = constr_kind;
831 break;
833 case OMP_CLAUSE:
834 kind = omp_clause_kind;
835 break;
837 default:
838 kind = x_kind;
839 break;
841 break;
843 case tcc_vl_exp:
844 kind = e_kind;
845 break;
847 default:
848 gcc_unreachable ();
851 tree_code_counts[(int) code]++;
852 tree_node_counts[(int) kind]++;
853 tree_node_sizes[(int) kind] += length;
856 /* Allocate and return a new UID from the DECL_UID namespace. */
859 allocate_decl_uid (void)
861 return next_decl_uid++;
864 /* Return a newly allocated node of code CODE. For decl and type
865 nodes, some other fields are initialized. The rest of the node is
866 initialized to zero. This function cannot be used for TREE_VEC,
867 INTEGER_CST or OMP_CLAUSE nodes, which is enforced by asserts in
868 tree_code_size.
870 Achoo! I got a code in the node. */
872 tree
873 make_node_stat (enum tree_code code MEM_STAT_DECL)
875 tree t;
876 enum tree_code_class type = TREE_CODE_CLASS (code);
877 size_t length = tree_code_size (code);
879 record_node_allocation_statistics (code, length);
881 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
882 TREE_SET_CODE (t, code);
884 switch (type)
886 case tcc_statement:
887 TREE_SIDE_EFFECTS (t) = 1;
888 break;
890 case tcc_declaration:
891 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
893 if (code == FUNCTION_DECL)
895 DECL_ALIGN (t) = FUNCTION_BOUNDARY;
896 DECL_MODE (t) = FUNCTION_MODE;
898 else
899 DECL_ALIGN (t) = 1;
901 DECL_SOURCE_LOCATION (t) = input_location;
902 if (TREE_CODE (t) == DEBUG_EXPR_DECL)
903 DECL_UID (t) = --next_debug_decl_uid;
904 else
906 DECL_UID (t) = allocate_decl_uid ();
907 SET_DECL_PT_UID (t, -1);
909 if (TREE_CODE (t) == LABEL_DECL)
910 LABEL_DECL_UID (t) = -1;
912 break;
914 case tcc_type:
915 TYPE_UID (t) = next_type_uid++;
916 TYPE_ALIGN (t) = BITS_PER_UNIT;
917 TYPE_USER_ALIGN (t) = 0;
918 TYPE_MAIN_VARIANT (t) = t;
919 TYPE_CANONICAL (t) = t;
921 /* Default to no attributes for type, but let target change that. */
922 TYPE_ATTRIBUTES (t) = NULL_TREE;
923 targetm.set_default_type_attributes (t);
925 /* We have not yet computed the alias set for this type. */
926 TYPE_ALIAS_SET (t) = -1;
927 break;
929 case tcc_constant:
930 TREE_CONSTANT (t) = 1;
931 break;
933 case tcc_expression:
934 switch (code)
936 case INIT_EXPR:
937 case MODIFY_EXPR:
938 case VA_ARG_EXPR:
939 case PREDECREMENT_EXPR:
940 case PREINCREMENT_EXPR:
941 case POSTDECREMENT_EXPR:
942 case POSTINCREMENT_EXPR:
943 /* All of these have side-effects, no matter what their
944 operands are. */
945 TREE_SIDE_EFFECTS (t) = 1;
946 break;
948 default:
949 break;
951 break;
953 default:
954 /* Other classes need no special treatment. */
955 break;
958 return t;
961 /* Return a new node with the same contents as NODE except that its
962 TREE_CHAIN, if it has one, is zero and it has a fresh uid. */
964 tree
965 copy_node_stat (tree node MEM_STAT_DECL)
967 tree t;
968 enum tree_code code = TREE_CODE (node);
969 size_t length;
971 gcc_assert (code != STATEMENT_LIST);
973 length = tree_size (node);
974 record_node_allocation_statistics (code, length);
975 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
976 memcpy (t, node, length);
978 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
979 TREE_CHAIN (t) = 0;
980 TREE_ASM_WRITTEN (t) = 0;
981 TREE_VISITED (t) = 0;
983 if (TREE_CODE_CLASS (code) == tcc_declaration)
985 if (code == DEBUG_EXPR_DECL)
986 DECL_UID (t) = --next_debug_decl_uid;
987 else
989 DECL_UID (t) = allocate_decl_uid ();
990 if (DECL_PT_UID_SET_P (node))
991 SET_DECL_PT_UID (t, DECL_PT_UID (node));
993 if ((TREE_CODE (node) == PARM_DECL || TREE_CODE (node) == VAR_DECL)
994 && DECL_HAS_VALUE_EXPR_P (node))
996 SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (node));
997 DECL_HAS_VALUE_EXPR_P (t) = 1;
999 /* DECL_DEBUG_EXPR is copied explicitely by callers. */
1000 if (TREE_CODE (node) == VAR_DECL)
1001 DECL_HAS_DEBUG_EXPR_P (t) = 0;
1002 if (TREE_CODE (node) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (node))
1004 SET_DECL_INIT_PRIORITY (t, DECL_INIT_PRIORITY (node));
1005 DECL_HAS_INIT_PRIORITY_P (t) = 1;
1007 if (TREE_CODE (node) == FUNCTION_DECL)
1008 DECL_STRUCT_FUNCTION (t) = NULL;
1010 else if (TREE_CODE_CLASS (code) == tcc_type)
1012 TYPE_UID (t) = next_type_uid++;
1013 /* The following is so that the debug code for
1014 the copy is different from the original type.
1015 The two statements usually duplicate each other
1016 (because they clear fields of the same union),
1017 but the optimizer should catch that. */
1018 TYPE_SYMTAB_POINTER (t) = 0;
1019 TYPE_SYMTAB_ADDRESS (t) = 0;
1021 /* Do not copy the values cache. */
1022 if (TYPE_CACHED_VALUES_P (t))
1024 TYPE_CACHED_VALUES_P (t) = 0;
1025 TYPE_CACHED_VALUES (t) = NULL_TREE;
1029 return t;
1032 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
1033 For example, this can copy a list made of TREE_LIST nodes. */
1035 tree
1036 copy_list (tree list)
1038 tree head;
1039 tree prev, next;
1041 if (list == 0)
1042 return 0;
1044 head = prev = copy_node (list);
1045 next = TREE_CHAIN (list);
1046 while (next)
1048 TREE_CHAIN (prev) = copy_node (next);
1049 prev = TREE_CHAIN (prev);
1050 next = TREE_CHAIN (next);
1052 return head;
1056 /* Return the value that TREE_INT_CST_EXT_NUNITS should have for an
1057 INTEGER_CST with value CST and type TYPE. */
1059 static unsigned int
1060 get_int_cst_ext_nunits (tree type, const wide_int &cst)
1062 gcc_checking_assert (cst.get_precision () == TYPE_PRECISION (type));
1063 /* We need an extra zero HWI if CST is an unsigned integer with its
1064 upper bit set, and if CST occupies a whole number of HWIs. */
1065 if (TYPE_UNSIGNED (type)
1066 && wi::neg_p (cst)
1067 && (cst.get_precision () % HOST_BITS_PER_WIDE_INT) == 0)
1068 return cst.get_precision () / HOST_BITS_PER_WIDE_INT + 1;
1069 return cst.get_len ();
1072 /* Return a new INTEGER_CST with value CST and type TYPE. */
1074 static tree
1075 build_new_int_cst (tree type, const wide_int &cst)
1077 unsigned int len = cst.get_len ();
1078 unsigned int ext_len = get_int_cst_ext_nunits (type, cst);
1079 tree nt = make_int_cst (len, ext_len);
1081 if (len < ext_len)
1083 --ext_len;
1084 TREE_INT_CST_ELT (nt, ext_len) = 0;
1085 for (unsigned int i = len; i < ext_len; ++i)
1086 TREE_INT_CST_ELT (nt, i) = -1;
1088 else if (TYPE_UNSIGNED (type)
1089 && cst.get_precision () < len * HOST_BITS_PER_WIDE_INT)
1091 len--;
1092 TREE_INT_CST_ELT (nt, len)
1093 = zext_hwi (cst.elt (len),
1094 cst.get_precision () % HOST_BITS_PER_WIDE_INT);
1097 for (unsigned int i = 0; i < len; i++)
1098 TREE_INT_CST_ELT (nt, i) = cst.elt (i);
1099 TREE_TYPE (nt) = type;
1100 return nt;
1103 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1105 tree
1106 build_int_cst (tree type, HOST_WIDE_INT low)
1108 /* Support legacy code. */
1109 if (!type)
1110 type = integer_type_node;
1112 return wide_int_to_tree (type, wi::shwi (low, TYPE_PRECISION (type)));
1115 tree
1116 build_int_cstu (tree type, unsigned HOST_WIDE_INT cst)
1118 return wide_int_to_tree (type, wi::uhwi (cst, TYPE_PRECISION (type)));
1121 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1123 tree
1124 build_int_cst_type (tree type, HOST_WIDE_INT low)
1126 gcc_assert (type);
1127 return wide_int_to_tree (type, wi::shwi (low, TYPE_PRECISION (type)));
1130 /* Constructs tree in type TYPE from with value given by CST. Signedness
1131 of CST is assumed to be the same as the signedness of TYPE. */
1133 tree
1134 double_int_to_tree (tree type, double_int cst)
1136 return wide_int_to_tree (type, widest_int::from (cst, TYPE_SIGN (type)));
1139 /* We force the wide_int CST to the range of the type TYPE by sign or
1140 zero extending it. OVERFLOWABLE indicates if we are interested in
1141 overflow of the value, when >0 we are only interested in signed
1142 overflow, for <0 we are interested in any overflow. OVERFLOWED
1143 indicates whether overflow has already occurred. CONST_OVERFLOWED
1144 indicates whether constant overflow has already occurred. We force
1145 T's value to be within range of T's type (by setting to 0 or 1 all
1146 the bits outside the type's range). We set TREE_OVERFLOWED if,
1147 OVERFLOWED is nonzero,
1148 or OVERFLOWABLE is >0 and signed overflow occurs
1149 or OVERFLOWABLE is <0 and any overflow occurs
1150 We return a new tree node for the extended wide_int. The node
1151 is shared if no overflow flags are set. */
1154 tree
1155 force_fit_type (tree type, const wide_int_ref &cst,
1156 int overflowable, bool overflowed)
1158 signop sign = TYPE_SIGN (type);
1160 /* If we need to set overflow flags, return a new unshared node. */
1161 if (overflowed || !wi::fits_to_tree_p (cst, type))
1163 if (overflowed
1164 || overflowable < 0
1165 || (overflowable > 0 && sign == SIGNED))
1167 wide_int tmp = wide_int::from (cst, TYPE_PRECISION (type), sign);
1168 tree t = build_new_int_cst (type, tmp);
1169 TREE_OVERFLOW (t) = 1;
1170 return t;
1174 /* Else build a shared node. */
1175 return wide_int_to_tree (type, cst);
1178 /* These are the hash table functions for the hash table of INTEGER_CST
1179 nodes of a sizetype. */
1181 /* Return the hash code code X, an INTEGER_CST. */
1183 static hashval_t
1184 int_cst_hash_hash (const void *x)
1186 const_tree const t = (const_tree) x;
1187 hashval_t code = htab_hash_pointer (TREE_TYPE (t));
1188 int i;
1190 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
1191 code ^= TREE_INT_CST_ELT (t, i);
1193 return code;
1196 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
1197 is the same as that given by *Y, which is the same. */
1199 static int
1200 int_cst_hash_eq (const void *x, const void *y)
1202 const_tree const xt = (const_tree) x;
1203 const_tree const yt = (const_tree) y;
1205 if (TREE_TYPE (xt) != TREE_TYPE (yt)
1206 || TREE_INT_CST_NUNITS (xt) != TREE_INT_CST_NUNITS (yt)
1207 || TREE_INT_CST_EXT_NUNITS (xt) != TREE_INT_CST_EXT_NUNITS (yt))
1208 return false;
1210 for (int i = 0; i < TREE_INT_CST_NUNITS (xt); i++)
1211 if (TREE_INT_CST_ELT (xt, i) != TREE_INT_CST_ELT (yt, i))
1212 return false;
1214 return true;
1217 /* Create an INT_CST node of TYPE and value CST.
1218 The returned node is always shared. For small integers we use a
1219 per-type vector cache, for larger ones we use a single hash table.
1220 The value is extended from its precision according to the sign of
1221 the type to be a multiple of HOST_BITS_PER_WIDE_INT. This defines
1222 the upper bits and ensures that hashing and value equality based
1223 upon the underlying HOST_WIDE_INTs works without masking. */
1225 tree
1226 wide_int_to_tree (tree type, const wide_int_ref &pcst)
1228 tree t;
1229 int ix = -1;
1230 int limit = 0;
1232 gcc_assert (type);
1233 unsigned int prec = TYPE_PRECISION (type);
1234 signop sgn = TYPE_SIGN (type);
1236 /* Verify that everything is canonical. */
1237 int l = pcst.get_len ();
1238 if (l > 1)
1240 if (pcst.elt (l - 1) == 0)
1241 gcc_checking_assert (pcst.elt (l - 2) < 0);
1242 if (pcst.elt (l - 1) == (HOST_WIDE_INT) -1)
1243 gcc_checking_assert (pcst.elt (l - 2) >= 0);
1246 wide_int cst = wide_int::from (pcst, prec, sgn);
1247 unsigned int ext_len = get_int_cst_ext_nunits (type, cst);
1249 if (ext_len == 1)
1251 /* We just need to store a single HOST_WIDE_INT. */
1252 HOST_WIDE_INT hwi;
1253 if (TYPE_UNSIGNED (type))
1254 hwi = cst.to_uhwi ();
1255 else
1256 hwi = cst.to_shwi ();
1258 switch (TREE_CODE (type))
1260 case NULLPTR_TYPE:
1261 gcc_assert (hwi == 0);
1262 /* Fallthru. */
1264 case POINTER_TYPE:
1265 case REFERENCE_TYPE:
1266 /* Cache NULL pointer. */
1267 if (hwi == 0)
1269 limit = 1;
1270 ix = 0;
1272 break;
1274 case BOOLEAN_TYPE:
1275 /* Cache false or true. */
1276 limit = 2;
1277 if (hwi < 2)
1278 ix = hwi;
1279 break;
1281 case INTEGER_TYPE:
1282 case OFFSET_TYPE:
1283 if (TYPE_SIGN (type) == UNSIGNED)
1285 /* Cache [0, N). */
1286 limit = INTEGER_SHARE_LIMIT;
1287 if (IN_RANGE (hwi, 0, INTEGER_SHARE_LIMIT - 1))
1288 ix = hwi;
1290 else
1292 /* Cache [-1, N). */
1293 limit = INTEGER_SHARE_LIMIT + 1;
1294 if (IN_RANGE (hwi, -1, INTEGER_SHARE_LIMIT - 1))
1295 ix = hwi + 1;
1297 break;
1299 case ENUMERAL_TYPE:
1300 break;
1302 default:
1303 gcc_unreachable ();
1306 if (ix >= 0)
1308 /* Look for it in the type's vector of small shared ints. */
1309 if (!TYPE_CACHED_VALUES_P (type))
1311 TYPE_CACHED_VALUES_P (type) = 1;
1312 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1315 t = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix);
1316 if (t)
1317 /* Make sure no one is clobbering the shared constant. */
1318 gcc_checking_assert (TREE_TYPE (t) == type
1319 && TREE_INT_CST_NUNITS (t) == 1
1320 && TREE_INT_CST_OFFSET_NUNITS (t) == 1
1321 && TREE_INT_CST_EXT_NUNITS (t) == 1
1322 && TREE_INT_CST_ELT (t, 0) == hwi);
1323 else
1325 /* Create a new shared int. */
1326 t = build_new_int_cst (type, cst);
1327 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1330 else
1332 /* Use the cache of larger shared ints, using int_cst_node as
1333 a temporary. */
1334 void **slot;
1336 TREE_INT_CST_ELT (int_cst_node, 0) = hwi;
1337 TREE_TYPE (int_cst_node) = type;
1339 slot = htab_find_slot (int_cst_hash_table, int_cst_node, INSERT);
1340 t = (tree) *slot;
1341 if (!t)
1343 /* Insert this one into the hash table. */
1344 t = int_cst_node;
1345 *slot = t;
1346 /* Make a new node for next time round. */
1347 int_cst_node = make_int_cst (1, 1);
1351 else
1353 /* The value either hashes properly or we drop it on the floor
1354 for the gc to take care of. There will not be enough of them
1355 to worry about. */
1356 void **slot;
1358 tree nt = build_new_int_cst (type, cst);
1359 slot = htab_find_slot (int_cst_hash_table, nt, INSERT);
1360 t = (tree) *slot;
1361 if (!t)
1363 /* Insert this one into the hash table. */
1364 t = nt;
1365 *slot = t;
1369 return t;
1372 void
1373 cache_integer_cst (tree t)
1375 tree type = TREE_TYPE (t);
1376 int ix = -1;
1377 int limit = 0;
1378 int prec = TYPE_PRECISION (type);
1380 gcc_assert (!TREE_OVERFLOW (t));
1382 switch (TREE_CODE (type))
1384 case NULLPTR_TYPE:
1385 gcc_assert (integer_zerop (t));
1386 /* Fallthru. */
1388 case POINTER_TYPE:
1389 case REFERENCE_TYPE:
1390 /* Cache NULL pointer. */
1391 if (integer_zerop (t))
1393 limit = 1;
1394 ix = 0;
1396 break;
1398 case BOOLEAN_TYPE:
1399 /* Cache false or true. */
1400 limit = 2;
1401 if (wi::ltu_p (t, 2))
1402 ix = TREE_INT_CST_ELT (t, 0);
1403 break;
1405 case INTEGER_TYPE:
1406 case OFFSET_TYPE:
1407 if (TYPE_UNSIGNED (type))
1409 /* Cache 0..N */
1410 limit = INTEGER_SHARE_LIMIT;
1412 /* This is a little hokie, but if the prec is smaller than
1413 what is necessary to hold INTEGER_SHARE_LIMIT, then the
1414 obvious test will not get the correct answer. */
1415 if (prec < HOST_BITS_PER_WIDE_INT)
1417 if (tree_to_uhwi (t) < (unsigned HOST_WIDE_INT) INTEGER_SHARE_LIMIT)
1418 ix = tree_to_uhwi (t);
1420 else if (wi::ltu_p (t, INTEGER_SHARE_LIMIT))
1421 ix = tree_to_uhwi (t);
1423 else
1425 /* Cache -1..N */
1426 limit = INTEGER_SHARE_LIMIT + 1;
1428 if (integer_minus_onep (t))
1429 ix = 0;
1430 else if (!wi::neg_p (t))
1432 if (prec < HOST_BITS_PER_WIDE_INT)
1434 if (tree_to_shwi (t) < INTEGER_SHARE_LIMIT)
1435 ix = tree_to_shwi (t) + 1;
1437 else if (wi::ltu_p (t, INTEGER_SHARE_LIMIT))
1438 ix = tree_to_shwi (t) + 1;
1441 break;
1443 case ENUMERAL_TYPE:
1444 break;
1446 default:
1447 gcc_unreachable ();
1450 if (ix >= 0)
1452 /* Look for it in the type's vector of small shared ints. */
1453 if (!TYPE_CACHED_VALUES_P (type))
1455 TYPE_CACHED_VALUES_P (type) = 1;
1456 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1459 gcc_assert (TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) == NULL_TREE);
1460 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1462 else
1464 /* Use the cache of larger shared ints. */
1465 void **slot;
1467 slot = htab_find_slot (int_cst_hash_table, t, INSERT);
1468 /* If there is already an entry for the number verify it's the
1469 same. */
1470 if (*slot)
1471 gcc_assert (wi::eq_p (tree (*slot), t));
1472 else
1473 /* Otherwise insert this one into the hash table. */
1474 *slot = t;
1479 /* Builds an integer constant in TYPE such that lowest BITS bits are ones
1480 and the rest are zeros. */
1482 tree
1483 build_low_bits_mask (tree type, unsigned bits)
1485 gcc_assert (bits <= TYPE_PRECISION (type));
1487 return wide_int_to_tree (type, wi::mask (bits, false,
1488 TYPE_PRECISION (type)));
1491 /* Checks that X is integer constant that can be expressed in (unsigned)
1492 HOST_WIDE_INT without loss of precision. */
1494 bool
1495 cst_and_fits_in_hwi (const_tree x)
1497 if (TREE_CODE (x) != INTEGER_CST)
1498 return false;
1500 if (TYPE_PRECISION (TREE_TYPE (x)) > HOST_BITS_PER_WIDE_INT)
1501 return false;
1503 return TREE_INT_CST_NUNITS (x) == 1;
1506 /* Build a newly constructed TREE_VEC node of length LEN. */
1508 tree
1509 make_vector_stat (unsigned len MEM_STAT_DECL)
1511 tree t;
1512 unsigned length = (len - 1) * sizeof (tree) + sizeof (struct tree_vector);
1514 record_node_allocation_statistics (VECTOR_CST, length);
1516 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1518 TREE_SET_CODE (t, VECTOR_CST);
1519 TREE_CONSTANT (t) = 1;
1521 return t;
1524 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1525 are in a list pointed to by VALS. */
1527 tree
1528 build_vector_stat (tree type, tree *vals MEM_STAT_DECL)
1530 int over = 0;
1531 unsigned cnt = 0;
1532 tree v = make_vector (TYPE_VECTOR_SUBPARTS (type));
1533 TREE_TYPE (v) = type;
1535 /* Iterate through elements and check for overflow. */
1536 for (cnt = 0; cnt < TYPE_VECTOR_SUBPARTS (type); ++cnt)
1538 tree value = vals[cnt];
1540 VECTOR_CST_ELT (v, cnt) = value;
1542 /* Don't crash if we get an address constant. */
1543 if (!CONSTANT_CLASS_P (value))
1544 continue;
1546 over |= TREE_OVERFLOW (value);
1549 TREE_OVERFLOW (v) = over;
1550 return v;
1553 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1554 are extracted from V, a vector of CONSTRUCTOR_ELT. */
1556 tree
1557 build_vector_from_ctor (tree type, vec<constructor_elt, va_gc> *v)
1559 tree *vec = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
1560 unsigned HOST_WIDE_INT idx;
1561 tree value;
1563 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1564 vec[idx] = value;
1565 for (; idx < TYPE_VECTOR_SUBPARTS (type); ++idx)
1566 vec[idx] = build_zero_cst (TREE_TYPE (type));
1568 return build_vector (type, vec);
1571 /* Build a vector of type VECTYPE where all the elements are SCs. */
1572 tree
1573 build_vector_from_val (tree vectype, tree sc)
1575 int i, nunits = TYPE_VECTOR_SUBPARTS (vectype);
1577 if (sc == error_mark_node)
1578 return sc;
1580 /* Verify that the vector type is suitable for SC. Note that there
1581 is some inconsistency in the type-system with respect to restrict
1582 qualifications of pointers. Vector types always have a main-variant
1583 element type and the qualification is applied to the vector-type.
1584 So TREE_TYPE (vector-type) does not return a properly qualified
1585 vector element-type. */
1586 gcc_checking_assert (types_compatible_p (TYPE_MAIN_VARIANT (TREE_TYPE (sc)),
1587 TREE_TYPE (vectype)));
1589 if (CONSTANT_CLASS_P (sc))
1591 tree *v = XALLOCAVEC (tree, nunits);
1592 for (i = 0; i < nunits; ++i)
1593 v[i] = sc;
1594 return build_vector (vectype, v);
1596 else
1598 vec<constructor_elt, va_gc> *v;
1599 vec_alloc (v, nunits);
1600 for (i = 0; i < nunits; ++i)
1601 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, sc);
1602 return build_constructor (vectype, v);
1606 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1607 are in the vec pointed to by VALS. */
1608 tree
1609 build_constructor (tree type, vec<constructor_elt, va_gc> *vals)
1611 tree c = make_node (CONSTRUCTOR);
1612 unsigned int i;
1613 constructor_elt *elt;
1614 bool constant_p = true;
1615 bool side_effects_p = false;
1617 TREE_TYPE (c) = type;
1618 CONSTRUCTOR_ELTS (c) = vals;
1620 FOR_EACH_VEC_SAFE_ELT (vals, i, elt)
1622 /* Mostly ctors will have elts that don't have side-effects, so
1623 the usual case is to scan all the elements. Hence a single
1624 loop for both const and side effects, rather than one loop
1625 each (with early outs). */
1626 if (!TREE_CONSTANT (elt->value))
1627 constant_p = false;
1628 if (TREE_SIDE_EFFECTS (elt->value))
1629 side_effects_p = true;
1632 TREE_SIDE_EFFECTS (c) = side_effects_p;
1633 TREE_CONSTANT (c) = constant_p;
1635 return c;
1638 /* Build a CONSTRUCTOR node made of a single initializer, with the specified
1639 INDEX and VALUE. */
1640 tree
1641 build_constructor_single (tree type, tree index, tree value)
1643 vec<constructor_elt, va_gc> *v;
1644 constructor_elt elt = {index, value};
1646 vec_alloc (v, 1);
1647 v->quick_push (elt);
1649 return build_constructor (type, v);
1653 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1654 are in a list pointed to by VALS. */
1655 tree
1656 build_constructor_from_list (tree type, tree vals)
1658 tree t;
1659 vec<constructor_elt, va_gc> *v = NULL;
1661 if (vals)
1663 vec_alloc (v, list_length (vals));
1664 for (t = vals; t; t = TREE_CHAIN (t))
1665 CONSTRUCTOR_APPEND_ELT (v, TREE_PURPOSE (t), TREE_VALUE (t));
1668 return build_constructor (type, v);
1671 /* Return a new CONSTRUCTOR node whose type is TYPE. NELTS is the number
1672 of elements, provided as index/value pairs. */
1674 tree
1675 build_constructor_va (tree type, int nelts, ...)
1677 vec<constructor_elt, va_gc> *v = NULL;
1678 va_list p;
1680 va_start (p, nelts);
1681 vec_alloc (v, nelts);
1682 while (nelts--)
1684 tree index = va_arg (p, tree);
1685 tree value = va_arg (p, tree);
1686 CONSTRUCTOR_APPEND_ELT (v, index, value);
1688 va_end (p);
1689 return build_constructor (type, v);
1692 /* Return a new FIXED_CST node whose type is TYPE and value is F. */
1694 tree
1695 build_fixed (tree type, FIXED_VALUE_TYPE f)
1697 tree v;
1698 FIXED_VALUE_TYPE *fp;
1700 v = make_node (FIXED_CST);
1701 fp = ggc_alloc<fixed_value> ();
1702 memcpy (fp, &f, sizeof (FIXED_VALUE_TYPE));
1704 TREE_TYPE (v) = type;
1705 TREE_FIXED_CST_PTR (v) = fp;
1706 return v;
1709 /* Return a new REAL_CST node whose type is TYPE and value is D. */
1711 tree
1712 build_real (tree type, REAL_VALUE_TYPE d)
1714 tree v;
1715 REAL_VALUE_TYPE *dp;
1716 int overflow = 0;
1718 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
1719 Consider doing it via real_convert now. */
1721 v = make_node (REAL_CST);
1722 dp = ggc_alloc<real_value> ();
1723 memcpy (dp, &d, sizeof (REAL_VALUE_TYPE));
1725 TREE_TYPE (v) = type;
1726 TREE_REAL_CST_PTR (v) = dp;
1727 TREE_OVERFLOW (v) = overflow;
1728 return v;
1731 /* Return a new REAL_CST node whose type is TYPE
1732 and whose value is the integer value of the INTEGER_CST node I. */
1734 REAL_VALUE_TYPE
1735 real_value_from_int_cst (const_tree type, const_tree i)
1737 REAL_VALUE_TYPE d;
1739 /* Clear all bits of the real value type so that we can later do
1740 bitwise comparisons to see if two values are the same. */
1741 memset (&d, 0, sizeof d);
1743 real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode, i,
1744 TYPE_SIGN (TREE_TYPE (i)));
1745 return d;
1748 /* Given a tree representing an integer constant I, return a tree
1749 representing the same value as a floating-point constant of type TYPE. */
1751 tree
1752 build_real_from_int_cst (tree type, const_tree i)
1754 tree v;
1755 int overflow = TREE_OVERFLOW (i);
1757 v = build_real (type, real_value_from_int_cst (type, i));
1759 TREE_OVERFLOW (v) |= overflow;
1760 return v;
1763 /* Return a newly constructed STRING_CST node whose value is
1764 the LEN characters at STR.
1765 Note that for a C string literal, LEN should include the trailing NUL.
1766 The TREE_TYPE is not initialized. */
1768 tree
1769 build_string (int len, const char *str)
1771 tree s;
1772 size_t length;
1774 /* Do not waste bytes provided by padding of struct tree_string. */
1775 length = len + offsetof (struct tree_string, str) + 1;
1777 record_node_allocation_statistics (STRING_CST, length);
1779 s = (tree) ggc_internal_alloc (length);
1781 memset (s, 0, sizeof (struct tree_typed));
1782 TREE_SET_CODE (s, STRING_CST);
1783 TREE_CONSTANT (s) = 1;
1784 TREE_STRING_LENGTH (s) = len;
1785 memcpy (s->string.str, str, len);
1786 s->string.str[len] = '\0';
1788 return s;
1791 /* Return a newly constructed COMPLEX_CST node whose value is
1792 specified by the real and imaginary parts REAL and IMAG.
1793 Both REAL and IMAG should be constant nodes. TYPE, if specified,
1794 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
1796 tree
1797 build_complex (tree type, tree real, tree imag)
1799 tree t = make_node (COMPLEX_CST);
1801 TREE_REALPART (t) = real;
1802 TREE_IMAGPART (t) = imag;
1803 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
1804 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
1805 return t;
1808 /* Return a constant of arithmetic type TYPE which is the
1809 multiplicative identity of the set TYPE. */
1811 tree
1812 build_one_cst (tree type)
1814 switch (TREE_CODE (type))
1816 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1817 case POINTER_TYPE: case REFERENCE_TYPE:
1818 case OFFSET_TYPE:
1819 return build_int_cst (type, 1);
1821 case REAL_TYPE:
1822 return build_real (type, dconst1);
1824 case FIXED_POINT_TYPE:
1825 /* We can only generate 1 for accum types. */
1826 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
1827 return build_fixed (type, FCONST1 (TYPE_MODE (type)));
1829 case VECTOR_TYPE:
1831 tree scalar = build_one_cst (TREE_TYPE (type));
1833 return build_vector_from_val (type, scalar);
1836 case COMPLEX_TYPE:
1837 return build_complex (type,
1838 build_one_cst (TREE_TYPE (type)),
1839 build_zero_cst (TREE_TYPE (type)));
1841 default:
1842 gcc_unreachable ();
1846 /* Return an integer of type TYPE containing all 1's in as much precision as
1847 it contains, or a complex or vector whose subparts are such integers. */
1849 tree
1850 build_all_ones_cst (tree type)
1852 if (TREE_CODE (type) == COMPLEX_TYPE)
1854 tree scalar = build_all_ones_cst (TREE_TYPE (type));
1855 return build_complex (type, scalar, scalar);
1857 else
1858 return build_minus_one_cst (type);
1861 /* Return a constant of arithmetic type TYPE which is the
1862 opposite of the multiplicative identity of the set TYPE. */
1864 tree
1865 build_minus_one_cst (tree type)
1867 switch (TREE_CODE (type))
1869 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1870 case POINTER_TYPE: case REFERENCE_TYPE:
1871 case OFFSET_TYPE:
1872 return build_int_cst (type, -1);
1874 case REAL_TYPE:
1875 return build_real (type, dconstm1);
1877 case FIXED_POINT_TYPE:
1878 /* We can only generate 1 for accum types. */
1879 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
1880 return build_fixed (type, fixed_from_double_int (double_int_minus_one,
1881 TYPE_MODE (type)));
1883 case VECTOR_TYPE:
1885 tree scalar = build_minus_one_cst (TREE_TYPE (type));
1887 return build_vector_from_val (type, scalar);
1890 case COMPLEX_TYPE:
1891 return build_complex (type,
1892 build_minus_one_cst (TREE_TYPE (type)),
1893 build_zero_cst (TREE_TYPE (type)));
1895 default:
1896 gcc_unreachable ();
1900 /* Build 0 constant of type TYPE. This is used by constructor folding
1901 and thus the constant should be represented in memory by
1902 zero(es). */
1904 tree
1905 build_zero_cst (tree type)
1907 switch (TREE_CODE (type))
1909 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1910 case POINTER_TYPE: case REFERENCE_TYPE:
1911 case OFFSET_TYPE: case NULLPTR_TYPE:
1912 return build_int_cst (type, 0);
1914 case REAL_TYPE:
1915 return build_real (type, dconst0);
1917 case FIXED_POINT_TYPE:
1918 return build_fixed (type, FCONST0 (TYPE_MODE (type)));
1920 case VECTOR_TYPE:
1922 tree scalar = build_zero_cst (TREE_TYPE (type));
1924 return build_vector_from_val (type, scalar);
1927 case COMPLEX_TYPE:
1929 tree zero = build_zero_cst (TREE_TYPE (type));
1931 return build_complex (type, zero, zero);
1934 default:
1935 if (!AGGREGATE_TYPE_P (type))
1936 return fold_convert (type, integer_zero_node);
1937 return build_constructor (type, NULL);
1942 /* Build a BINFO with LEN language slots. */
1944 tree
1945 make_tree_binfo_stat (unsigned base_binfos MEM_STAT_DECL)
1947 tree t;
1948 size_t length = (offsetof (struct tree_binfo, base_binfos)
1949 + vec<tree, va_gc>::embedded_size (base_binfos));
1951 record_node_allocation_statistics (TREE_BINFO, length);
1953 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
1955 memset (t, 0, offsetof (struct tree_binfo, base_binfos));
1957 TREE_SET_CODE (t, TREE_BINFO);
1959 BINFO_BASE_BINFOS (t)->embedded_init (base_binfos);
1961 return t;
1964 /* Create a CASE_LABEL_EXPR tree node and return it. */
1966 tree
1967 build_case_label (tree low_value, tree high_value, tree label_decl)
1969 tree t = make_node (CASE_LABEL_EXPR);
1971 TREE_TYPE (t) = void_type_node;
1972 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (label_decl));
1974 CASE_LOW (t) = low_value;
1975 CASE_HIGH (t) = high_value;
1976 CASE_LABEL (t) = label_decl;
1977 CASE_CHAIN (t) = NULL_TREE;
1979 return t;
1982 /* Build a newly constructed INTEGER_CST node. LEN and EXT_LEN are the
1983 values of TREE_INT_CST_NUNITS and TREE_INT_CST_EXT_NUNITS respectively.
1984 The latter determines the length of the HOST_WIDE_INT vector. */
1986 tree
1987 make_int_cst_stat (int len, int ext_len MEM_STAT_DECL)
1989 tree t;
1990 int length = ((ext_len - 1) * sizeof (HOST_WIDE_INT)
1991 + sizeof (struct tree_int_cst));
1993 gcc_assert (len);
1994 record_node_allocation_statistics (INTEGER_CST, length);
1996 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1998 TREE_SET_CODE (t, INTEGER_CST);
1999 TREE_INT_CST_NUNITS (t) = len;
2000 TREE_INT_CST_EXT_NUNITS (t) = ext_len;
2001 /* to_offset can only be applied to trees that are offset_int-sized
2002 or smaller. EXT_LEN is correct if it fits, otherwise the constant
2003 must be exactly the precision of offset_int and so LEN is correct. */
2004 if (ext_len <= OFFSET_INT_ELTS)
2005 TREE_INT_CST_OFFSET_NUNITS (t) = ext_len;
2006 else
2007 TREE_INT_CST_OFFSET_NUNITS (t) = len;
2009 TREE_CONSTANT (t) = 1;
2011 return t;
2014 /* Build a newly constructed TREE_VEC node of length LEN. */
2016 tree
2017 make_tree_vec_stat (int len MEM_STAT_DECL)
2019 tree t;
2020 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
2022 record_node_allocation_statistics (TREE_VEC, length);
2024 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2026 TREE_SET_CODE (t, TREE_VEC);
2027 TREE_VEC_LENGTH (t) = len;
2029 return t;
2032 /* Grow a TREE_VEC node to new length LEN. */
2034 tree
2035 grow_tree_vec_stat (tree v, int len MEM_STAT_DECL)
2037 gcc_assert (TREE_CODE (v) == TREE_VEC);
2039 int oldlen = TREE_VEC_LENGTH (v);
2040 gcc_assert (len > oldlen);
2042 int oldlength = (oldlen - 1) * sizeof (tree) + sizeof (struct tree_vec);
2043 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
2045 record_node_allocation_statistics (TREE_VEC, length - oldlength);
2047 v = (tree) ggc_realloc (v, length PASS_MEM_STAT);
2049 TREE_VEC_LENGTH (v) = len;
2051 return v;
2054 /* Return 1 if EXPR is the integer constant zero or a complex constant
2055 of zero. */
2058 integer_zerop (const_tree expr)
2060 STRIP_NOPS (expr);
2062 switch (TREE_CODE (expr))
2064 case INTEGER_CST:
2065 return wi::eq_p (expr, 0);
2066 case COMPLEX_CST:
2067 return (integer_zerop (TREE_REALPART (expr))
2068 && integer_zerop (TREE_IMAGPART (expr)));
2069 case VECTOR_CST:
2071 unsigned i;
2072 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2073 if (!integer_zerop (VECTOR_CST_ELT (expr, i)))
2074 return false;
2075 return true;
2077 default:
2078 return false;
2082 /* Return 1 if EXPR is the integer constant one or the corresponding
2083 complex constant. */
2086 integer_onep (const_tree expr)
2088 STRIP_NOPS (expr);
2090 switch (TREE_CODE (expr))
2092 case INTEGER_CST:
2093 return wi::eq_p (wi::to_widest (expr), 1);
2094 case COMPLEX_CST:
2095 return (integer_onep (TREE_REALPART (expr))
2096 && integer_zerop (TREE_IMAGPART (expr)));
2097 case VECTOR_CST:
2099 unsigned i;
2100 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2101 if (!integer_onep (VECTOR_CST_ELT (expr, i)))
2102 return false;
2103 return true;
2105 default:
2106 return false;
2110 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
2111 it contains, or a complex or vector whose subparts are such integers. */
2114 integer_all_onesp (const_tree expr)
2116 STRIP_NOPS (expr);
2118 if (TREE_CODE (expr) == COMPLEX_CST
2119 && integer_all_onesp (TREE_REALPART (expr))
2120 && integer_all_onesp (TREE_IMAGPART (expr)))
2121 return 1;
2123 else if (TREE_CODE (expr) == VECTOR_CST)
2125 unsigned i;
2126 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2127 if (!integer_all_onesp (VECTOR_CST_ELT (expr, i)))
2128 return 0;
2129 return 1;
2132 else if (TREE_CODE (expr) != INTEGER_CST)
2133 return 0;
2135 return wi::max_value (TYPE_PRECISION (TREE_TYPE (expr)), UNSIGNED) == expr;
2138 /* Return 1 if EXPR is the integer constant minus one. */
2141 integer_minus_onep (const_tree expr)
2143 STRIP_NOPS (expr);
2145 if (TREE_CODE (expr) == COMPLEX_CST)
2146 return (integer_all_onesp (TREE_REALPART (expr))
2147 && integer_zerop (TREE_IMAGPART (expr)));
2148 else
2149 return integer_all_onesp (expr);
2152 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
2153 one bit on). */
2156 integer_pow2p (const_tree expr)
2158 STRIP_NOPS (expr);
2160 if (TREE_CODE (expr) == COMPLEX_CST
2161 && integer_pow2p (TREE_REALPART (expr))
2162 && integer_zerop (TREE_IMAGPART (expr)))
2163 return 1;
2165 if (TREE_CODE (expr) != INTEGER_CST)
2166 return 0;
2168 return wi::popcount (expr) == 1;
2171 /* Return 1 if EXPR is an integer constant other than zero or a
2172 complex constant other than zero. */
2175 integer_nonzerop (const_tree expr)
2177 STRIP_NOPS (expr);
2179 return ((TREE_CODE (expr) == INTEGER_CST
2180 && !wi::eq_p (expr, 0))
2181 || (TREE_CODE (expr) == COMPLEX_CST
2182 && (integer_nonzerop (TREE_REALPART (expr))
2183 || integer_nonzerop (TREE_IMAGPART (expr)))));
2186 /* Return 1 if EXPR is the fixed-point constant zero. */
2189 fixed_zerop (const_tree expr)
2191 return (TREE_CODE (expr) == FIXED_CST
2192 && TREE_FIXED_CST (expr).data.is_zero ());
2195 /* Return the power of two represented by a tree node known to be a
2196 power of two. */
2199 tree_log2 (const_tree expr)
2201 STRIP_NOPS (expr);
2203 if (TREE_CODE (expr) == COMPLEX_CST)
2204 return tree_log2 (TREE_REALPART (expr));
2206 return wi::exact_log2 (expr);
2209 /* Similar, but return the largest integer Y such that 2 ** Y is less
2210 than or equal to EXPR. */
2213 tree_floor_log2 (const_tree expr)
2215 STRIP_NOPS (expr);
2217 if (TREE_CODE (expr) == COMPLEX_CST)
2218 return tree_log2 (TREE_REALPART (expr));
2220 return wi::floor_log2 (expr);
2223 /* Return number of known trailing zero bits in EXPR, or, if the value of
2224 EXPR is known to be zero, the precision of it's type. */
2226 unsigned int
2227 tree_ctz (const_tree expr)
2229 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
2230 && !POINTER_TYPE_P (TREE_TYPE (expr)))
2231 return 0;
2233 unsigned int ret1, ret2, prec = TYPE_PRECISION (TREE_TYPE (expr));
2234 switch (TREE_CODE (expr))
2236 case INTEGER_CST:
2237 ret1 = wi::ctz (expr);
2238 return MIN (ret1, prec);
2239 case SSA_NAME:
2240 ret1 = wi::ctz (get_nonzero_bits (expr));
2241 return MIN (ret1, prec);
2242 case PLUS_EXPR:
2243 case MINUS_EXPR:
2244 case BIT_IOR_EXPR:
2245 case BIT_XOR_EXPR:
2246 case MIN_EXPR:
2247 case MAX_EXPR:
2248 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2249 if (ret1 == 0)
2250 return ret1;
2251 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2252 return MIN (ret1, ret2);
2253 case POINTER_PLUS_EXPR:
2254 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2255 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2256 /* Second operand is sizetype, which could be in theory
2257 wider than pointer's precision. Make sure we never
2258 return more than prec. */
2259 ret2 = MIN (ret2, prec);
2260 return MIN (ret1, ret2);
2261 case BIT_AND_EXPR:
2262 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2263 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2264 return MAX (ret1, ret2);
2265 case MULT_EXPR:
2266 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2267 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2268 return MIN (ret1 + ret2, prec);
2269 case LSHIFT_EXPR:
2270 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2271 if (tree_fits_uhwi_p (TREE_OPERAND (expr, 1))
2272 && (tree_to_uhwi (TREE_OPERAND (expr, 1)) < prec))
2274 ret2 = tree_to_uhwi (TREE_OPERAND (expr, 1));
2275 return MIN (ret1 + ret2, prec);
2277 return ret1;
2278 case RSHIFT_EXPR:
2279 if (tree_fits_uhwi_p (TREE_OPERAND (expr, 1))
2280 && (tree_to_uhwi (TREE_OPERAND (expr, 1)) < prec))
2282 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2283 ret2 = tree_to_uhwi (TREE_OPERAND (expr, 1));
2284 if (ret1 > ret2)
2285 return ret1 - ret2;
2287 return 0;
2288 case TRUNC_DIV_EXPR:
2289 case CEIL_DIV_EXPR:
2290 case FLOOR_DIV_EXPR:
2291 case ROUND_DIV_EXPR:
2292 case EXACT_DIV_EXPR:
2293 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
2294 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) == 1)
2296 int l = tree_log2 (TREE_OPERAND (expr, 1));
2297 if (l >= 0)
2299 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2300 ret2 = l;
2301 if (ret1 > ret2)
2302 return ret1 - ret2;
2305 return 0;
2306 CASE_CONVERT:
2307 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2308 if (ret1 && ret1 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2309 ret1 = prec;
2310 return MIN (ret1, prec);
2311 case SAVE_EXPR:
2312 return tree_ctz (TREE_OPERAND (expr, 0));
2313 case COND_EXPR:
2314 ret1 = tree_ctz (TREE_OPERAND (expr, 1));
2315 if (ret1 == 0)
2316 return 0;
2317 ret2 = tree_ctz (TREE_OPERAND (expr, 2));
2318 return MIN (ret1, ret2);
2319 case COMPOUND_EXPR:
2320 return tree_ctz (TREE_OPERAND (expr, 1));
2321 case ADDR_EXPR:
2322 ret1 = get_pointer_alignment (CONST_CAST_TREE (expr));
2323 if (ret1 > BITS_PER_UNIT)
2325 ret1 = ctz_hwi (ret1 / BITS_PER_UNIT);
2326 return MIN (ret1, prec);
2328 return 0;
2329 default:
2330 return 0;
2334 /* Return 1 if EXPR is the real constant zero. Trailing zeroes matter for
2335 decimal float constants, so don't return 1 for them. */
2338 real_zerop (const_tree expr)
2340 STRIP_NOPS (expr);
2342 switch (TREE_CODE (expr))
2344 case REAL_CST:
2345 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0)
2346 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2347 case COMPLEX_CST:
2348 return real_zerop (TREE_REALPART (expr))
2349 && real_zerop (TREE_IMAGPART (expr));
2350 case VECTOR_CST:
2352 unsigned i;
2353 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2354 if (!real_zerop (VECTOR_CST_ELT (expr, i)))
2355 return false;
2356 return true;
2358 default:
2359 return false;
2363 /* Return 1 if EXPR is the real constant one in real or complex form.
2364 Trailing zeroes matter for decimal float constants, so don't return
2365 1 for them. */
2368 real_onep (const_tree expr)
2370 STRIP_NOPS (expr);
2372 switch (TREE_CODE (expr))
2374 case REAL_CST:
2375 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1)
2376 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2377 case COMPLEX_CST:
2378 return real_onep (TREE_REALPART (expr))
2379 && real_zerop (TREE_IMAGPART (expr));
2380 case VECTOR_CST:
2382 unsigned i;
2383 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2384 if (!real_onep (VECTOR_CST_ELT (expr, i)))
2385 return false;
2386 return true;
2388 default:
2389 return false;
2393 /* Return 1 if EXPR is the real constant minus one. Trailing zeroes
2394 matter for decimal float constants, so don't return 1 for them. */
2397 real_minus_onep (const_tree expr)
2399 STRIP_NOPS (expr);
2401 switch (TREE_CODE (expr))
2403 case REAL_CST:
2404 return REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconstm1)
2405 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2406 case COMPLEX_CST:
2407 return real_minus_onep (TREE_REALPART (expr))
2408 && real_zerop (TREE_IMAGPART (expr));
2409 case VECTOR_CST:
2411 unsigned i;
2412 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2413 if (!real_minus_onep (VECTOR_CST_ELT (expr, i)))
2414 return false;
2415 return true;
2417 default:
2418 return false;
2422 /* Nonzero if EXP is a constant or a cast of a constant. */
2425 really_constant_p (const_tree exp)
2427 /* This is not quite the same as STRIP_NOPS. It does more. */
2428 while (CONVERT_EXPR_P (exp)
2429 || TREE_CODE (exp) == NON_LVALUE_EXPR)
2430 exp = TREE_OPERAND (exp, 0);
2431 return TREE_CONSTANT (exp);
2434 /* Return first list element whose TREE_VALUE is ELEM.
2435 Return 0 if ELEM is not in LIST. */
2437 tree
2438 value_member (tree elem, tree list)
2440 while (list)
2442 if (elem == TREE_VALUE (list))
2443 return list;
2444 list = TREE_CHAIN (list);
2446 return NULL_TREE;
2449 /* Return first list element whose TREE_PURPOSE is ELEM.
2450 Return 0 if ELEM is not in LIST. */
2452 tree
2453 purpose_member (const_tree elem, tree list)
2455 while (list)
2457 if (elem == TREE_PURPOSE (list))
2458 return list;
2459 list = TREE_CHAIN (list);
2461 return NULL_TREE;
2464 /* Return true if ELEM is in V. */
2466 bool
2467 vec_member (const_tree elem, vec<tree, va_gc> *v)
2469 unsigned ix;
2470 tree t;
2471 FOR_EACH_VEC_SAFE_ELT (v, ix, t)
2472 if (elem == t)
2473 return true;
2474 return false;
2477 /* Returns element number IDX (zero-origin) of chain CHAIN, or
2478 NULL_TREE. */
2480 tree
2481 chain_index (int idx, tree chain)
2483 for (; chain && idx > 0; --idx)
2484 chain = TREE_CHAIN (chain);
2485 return chain;
2488 /* Return nonzero if ELEM is part of the chain CHAIN. */
2491 chain_member (const_tree elem, const_tree chain)
2493 while (chain)
2495 if (elem == chain)
2496 return 1;
2497 chain = DECL_CHAIN (chain);
2500 return 0;
2503 /* Return the length of a chain of nodes chained through TREE_CHAIN.
2504 We expect a null pointer to mark the end of the chain.
2505 This is the Lisp primitive `length'. */
2508 list_length (const_tree t)
2510 const_tree p = t;
2511 #ifdef ENABLE_TREE_CHECKING
2512 const_tree q = t;
2513 #endif
2514 int len = 0;
2516 while (p)
2518 p = TREE_CHAIN (p);
2519 #ifdef ENABLE_TREE_CHECKING
2520 if (len % 2)
2521 q = TREE_CHAIN (q);
2522 gcc_assert (p != q);
2523 #endif
2524 len++;
2527 return len;
2530 /* Returns the first FIELD_DECL in the TYPE_FIELDS of the RECORD_TYPE or
2531 UNION_TYPE TYPE, or NULL_TREE if none. */
2533 tree
2534 first_field (const_tree type)
2536 tree t = TYPE_FIELDS (type);
2537 while (t && TREE_CODE (t) != FIELD_DECL)
2538 t = TREE_CHAIN (t);
2539 return t;
2542 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
2543 by modifying the last node in chain 1 to point to chain 2.
2544 This is the Lisp primitive `nconc'. */
2546 tree
2547 chainon (tree op1, tree op2)
2549 tree t1;
2551 if (!op1)
2552 return op2;
2553 if (!op2)
2554 return op1;
2556 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
2557 continue;
2558 TREE_CHAIN (t1) = op2;
2560 #ifdef ENABLE_TREE_CHECKING
2562 tree t2;
2563 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
2564 gcc_assert (t2 != t1);
2566 #endif
2568 return op1;
2571 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
2573 tree
2574 tree_last (tree chain)
2576 tree next;
2577 if (chain)
2578 while ((next = TREE_CHAIN (chain)))
2579 chain = next;
2580 return chain;
2583 /* Reverse the order of elements in the chain T,
2584 and return the new head of the chain (old last element). */
2586 tree
2587 nreverse (tree t)
2589 tree prev = 0, decl, next;
2590 for (decl = t; decl; decl = next)
2592 /* We shouldn't be using this function to reverse BLOCK chains; we
2593 have blocks_nreverse for that. */
2594 gcc_checking_assert (TREE_CODE (decl) != BLOCK);
2595 next = TREE_CHAIN (decl);
2596 TREE_CHAIN (decl) = prev;
2597 prev = decl;
2599 return prev;
2602 /* Return a newly created TREE_LIST node whose
2603 purpose and value fields are PARM and VALUE. */
2605 tree
2606 build_tree_list_stat (tree parm, tree value MEM_STAT_DECL)
2608 tree t = make_node_stat (TREE_LIST PASS_MEM_STAT);
2609 TREE_PURPOSE (t) = parm;
2610 TREE_VALUE (t) = value;
2611 return t;
2614 /* Build a chain of TREE_LIST nodes from a vector. */
2616 tree
2617 build_tree_list_vec_stat (const vec<tree, va_gc> *vec MEM_STAT_DECL)
2619 tree ret = NULL_TREE;
2620 tree *pp = &ret;
2621 unsigned int i;
2622 tree t;
2623 FOR_EACH_VEC_SAFE_ELT (vec, i, t)
2625 *pp = build_tree_list_stat (NULL, t PASS_MEM_STAT);
2626 pp = &TREE_CHAIN (*pp);
2628 return ret;
2631 /* Return a newly created TREE_LIST node whose
2632 purpose and value fields are PURPOSE and VALUE
2633 and whose TREE_CHAIN is CHAIN. */
2635 tree
2636 tree_cons_stat (tree purpose, tree value, tree chain MEM_STAT_DECL)
2638 tree node;
2640 node = ggc_alloc_tree_node_stat (sizeof (struct tree_list) PASS_MEM_STAT);
2641 memset (node, 0, sizeof (struct tree_common));
2643 record_node_allocation_statistics (TREE_LIST, sizeof (struct tree_list));
2645 TREE_SET_CODE (node, TREE_LIST);
2646 TREE_CHAIN (node) = chain;
2647 TREE_PURPOSE (node) = purpose;
2648 TREE_VALUE (node) = value;
2649 return node;
2652 /* Return the values of the elements of a CONSTRUCTOR as a vector of
2653 trees. */
2655 vec<tree, va_gc> *
2656 ctor_to_vec (tree ctor)
2658 vec<tree, va_gc> *vec;
2659 vec_alloc (vec, CONSTRUCTOR_NELTS (ctor));
2660 unsigned int ix;
2661 tree val;
2663 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), ix, val)
2664 vec->quick_push (val);
2666 return vec;
2669 /* Return the size nominally occupied by an object of type TYPE
2670 when it resides in memory. The value is measured in units of bytes,
2671 and its data type is that normally used for type sizes
2672 (which is the first type created by make_signed_type or
2673 make_unsigned_type). */
2675 tree
2676 size_in_bytes (const_tree type)
2678 tree t;
2680 if (type == error_mark_node)
2681 return integer_zero_node;
2683 type = TYPE_MAIN_VARIANT (type);
2684 t = TYPE_SIZE_UNIT (type);
2686 if (t == 0)
2688 lang_hooks.types.incomplete_type_error (NULL_TREE, type);
2689 return size_zero_node;
2692 return t;
2695 /* Return the size of TYPE (in bytes) as a wide integer
2696 or return -1 if the size can vary or is larger than an integer. */
2698 HOST_WIDE_INT
2699 int_size_in_bytes (const_tree type)
2701 tree t;
2703 if (type == error_mark_node)
2704 return 0;
2706 type = TYPE_MAIN_VARIANT (type);
2707 t = TYPE_SIZE_UNIT (type);
2709 if (t && tree_fits_uhwi_p (t))
2710 return TREE_INT_CST_LOW (t);
2711 else
2712 return -1;
2715 /* Return the maximum size of TYPE (in bytes) as a wide integer
2716 or return -1 if the size can vary or is larger than an integer. */
2718 HOST_WIDE_INT
2719 max_int_size_in_bytes (const_tree type)
2721 HOST_WIDE_INT size = -1;
2722 tree size_tree;
2724 /* If this is an array type, check for a possible MAX_SIZE attached. */
2726 if (TREE_CODE (type) == ARRAY_TYPE)
2728 size_tree = TYPE_ARRAY_MAX_SIZE (type);
2730 if (size_tree && tree_fits_uhwi_p (size_tree))
2731 size = tree_to_uhwi (size_tree);
2734 /* If we still haven't been able to get a size, see if the language
2735 can compute a maximum size. */
2737 if (size == -1)
2739 size_tree = lang_hooks.types.max_size (type);
2741 if (size_tree && tree_fits_uhwi_p (size_tree))
2742 size = tree_to_uhwi (size_tree);
2745 return size;
2748 /* Return the bit position of FIELD, in bits from the start of the record.
2749 This is a tree of type bitsizetype. */
2751 tree
2752 bit_position (const_tree field)
2754 return bit_from_pos (DECL_FIELD_OFFSET (field),
2755 DECL_FIELD_BIT_OFFSET (field));
2758 /* Likewise, but return as an integer. It must be representable in
2759 that way (since it could be a signed value, we don't have the
2760 option of returning -1 like int_size_in_byte can. */
2762 HOST_WIDE_INT
2763 int_bit_position (const_tree field)
2765 return tree_to_shwi (bit_position (field));
2768 /* Return the byte position of FIELD, in bytes from the start of the record.
2769 This is a tree of type sizetype. */
2771 tree
2772 byte_position (const_tree field)
2774 return byte_from_pos (DECL_FIELD_OFFSET (field),
2775 DECL_FIELD_BIT_OFFSET (field));
2778 /* Likewise, but return as an integer. It must be representable in
2779 that way (since it could be a signed value, we don't have the
2780 option of returning -1 like int_size_in_byte can. */
2782 HOST_WIDE_INT
2783 int_byte_position (const_tree field)
2785 return tree_to_shwi (byte_position (field));
2788 /* Return the strictest alignment, in bits, that T is known to have. */
2790 unsigned int
2791 expr_align (const_tree t)
2793 unsigned int align0, align1;
2795 switch (TREE_CODE (t))
2797 CASE_CONVERT: case NON_LVALUE_EXPR:
2798 /* If we have conversions, we know that the alignment of the
2799 object must meet each of the alignments of the types. */
2800 align0 = expr_align (TREE_OPERAND (t, 0));
2801 align1 = TYPE_ALIGN (TREE_TYPE (t));
2802 return MAX (align0, align1);
2804 case SAVE_EXPR: case COMPOUND_EXPR: case MODIFY_EXPR:
2805 case INIT_EXPR: case TARGET_EXPR: case WITH_CLEANUP_EXPR:
2806 case CLEANUP_POINT_EXPR:
2807 /* These don't change the alignment of an object. */
2808 return expr_align (TREE_OPERAND (t, 0));
2810 case COND_EXPR:
2811 /* The best we can do is say that the alignment is the least aligned
2812 of the two arms. */
2813 align0 = expr_align (TREE_OPERAND (t, 1));
2814 align1 = expr_align (TREE_OPERAND (t, 2));
2815 return MIN (align0, align1);
2817 /* FIXME: LABEL_DECL and CONST_DECL never have DECL_ALIGN set
2818 meaningfully, it's always 1. */
2819 case LABEL_DECL: case CONST_DECL:
2820 case VAR_DECL: case PARM_DECL: case RESULT_DECL:
2821 case FUNCTION_DECL:
2822 gcc_assert (DECL_ALIGN (t) != 0);
2823 return DECL_ALIGN (t);
2825 default:
2826 break;
2829 /* Otherwise take the alignment from that of the type. */
2830 return TYPE_ALIGN (TREE_TYPE (t));
2833 /* Return, as a tree node, the number of elements for TYPE (which is an
2834 ARRAY_TYPE) minus one. This counts only elements of the top array. */
2836 tree
2837 array_type_nelts (const_tree type)
2839 tree index_type, min, max;
2841 /* If they did it with unspecified bounds, then we should have already
2842 given an error about it before we got here. */
2843 if (! TYPE_DOMAIN (type))
2844 return error_mark_node;
2846 index_type = TYPE_DOMAIN (type);
2847 min = TYPE_MIN_VALUE (index_type);
2848 max = TYPE_MAX_VALUE (index_type);
2850 /* TYPE_MAX_VALUE may not be set if the array has unknown length. */
2851 if (!max)
2852 return error_mark_node;
2854 return (integer_zerop (min)
2855 ? max
2856 : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
2859 /* If arg is static -- a reference to an object in static storage -- then
2860 return the object. This is not the same as the C meaning of `static'.
2861 If arg isn't static, return NULL. */
2863 tree
2864 staticp (tree arg)
2866 switch (TREE_CODE (arg))
2868 case FUNCTION_DECL:
2869 /* Nested functions are static, even though taking their address will
2870 involve a trampoline as we unnest the nested function and create
2871 the trampoline on the tree level. */
2872 return arg;
2874 case VAR_DECL:
2875 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2876 && ! DECL_THREAD_LOCAL_P (arg)
2877 && ! DECL_DLLIMPORT_P (arg)
2878 ? arg : NULL);
2880 case CONST_DECL:
2881 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2882 ? arg : NULL);
2884 case CONSTRUCTOR:
2885 return TREE_STATIC (arg) ? arg : NULL;
2887 case LABEL_DECL:
2888 case STRING_CST:
2889 return arg;
2891 case COMPONENT_REF:
2892 /* If the thing being referenced is not a field, then it is
2893 something language specific. */
2894 gcc_assert (TREE_CODE (TREE_OPERAND (arg, 1)) == FIELD_DECL);
2896 /* If we are referencing a bitfield, we can't evaluate an
2897 ADDR_EXPR at compile time and so it isn't a constant. */
2898 if (DECL_BIT_FIELD (TREE_OPERAND (arg, 1)))
2899 return NULL;
2901 return staticp (TREE_OPERAND (arg, 0));
2903 case BIT_FIELD_REF:
2904 return NULL;
2906 case INDIRECT_REF:
2907 return TREE_CONSTANT (TREE_OPERAND (arg, 0)) ? arg : NULL;
2909 case ARRAY_REF:
2910 case ARRAY_RANGE_REF:
2911 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
2912 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
2913 return staticp (TREE_OPERAND (arg, 0));
2914 else
2915 return NULL;
2917 case COMPOUND_LITERAL_EXPR:
2918 return TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (arg)) ? arg : NULL;
2920 default:
2921 return NULL;
2928 /* Return whether OP is a DECL whose address is function-invariant. */
2930 bool
2931 decl_address_invariant_p (const_tree op)
2933 /* The conditions below are slightly less strict than the one in
2934 staticp. */
2936 switch (TREE_CODE (op))
2938 case PARM_DECL:
2939 case RESULT_DECL:
2940 case LABEL_DECL:
2941 case FUNCTION_DECL:
2942 return true;
2944 case VAR_DECL:
2945 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
2946 || DECL_THREAD_LOCAL_P (op)
2947 || DECL_CONTEXT (op) == current_function_decl
2948 || decl_function_context (op) == current_function_decl)
2949 return true;
2950 break;
2952 case CONST_DECL:
2953 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
2954 || decl_function_context (op) == current_function_decl)
2955 return true;
2956 break;
2958 default:
2959 break;
2962 return false;
2965 /* Return whether OP is a DECL whose address is interprocedural-invariant. */
2967 bool
2968 decl_address_ip_invariant_p (const_tree op)
2970 /* The conditions below are slightly less strict than the one in
2971 staticp. */
2973 switch (TREE_CODE (op))
2975 case LABEL_DECL:
2976 case FUNCTION_DECL:
2977 case STRING_CST:
2978 return true;
2980 case VAR_DECL:
2981 if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
2982 && !DECL_DLLIMPORT_P (op))
2983 || DECL_THREAD_LOCAL_P (op))
2984 return true;
2985 break;
2987 case CONST_DECL:
2988 if ((TREE_STATIC (op) || DECL_EXTERNAL (op)))
2989 return true;
2990 break;
2992 default:
2993 break;
2996 return false;
3000 /* Return true if T is function-invariant (internal function, does
3001 not handle arithmetic; that's handled in skip_simple_arithmetic and
3002 tree_invariant_p). */
3004 static bool tree_invariant_p (tree t);
3006 static bool
3007 tree_invariant_p_1 (tree t)
3009 tree op;
3011 if (TREE_CONSTANT (t)
3012 || (TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t)))
3013 return true;
3015 switch (TREE_CODE (t))
3017 case SAVE_EXPR:
3018 return true;
3020 case ADDR_EXPR:
3021 op = TREE_OPERAND (t, 0);
3022 while (handled_component_p (op))
3024 switch (TREE_CODE (op))
3026 case ARRAY_REF:
3027 case ARRAY_RANGE_REF:
3028 if (!tree_invariant_p (TREE_OPERAND (op, 1))
3029 || TREE_OPERAND (op, 2) != NULL_TREE
3030 || TREE_OPERAND (op, 3) != NULL_TREE)
3031 return false;
3032 break;
3034 case COMPONENT_REF:
3035 if (TREE_OPERAND (op, 2) != NULL_TREE)
3036 return false;
3037 break;
3039 default:;
3041 op = TREE_OPERAND (op, 0);
3044 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
3046 default:
3047 break;
3050 return false;
3053 /* Return true if T is function-invariant. */
3055 static bool
3056 tree_invariant_p (tree t)
3058 tree inner = skip_simple_arithmetic (t);
3059 return tree_invariant_p_1 (inner);
3062 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
3063 Do this to any expression which may be used in more than one place,
3064 but must be evaluated only once.
3066 Normally, expand_expr would reevaluate the expression each time.
3067 Calling save_expr produces something that is evaluated and recorded
3068 the first time expand_expr is called on it. Subsequent calls to
3069 expand_expr just reuse the recorded value.
3071 The call to expand_expr that generates code that actually computes
3072 the value is the first call *at compile time*. Subsequent calls
3073 *at compile time* generate code to use the saved value.
3074 This produces correct result provided that *at run time* control
3075 always flows through the insns made by the first expand_expr
3076 before reaching the other places where the save_expr was evaluated.
3077 You, the caller of save_expr, must make sure this is so.
3079 Constants, and certain read-only nodes, are returned with no
3080 SAVE_EXPR because that is safe. Expressions containing placeholders
3081 are not touched; see tree.def for an explanation of what these
3082 are used for. */
3084 tree
3085 save_expr (tree expr)
3087 tree t = fold (expr);
3088 tree inner;
3090 /* If the tree evaluates to a constant, then we don't want to hide that
3091 fact (i.e. this allows further folding, and direct checks for constants).
3092 However, a read-only object that has side effects cannot be bypassed.
3093 Since it is no problem to reevaluate literals, we just return the
3094 literal node. */
3095 inner = skip_simple_arithmetic (t);
3096 if (TREE_CODE (inner) == ERROR_MARK)
3097 return inner;
3099 if (tree_invariant_p_1 (inner))
3100 return t;
3102 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
3103 it means that the size or offset of some field of an object depends on
3104 the value within another field.
3106 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
3107 and some variable since it would then need to be both evaluated once and
3108 evaluated more than once. Front-ends must assure this case cannot
3109 happen by surrounding any such subexpressions in their own SAVE_EXPR
3110 and forcing evaluation at the proper time. */
3111 if (contains_placeholder_p (inner))
3112 return t;
3114 t = build1 (SAVE_EXPR, TREE_TYPE (expr), t);
3115 SET_EXPR_LOCATION (t, EXPR_LOCATION (expr));
3117 /* This expression might be placed ahead of a jump to ensure that the
3118 value was computed on both sides of the jump. So make sure it isn't
3119 eliminated as dead. */
3120 TREE_SIDE_EFFECTS (t) = 1;
3121 return t;
3124 /* Look inside EXPR into any simple arithmetic operations. Return the
3125 outermost non-arithmetic or non-invariant node. */
3127 tree
3128 skip_simple_arithmetic (tree expr)
3130 /* We don't care about whether this can be used as an lvalue in this
3131 context. */
3132 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3133 expr = TREE_OPERAND (expr, 0);
3135 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
3136 a constant, it will be more efficient to not make another SAVE_EXPR since
3137 it will allow better simplification and GCSE will be able to merge the
3138 computations if they actually occur. */
3139 while (true)
3141 if (UNARY_CLASS_P (expr))
3142 expr = TREE_OPERAND (expr, 0);
3143 else if (BINARY_CLASS_P (expr))
3145 if (tree_invariant_p (TREE_OPERAND (expr, 1)))
3146 expr = TREE_OPERAND (expr, 0);
3147 else if (tree_invariant_p (TREE_OPERAND (expr, 0)))
3148 expr = TREE_OPERAND (expr, 1);
3149 else
3150 break;
3152 else
3153 break;
3156 return expr;
3159 /* Look inside EXPR into simple arithmetic operations involving constants.
3160 Return the outermost non-arithmetic or non-constant node. */
3162 tree
3163 skip_simple_constant_arithmetic (tree expr)
3165 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3166 expr = TREE_OPERAND (expr, 0);
3168 while (true)
3170 if (UNARY_CLASS_P (expr))
3171 expr = TREE_OPERAND (expr, 0);
3172 else if (BINARY_CLASS_P (expr))
3174 if (TREE_CONSTANT (TREE_OPERAND (expr, 1)))
3175 expr = TREE_OPERAND (expr, 0);
3176 else if (TREE_CONSTANT (TREE_OPERAND (expr, 0)))
3177 expr = TREE_OPERAND (expr, 1);
3178 else
3179 break;
3181 else
3182 break;
3185 return expr;
3188 /* Return which tree structure is used by T. */
3190 enum tree_node_structure_enum
3191 tree_node_structure (const_tree t)
3193 const enum tree_code code = TREE_CODE (t);
3194 return tree_node_structure_for_code (code);
3197 /* Set various status flags when building a CALL_EXPR object T. */
3199 static void
3200 process_call_operands (tree t)
3202 bool side_effects = TREE_SIDE_EFFECTS (t);
3203 bool read_only = false;
3204 int i = call_expr_flags (t);
3206 /* Calls have side-effects, except those to const or pure functions. */
3207 if ((i & ECF_LOOPING_CONST_OR_PURE) || !(i & (ECF_CONST | ECF_PURE)))
3208 side_effects = true;
3209 /* Propagate TREE_READONLY of arguments for const functions. */
3210 if (i & ECF_CONST)
3211 read_only = true;
3213 if (!side_effects || read_only)
3214 for (i = 1; i < TREE_OPERAND_LENGTH (t); i++)
3216 tree op = TREE_OPERAND (t, i);
3217 if (op && TREE_SIDE_EFFECTS (op))
3218 side_effects = true;
3219 if (op && !TREE_READONLY (op) && !CONSTANT_CLASS_P (op))
3220 read_only = false;
3223 TREE_SIDE_EFFECTS (t) = side_effects;
3224 TREE_READONLY (t) = read_only;
3227 /* Return true if EXP contains a PLACEHOLDER_EXPR, i.e. if it represents a
3228 size or offset that depends on a field within a record. */
3230 bool
3231 contains_placeholder_p (const_tree exp)
3233 enum tree_code code;
3235 if (!exp)
3236 return 0;
3238 code = TREE_CODE (exp);
3239 if (code == PLACEHOLDER_EXPR)
3240 return 1;
3242 switch (TREE_CODE_CLASS (code))
3244 case tcc_reference:
3245 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
3246 position computations since they will be converted into a
3247 WITH_RECORD_EXPR involving the reference, which will assume
3248 here will be valid. */
3249 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3251 case tcc_exceptional:
3252 if (code == TREE_LIST)
3253 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
3254 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
3255 break;
3257 case tcc_unary:
3258 case tcc_binary:
3259 case tcc_comparison:
3260 case tcc_expression:
3261 switch (code)
3263 case COMPOUND_EXPR:
3264 /* Ignoring the first operand isn't quite right, but works best. */
3265 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
3267 case COND_EXPR:
3268 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3269 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
3270 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
3272 case SAVE_EXPR:
3273 /* The save_expr function never wraps anything containing
3274 a PLACEHOLDER_EXPR. */
3275 return 0;
3277 default:
3278 break;
3281 switch (TREE_CODE_LENGTH (code))
3283 case 1:
3284 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3285 case 2:
3286 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3287 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
3288 default:
3289 return 0;
3292 case tcc_vl_exp:
3293 switch (code)
3295 case CALL_EXPR:
3297 const_tree arg;
3298 const_call_expr_arg_iterator iter;
3299 FOR_EACH_CONST_CALL_EXPR_ARG (arg, iter, exp)
3300 if (CONTAINS_PLACEHOLDER_P (arg))
3301 return 1;
3302 return 0;
3304 default:
3305 return 0;
3308 default:
3309 return 0;
3311 return 0;
3314 /* Return true if any part of the structure of TYPE involves a PLACEHOLDER_EXPR
3315 directly. This includes size, bounds, qualifiers (for QUAL_UNION_TYPE) and
3316 field positions. */
3318 static bool
3319 type_contains_placeholder_1 (const_tree type)
3321 /* If the size contains a placeholder or the parent type (component type in
3322 the case of arrays) type involves a placeholder, this type does. */
3323 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
3324 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
3325 || (!POINTER_TYPE_P (type)
3326 && TREE_TYPE (type)
3327 && type_contains_placeholder_p (TREE_TYPE (type))))
3328 return true;
3330 /* Now do type-specific checks. Note that the last part of the check above
3331 greatly limits what we have to do below. */
3332 switch (TREE_CODE (type))
3334 case VOID_TYPE:
3335 case COMPLEX_TYPE:
3336 case ENUMERAL_TYPE:
3337 case BOOLEAN_TYPE:
3338 case POINTER_TYPE:
3339 case OFFSET_TYPE:
3340 case REFERENCE_TYPE:
3341 case METHOD_TYPE:
3342 case FUNCTION_TYPE:
3343 case VECTOR_TYPE:
3344 case NULLPTR_TYPE:
3345 return false;
3347 case INTEGER_TYPE:
3348 case REAL_TYPE:
3349 case FIXED_POINT_TYPE:
3350 /* Here we just check the bounds. */
3351 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
3352 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
3354 case ARRAY_TYPE:
3355 /* We have already checked the component type above, so just check the
3356 domain type. */
3357 return type_contains_placeholder_p (TYPE_DOMAIN (type));
3359 case RECORD_TYPE:
3360 case UNION_TYPE:
3361 case QUAL_UNION_TYPE:
3363 tree field;
3365 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3366 if (TREE_CODE (field) == FIELD_DECL
3367 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
3368 || (TREE_CODE (type) == QUAL_UNION_TYPE
3369 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
3370 || type_contains_placeholder_p (TREE_TYPE (field))))
3371 return true;
3373 return false;
3376 default:
3377 gcc_unreachable ();
3381 /* Wrapper around above function used to cache its result. */
3383 bool
3384 type_contains_placeholder_p (tree type)
3386 bool result;
3388 /* If the contains_placeholder_bits field has been initialized,
3389 then we know the answer. */
3390 if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
3391 return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
3393 /* Indicate that we've seen this type node, and the answer is false.
3394 This is what we want to return if we run into recursion via fields. */
3395 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
3397 /* Compute the real value. */
3398 result = type_contains_placeholder_1 (type);
3400 /* Store the real value. */
3401 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
3403 return result;
3406 /* Push tree EXP onto vector QUEUE if it is not already present. */
3408 static void
3409 push_without_duplicates (tree exp, vec<tree> *queue)
3411 unsigned int i;
3412 tree iter;
3414 FOR_EACH_VEC_ELT (*queue, i, iter)
3415 if (simple_cst_equal (iter, exp) == 1)
3416 break;
3418 if (!iter)
3419 queue->safe_push (exp);
3422 /* Given a tree EXP, find all occurrences of references to fields
3423 in a PLACEHOLDER_EXPR and place them in vector REFS without
3424 duplicates. Also record VAR_DECLs and CONST_DECLs. Note that
3425 we assume here that EXP contains only arithmetic expressions
3426 or CALL_EXPRs with PLACEHOLDER_EXPRs occurring only in their
3427 argument list. */
3429 void
3430 find_placeholder_in_expr (tree exp, vec<tree> *refs)
3432 enum tree_code code = TREE_CODE (exp);
3433 tree inner;
3434 int i;
3436 /* We handle TREE_LIST and COMPONENT_REF separately. */
3437 if (code == TREE_LIST)
3439 FIND_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), refs);
3440 FIND_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), refs);
3442 else if (code == COMPONENT_REF)
3444 for (inner = TREE_OPERAND (exp, 0);
3445 REFERENCE_CLASS_P (inner);
3446 inner = TREE_OPERAND (inner, 0))
3449 if (TREE_CODE (inner) == PLACEHOLDER_EXPR)
3450 push_without_duplicates (exp, refs);
3451 else
3452 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), refs);
3454 else
3455 switch (TREE_CODE_CLASS (code))
3457 case tcc_constant:
3458 break;
3460 case tcc_declaration:
3461 /* Variables allocated to static storage can stay. */
3462 if (!TREE_STATIC (exp))
3463 push_without_duplicates (exp, refs);
3464 break;
3466 case tcc_expression:
3467 /* This is the pattern built in ada/make_aligning_type. */
3468 if (code == ADDR_EXPR
3469 && TREE_CODE (TREE_OPERAND (exp, 0)) == PLACEHOLDER_EXPR)
3471 push_without_duplicates (exp, refs);
3472 break;
3475 /* Fall through... */
3477 case tcc_exceptional:
3478 case tcc_unary:
3479 case tcc_binary:
3480 case tcc_comparison:
3481 case tcc_reference:
3482 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
3483 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3484 break;
3486 case tcc_vl_exp:
3487 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3488 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3489 break;
3491 default:
3492 gcc_unreachable ();
3496 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
3497 return a tree with all occurrences of references to F in a
3498 PLACEHOLDER_EXPR replaced by R. Also handle VAR_DECLs and
3499 CONST_DECLs. Note that we assume here that EXP contains only
3500 arithmetic expressions or CALL_EXPRs with PLACEHOLDER_EXPRs
3501 occurring only in their argument list. */
3503 tree
3504 substitute_in_expr (tree exp, tree f, tree r)
3506 enum tree_code code = TREE_CODE (exp);
3507 tree op0, op1, op2, op3;
3508 tree new_tree;
3510 /* We handle TREE_LIST and COMPONENT_REF separately. */
3511 if (code == TREE_LIST)
3513 op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
3514 op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
3515 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3516 return exp;
3518 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3520 else if (code == COMPONENT_REF)
3522 tree inner;
3524 /* If this expression is getting a value from a PLACEHOLDER_EXPR
3525 and it is the right field, replace it with R. */
3526 for (inner = TREE_OPERAND (exp, 0);
3527 REFERENCE_CLASS_P (inner);
3528 inner = TREE_OPERAND (inner, 0))
3531 /* The field. */
3532 op1 = TREE_OPERAND (exp, 1);
3534 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f)
3535 return r;
3537 /* If this expression hasn't been completed let, leave it alone. */
3538 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && !TREE_TYPE (inner))
3539 return exp;
3541 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3542 if (op0 == TREE_OPERAND (exp, 0))
3543 return exp;
3545 new_tree
3546 = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE);
3548 else
3549 switch (TREE_CODE_CLASS (code))
3551 case tcc_constant:
3552 return exp;
3554 case tcc_declaration:
3555 if (exp == f)
3556 return r;
3557 else
3558 return exp;
3560 case tcc_expression:
3561 if (exp == f)
3562 return r;
3564 /* Fall through... */
3566 case tcc_exceptional:
3567 case tcc_unary:
3568 case tcc_binary:
3569 case tcc_comparison:
3570 case tcc_reference:
3571 switch (TREE_CODE_LENGTH (code))
3573 case 0:
3574 return exp;
3576 case 1:
3577 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3578 if (op0 == TREE_OPERAND (exp, 0))
3579 return exp;
3581 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3582 break;
3584 case 2:
3585 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3586 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3588 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3589 return exp;
3591 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3592 break;
3594 case 3:
3595 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3596 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3597 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3599 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3600 && op2 == TREE_OPERAND (exp, 2))
3601 return exp;
3603 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3604 break;
3606 case 4:
3607 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3608 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3609 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3610 op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
3612 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3613 && op2 == TREE_OPERAND (exp, 2)
3614 && op3 == TREE_OPERAND (exp, 3))
3615 return exp;
3617 new_tree
3618 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3619 break;
3621 default:
3622 gcc_unreachable ();
3624 break;
3626 case tcc_vl_exp:
3628 int i;
3630 new_tree = NULL_TREE;
3632 /* If we are trying to replace F with a constant, inline back
3633 functions which do nothing else than computing a value from
3634 the arguments they are passed. This makes it possible to
3635 fold partially or entirely the replacement expression. */
3636 if (CONSTANT_CLASS_P (r) && code == CALL_EXPR)
3638 tree t = maybe_inline_call_in_expr (exp);
3639 if (t)
3640 return SUBSTITUTE_IN_EXPR (t, f, r);
3643 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3645 tree op = TREE_OPERAND (exp, i);
3646 tree new_op = SUBSTITUTE_IN_EXPR (op, f, r);
3647 if (new_op != op)
3649 if (!new_tree)
3650 new_tree = copy_node (exp);
3651 TREE_OPERAND (new_tree, i) = new_op;
3655 if (new_tree)
3657 new_tree = fold (new_tree);
3658 if (TREE_CODE (new_tree) == CALL_EXPR)
3659 process_call_operands (new_tree);
3661 else
3662 return exp;
3664 break;
3666 default:
3667 gcc_unreachable ();
3670 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3672 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3673 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3675 return new_tree;
3678 /* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
3679 for it within OBJ, a tree that is an object or a chain of references. */
3681 tree
3682 substitute_placeholder_in_expr (tree exp, tree obj)
3684 enum tree_code code = TREE_CODE (exp);
3685 tree op0, op1, op2, op3;
3686 tree new_tree;
3688 /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
3689 in the chain of OBJ. */
3690 if (code == PLACEHOLDER_EXPR)
3692 tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
3693 tree elt;
3695 for (elt = obj; elt != 0;
3696 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3697 || TREE_CODE (elt) == COND_EXPR)
3698 ? TREE_OPERAND (elt, 1)
3699 : (REFERENCE_CLASS_P (elt)
3700 || UNARY_CLASS_P (elt)
3701 || BINARY_CLASS_P (elt)
3702 || VL_EXP_CLASS_P (elt)
3703 || EXPRESSION_CLASS_P (elt))
3704 ? TREE_OPERAND (elt, 0) : 0))
3705 if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
3706 return elt;
3708 for (elt = obj; elt != 0;
3709 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3710 || TREE_CODE (elt) == COND_EXPR)
3711 ? TREE_OPERAND (elt, 1)
3712 : (REFERENCE_CLASS_P (elt)
3713 || UNARY_CLASS_P (elt)
3714 || BINARY_CLASS_P (elt)
3715 || VL_EXP_CLASS_P (elt)
3716 || EXPRESSION_CLASS_P (elt))
3717 ? TREE_OPERAND (elt, 0) : 0))
3718 if (POINTER_TYPE_P (TREE_TYPE (elt))
3719 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
3720 == need_type))
3721 return fold_build1 (INDIRECT_REF, need_type, elt);
3723 /* If we didn't find it, return the original PLACEHOLDER_EXPR. If it
3724 survives until RTL generation, there will be an error. */
3725 return exp;
3728 /* TREE_LIST is special because we need to look at TREE_VALUE
3729 and TREE_CHAIN, not TREE_OPERANDS. */
3730 else if (code == TREE_LIST)
3732 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
3733 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
3734 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3735 return exp;
3737 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3739 else
3740 switch (TREE_CODE_CLASS (code))
3742 case tcc_constant:
3743 case tcc_declaration:
3744 return exp;
3746 case tcc_exceptional:
3747 case tcc_unary:
3748 case tcc_binary:
3749 case tcc_comparison:
3750 case tcc_expression:
3751 case tcc_reference:
3752 case tcc_statement:
3753 switch (TREE_CODE_LENGTH (code))
3755 case 0:
3756 return exp;
3758 case 1:
3759 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3760 if (op0 == TREE_OPERAND (exp, 0))
3761 return exp;
3763 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3764 break;
3766 case 2:
3767 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3768 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3770 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3771 return exp;
3773 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3774 break;
3776 case 3:
3777 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3778 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3779 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3781 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3782 && op2 == TREE_OPERAND (exp, 2))
3783 return exp;
3785 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3786 break;
3788 case 4:
3789 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3790 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3791 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3792 op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
3794 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3795 && op2 == TREE_OPERAND (exp, 2)
3796 && op3 == TREE_OPERAND (exp, 3))
3797 return exp;
3799 new_tree
3800 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3801 break;
3803 default:
3804 gcc_unreachable ();
3806 break;
3808 case tcc_vl_exp:
3810 int i;
3812 new_tree = NULL_TREE;
3814 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3816 tree op = TREE_OPERAND (exp, i);
3817 tree new_op = SUBSTITUTE_PLACEHOLDER_IN_EXPR (op, obj);
3818 if (new_op != op)
3820 if (!new_tree)
3821 new_tree = copy_node (exp);
3822 TREE_OPERAND (new_tree, i) = new_op;
3826 if (new_tree)
3828 new_tree = fold (new_tree);
3829 if (TREE_CODE (new_tree) == CALL_EXPR)
3830 process_call_operands (new_tree);
3832 else
3833 return exp;
3835 break;
3837 default:
3838 gcc_unreachable ();
3841 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3843 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3844 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3846 return new_tree;
3850 /* Subroutine of stabilize_reference; this is called for subtrees of
3851 references. Any expression with side-effects must be put in a SAVE_EXPR
3852 to ensure that it is only evaluated once.
3854 We don't put SAVE_EXPR nodes around everything, because assigning very
3855 simple expressions to temporaries causes us to miss good opportunities
3856 for optimizations. Among other things, the opportunity to fold in the
3857 addition of a constant into an addressing mode often gets lost, e.g.
3858 "y[i+1] += x;". In general, we take the approach that we should not make
3859 an assignment unless we are forced into it - i.e., that any non-side effect
3860 operator should be allowed, and that cse should take care of coalescing
3861 multiple utterances of the same expression should that prove fruitful. */
3863 static tree
3864 stabilize_reference_1 (tree e)
3866 tree result;
3867 enum tree_code code = TREE_CODE (e);
3869 /* We cannot ignore const expressions because it might be a reference
3870 to a const array but whose index contains side-effects. But we can
3871 ignore things that are actual constant or that already have been
3872 handled by this function. */
3874 if (tree_invariant_p (e))
3875 return e;
3877 switch (TREE_CODE_CLASS (code))
3879 case tcc_exceptional:
3880 case tcc_type:
3881 case tcc_declaration:
3882 case tcc_comparison:
3883 case tcc_statement:
3884 case tcc_expression:
3885 case tcc_reference:
3886 case tcc_vl_exp:
3887 /* If the expression has side-effects, then encase it in a SAVE_EXPR
3888 so that it will only be evaluated once. */
3889 /* The reference (r) and comparison (<) classes could be handled as
3890 below, but it is generally faster to only evaluate them once. */
3891 if (TREE_SIDE_EFFECTS (e))
3892 return save_expr (e);
3893 return e;
3895 case tcc_constant:
3896 /* Constants need no processing. In fact, we should never reach
3897 here. */
3898 return e;
3900 case tcc_binary:
3901 /* Division is slow and tends to be compiled with jumps,
3902 especially the division by powers of 2 that is often
3903 found inside of an array reference. So do it just once. */
3904 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
3905 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
3906 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
3907 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
3908 return save_expr (e);
3909 /* Recursively stabilize each operand. */
3910 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
3911 stabilize_reference_1 (TREE_OPERAND (e, 1)));
3912 break;
3914 case tcc_unary:
3915 /* Recursively stabilize each operand. */
3916 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
3917 break;
3919 default:
3920 gcc_unreachable ();
3923 TREE_TYPE (result) = TREE_TYPE (e);
3924 TREE_READONLY (result) = TREE_READONLY (e);
3925 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
3926 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
3928 return result;
3931 /* Stabilize a reference so that we can use it any number of times
3932 without causing its operands to be evaluated more than once.
3933 Returns the stabilized reference. This works by means of save_expr,
3934 so see the caveats in the comments about save_expr.
3936 Also allows conversion expressions whose operands are references.
3937 Any other kind of expression is returned unchanged. */
3939 tree
3940 stabilize_reference (tree ref)
3942 tree result;
3943 enum tree_code code = TREE_CODE (ref);
3945 switch (code)
3947 case VAR_DECL:
3948 case PARM_DECL:
3949 case RESULT_DECL:
3950 /* No action is needed in this case. */
3951 return ref;
3953 CASE_CONVERT:
3954 case FLOAT_EXPR:
3955 case FIX_TRUNC_EXPR:
3956 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
3957 break;
3959 case INDIRECT_REF:
3960 result = build_nt (INDIRECT_REF,
3961 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
3962 break;
3964 case COMPONENT_REF:
3965 result = build_nt (COMPONENT_REF,
3966 stabilize_reference (TREE_OPERAND (ref, 0)),
3967 TREE_OPERAND (ref, 1), NULL_TREE);
3968 break;
3970 case BIT_FIELD_REF:
3971 result = build_nt (BIT_FIELD_REF,
3972 stabilize_reference (TREE_OPERAND (ref, 0)),
3973 TREE_OPERAND (ref, 1), TREE_OPERAND (ref, 2));
3974 break;
3976 case ARRAY_REF:
3977 result = build_nt (ARRAY_REF,
3978 stabilize_reference (TREE_OPERAND (ref, 0)),
3979 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
3980 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
3981 break;
3983 case ARRAY_RANGE_REF:
3984 result = build_nt (ARRAY_RANGE_REF,
3985 stabilize_reference (TREE_OPERAND (ref, 0)),
3986 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
3987 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
3988 break;
3990 case COMPOUND_EXPR:
3991 /* We cannot wrap the first expression in a SAVE_EXPR, as then
3992 it wouldn't be ignored. This matters when dealing with
3993 volatiles. */
3994 return stabilize_reference_1 (ref);
3996 /* If arg isn't a kind of lvalue we recognize, make no change.
3997 Caller should recognize the error for an invalid lvalue. */
3998 default:
3999 return ref;
4001 case ERROR_MARK:
4002 return error_mark_node;
4005 TREE_TYPE (result) = TREE_TYPE (ref);
4006 TREE_READONLY (result) = TREE_READONLY (ref);
4007 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
4008 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
4010 return result;
4013 /* Low-level constructors for expressions. */
4015 /* A helper function for build1 and constant folders. Set TREE_CONSTANT,
4016 and TREE_SIDE_EFFECTS for an ADDR_EXPR. */
4018 void
4019 recompute_tree_invariant_for_addr_expr (tree t)
4021 tree node;
4022 bool tc = true, se = false;
4024 /* We started out assuming this address is both invariant and constant, but
4025 does not have side effects. Now go down any handled components and see if
4026 any of them involve offsets that are either non-constant or non-invariant.
4027 Also check for side-effects.
4029 ??? Note that this code makes no attempt to deal with the case where
4030 taking the address of something causes a copy due to misalignment. */
4032 #define UPDATE_FLAGS(NODE) \
4033 do { tree _node = (NODE); \
4034 if (_node && !TREE_CONSTANT (_node)) tc = false; \
4035 if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
4037 for (node = TREE_OPERAND (t, 0); handled_component_p (node);
4038 node = TREE_OPERAND (node, 0))
4040 /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
4041 array reference (probably made temporarily by the G++ front end),
4042 so ignore all the operands. */
4043 if ((TREE_CODE (node) == ARRAY_REF
4044 || TREE_CODE (node) == ARRAY_RANGE_REF)
4045 && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
4047 UPDATE_FLAGS (TREE_OPERAND (node, 1));
4048 if (TREE_OPERAND (node, 2))
4049 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4050 if (TREE_OPERAND (node, 3))
4051 UPDATE_FLAGS (TREE_OPERAND (node, 3));
4053 /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
4054 FIELD_DECL, apparently. The G++ front end can put something else
4055 there, at least temporarily. */
4056 else if (TREE_CODE (node) == COMPONENT_REF
4057 && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
4059 if (TREE_OPERAND (node, 2))
4060 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4064 node = lang_hooks.expr_to_decl (node, &tc, &se);
4066 /* Now see what's inside. If it's an INDIRECT_REF, copy our properties from
4067 the address, since &(*a)->b is a form of addition. If it's a constant, the
4068 address is constant too. If it's a decl, its address is constant if the
4069 decl is static. Everything else is not constant and, furthermore,
4070 taking the address of a volatile variable is not volatile. */
4071 if (TREE_CODE (node) == INDIRECT_REF
4072 || TREE_CODE (node) == MEM_REF)
4073 UPDATE_FLAGS (TREE_OPERAND (node, 0));
4074 else if (CONSTANT_CLASS_P (node))
4076 else if (DECL_P (node))
4077 tc &= (staticp (node) != NULL_TREE);
4078 else
4080 tc = false;
4081 se |= TREE_SIDE_EFFECTS (node);
4085 TREE_CONSTANT (t) = tc;
4086 TREE_SIDE_EFFECTS (t) = se;
4087 #undef UPDATE_FLAGS
4090 /* Build an expression of code CODE, data type TYPE, and operands as
4091 specified. Expressions and reference nodes can be created this way.
4092 Constants, decls, types and misc nodes cannot be.
4094 We define 5 non-variadic functions, from 0 to 4 arguments. This is
4095 enough for all extant tree codes. */
4097 tree
4098 build0_stat (enum tree_code code, tree tt MEM_STAT_DECL)
4100 tree t;
4102 gcc_assert (TREE_CODE_LENGTH (code) == 0);
4104 t = make_node_stat (code PASS_MEM_STAT);
4105 TREE_TYPE (t) = tt;
4107 return t;
4110 tree
4111 build1_stat (enum tree_code code, tree type, tree node MEM_STAT_DECL)
4113 int length = sizeof (struct tree_exp);
4114 tree t;
4116 record_node_allocation_statistics (code, length);
4118 gcc_assert (TREE_CODE_LENGTH (code) == 1);
4120 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
4122 memset (t, 0, sizeof (struct tree_common));
4124 TREE_SET_CODE (t, code);
4126 TREE_TYPE (t) = type;
4127 SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
4128 TREE_OPERAND (t, 0) = node;
4129 if (node && !TYPE_P (node))
4131 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
4132 TREE_READONLY (t) = TREE_READONLY (node);
4135 if (TREE_CODE_CLASS (code) == tcc_statement)
4136 TREE_SIDE_EFFECTS (t) = 1;
4137 else switch (code)
4139 case VA_ARG_EXPR:
4140 /* All of these have side-effects, no matter what their
4141 operands are. */
4142 TREE_SIDE_EFFECTS (t) = 1;
4143 TREE_READONLY (t) = 0;
4144 break;
4146 case INDIRECT_REF:
4147 /* Whether a dereference is readonly has nothing to do with whether
4148 its operand is readonly. */
4149 TREE_READONLY (t) = 0;
4150 break;
4152 case ADDR_EXPR:
4153 if (node)
4154 recompute_tree_invariant_for_addr_expr (t);
4155 break;
4157 default:
4158 if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
4159 && node && !TYPE_P (node)
4160 && TREE_CONSTANT (node))
4161 TREE_CONSTANT (t) = 1;
4162 if (TREE_CODE_CLASS (code) == tcc_reference
4163 && node && TREE_THIS_VOLATILE (node))
4164 TREE_THIS_VOLATILE (t) = 1;
4165 break;
4168 return t;
4171 #define PROCESS_ARG(N) \
4172 do { \
4173 TREE_OPERAND (t, N) = arg##N; \
4174 if (arg##N &&!TYPE_P (arg##N)) \
4176 if (TREE_SIDE_EFFECTS (arg##N)) \
4177 side_effects = 1; \
4178 if (!TREE_READONLY (arg##N) \
4179 && !CONSTANT_CLASS_P (arg##N)) \
4180 (void) (read_only = 0); \
4181 if (!TREE_CONSTANT (arg##N)) \
4182 (void) (constant = 0); \
4184 } while (0)
4186 tree
4187 build2_stat (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
4189 bool constant, read_only, side_effects;
4190 tree t;
4192 gcc_assert (TREE_CODE_LENGTH (code) == 2);
4194 if ((code == MINUS_EXPR || code == PLUS_EXPR || code == MULT_EXPR)
4195 && arg0 && arg1 && tt && POINTER_TYPE_P (tt)
4196 /* When sizetype precision doesn't match that of pointers
4197 we need to be able to build explicit extensions or truncations
4198 of the offset argument. */
4199 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (tt))
4200 gcc_assert (TREE_CODE (arg0) == INTEGER_CST
4201 && TREE_CODE (arg1) == INTEGER_CST);
4203 if (code == POINTER_PLUS_EXPR && arg0 && arg1 && tt)
4204 gcc_assert (POINTER_TYPE_P (tt) && POINTER_TYPE_P (TREE_TYPE (arg0))
4205 && ptrofftype_p (TREE_TYPE (arg1)));
4207 t = make_node_stat (code PASS_MEM_STAT);
4208 TREE_TYPE (t) = tt;
4210 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
4211 result based on those same flags for the arguments. But if the
4212 arguments aren't really even `tree' expressions, we shouldn't be trying
4213 to do this. */
4215 /* Expressions without side effects may be constant if their
4216 arguments are as well. */
4217 constant = (TREE_CODE_CLASS (code) == tcc_comparison
4218 || TREE_CODE_CLASS (code) == tcc_binary);
4219 read_only = 1;
4220 side_effects = TREE_SIDE_EFFECTS (t);
4222 PROCESS_ARG (0);
4223 PROCESS_ARG (1);
4225 TREE_READONLY (t) = read_only;
4226 TREE_CONSTANT (t) = constant;
4227 TREE_SIDE_EFFECTS (t) = side_effects;
4228 TREE_THIS_VOLATILE (t)
4229 = (TREE_CODE_CLASS (code) == tcc_reference
4230 && arg0 && TREE_THIS_VOLATILE (arg0));
4232 return t;
4236 tree
4237 build3_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4238 tree arg2 MEM_STAT_DECL)
4240 bool constant, read_only, side_effects;
4241 tree t;
4243 gcc_assert (TREE_CODE_LENGTH (code) == 3);
4244 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4246 t = make_node_stat (code PASS_MEM_STAT);
4247 TREE_TYPE (t) = tt;
4249 read_only = 1;
4251 /* As a special exception, if COND_EXPR has NULL branches, we
4252 assume that it is a gimple statement and always consider
4253 it to have side effects. */
4254 if (code == COND_EXPR
4255 && tt == void_type_node
4256 && arg1 == NULL_TREE
4257 && arg2 == NULL_TREE)
4258 side_effects = true;
4259 else
4260 side_effects = TREE_SIDE_EFFECTS (t);
4262 PROCESS_ARG (0);
4263 PROCESS_ARG (1);
4264 PROCESS_ARG (2);
4266 if (code == COND_EXPR)
4267 TREE_READONLY (t) = read_only;
4269 TREE_SIDE_EFFECTS (t) = side_effects;
4270 TREE_THIS_VOLATILE (t)
4271 = (TREE_CODE_CLASS (code) == tcc_reference
4272 && arg0 && TREE_THIS_VOLATILE (arg0));
4274 return t;
4277 tree
4278 build4_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4279 tree arg2, tree arg3 MEM_STAT_DECL)
4281 bool constant, read_only, side_effects;
4282 tree t;
4284 gcc_assert (TREE_CODE_LENGTH (code) == 4);
4286 t = make_node_stat (code PASS_MEM_STAT);
4287 TREE_TYPE (t) = tt;
4289 side_effects = TREE_SIDE_EFFECTS (t);
4291 PROCESS_ARG (0);
4292 PROCESS_ARG (1);
4293 PROCESS_ARG (2);
4294 PROCESS_ARG (3);
4296 TREE_SIDE_EFFECTS (t) = side_effects;
4297 TREE_THIS_VOLATILE (t)
4298 = (TREE_CODE_CLASS (code) == tcc_reference
4299 && arg0 && TREE_THIS_VOLATILE (arg0));
4301 return t;
4304 tree
4305 build5_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4306 tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
4308 bool constant, read_only, side_effects;
4309 tree t;
4311 gcc_assert (TREE_CODE_LENGTH (code) == 5);
4313 t = make_node_stat (code PASS_MEM_STAT);
4314 TREE_TYPE (t) = tt;
4316 side_effects = TREE_SIDE_EFFECTS (t);
4318 PROCESS_ARG (0);
4319 PROCESS_ARG (1);
4320 PROCESS_ARG (2);
4321 PROCESS_ARG (3);
4322 PROCESS_ARG (4);
4324 TREE_SIDE_EFFECTS (t) = side_effects;
4325 TREE_THIS_VOLATILE (t)
4326 = (TREE_CODE_CLASS (code) == tcc_reference
4327 && arg0 && TREE_THIS_VOLATILE (arg0));
4329 return t;
4332 /* Build a simple MEM_REF tree with the sematics of a plain INDIRECT_REF
4333 on the pointer PTR. */
4335 tree
4336 build_simple_mem_ref_loc (location_t loc, tree ptr)
4338 HOST_WIDE_INT offset = 0;
4339 tree ptype = TREE_TYPE (ptr);
4340 tree tem;
4341 /* For convenience allow addresses that collapse to a simple base
4342 and offset. */
4343 if (TREE_CODE (ptr) == ADDR_EXPR
4344 && (handled_component_p (TREE_OPERAND (ptr, 0))
4345 || TREE_CODE (TREE_OPERAND (ptr, 0)) == MEM_REF))
4347 ptr = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &offset);
4348 gcc_assert (ptr);
4349 ptr = build_fold_addr_expr (ptr);
4350 gcc_assert (is_gimple_reg (ptr) || is_gimple_min_invariant (ptr));
4352 tem = build2 (MEM_REF, TREE_TYPE (ptype),
4353 ptr, build_int_cst (ptype, offset));
4354 SET_EXPR_LOCATION (tem, loc);
4355 return tem;
4358 /* Return the constant offset of a MEM_REF or TARGET_MEM_REF tree T. */
4360 offset_int
4361 mem_ref_offset (const_tree t)
4363 return offset_int::from (TREE_OPERAND (t, 1), SIGNED);
4366 /* Return an invariant ADDR_EXPR of type TYPE taking the address of BASE
4367 offsetted by OFFSET units. */
4369 tree
4370 build_invariant_address (tree type, tree base, HOST_WIDE_INT offset)
4372 tree ref = fold_build2 (MEM_REF, TREE_TYPE (type),
4373 build_fold_addr_expr (base),
4374 build_int_cst (ptr_type_node, offset));
4375 tree addr = build1 (ADDR_EXPR, type, ref);
4376 recompute_tree_invariant_for_addr_expr (addr);
4377 return addr;
4380 /* Similar except don't specify the TREE_TYPE
4381 and leave the TREE_SIDE_EFFECTS as 0.
4382 It is permissible for arguments to be null,
4383 or even garbage if their values do not matter. */
4385 tree
4386 build_nt (enum tree_code code, ...)
4388 tree t;
4389 int length;
4390 int i;
4391 va_list p;
4393 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4395 va_start (p, code);
4397 t = make_node (code);
4398 length = TREE_CODE_LENGTH (code);
4400 for (i = 0; i < length; i++)
4401 TREE_OPERAND (t, i) = va_arg (p, tree);
4403 va_end (p);
4404 return t;
4407 /* Similar to build_nt, but for creating a CALL_EXPR object with a
4408 tree vec. */
4410 tree
4411 build_nt_call_vec (tree fn, vec<tree, va_gc> *args)
4413 tree ret, t;
4414 unsigned int ix;
4416 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
4417 CALL_EXPR_FN (ret) = fn;
4418 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
4419 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
4420 CALL_EXPR_ARG (ret, ix) = t;
4421 return ret;
4424 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
4425 We do NOT enter this node in any sort of symbol table.
4427 LOC is the location of the decl.
4429 layout_decl is used to set up the decl's storage layout.
4430 Other slots are initialized to 0 or null pointers. */
4432 tree
4433 build_decl_stat (location_t loc, enum tree_code code, tree name,
4434 tree type MEM_STAT_DECL)
4436 tree t;
4438 t = make_node_stat (code PASS_MEM_STAT);
4439 DECL_SOURCE_LOCATION (t) = loc;
4441 /* if (type == error_mark_node)
4442 type = integer_type_node; */
4443 /* That is not done, deliberately, so that having error_mark_node
4444 as the type can suppress useless errors in the use of this variable. */
4446 DECL_NAME (t) = name;
4447 TREE_TYPE (t) = type;
4449 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
4450 layout_decl (t, 0);
4452 return t;
4455 /* Builds and returns function declaration with NAME and TYPE. */
4457 tree
4458 build_fn_decl (const char *name, tree type)
4460 tree id = get_identifier (name);
4461 tree decl = build_decl (input_location, FUNCTION_DECL, id, type);
4463 DECL_EXTERNAL (decl) = 1;
4464 TREE_PUBLIC (decl) = 1;
4465 DECL_ARTIFICIAL (decl) = 1;
4466 TREE_NOTHROW (decl) = 1;
4468 return decl;
4471 vec<tree, va_gc> *all_translation_units;
4473 /* Builds a new translation-unit decl with name NAME, queues it in the
4474 global list of translation-unit decls and returns it. */
4476 tree
4477 build_translation_unit_decl (tree name)
4479 tree tu = build_decl (UNKNOWN_LOCATION, TRANSLATION_UNIT_DECL,
4480 name, NULL_TREE);
4481 TRANSLATION_UNIT_LANGUAGE (tu) = lang_hooks.name;
4482 vec_safe_push (all_translation_units, tu);
4483 return tu;
4487 /* BLOCK nodes are used to represent the structure of binding contours
4488 and declarations, once those contours have been exited and their contents
4489 compiled. This information is used for outputting debugging info. */
4491 tree
4492 build_block (tree vars, tree subblocks, tree supercontext, tree chain)
4494 tree block = make_node (BLOCK);
4496 BLOCK_VARS (block) = vars;
4497 BLOCK_SUBBLOCKS (block) = subblocks;
4498 BLOCK_SUPERCONTEXT (block) = supercontext;
4499 BLOCK_CHAIN (block) = chain;
4500 return block;
4504 /* Like SET_EXPR_LOCATION, but make sure the tree can have a location.
4506 LOC is the location to use in tree T. */
4508 void
4509 protected_set_expr_location (tree t, location_t loc)
4511 if (t && CAN_HAVE_LOCATION_P (t))
4512 SET_EXPR_LOCATION (t, loc);
4515 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
4516 is ATTRIBUTE. */
4518 tree
4519 build_decl_attribute_variant (tree ddecl, tree attribute)
4521 DECL_ATTRIBUTES (ddecl) = attribute;
4522 return ddecl;
4525 /* Borrowed from hashtab.c iterative_hash implementation. */
4526 #define mix(a,b,c) \
4528 a -= b; a -= c; a ^= (c>>13); \
4529 b -= c; b -= a; b ^= (a<< 8); \
4530 c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \
4531 a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \
4532 b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \
4533 c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \
4534 a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \
4535 b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \
4536 c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \
4540 /* Produce good hash value combining VAL and VAL2. */
4541 hashval_t
4542 iterative_hash_hashval_t (hashval_t val, hashval_t val2)
4544 /* the golden ratio; an arbitrary value. */
4545 hashval_t a = 0x9e3779b9;
4547 mix (a, val, val2);
4548 return val2;
4551 /* Produce good hash value combining VAL and VAL2. */
4552 hashval_t
4553 iterative_hash_host_wide_int (HOST_WIDE_INT val, hashval_t val2)
4555 if (sizeof (HOST_WIDE_INT) == sizeof (hashval_t))
4556 return iterative_hash_hashval_t (val, val2);
4557 else
4559 hashval_t a = (hashval_t) val;
4560 /* Avoid warnings about shifting of more than the width of the type on
4561 hosts that won't execute this path. */
4562 int zero = 0;
4563 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 8 + zero));
4564 mix (a, b, val2);
4565 if (sizeof (HOST_WIDE_INT) > 2 * sizeof (hashval_t))
4567 hashval_t a = (hashval_t) (val >> (sizeof (hashval_t) * 16 + zero));
4568 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 24 + zero));
4569 mix (a, b, val2);
4571 return val2;
4575 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4576 is ATTRIBUTE and its qualifiers are QUALS.
4578 Record such modified types already made so we don't make duplicates. */
4580 tree
4581 build_type_attribute_qual_variant (tree ttype, tree attribute, int quals)
4583 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
4585 hashval_t hashcode = 0;
4586 tree ntype;
4587 int i;
4588 tree t;
4589 enum tree_code code = TREE_CODE (ttype);
4591 /* Building a distinct copy of a tagged type is inappropriate; it
4592 causes breakage in code that expects there to be a one-to-one
4593 relationship between a struct and its fields.
4594 build_duplicate_type is another solution (as used in
4595 handle_transparent_union_attribute), but that doesn't play well
4596 with the stronger C++ type identity model. */
4597 if (TREE_CODE (ttype) == RECORD_TYPE
4598 || TREE_CODE (ttype) == UNION_TYPE
4599 || TREE_CODE (ttype) == QUAL_UNION_TYPE
4600 || TREE_CODE (ttype) == ENUMERAL_TYPE)
4602 warning (OPT_Wattributes,
4603 "ignoring attributes applied to %qT after definition",
4604 TYPE_MAIN_VARIANT (ttype));
4605 return build_qualified_type (ttype, quals);
4608 ttype = build_qualified_type (ttype, TYPE_UNQUALIFIED);
4609 ntype = build_distinct_type_copy (ttype);
4611 TYPE_ATTRIBUTES (ntype) = attribute;
4613 hashcode = iterative_hash_object (code, hashcode);
4614 if (TREE_TYPE (ntype))
4615 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (ntype)),
4616 hashcode);
4617 hashcode = attribute_hash_list (attribute, hashcode);
4619 switch (TREE_CODE (ntype))
4621 case FUNCTION_TYPE:
4622 hashcode = type_hash_list (TYPE_ARG_TYPES (ntype), hashcode);
4623 break;
4624 case ARRAY_TYPE:
4625 if (TYPE_DOMAIN (ntype))
4626 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (ntype)),
4627 hashcode);
4628 break;
4629 case INTEGER_TYPE:
4630 t = TYPE_MAX_VALUE (ntype);
4631 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
4632 hashcode = iterative_hash_object (TREE_INT_CST_ELT (t, i), hashcode);
4633 break;
4634 case REAL_TYPE:
4635 case FIXED_POINT_TYPE:
4637 unsigned int precision = TYPE_PRECISION (ntype);
4638 hashcode = iterative_hash_object (precision, hashcode);
4640 break;
4641 default:
4642 break;
4645 ntype = type_hash_canon (hashcode, ntype);
4647 /* If the target-dependent attributes make NTYPE different from
4648 its canonical type, we will need to use structural equality
4649 checks for this type. */
4650 if (TYPE_STRUCTURAL_EQUALITY_P (ttype)
4651 || !comp_type_attributes (ntype, ttype))
4652 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
4653 else if (TYPE_CANONICAL (ntype) == ntype)
4654 TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype);
4656 ttype = build_qualified_type (ntype, quals);
4658 else if (TYPE_QUALS (ttype) != quals)
4659 ttype = build_qualified_type (ttype, quals);
4661 return ttype;
4664 /* Check if "omp declare simd" attribute arguments, CLAUSES1 and CLAUSES2, are
4665 the same. */
4667 static bool
4668 omp_declare_simd_clauses_equal (tree clauses1, tree clauses2)
4670 tree cl1, cl2;
4671 for (cl1 = clauses1, cl2 = clauses2;
4672 cl1 && cl2;
4673 cl1 = OMP_CLAUSE_CHAIN (cl1), cl2 = OMP_CLAUSE_CHAIN (cl2))
4675 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_CODE (cl2))
4676 return false;
4677 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_SIMDLEN)
4679 if (simple_cst_equal (OMP_CLAUSE_DECL (cl1),
4680 OMP_CLAUSE_DECL (cl2)) != 1)
4681 return false;
4683 switch (OMP_CLAUSE_CODE (cl1))
4685 case OMP_CLAUSE_ALIGNED:
4686 if (simple_cst_equal (OMP_CLAUSE_ALIGNED_ALIGNMENT (cl1),
4687 OMP_CLAUSE_ALIGNED_ALIGNMENT (cl2)) != 1)
4688 return false;
4689 break;
4690 case OMP_CLAUSE_LINEAR:
4691 if (simple_cst_equal (OMP_CLAUSE_LINEAR_STEP (cl1),
4692 OMP_CLAUSE_LINEAR_STEP (cl2)) != 1)
4693 return false;
4694 break;
4695 case OMP_CLAUSE_SIMDLEN:
4696 if (simple_cst_equal (OMP_CLAUSE_SIMDLEN_EXPR (cl1),
4697 OMP_CLAUSE_SIMDLEN_EXPR (cl2)) != 1)
4698 return false;
4699 default:
4700 break;
4703 return true;
4706 /* Compare two constructor-element-type constants. Return 1 if the lists
4707 are known to be equal; otherwise return 0. */
4709 static bool
4710 simple_cst_list_equal (const_tree l1, const_tree l2)
4712 while (l1 != NULL_TREE && l2 != NULL_TREE)
4714 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
4715 return false;
4717 l1 = TREE_CHAIN (l1);
4718 l2 = TREE_CHAIN (l2);
4721 return l1 == l2;
4724 /* Compare two attributes for their value identity. Return true if the
4725 attribute values are known to be equal; otherwise return false.
4728 static bool
4729 attribute_value_equal (const_tree attr1, const_tree attr2)
4731 if (TREE_VALUE (attr1) == TREE_VALUE (attr2))
4732 return true;
4734 if (TREE_VALUE (attr1) != NULL_TREE
4735 && TREE_CODE (TREE_VALUE (attr1)) == TREE_LIST
4736 && TREE_VALUE (attr2) != NULL
4737 && TREE_CODE (TREE_VALUE (attr2)) == TREE_LIST)
4738 return (simple_cst_list_equal (TREE_VALUE (attr1),
4739 TREE_VALUE (attr2)) == 1);
4741 if ((flag_openmp || flag_openmp_simd)
4742 && TREE_VALUE (attr1) && TREE_VALUE (attr2)
4743 && TREE_CODE (TREE_VALUE (attr1)) == OMP_CLAUSE
4744 && TREE_CODE (TREE_VALUE (attr2)) == OMP_CLAUSE)
4745 return omp_declare_simd_clauses_equal (TREE_VALUE (attr1),
4746 TREE_VALUE (attr2));
4748 return (simple_cst_equal (TREE_VALUE (attr1), TREE_VALUE (attr2)) == 1);
4751 /* Return 0 if the attributes for two types are incompatible, 1 if they
4752 are compatible, and 2 if they are nearly compatible (which causes a
4753 warning to be generated). */
4755 comp_type_attributes (const_tree type1, const_tree type2)
4757 const_tree a1 = TYPE_ATTRIBUTES (type1);
4758 const_tree a2 = TYPE_ATTRIBUTES (type2);
4759 const_tree a;
4761 if (a1 == a2)
4762 return 1;
4763 for (a = a1; a != NULL_TREE; a = TREE_CHAIN (a))
4765 const struct attribute_spec *as;
4766 const_tree attr;
4768 as = lookup_attribute_spec (get_attribute_name (a));
4769 if (!as || as->affects_type_identity == false)
4770 continue;
4772 attr = lookup_attribute (as->name, CONST_CAST_TREE (a2));
4773 if (!attr || !attribute_value_equal (a, attr))
4774 break;
4776 if (!a)
4778 for (a = a2; a != NULL_TREE; a = TREE_CHAIN (a))
4780 const struct attribute_spec *as;
4782 as = lookup_attribute_spec (get_attribute_name (a));
4783 if (!as || as->affects_type_identity == false)
4784 continue;
4786 if (!lookup_attribute (as->name, CONST_CAST_TREE (a1)))
4787 break;
4788 /* We don't need to compare trees again, as we did this
4789 already in first loop. */
4791 /* All types - affecting identity - are equal, so
4792 there is no need to call target hook for comparison. */
4793 if (!a)
4794 return 1;
4796 /* As some type combinations - like default calling-convention - might
4797 be compatible, we have to call the target hook to get the final result. */
4798 return targetm.comp_type_attributes (type1, type2);
4801 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4802 is ATTRIBUTE.
4804 Record such modified types already made so we don't make duplicates. */
4806 tree
4807 build_type_attribute_variant (tree ttype, tree attribute)
4809 return build_type_attribute_qual_variant (ttype, attribute,
4810 TYPE_QUALS (ttype));
4814 /* Reset the expression *EXPR_P, a size or position.
4816 ??? We could reset all non-constant sizes or positions. But it's cheap
4817 enough to not do so and refrain from adding workarounds to dwarf2out.c.
4819 We need to reset self-referential sizes or positions because they cannot
4820 be gimplified and thus can contain a CALL_EXPR after the gimplification
4821 is finished, which will run afoul of LTO streaming. And they need to be
4822 reset to something essentially dummy but not constant, so as to preserve
4823 the properties of the object they are attached to. */
4825 static inline void
4826 free_lang_data_in_one_sizepos (tree *expr_p)
4828 tree expr = *expr_p;
4829 if (CONTAINS_PLACEHOLDER_P (expr))
4830 *expr_p = build0 (PLACEHOLDER_EXPR, TREE_TYPE (expr));
4834 /* Reset all the fields in a binfo node BINFO. We only keep
4835 BINFO_VTABLE, which is used by gimple_fold_obj_type_ref. */
4837 static void
4838 free_lang_data_in_binfo (tree binfo)
4840 unsigned i;
4841 tree t;
4843 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
4845 BINFO_VIRTUALS (binfo) = NULL_TREE;
4846 BINFO_BASE_ACCESSES (binfo) = NULL;
4847 BINFO_INHERITANCE_CHAIN (binfo) = NULL_TREE;
4848 BINFO_SUBVTT_INDEX (binfo) = NULL_TREE;
4850 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (binfo), i, t)
4851 free_lang_data_in_binfo (t);
4855 /* Reset all language specific information still present in TYPE. */
4857 static void
4858 free_lang_data_in_type (tree type)
4860 gcc_assert (TYPE_P (type));
4862 /* Give the FE a chance to remove its own data first. */
4863 lang_hooks.free_lang_data (type);
4865 TREE_LANG_FLAG_0 (type) = 0;
4866 TREE_LANG_FLAG_1 (type) = 0;
4867 TREE_LANG_FLAG_2 (type) = 0;
4868 TREE_LANG_FLAG_3 (type) = 0;
4869 TREE_LANG_FLAG_4 (type) = 0;
4870 TREE_LANG_FLAG_5 (type) = 0;
4871 TREE_LANG_FLAG_6 (type) = 0;
4873 if (TREE_CODE (type) == FUNCTION_TYPE)
4875 /* Remove the const and volatile qualifiers from arguments. The
4876 C++ front end removes them, but the C front end does not,
4877 leading to false ODR violation errors when merging two
4878 instances of the same function signature compiled by
4879 different front ends. */
4880 tree p;
4882 for (p = TYPE_ARG_TYPES (type); p; p = TREE_CHAIN (p))
4884 tree arg_type = TREE_VALUE (p);
4886 if (TYPE_READONLY (arg_type) || TYPE_VOLATILE (arg_type))
4888 int quals = TYPE_QUALS (arg_type)
4889 & ~TYPE_QUAL_CONST
4890 & ~TYPE_QUAL_VOLATILE;
4891 TREE_VALUE (p) = build_qualified_type (arg_type, quals);
4892 free_lang_data_in_type (TREE_VALUE (p));
4897 /* Remove members that are not actually FIELD_DECLs from the field
4898 list of an aggregate. These occur in C++. */
4899 if (RECORD_OR_UNION_TYPE_P (type))
4901 tree prev, member;
4903 /* Note that TYPE_FIELDS can be shared across distinct
4904 TREE_TYPEs. Therefore, if the first field of TYPE_FIELDS is
4905 to be removed, we cannot set its TREE_CHAIN to NULL.
4906 Otherwise, we would not be able to find all the other fields
4907 in the other instances of this TREE_TYPE.
4909 This was causing an ICE in testsuite/g++.dg/lto/20080915.C. */
4910 prev = NULL_TREE;
4911 member = TYPE_FIELDS (type);
4912 while (member)
4914 if (TREE_CODE (member) == FIELD_DECL
4915 || TREE_CODE (member) == TYPE_DECL)
4917 if (prev)
4918 TREE_CHAIN (prev) = member;
4919 else
4920 TYPE_FIELDS (type) = member;
4921 prev = member;
4924 member = TREE_CHAIN (member);
4927 if (prev)
4928 TREE_CHAIN (prev) = NULL_TREE;
4929 else
4930 TYPE_FIELDS (type) = NULL_TREE;
4932 TYPE_METHODS (type) = NULL_TREE;
4933 if (TYPE_BINFO (type))
4934 free_lang_data_in_binfo (TYPE_BINFO (type));
4936 else
4938 /* For non-aggregate types, clear out the language slot (which
4939 overloads TYPE_BINFO). */
4940 TYPE_LANG_SLOT_1 (type) = NULL_TREE;
4942 if (INTEGRAL_TYPE_P (type)
4943 || SCALAR_FLOAT_TYPE_P (type)
4944 || FIXED_POINT_TYPE_P (type))
4946 free_lang_data_in_one_sizepos (&TYPE_MIN_VALUE (type));
4947 free_lang_data_in_one_sizepos (&TYPE_MAX_VALUE (type));
4951 free_lang_data_in_one_sizepos (&TYPE_SIZE (type));
4952 free_lang_data_in_one_sizepos (&TYPE_SIZE_UNIT (type));
4954 if (TYPE_CONTEXT (type)
4955 && TREE_CODE (TYPE_CONTEXT (type)) == BLOCK)
4957 tree ctx = TYPE_CONTEXT (type);
4960 ctx = BLOCK_SUPERCONTEXT (ctx);
4962 while (ctx && TREE_CODE (ctx) == BLOCK);
4963 TYPE_CONTEXT (type) = ctx;
4968 /* Return true if DECL may need an assembler name to be set. */
4970 static inline bool
4971 need_assembler_name_p (tree decl)
4973 /* Only FUNCTION_DECLs and VAR_DECLs are considered. */
4974 if (TREE_CODE (decl) != FUNCTION_DECL
4975 && TREE_CODE (decl) != VAR_DECL)
4976 return false;
4978 /* If DECL already has its assembler name set, it does not need a
4979 new one. */
4980 if (!HAS_DECL_ASSEMBLER_NAME_P (decl)
4981 || DECL_ASSEMBLER_NAME_SET_P (decl))
4982 return false;
4984 /* Abstract decls do not need an assembler name. */
4985 if (DECL_ABSTRACT (decl))
4986 return false;
4988 /* For VAR_DECLs, only static, public and external symbols need an
4989 assembler name. */
4990 if (TREE_CODE (decl) == VAR_DECL
4991 && !TREE_STATIC (decl)
4992 && !TREE_PUBLIC (decl)
4993 && !DECL_EXTERNAL (decl))
4994 return false;
4996 if (TREE_CODE (decl) == FUNCTION_DECL)
4998 /* Do not set assembler name on builtins. Allow RTL expansion to
4999 decide whether to expand inline or via a regular call. */
5000 if (DECL_BUILT_IN (decl)
5001 && DECL_BUILT_IN_CLASS (decl) != BUILT_IN_FRONTEND)
5002 return false;
5004 /* Functions represented in the callgraph need an assembler name. */
5005 if (cgraph_get_node (decl) != NULL)
5006 return true;
5008 /* Unused and not public functions don't need an assembler name. */
5009 if (!TREE_USED (decl) && !TREE_PUBLIC (decl))
5010 return false;
5013 return true;
5017 /* Reset all language specific information still present in symbol
5018 DECL. */
5020 static void
5021 free_lang_data_in_decl (tree decl)
5023 gcc_assert (DECL_P (decl));
5025 /* Give the FE a chance to remove its own data first. */
5026 lang_hooks.free_lang_data (decl);
5028 TREE_LANG_FLAG_0 (decl) = 0;
5029 TREE_LANG_FLAG_1 (decl) = 0;
5030 TREE_LANG_FLAG_2 (decl) = 0;
5031 TREE_LANG_FLAG_3 (decl) = 0;
5032 TREE_LANG_FLAG_4 (decl) = 0;
5033 TREE_LANG_FLAG_5 (decl) = 0;
5034 TREE_LANG_FLAG_6 (decl) = 0;
5036 free_lang_data_in_one_sizepos (&DECL_SIZE (decl));
5037 free_lang_data_in_one_sizepos (&DECL_SIZE_UNIT (decl));
5038 if (TREE_CODE (decl) == FIELD_DECL)
5040 free_lang_data_in_one_sizepos (&DECL_FIELD_OFFSET (decl));
5041 if (TREE_CODE (DECL_CONTEXT (decl)) == QUAL_UNION_TYPE)
5042 DECL_QUALIFIER (decl) = NULL_TREE;
5045 if (TREE_CODE (decl) == FUNCTION_DECL)
5047 struct cgraph_node *node;
5048 if (!(node = cgraph_get_node (decl))
5049 || (!node->definition && !node->clones))
5051 if (node)
5052 cgraph_release_function_body (node);
5053 else
5055 release_function_body (decl);
5056 DECL_ARGUMENTS (decl) = NULL;
5057 DECL_RESULT (decl) = NULL;
5058 DECL_INITIAL (decl) = error_mark_node;
5061 if (gimple_has_body_p (decl))
5063 tree t;
5065 /* If DECL has a gimple body, then the context for its
5066 arguments must be DECL. Otherwise, it doesn't really
5067 matter, as we will not be emitting any code for DECL. In
5068 general, there may be other instances of DECL created by
5069 the front end and since PARM_DECLs are generally shared,
5070 their DECL_CONTEXT changes as the replicas of DECL are
5071 created. The only time where DECL_CONTEXT is important
5072 is for the FUNCTION_DECLs that have a gimple body (since
5073 the PARM_DECL will be used in the function's body). */
5074 for (t = DECL_ARGUMENTS (decl); t; t = TREE_CHAIN (t))
5075 DECL_CONTEXT (t) = decl;
5078 /* DECL_SAVED_TREE holds the GENERIC representation for DECL.
5079 At this point, it is not needed anymore. */
5080 DECL_SAVED_TREE (decl) = NULL_TREE;
5082 /* Clear the abstract origin if it refers to a method. Otherwise
5083 dwarf2out.c will ICE as we clear TYPE_METHODS and thus the
5084 origin will not be output correctly. */
5085 if (DECL_ABSTRACT_ORIGIN (decl)
5086 && DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))
5087 && RECORD_OR_UNION_TYPE_P
5088 (DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))))
5089 DECL_ABSTRACT_ORIGIN (decl) = NULL_TREE;
5091 /* Sometimes the C++ frontend doesn't manage to transform a temporary
5092 DECL_VINDEX referring to itself into a vtable slot number as it
5093 should. Happens with functions that are copied and then forgotten
5094 about. Just clear it, it won't matter anymore. */
5095 if (DECL_VINDEX (decl) && !tree_fits_shwi_p (DECL_VINDEX (decl)))
5096 DECL_VINDEX (decl) = NULL_TREE;
5098 else if (TREE_CODE (decl) == VAR_DECL)
5100 if ((DECL_EXTERNAL (decl)
5101 && (!TREE_STATIC (decl) || !TREE_READONLY (decl)))
5102 || (decl_function_context (decl) && !TREE_STATIC (decl)))
5103 DECL_INITIAL (decl) = NULL_TREE;
5105 else if (TREE_CODE (decl) == TYPE_DECL
5106 || TREE_CODE (decl) == FIELD_DECL)
5107 DECL_INITIAL (decl) = NULL_TREE;
5108 else if (TREE_CODE (decl) == TRANSLATION_UNIT_DECL
5109 && DECL_INITIAL (decl)
5110 && TREE_CODE (DECL_INITIAL (decl)) == BLOCK)
5112 /* Strip builtins from the translation-unit BLOCK. We still have targets
5113 without builtin_decl_explicit support and also builtins are shared
5114 nodes and thus we can't use TREE_CHAIN in multiple lists. */
5115 tree *nextp = &BLOCK_VARS (DECL_INITIAL (decl));
5116 while (*nextp)
5118 tree var = *nextp;
5119 if (TREE_CODE (var) == FUNCTION_DECL
5120 && DECL_BUILT_IN (var))
5121 *nextp = TREE_CHAIN (var);
5122 else
5123 nextp = &TREE_CHAIN (var);
5129 /* Data used when collecting DECLs and TYPEs for language data removal. */
5131 struct free_lang_data_d
5133 /* Worklist to avoid excessive recursion. */
5134 vec<tree> worklist;
5136 /* Set of traversed objects. Used to avoid duplicate visits. */
5137 struct pointer_set_t *pset;
5139 /* Array of symbols to process with free_lang_data_in_decl. */
5140 vec<tree> decls;
5142 /* Array of types to process with free_lang_data_in_type. */
5143 vec<tree> types;
5147 /* Save all language fields needed to generate proper debug information
5148 for DECL. This saves most fields cleared out by free_lang_data_in_decl. */
5150 static void
5151 save_debug_info_for_decl (tree t)
5153 /*struct saved_debug_info_d *sdi;*/
5155 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && DECL_P (t));
5157 /* FIXME. Partial implementation for saving debug info removed. */
5161 /* Save all language fields needed to generate proper debug information
5162 for TYPE. This saves most fields cleared out by free_lang_data_in_type. */
5164 static void
5165 save_debug_info_for_type (tree t)
5167 /*struct saved_debug_info_d *sdi;*/
5169 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && TYPE_P (t));
5171 /* FIXME. Partial implementation for saving debug info removed. */
5175 /* Add type or decl T to one of the list of tree nodes that need their
5176 language data removed. The lists are held inside FLD. */
5178 static void
5179 add_tree_to_fld_list (tree t, struct free_lang_data_d *fld)
5181 if (DECL_P (t))
5183 fld->decls.safe_push (t);
5184 if (debug_info_level > DINFO_LEVEL_TERSE)
5185 save_debug_info_for_decl (t);
5187 else if (TYPE_P (t))
5189 fld->types.safe_push (t);
5190 if (debug_info_level > DINFO_LEVEL_TERSE)
5191 save_debug_info_for_type (t);
5193 else
5194 gcc_unreachable ();
5197 /* Push tree node T into FLD->WORKLIST. */
5199 static inline void
5200 fld_worklist_push (tree t, struct free_lang_data_d *fld)
5202 if (t && !is_lang_specific (t) && !pointer_set_contains (fld->pset, t))
5203 fld->worklist.safe_push ((t));
5207 /* Operand callback helper for free_lang_data_in_node. *TP is the
5208 subtree operand being considered. */
5210 static tree
5211 find_decls_types_r (tree *tp, int *ws, void *data)
5213 tree t = *tp;
5214 struct free_lang_data_d *fld = (struct free_lang_data_d *) data;
5216 if (TREE_CODE (t) == TREE_LIST)
5217 return NULL_TREE;
5219 /* Language specific nodes will be removed, so there is no need
5220 to gather anything under them. */
5221 if (is_lang_specific (t))
5223 *ws = 0;
5224 return NULL_TREE;
5227 if (DECL_P (t))
5229 /* Note that walk_tree does not traverse every possible field in
5230 decls, so we have to do our own traversals here. */
5231 add_tree_to_fld_list (t, fld);
5233 fld_worklist_push (DECL_NAME (t), fld);
5234 fld_worklist_push (DECL_CONTEXT (t), fld);
5235 fld_worklist_push (DECL_SIZE (t), fld);
5236 fld_worklist_push (DECL_SIZE_UNIT (t), fld);
5238 /* We are going to remove everything under DECL_INITIAL for
5239 TYPE_DECLs. No point walking them. */
5240 if (TREE_CODE (t) != TYPE_DECL)
5241 fld_worklist_push (DECL_INITIAL (t), fld);
5243 fld_worklist_push (DECL_ATTRIBUTES (t), fld);
5244 fld_worklist_push (DECL_ABSTRACT_ORIGIN (t), fld);
5246 if (TREE_CODE (t) == FUNCTION_DECL)
5248 fld_worklist_push (DECL_ARGUMENTS (t), fld);
5249 fld_worklist_push (DECL_RESULT (t), fld);
5251 else if (TREE_CODE (t) == TYPE_DECL)
5253 fld_worklist_push (DECL_ARGUMENT_FLD (t), fld);
5254 fld_worklist_push (DECL_VINDEX (t), fld);
5255 fld_worklist_push (DECL_ORIGINAL_TYPE (t), fld);
5257 else if (TREE_CODE (t) == FIELD_DECL)
5259 fld_worklist_push (DECL_FIELD_OFFSET (t), fld);
5260 fld_worklist_push (DECL_BIT_FIELD_TYPE (t), fld);
5261 fld_worklist_push (DECL_FIELD_BIT_OFFSET (t), fld);
5262 fld_worklist_push (DECL_FCONTEXT (t), fld);
5264 else if (TREE_CODE (t) == VAR_DECL)
5266 fld_worklist_push (DECL_SECTION_NAME (t), fld);
5267 fld_worklist_push (DECL_COMDAT_GROUP (t), fld);
5270 if ((TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
5271 && DECL_HAS_VALUE_EXPR_P (t))
5272 fld_worklist_push (DECL_VALUE_EXPR (t), fld);
5274 if (TREE_CODE (t) != FIELD_DECL
5275 && TREE_CODE (t) != TYPE_DECL)
5276 fld_worklist_push (TREE_CHAIN (t), fld);
5277 *ws = 0;
5279 else if (TYPE_P (t))
5281 /* Note that walk_tree does not traverse every possible field in
5282 types, so we have to do our own traversals here. */
5283 add_tree_to_fld_list (t, fld);
5285 if (!RECORD_OR_UNION_TYPE_P (t))
5286 fld_worklist_push (TYPE_CACHED_VALUES (t), fld);
5287 fld_worklist_push (TYPE_SIZE (t), fld);
5288 fld_worklist_push (TYPE_SIZE_UNIT (t), fld);
5289 fld_worklist_push (TYPE_ATTRIBUTES (t), fld);
5290 fld_worklist_push (TYPE_POINTER_TO (t), fld);
5291 fld_worklist_push (TYPE_REFERENCE_TO (t), fld);
5292 fld_worklist_push (TYPE_NAME (t), fld);
5293 /* Do not walk TYPE_NEXT_PTR_TO or TYPE_NEXT_REF_TO. We do not stream
5294 them and thus do not and want not to reach unused pointer types
5295 this way. */
5296 if (!POINTER_TYPE_P (t))
5297 fld_worklist_push (TYPE_MINVAL (t), fld);
5298 if (!RECORD_OR_UNION_TYPE_P (t))
5299 fld_worklist_push (TYPE_MAXVAL (t), fld);
5300 fld_worklist_push (TYPE_MAIN_VARIANT (t), fld);
5301 /* Do not walk TYPE_NEXT_VARIANT. We do not stream it and thus
5302 do not and want not to reach unused variants this way. */
5303 if (TYPE_CONTEXT (t))
5305 tree ctx = TYPE_CONTEXT (t);
5306 /* We adjust BLOCK TYPE_CONTEXTs to the innermost non-BLOCK one.
5307 So push that instead. */
5308 while (ctx && TREE_CODE (ctx) == BLOCK)
5309 ctx = BLOCK_SUPERCONTEXT (ctx);
5310 fld_worklist_push (ctx, fld);
5312 /* Do not walk TYPE_CANONICAL. We do not stream it and thus do not
5313 and want not to reach unused types this way. */
5315 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t))
5317 unsigned i;
5318 tree tem;
5319 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (TYPE_BINFO (t)), i, tem)
5320 fld_worklist_push (TREE_TYPE (tem), fld);
5321 tem = BINFO_VIRTUALS (TYPE_BINFO (t));
5322 if (tem
5323 /* The Java FE overloads BINFO_VIRTUALS for its own purpose. */
5324 && TREE_CODE (tem) == TREE_LIST)
5327 fld_worklist_push (TREE_VALUE (tem), fld);
5328 tem = TREE_CHAIN (tem);
5330 while (tem);
5332 if (RECORD_OR_UNION_TYPE_P (t))
5334 tree tem;
5335 /* Push all TYPE_FIELDS - there can be interleaving interesting
5336 and non-interesting things. */
5337 tem = TYPE_FIELDS (t);
5338 while (tem)
5340 if (TREE_CODE (tem) == FIELD_DECL
5341 || TREE_CODE (tem) == TYPE_DECL)
5342 fld_worklist_push (tem, fld);
5343 tem = TREE_CHAIN (tem);
5347 fld_worklist_push (TYPE_STUB_DECL (t), fld);
5348 *ws = 0;
5350 else if (TREE_CODE (t) == BLOCK)
5352 tree tem;
5353 for (tem = BLOCK_VARS (t); tem; tem = TREE_CHAIN (tem))
5354 fld_worklist_push (tem, fld);
5355 for (tem = BLOCK_SUBBLOCKS (t); tem; tem = BLOCK_CHAIN (tem))
5356 fld_worklist_push (tem, fld);
5357 fld_worklist_push (BLOCK_ABSTRACT_ORIGIN (t), fld);
5360 if (TREE_CODE (t) != IDENTIFIER_NODE
5361 && CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED))
5362 fld_worklist_push (TREE_TYPE (t), fld);
5364 return NULL_TREE;
5368 /* Find decls and types in T. */
5370 static void
5371 find_decls_types (tree t, struct free_lang_data_d *fld)
5373 while (1)
5375 if (!pointer_set_contains (fld->pset, t))
5376 walk_tree (&t, find_decls_types_r, fld, fld->pset);
5377 if (fld->worklist.is_empty ())
5378 break;
5379 t = fld->worklist.pop ();
5383 /* Translate all the types in LIST with the corresponding runtime
5384 types. */
5386 static tree
5387 get_eh_types_for_runtime (tree list)
5389 tree head, prev;
5391 if (list == NULL_TREE)
5392 return NULL_TREE;
5394 head = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5395 prev = head;
5396 list = TREE_CHAIN (list);
5397 while (list)
5399 tree n = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5400 TREE_CHAIN (prev) = n;
5401 prev = TREE_CHAIN (prev);
5402 list = TREE_CHAIN (list);
5405 return head;
5409 /* Find decls and types referenced in EH region R and store them in
5410 FLD->DECLS and FLD->TYPES. */
5412 static void
5413 find_decls_types_in_eh_region (eh_region r, struct free_lang_data_d *fld)
5415 switch (r->type)
5417 case ERT_CLEANUP:
5418 break;
5420 case ERT_TRY:
5422 eh_catch c;
5424 /* The types referenced in each catch must first be changed to the
5425 EH types used at runtime. This removes references to FE types
5426 in the region. */
5427 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
5429 c->type_list = get_eh_types_for_runtime (c->type_list);
5430 walk_tree (&c->type_list, find_decls_types_r, fld, fld->pset);
5433 break;
5435 case ERT_ALLOWED_EXCEPTIONS:
5436 r->u.allowed.type_list
5437 = get_eh_types_for_runtime (r->u.allowed.type_list);
5438 walk_tree (&r->u.allowed.type_list, find_decls_types_r, fld, fld->pset);
5439 break;
5441 case ERT_MUST_NOT_THROW:
5442 walk_tree (&r->u.must_not_throw.failure_decl,
5443 find_decls_types_r, fld, fld->pset);
5444 break;
5449 /* Find decls and types referenced in cgraph node N and store them in
5450 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5451 look for *every* kind of DECL and TYPE node reachable from N,
5452 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5453 NAMESPACE_DECLs, etc). */
5455 static void
5456 find_decls_types_in_node (struct cgraph_node *n, struct free_lang_data_d *fld)
5458 basic_block bb;
5459 struct function *fn;
5460 unsigned ix;
5461 tree t;
5463 find_decls_types (n->decl, fld);
5465 if (!gimple_has_body_p (n->decl))
5466 return;
5468 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
5470 fn = DECL_STRUCT_FUNCTION (n->decl);
5472 /* Traverse locals. */
5473 FOR_EACH_LOCAL_DECL (fn, ix, t)
5474 find_decls_types (t, fld);
5476 /* Traverse EH regions in FN. */
5478 eh_region r;
5479 FOR_ALL_EH_REGION_FN (r, fn)
5480 find_decls_types_in_eh_region (r, fld);
5483 /* Traverse every statement in FN. */
5484 FOR_EACH_BB_FN (bb, fn)
5486 gimple_stmt_iterator si;
5487 unsigned i;
5489 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
5491 gimple phi = gsi_stmt (si);
5493 for (i = 0; i < gimple_phi_num_args (phi); i++)
5495 tree *arg_p = gimple_phi_arg_def_ptr (phi, i);
5496 find_decls_types (*arg_p, fld);
5500 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
5502 gimple stmt = gsi_stmt (si);
5504 if (is_gimple_call (stmt))
5505 find_decls_types (gimple_call_fntype (stmt), fld);
5507 for (i = 0; i < gimple_num_ops (stmt); i++)
5509 tree arg = gimple_op (stmt, i);
5510 find_decls_types (arg, fld);
5517 /* Find decls and types referenced in varpool node N and store them in
5518 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5519 look for *every* kind of DECL and TYPE node reachable from N,
5520 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5521 NAMESPACE_DECLs, etc). */
5523 static void
5524 find_decls_types_in_var (varpool_node *v, struct free_lang_data_d *fld)
5526 find_decls_types (v->decl, fld);
5529 /* If T needs an assembler name, have one created for it. */
5531 void
5532 assign_assembler_name_if_neeeded (tree t)
5534 if (need_assembler_name_p (t))
5536 /* When setting DECL_ASSEMBLER_NAME, the C++ mangler may emit
5537 diagnostics that use input_location to show locus
5538 information. The problem here is that, at this point,
5539 input_location is generally anchored to the end of the file
5540 (since the parser is long gone), so we don't have a good
5541 position to pin it to.
5543 To alleviate this problem, this uses the location of T's
5544 declaration. Examples of this are
5545 testsuite/g++.dg/template/cond2.C and
5546 testsuite/g++.dg/template/pr35240.C. */
5547 location_t saved_location = input_location;
5548 input_location = DECL_SOURCE_LOCATION (t);
5550 decl_assembler_name (t);
5552 input_location = saved_location;
5557 /* Free language specific information for every operand and expression
5558 in every node of the call graph. This process operates in three stages:
5560 1- Every callgraph node and varpool node is traversed looking for
5561 decls and types embedded in them. This is a more exhaustive
5562 search than that done by find_referenced_vars, because it will
5563 also collect individual fields, decls embedded in types, etc.
5565 2- All the decls found are sent to free_lang_data_in_decl.
5567 3- All the types found are sent to free_lang_data_in_type.
5569 The ordering between decls and types is important because
5570 free_lang_data_in_decl sets assembler names, which includes
5571 mangling. So types cannot be freed up until assembler names have
5572 been set up. */
5574 static void
5575 free_lang_data_in_cgraph (void)
5577 struct cgraph_node *n;
5578 varpool_node *v;
5579 struct free_lang_data_d fld;
5580 tree t;
5581 unsigned i;
5582 alias_pair *p;
5584 /* Initialize sets and arrays to store referenced decls and types. */
5585 fld.pset = pointer_set_create ();
5586 fld.worklist.create (0);
5587 fld.decls.create (100);
5588 fld.types.create (100);
5590 /* Find decls and types in the body of every function in the callgraph. */
5591 FOR_EACH_FUNCTION (n)
5592 find_decls_types_in_node (n, &fld);
5594 FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
5595 find_decls_types (p->decl, &fld);
5597 /* Find decls and types in every varpool symbol. */
5598 FOR_EACH_VARIABLE (v)
5599 find_decls_types_in_var (v, &fld);
5601 /* Set the assembler name on every decl found. We need to do this
5602 now because free_lang_data_in_decl will invalidate data needed
5603 for mangling. This breaks mangling on interdependent decls. */
5604 FOR_EACH_VEC_ELT (fld.decls, i, t)
5605 assign_assembler_name_if_neeeded (t);
5607 /* Traverse every decl found freeing its language data. */
5608 FOR_EACH_VEC_ELT (fld.decls, i, t)
5609 free_lang_data_in_decl (t);
5611 /* Traverse every type found freeing its language data. */
5612 FOR_EACH_VEC_ELT (fld.types, i, t)
5613 free_lang_data_in_type (t);
5615 pointer_set_destroy (fld.pset);
5616 fld.worklist.release ();
5617 fld.decls.release ();
5618 fld.types.release ();
5622 /* Free resources that are used by FE but are not needed once they are done. */
5624 static unsigned
5625 free_lang_data (void)
5627 unsigned i;
5629 /* If we are the LTO frontend we have freed lang-specific data already. */
5630 if (in_lto_p
5631 || !flag_generate_lto)
5632 return 0;
5634 /* Allocate and assign alias sets to the standard integer types
5635 while the slots are still in the way the frontends generated them. */
5636 for (i = 0; i < itk_none; ++i)
5637 if (integer_types[i])
5638 TYPE_ALIAS_SET (integer_types[i]) = get_alias_set (integer_types[i]);
5640 /* Traverse the IL resetting language specific information for
5641 operands, expressions, etc. */
5642 free_lang_data_in_cgraph ();
5644 /* Create gimple variants for common types. */
5645 ptrdiff_type_node = integer_type_node;
5646 fileptr_type_node = ptr_type_node;
5648 /* Reset some langhooks. Do not reset types_compatible_p, it may
5649 still be used indirectly via the get_alias_set langhook. */
5650 lang_hooks.dwarf_name = lhd_dwarf_name;
5651 lang_hooks.decl_printable_name = gimple_decl_printable_name;
5652 /* We do not want the default decl_assembler_name implementation,
5653 rather if we have fixed everything we want a wrapper around it
5654 asserting that all non-local symbols already got their assembler
5655 name and only produce assembler names for local symbols. Or rather
5656 make sure we never call decl_assembler_name on local symbols and
5657 devise a separate, middle-end private scheme for it. */
5659 /* Reset diagnostic machinery. */
5660 tree_diagnostics_defaults (global_dc);
5662 return 0;
5666 namespace {
5668 const pass_data pass_data_ipa_free_lang_data =
5670 SIMPLE_IPA_PASS, /* type */
5671 "*free_lang_data", /* name */
5672 OPTGROUP_NONE, /* optinfo_flags */
5673 true, /* has_execute */
5674 TV_IPA_FREE_LANG_DATA, /* tv_id */
5675 0, /* properties_required */
5676 0, /* properties_provided */
5677 0, /* properties_destroyed */
5678 0, /* todo_flags_start */
5679 0, /* todo_flags_finish */
5682 class pass_ipa_free_lang_data : public simple_ipa_opt_pass
5684 public:
5685 pass_ipa_free_lang_data (gcc::context *ctxt)
5686 : simple_ipa_opt_pass (pass_data_ipa_free_lang_data, ctxt)
5689 /* opt_pass methods: */
5690 virtual unsigned int execute (function *) { return free_lang_data (); }
5692 }; // class pass_ipa_free_lang_data
5694 } // anon namespace
5696 simple_ipa_opt_pass *
5697 make_pass_ipa_free_lang_data (gcc::context *ctxt)
5699 return new pass_ipa_free_lang_data (ctxt);
5702 /* The backbone of is_attribute_p(). ATTR_LEN is the string length of
5703 ATTR_NAME. Also used internally by remove_attribute(). */
5704 bool
5705 private_is_attribute_p (const char *attr_name, size_t attr_len, const_tree ident)
5707 size_t ident_len = IDENTIFIER_LENGTH (ident);
5709 if (ident_len == attr_len)
5711 if (strcmp (attr_name, IDENTIFIER_POINTER (ident)) == 0)
5712 return true;
5714 else if (ident_len == attr_len + 4)
5716 /* There is the possibility that ATTR is 'text' and IDENT is
5717 '__text__'. */
5718 const char *p = IDENTIFIER_POINTER (ident);
5719 if (p[0] == '_' && p[1] == '_'
5720 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5721 && strncmp (attr_name, p + 2, attr_len) == 0)
5722 return true;
5725 return false;
5728 /* The backbone of lookup_attribute(). ATTR_LEN is the string length
5729 of ATTR_NAME, and LIST is not NULL_TREE. */
5730 tree
5731 private_lookup_attribute (const char *attr_name, size_t attr_len, tree list)
5733 while (list)
5735 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5737 if (ident_len == attr_len)
5739 if (!strcmp (attr_name,
5740 IDENTIFIER_POINTER (get_attribute_name (list))))
5741 break;
5743 /* TODO: If we made sure that attributes were stored in the
5744 canonical form without '__...__' (ie, as in 'text' as opposed
5745 to '__text__') then we could avoid the following case. */
5746 else if (ident_len == attr_len + 4)
5748 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5749 if (p[0] == '_' && p[1] == '_'
5750 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5751 && strncmp (attr_name, p + 2, attr_len) == 0)
5752 break;
5754 list = TREE_CHAIN (list);
5757 return list;
5760 /* A variant of lookup_attribute() that can be used with an identifier
5761 as the first argument, and where the identifier can be either
5762 'text' or '__text__'.
5764 Given an attribute ATTR_IDENTIFIER, and a list of attributes LIST,
5765 return a pointer to the attribute's list element if the attribute
5766 is part of the list, or NULL_TREE if not found. If the attribute
5767 appears more than once, this only returns the first occurrence; the
5768 TREE_CHAIN of the return value should be passed back in if further
5769 occurrences are wanted. ATTR_IDENTIFIER must be an identifier but
5770 can be in the form 'text' or '__text__'. */
5771 static tree
5772 lookup_ident_attribute (tree attr_identifier, tree list)
5774 gcc_checking_assert (TREE_CODE (attr_identifier) == IDENTIFIER_NODE);
5776 while (list)
5778 gcc_checking_assert (TREE_CODE (get_attribute_name (list))
5779 == IDENTIFIER_NODE);
5781 /* Identifiers can be compared directly for equality. */
5782 if (attr_identifier == get_attribute_name (list))
5783 break;
5785 /* If they are not equal, they may still be one in the form
5786 'text' while the other one is in the form '__text__'. TODO:
5787 If we were storing attributes in normalized 'text' form, then
5788 this could all go away and we could take full advantage of
5789 the fact that we're comparing identifiers. :-) */
5791 size_t attr_len = IDENTIFIER_LENGTH (attr_identifier);
5792 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
5794 if (ident_len == attr_len + 4)
5796 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5797 const char *q = IDENTIFIER_POINTER (attr_identifier);
5798 if (p[0] == '_' && p[1] == '_'
5799 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
5800 && strncmp (q, p + 2, attr_len) == 0)
5801 break;
5803 else if (ident_len + 4 == attr_len)
5805 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
5806 const char *q = IDENTIFIER_POINTER (attr_identifier);
5807 if (q[0] == '_' && q[1] == '_'
5808 && q[attr_len - 2] == '_' && q[attr_len - 1] == '_'
5809 && strncmp (q + 2, p, ident_len) == 0)
5810 break;
5813 list = TREE_CHAIN (list);
5816 return list;
5819 /* Remove any instances of attribute ATTR_NAME in LIST and return the
5820 modified list. */
5822 tree
5823 remove_attribute (const char *attr_name, tree list)
5825 tree *p;
5826 size_t attr_len = strlen (attr_name);
5828 gcc_checking_assert (attr_name[0] != '_');
5830 for (p = &list; *p; )
5832 tree l = *p;
5833 /* TODO: If we were storing attributes in normalized form, here
5834 we could use a simple strcmp(). */
5835 if (private_is_attribute_p (attr_name, attr_len, get_attribute_name (l)))
5836 *p = TREE_CHAIN (l);
5837 else
5838 p = &TREE_CHAIN (l);
5841 return list;
5844 /* Return an attribute list that is the union of a1 and a2. */
5846 tree
5847 merge_attributes (tree a1, tree a2)
5849 tree attributes;
5851 /* Either one unset? Take the set one. */
5853 if ((attributes = a1) == 0)
5854 attributes = a2;
5856 /* One that completely contains the other? Take it. */
5858 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
5860 if (attribute_list_contained (a2, a1))
5861 attributes = a2;
5862 else
5864 /* Pick the longest list, and hang on the other list. */
5866 if (list_length (a1) < list_length (a2))
5867 attributes = a2, a2 = a1;
5869 for (; a2 != 0; a2 = TREE_CHAIN (a2))
5871 tree a;
5872 for (a = lookup_ident_attribute (get_attribute_name (a2),
5873 attributes);
5874 a != NULL_TREE && !attribute_value_equal (a, a2);
5875 a = lookup_ident_attribute (get_attribute_name (a2),
5876 TREE_CHAIN (a)))
5878 if (a == NULL_TREE)
5880 a1 = copy_node (a2);
5881 TREE_CHAIN (a1) = attributes;
5882 attributes = a1;
5887 return attributes;
5890 /* Given types T1 and T2, merge their attributes and return
5891 the result. */
5893 tree
5894 merge_type_attributes (tree t1, tree t2)
5896 return merge_attributes (TYPE_ATTRIBUTES (t1),
5897 TYPE_ATTRIBUTES (t2));
5900 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
5901 the result. */
5903 tree
5904 merge_decl_attributes (tree olddecl, tree newdecl)
5906 return merge_attributes (DECL_ATTRIBUTES (olddecl),
5907 DECL_ATTRIBUTES (newdecl));
5910 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
5912 /* Specialization of merge_decl_attributes for various Windows targets.
5914 This handles the following situation:
5916 __declspec (dllimport) int foo;
5917 int foo;
5919 The second instance of `foo' nullifies the dllimport. */
5921 tree
5922 merge_dllimport_decl_attributes (tree old, tree new_tree)
5924 tree a;
5925 int delete_dllimport_p = 1;
5927 /* What we need to do here is remove from `old' dllimport if it doesn't
5928 appear in `new'. dllimport behaves like extern: if a declaration is
5929 marked dllimport and a definition appears later, then the object
5930 is not dllimport'd. We also remove a `new' dllimport if the old list
5931 contains dllexport: dllexport always overrides dllimport, regardless
5932 of the order of declaration. */
5933 if (!VAR_OR_FUNCTION_DECL_P (new_tree))
5934 delete_dllimport_p = 0;
5935 else if (DECL_DLLIMPORT_P (new_tree)
5936 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
5938 DECL_DLLIMPORT_P (new_tree) = 0;
5939 warning (OPT_Wattributes, "%q+D already declared with dllexport attribute: "
5940 "dllimport ignored", new_tree);
5942 else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new_tree))
5944 /* Warn about overriding a symbol that has already been used, e.g.:
5945 extern int __attribute__ ((dllimport)) foo;
5946 int* bar () {return &foo;}
5947 int foo;
5949 if (TREE_USED (old))
5951 warning (0, "%q+D redeclared without dllimport attribute "
5952 "after being referenced with dll linkage", new_tree);
5953 /* If we have used a variable's address with dllimport linkage,
5954 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
5955 decl may already have had TREE_CONSTANT computed.
5956 We still remove the attribute so that assembler code refers
5957 to '&foo rather than '_imp__foo'. */
5958 if (TREE_CODE (old) == VAR_DECL && TREE_ADDRESSABLE (old))
5959 DECL_DLLIMPORT_P (new_tree) = 1;
5962 /* Let an inline definition silently override the external reference,
5963 but otherwise warn about attribute inconsistency. */
5964 else if (TREE_CODE (new_tree) == VAR_DECL
5965 || !DECL_DECLARED_INLINE_P (new_tree))
5966 warning (OPT_Wattributes, "%q+D redeclared without dllimport attribute: "
5967 "previous dllimport ignored", new_tree);
5969 else
5970 delete_dllimport_p = 0;
5972 a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new_tree));
5974 if (delete_dllimport_p)
5975 a = remove_attribute ("dllimport", a);
5977 return a;
5980 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
5981 struct attribute_spec.handler. */
5983 tree
5984 handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
5985 bool *no_add_attrs)
5987 tree node = *pnode;
5988 bool is_dllimport;
5990 /* These attributes may apply to structure and union types being created,
5991 but otherwise should pass to the declaration involved. */
5992 if (!DECL_P (node))
5994 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
5995 | (int) ATTR_FLAG_ARRAY_NEXT))
5997 *no_add_attrs = true;
5998 return tree_cons (name, args, NULL_TREE);
6000 if (TREE_CODE (node) == RECORD_TYPE
6001 || TREE_CODE (node) == UNION_TYPE)
6003 node = TYPE_NAME (node);
6004 if (!node)
6005 return NULL_TREE;
6007 else
6009 warning (OPT_Wattributes, "%qE attribute ignored",
6010 name);
6011 *no_add_attrs = true;
6012 return NULL_TREE;
6016 if (TREE_CODE (node) != FUNCTION_DECL
6017 && TREE_CODE (node) != VAR_DECL
6018 && TREE_CODE (node) != TYPE_DECL)
6020 *no_add_attrs = true;
6021 warning (OPT_Wattributes, "%qE attribute ignored",
6022 name);
6023 return NULL_TREE;
6026 if (TREE_CODE (node) == TYPE_DECL
6027 && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE
6028 && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE)
6030 *no_add_attrs = true;
6031 warning (OPT_Wattributes, "%qE attribute ignored",
6032 name);
6033 return NULL_TREE;
6036 is_dllimport = is_attribute_p ("dllimport", name);
6038 /* Report error on dllimport ambiguities seen now before they cause
6039 any damage. */
6040 if (is_dllimport)
6042 /* Honor any target-specific overrides. */
6043 if (!targetm.valid_dllimport_attribute_p (node))
6044 *no_add_attrs = true;
6046 else if (TREE_CODE (node) == FUNCTION_DECL
6047 && DECL_DECLARED_INLINE_P (node))
6049 warning (OPT_Wattributes, "inline function %q+D declared as "
6050 " dllimport: attribute ignored", node);
6051 *no_add_attrs = true;
6053 /* Like MS, treat definition of dllimported variables and
6054 non-inlined functions on declaration as syntax errors. */
6055 else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
6057 error ("function %q+D definition is marked dllimport", node);
6058 *no_add_attrs = true;
6061 else if (TREE_CODE (node) == VAR_DECL)
6063 if (DECL_INITIAL (node))
6065 error ("variable %q+D definition is marked dllimport",
6066 node);
6067 *no_add_attrs = true;
6070 /* `extern' needn't be specified with dllimport.
6071 Specify `extern' now and hope for the best. Sigh. */
6072 DECL_EXTERNAL (node) = 1;
6073 /* Also, implicitly give dllimport'd variables declared within
6074 a function global scope, unless declared static. */
6075 if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
6076 TREE_PUBLIC (node) = 1;
6079 if (*no_add_attrs == false)
6080 DECL_DLLIMPORT_P (node) = 1;
6082 else if (TREE_CODE (node) == FUNCTION_DECL
6083 && DECL_DECLARED_INLINE_P (node)
6084 && flag_keep_inline_dllexport)
6085 /* An exported function, even if inline, must be emitted. */
6086 DECL_EXTERNAL (node) = 0;
6088 /* Report error if symbol is not accessible at global scope. */
6089 if (!TREE_PUBLIC (node)
6090 && (TREE_CODE (node) == VAR_DECL
6091 || TREE_CODE (node) == FUNCTION_DECL))
6093 error ("external linkage required for symbol %q+D because of "
6094 "%qE attribute", node, name);
6095 *no_add_attrs = true;
6098 /* A dllexport'd entity must have default visibility so that other
6099 program units (shared libraries or the main executable) can see
6100 it. A dllimport'd entity must have default visibility so that
6101 the linker knows that undefined references within this program
6102 unit can be resolved by the dynamic linker. */
6103 if (!*no_add_attrs)
6105 if (DECL_VISIBILITY_SPECIFIED (node)
6106 && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT)
6107 error ("%qE implies default visibility, but %qD has already "
6108 "been declared with a different visibility",
6109 name, node);
6110 DECL_VISIBILITY (node) = VISIBILITY_DEFAULT;
6111 DECL_VISIBILITY_SPECIFIED (node) = 1;
6114 return NULL_TREE;
6117 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
6119 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
6120 of the various TYPE_QUAL values. */
6122 static void
6123 set_type_quals (tree type, int type_quals)
6125 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
6126 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
6127 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
6128 TYPE_ATOMIC (type) = (type_quals & TYPE_QUAL_ATOMIC) != 0;
6129 TYPE_ADDR_SPACE (type) = DECODE_QUAL_ADDR_SPACE (type_quals);
6132 /* Returns true iff CAND is equivalent to BASE with TYPE_QUALS. */
6134 bool
6135 check_qualified_type (const_tree cand, const_tree base, int type_quals)
6137 return (TYPE_QUALS (cand) == type_quals
6138 && TYPE_NAME (cand) == TYPE_NAME (base)
6139 /* Apparently this is needed for Objective-C. */
6140 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
6141 /* Check alignment. */
6142 && TYPE_ALIGN (cand) == TYPE_ALIGN (base)
6143 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
6144 TYPE_ATTRIBUTES (base)));
6147 /* Returns true iff CAND is equivalent to BASE with ALIGN. */
6149 static bool
6150 check_aligned_type (const_tree cand, const_tree base, unsigned int align)
6152 return (TYPE_QUALS (cand) == TYPE_QUALS (base)
6153 && TYPE_NAME (cand) == TYPE_NAME (base)
6154 /* Apparently this is needed for Objective-C. */
6155 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
6156 /* Check alignment. */
6157 && TYPE_ALIGN (cand) == align
6158 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
6159 TYPE_ATTRIBUTES (base)));
6162 /* This function checks to see if TYPE matches the size one of the built-in
6163 atomic types, and returns that core atomic type. */
6165 static tree
6166 find_atomic_core_type (tree type)
6168 tree base_atomic_type;
6170 /* Only handle complete types. */
6171 if (TYPE_SIZE (type) == NULL_TREE)
6172 return NULL_TREE;
6174 HOST_WIDE_INT type_size = tree_to_uhwi (TYPE_SIZE (type));
6175 switch (type_size)
6177 case 8:
6178 base_atomic_type = atomicQI_type_node;
6179 break;
6181 case 16:
6182 base_atomic_type = atomicHI_type_node;
6183 break;
6185 case 32:
6186 base_atomic_type = atomicSI_type_node;
6187 break;
6189 case 64:
6190 base_atomic_type = atomicDI_type_node;
6191 break;
6193 case 128:
6194 base_atomic_type = atomicTI_type_node;
6195 break;
6197 default:
6198 base_atomic_type = NULL_TREE;
6201 return base_atomic_type;
6204 /* Return a version of the TYPE, qualified as indicated by the
6205 TYPE_QUALS, if one exists. If no qualified version exists yet,
6206 return NULL_TREE. */
6208 tree
6209 get_qualified_type (tree type, int type_quals)
6211 tree t;
6213 if (TYPE_QUALS (type) == type_quals)
6214 return type;
6216 /* Search the chain of variants to see if there is already one there just
6217 like the one we need to have. If so, use that existing one. We must
6218 preserve the TYPE_NAME, since there is code that depends on this. */
6219 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6220 if (check_qualified_type (t, type, type_quals))
6221 return t;
6223 return NULL_TREE;
6226 /* Like get_qualified_type, but creates the type if it does not
6227 exist. This function never returns NULL_TREE. */
6229 tree
6230 build_qualified_type (tree type, int type_quals)
6232 tree t;
6234 /* See if we already have the appropriate qualified variant. */
6235 t = get_qualified_type (type, type_quals);
6237 /* If not, build it. */
6238 if (!t)
6240 t = build_variant_type_copy (type);
6241 set_type_quals (t, type_quals);
6243 if (((type_quals & TYPE_QUAL_ATOMIC) == TYPE_QUAL_ATOMIC))
6245 /* See if this object can map to a basic atomic type. */
6246 tree atomic_type = find_atomic_core_type (type);
6247 if (atomic_type)
6249 /* Ensure the alignment of this type is compatible with
6250 the required alignment of the atomic type. */
6251 if (TYPE_ALIGN (atomic_type) > TYPE_ALIGN (t))
6252 TYPE_ALIGN (t) = TYPE_ALIGN (atomic_type);
6256 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6257 /* Propagate structural equality. */
6258 SET_TYPE_STRUCTURAL_EQUALITY (t);
6259 else if (TYPE_CANONICAL (type) != type)
6260 /* Build the underlying canonical type, since it is different
6261 from TYPE. */
6262 TYPE_CANONICAL (t) = build_qualified_type (TYPE_CANONICAL (type),
6263 type_quals);
6264 else
6265 /* T is its own canonical type. */
6266 TYPE_CANONICAL (t) = t;
6270 return t;
6273 /* Create a variant of type T with alignment ALIGN. */
6275 tree
6276 build_aligned_type (tree type, unsigned int align)
6278 tree t;
6280 if (TYPE_PACKED (type)
6281 || TYPE_ALIGN (type) == align)
6282 return type;
6284 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6285 if (check_aligned_type (t, type, align))
6286 return t;
6288 t = build_variant_type_copy (type);
6289 TYPE_ALIGN (t) = align;
6291 return t;
6294 /* Create a new distinct copy of TYPE. The new type is made its own
6295 MAIN_VARIANT. If TYPE requires structural equality checks, the
6296 resulting type requires structural equality checks; otherwise, its
6297 TYPE_CANONICAL points to itself. */
6299 tree
6300 build_distinct_type_copy (tree type)
6302 tree t = copy_node (type);
6304 TYPE_POINTER_TO (t) = 0;
6305 TYPE_REFERENCE_TO (t) = 0;
6307 /* Set the canonical type either to a new equivalence class, or
6308 propagate the need for structural equality checks. */
6309 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6310 SET_TYPE_STRUCTURAL_EQUALITY (t);
6311 else
6312 TYPE_CANONICAL (t) = t;
6314 /* Make it its own variant. */
6315 TYPE_MAIN_VARIANT (t) = t;
6316 TYPE_NEXT_VARIANT (t) = 0;
6318 /* Note that it is now possible for TYPE_MIN_VALUE to be a value
6319 whose TREE_TYPE is not t. This can also happen in the Ada
6320 frontend when using subtypes. */
6322 return t;
6325 /* Create a new variant of TYPE, equivalent but distinct. This is so
6326 the caller can modify it. TYPE_CANONICAL for the return type will
6327 be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
6328 are considered equal by the language itself (or that both types
6329 require structural equality checks). */
6331 tree
6332 build_variant_type_copy (tree type)
6334 tree t, m = TYPE_MAIN_VARIANT (type);
6336 t = build_distinct_type_copy (type);
6338 /* Since we're building a variant, assume that it is a non-semantic
6339 variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
6340 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
6342 /* Add the new type to the chain of variants of TYPE. */
6343 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
6344 TYPE_NEXT_VARIANT (m) = t;
6345 TYPE_MAIN_VARIANT (t) = m;
6347 return t;
6350 /* Return true if the from tree in both tree maps are equal. */
6353 tree_map_base_eq (const void *va, const void *vb)
6355 const struct tree_map_base *const a = (const struct tree_map_base *) va,
6356 *const b = (const struct tree_map_base *) vb;
6357 return (a->from == b->from);
6360 /* Hash a from tree in a tree_base_map. */
6362 unsigned int
6363 tree_map_base_hash (const void *item)
6365 return htab_hash_pointer (((const struct tree_map_base *)item)->from);
6368 /* Return true if this tree map structure is marked for garbage collection
6369 purposes. We simply return true if the from tree is marked, so that this
6370 structure goes away when the from tree goes away. */
6373 tree_map_base_marked_p (const void *p)
6375 return ggc_marked_p (((const struct tree_map_base *) p)->from);
6378 /* Hash a from tree in a tree_map. */
6380 unsigned int
6381 tree_map_hash (const void *item)
6383 return (((const struct tree_map *) item)->hash);
6386 /* Hash a from tree in a tree_decl_map. */
6388 unsigned int
6389 tree_decl_map_hash (const void *item)
6391 return DECL_UID (((const struct tree_decl_map *) item)->base.from);
6394 /* Return the initialization priority for DECL. */
6396 priority_type
6397 decl_init_priority_lookup (tree decl)
6399 struct tree_priority_map *h;
6400 struct tree_map_base in;
6402 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
6403 in.from = decl;
6404 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
6405 return h ? h->init : DEFAULT_INIT_PRIORITY;
6408 /* Return the finalization priority for DECL. */
6410 priority_type
6411 decl_fini_priority_lookup (tree decl)
6413 struct tree_priority_map *h;
6414 struct tree_map_base in;
6416 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
6417 in.from = decl;
6418 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
6419 return h ? h->fini : DEFAULT_INIT_PRIORITY;
6422 /* Return the initialization and finalization priority information for
6423 DECL. If there is no previous priority information, a freshly
6424 allocated structure is returned. */
6426 static struct tree_priority_map *
6427 decl_priority_info (tree decl)
6429 struct tree_priority_map in;
6430 struct tree_priority_map *h;
6431 void **loc;
6433 in.base.from = decl;
6434 loc = htab_find_slot (init_priority_for_decl, &in, INSERT);
6435 h = (struct tree_priority_map *) *loc;
6436 if (!h)
6438 h = ggc_cleared_alloc<tree_priority_map> ();
6439 *loc = h;
6440 h->base.from = decl;
6441 h->init = DEFAULT_INIT_PRIORITY;
6442 h->fini = DEFAULT_INIT_PRIORITY;
6445 return h;
6448 /* Set the initialization priority for DECL to PRIORITY. */
6450 void
6451 decl_init_priority_insert (tree decl, priority_type priority)
6453 struct tree_priority_map *h;
6455 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
6456 if (priority == DEFAULT_INIT_PRIORITY)
6457 return;
6458 h = decl_priority_info (decl);
6459 h->init = priority;
6462 /* Set the finalization priority for DECL to PRIORITY. */
6464 void
6465 decl_fini_priority_insert (tree decl, priority_type priority)
6467 struct tree_priority_map *h;
6469 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
6470 if (priority == DEFAULT_INIT_PRIORITY)
6471 return;
6472 h = decl_priority_info (decl);
6473 h->fini = priority;
6476 /* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
6478 static void
6479 print_debug_expr_statistics (void)
6481 fprintf (stderr, "DECL_DEBUG_EXPR hash: size %ld, %ld elements, %f collisions\n",
6482 (long) htab_size (debug_expr_for_decl),
6483 (long) htab_elements (debug_expr_for_decl),
6484 htab_collisions (debug_expr_for_decl));
6487 /* Print out the statistics for the DECL_VALUE_EXPR hash table. */
6489 static void
6490 print_value_expr_statistics (void)
6492 fprintf (stderr, "DECL_VALUE_EXPR hash: size %ld, %ld elements, %f collisions\n",
6493 (long) htab_size (value_expr_for_decl),
6494 (long) htab_elements (value_expr_for_decl),
6495 htab_collisions (value_expr_for_decl));
6498 /* Lookup a debug expression for FROM, and return it if we find one. */
6500 tree
6501 decl_debug_expr_lookup (tree from)
6503 struct tree_decl_map *h, in;
6504 in.base.from = from;
6506 h = (struct tree_decl_map *)
6507 htab_find_with_hash (debug_expr_for_decl, &in, DECL_UID (from));
6508 if (h)
6509 return h->to;
6510 return NULL_TREE;
6513 /* Insert a mapping FROM->TO in the debug expression hashtable. */
6515 void
6516 decl_debug_expr_insert (tree from, tree to)
6518 struct tree_decl_map *h;
6519 void **loc;
6521 h = ggc_alloc<tree_decl_map> ();
6522 h->base.from = from;
6523 h->to = to;
6524 loc = htab_find_slot_with_hash (debug_expr_for_decl, h, DECL_UID (from),
6525 INSERT);
6526 *(struct tree_decl_map **) loc = h;
6529 /* Lookup a value expression for FROM, and return it if we find one. */
6531 tree
6532 decl_value_expr_lookup (tree from)
6534 struct tree_decl_map *h, in;
6535 in.base.from = from;
6537 h = (struct tree_decl_map *)
6538 htab_find_with_hash (value_expr_for_decl, &in, DECL_UID (from));
6539 if (h)
6540 return h->to;
6541 return NULL_TREE;
6544 /* Insert a mapping FROM->TO in the value expression hashtable. */
6546 void
6547 decl_value_expr_insert (tree from, tree to)
6549 struct tree_decl_map *h;
6550 void **loc;
6552 h = ggc_alloc<tree_decl_map> ();
6553 h->base.from = from;
6554 h->to = to;
6555 loc = htab_find_slot_with_hash (value_expr_for_decl, h, DECL_UID (from),
6556 INSERT);
6557 *(struct tree_decl_map **) loc = h;
6560 /* Lookup a vector of debug arguments for FROM, and return it if we
6561 find one. */
6563 vec<tree, va_gc> **
6564 decl_debug_args_lookup (tree from)
6566 struct tree_vec_map *h, in;
6568 if (!DECL_HAS_DEBUG_ARGS_P (from))
6569 return NULL;
6570 gcc_checking_assert (debug_args_for_decl != NULL);
6571 in.base.from = from;
6572 h = (struct tree_vec_map *)
6573 htab_find_with_hash (debug_args_for_decl, &in, DECL_UID (from));
6574 if (h)
6575 return &h->to;
6576 return NULL;
6579 /* Insert a mapping FROM->empty vector of debug arguments in the value
6580 expression hashtable. */
6582 vec<tree, va_gc> **
6583 decl_debug_args_insert (tree from)
6585 struct tree_vec_map *h;
6586 void **loc;
6588 if (DECL_HAS_DEBUG_ARGS_P (from))
6589 return decl_debug_args_lookup (from);
6590 if (debug_args_for_decl == NULL)
6591 debug_args_for_decl = htab_create_ggc (64, tree_vec_map_hash,
6592 tree_vec_map_eq, 0);
6593 h = ggc_alloc<tree_vec_map> ();
6594 h->base.from = from;
6595 h->to = NULL;
6596 loc = htab_find_slot_with_hash (debug_args_for_decl, h, DECL_UID (from),
6597 INSERT);
6598 *(struct tree_vec_map **) loc = h;
6599 DECL_HAS_DEBUG_ARGS_P (from) = 1;
6600 return &h->to;
6603 /* Hashing of types so that we don't make duplicates.
6604 The entry point is `type_hash_canon'. */
6606 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
6607 with types in the TREE_VALUE slots), by adding the hash codes
6608 of the individual types. */
6610 static unsigned int
6611 type_hash_list (const_tree list, hashval_t hashcode)
6613 const_tree tail;
6615 for (tail = list; tail; tail = TREE_CHAIN (tail))
6616 if (TREE_VALUE (tail) != error_mark_node)
6617 hashcode = iterative_hash_object (TYPE_HASH (TREE_VALUE (tail)),
6618 hashcode);
6620 return hashcode;
6623 /* These are the Hashtable callback functions. */
6625 /* Returns true iff the types are equivalent. */
6627 static int
6628 type_hash_eq (const void *va, const void *vb)
6630 const struct type_hash *const a = (const struct type_hash *) va,
6631 *const b = (const struct type_hash *) vb;
6633 /* First test the things that are the same for all types. */
6634 if (a->hash != b->hash
6635 || TREE_CODE (a->type) != TREE_CODE (b->type)
6636 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
6637 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
6638 TYPE_ATTRIBUTES (b->type))
6639 || (TREE_CODE (a->type) != COMPLEX_TYPE
6640 && TYPE_NAME (a->type) != TYPE_NAME (b->type)))
6641 return 0;
6643 /* Be careful about comparing arrays before and after the element type
6644 has been completed; don't compare TYPE_ALIGN unless both types are
6645 complete. */
6646 if (COMPLETE_TYPE_P (a->type) && COMPLETE_TYPE_P (b->type)
6647 && (TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
6648 || TYPE_MODE (a->type) != TYPE_MODE (b->type)))
6649 return 0;
6651 switch (TREE_CODE (a->type))
6653 case VOID_TYPE:
6654 case COMPLEX_TYPE:
6655 case POINTER_TYPE:
6656 case REFERENCE_TYPE:
6657 case NULLPTR_TYPE:
6658 return 1;
6660 case VECTOR_TYPE:
6661 return TYPE_VECTOR_SUBPARTS (a->type) == TYPE_VECTOR_SUBPARTS (b->type);
6663 case ENUMERAL_TYPE:
6664 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
6665 && !(TYPE_VALUES (a->type)
6666 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
6667 && TYPE_VALUES (b->type)
6668 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
6669 && type_list_equal (TYPE_VALUES (a->type),
6670 TYPE_VALUES (b->type))))
6671 return 0;
6673 /* ... fall through ... */
6675 case INTEGER_TYPE:
6676 case REAL_TYPE:
6677 case BOOLEAN_TYPE:
6678 if (TYPE_PRECISION (a->type) != TYPE_PRECISION (b->type))
6679 return false;
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 && wi::to_widest (t1) == wi::to_widest (t2))
6979 return 1;
6981 return 0;
6984 /* Return true if T is an INTEGER_CST whose numerical value (extended
6985 according to TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. */
6987 bool
6988 tree_fits_shwi_p (const_tree t)
6990 return (t != NULL_TREE
6991 && TREE_CODE (t) == INTEGER_CST
6992 && wi::fits_shwi_p (wi::to_widest (t)));
6995 /* Return true if T is an INTEGER_CST whose numerical value (extended
6996 according to TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. */
6998 bool
6999 tree_fits_uhwi_p (const_tree t)
7001 return (t != NULL_TREE
7002 && TREE_CODE (t) == INTEGER_CST
7003 && wi::fits_uhwi_p (wi::to_widest (t)));
7006 /* T is an INTEGER_CST whose numerical value (extended according to
7007 TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. Return that
7008 HOST_WIDE_INT. */
7010 HOST_WIDE_INT
7011 tree_to_shwi (const_tree t)
7013 gcc_assert (tree_fits_shwi_p (t));
7014 return TREE_INT_CST_LOW (t);
7017 /* T is an INTEGER_CST whose numerical value (extended according to
7018 TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. Return that
7019 HOST_WIDE_INT. */
7021 unsigned HOST_WIDE_INT
7022 tree_to_uhwi (const_tree t)
7024 gcc_assert (tree_fits_uhwi_p (t));
7025 return TREE_INT_CST_LOW (t);
7028 /* Return the most significant (sign) bit of T. */
7031 tree_int_cst_sign_bit (const_tree t)
7033 unsigned bitno = TYPE_PRECISION (TREE_TYPE (t)) - 1;
7035 return wi::extract_uhwi (t, bitno, 1);
7038 /* Return an indication of the sign of the integer constant T.
7039 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
7040 Note that -1 will never be returned if T's type is unsigned. */
7043 tree_int_cst_sgn (const_tree t)
7045 if (wi::eq_p (t, 0))
7046 return 0;
7047 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
7048 return 1;
7049 else if (wi::neg_p (t))
7050 return -1;
7051 else
7052 return 1;
7055 /* Return the minimum number of bits needed to represent VALUE in a
7056 signed or unsigned type, UNSIGNEDP says which. */
7058 unsigned int
7059 tree_int_cst_min_precision (tree value, signop sgn)
7061 /* If the value is negative, compute its negative minus 1. The latter
7062 adjustment is because the absolute value of the largest negative value
7063 is one larger than the largest positive value. This is equivalent to
7064 a bit-wise negation, so use that operation instead. */
7066 if (tree_int_cst_sgn (value) < 0)
7067 value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
7069 /* Return the number of bits needed, taking into account the fact
7070 that we need one more bit for a signed than unsigned type.
7071 If value is 0 or -1, the minimum precision is 1 no matter
7072 whether unsignedp is true or false. */
7074 if (integer_zerop (value))
7075 return 1;
7076 else
7077 return tree_floor_log2 (value) + 1 + (sgn == SIGNED ? 1 : 0) ;
7080 /* Return truthvalue of whether T1 is the same tree structure as T2.
7081 Return 1 if they are the same.
7082 Return 0 if they are understandably different.
7083 Return -1 if either contains tree structure not understood by
7084 this function. */
7087 simple_cst_equal (const_tree t1, const_tree t2)
7089 enum tree_code code1, code2;
7090 int cmp;
7091 int i;
7093 if (t1 == t2)
7094 return 1;
7095 if (t1 == 0 || t2 == 0)
7096 return 0;
7098 code1 = TREE_CODE (t1);
7099 code2 = TREE_CODE (t2);
7101 if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
7103 if (CONVERT_EXPR_CODE_P (code2)
7104 || code2 == NON_LVALUE_EXPR)
7105 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7106 else
7107 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
7110 else if (CONVERT_EXPR_CODE_P (code2)
7111 || code2 == NON_LVALUE_EXPR)
7112 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
7114 if (code1 != code2)
7115 return 0;
7117 switch (code1)
7119 case INTEGER_CST:
7120 return wi::to_widest (t1) == wi::to_widest (t2);
7122 case REAL_CST:
7123 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
7125 case FIXED_CST:
7126 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
7128 case STRING_CST:
7129 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
7130 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
7131 TREE_STRING_LENGTH (t1)));
7133 case CONSTRUCTOR:
7135 unsigned HOST_WIDE_INT idx;
7136 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (t1);
7137 vec<constructor_elt, va_gc> *v2 = CONSTRUCTOR_ELTS (t2);
7139 if (vec_safe_length (v1) != vec_safe_length (v2))
7140 return false;
7142 for (idx = 0; idx < vec_safe_length (v1); ++idx)
7143 /* ??? Should we handle also fields here? */
7144 if (!simple_cst_equal ((*v1)[idx].value, (*v2)[idx].value))
7145 return false;
7146 return true;
7149 case SAVE_EXPR:
7150 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7152 case CALL_EXPR:
7153 cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
7154 if (cmp <= 0)
7155 return cmp;
7156 if (call_expr_nargs (t1) != call_expr_nargs (t2))
7157 return 0;
7159 const_tree arg1, arg2;
7160 const_call_expr_arg_iterator iter1, iter2;
7161 for (arg1 = first_const_call_expr_arg (t1, &iter1),
7162 arg2 = first_const_call_expr_arg (t2, &iter2);
7163 arg1 && arg2;
7164 arg1 = next_const_call_expr_arg (&iter1),
7165 arg2 = next_const_call_expr_arg (&iter2))
7167 cmp = simple_cst_equal (arg1, arg2);
7168 if (cmp <= 0)
7169 return cmp;
7171 return arg1 == arg2;
7174 case TARGET_EXPR:
7175 /* Special case: if either target is an unallocated VAR_DECL,
7176 it means that it's going to be unified with whatever the
7177 TARGET_EXPR is really supposed to initialize, so treat it
7178 as being equivalent to anything. */
7179 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
7180 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
7181 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
7182 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
7183 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
7184 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
7185 cmp = 1;
7186 else
7187 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7189 if (cmp <= 0)
7190 return cmp;
7192 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
7194 case WITH_CLEANUP_EXPR:
7195 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7196 if (cmp <= 0)
7197 return cmp;
7199 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
7201 case COMPONENT_REF:
7202 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
7203 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7205 return 0;
7207 case VAR_DECL:
7208 case PARM_DECL:
7209 case CONST_DECL:
7210 case FUNCTION_DECL:
7211 return 0;
7213 default:
7214 break;
7217 /* This general rule works for most tree codes. All exceptions should be
7218 handled above. If this is a language-specific tree code, we can't
7219 trust what might be in the operand, so say we don't know
7220 the situation. */
7221 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
7222 return -1;
7224 switch (TREE_CODE_CLASS (code1))
7226 case tcc_unary:
7227 case tcc_binary:
7228 case tcc_comparison:
7229 case tcc_expression:
7230 case tcc_reference:
7231 case tcc_statement:
7232 cmp = 1;
7233 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
7235 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
7236 if (cmp <= 0)
7237 return cmp;
7240 return cmp;
7242 default:
7243 return -1;
7247 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
7248 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
7249 than U, respectively. */
7252 compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
7254 if (tree_int_cst_sgn (t) < 0)
7255 return -1;
7256 else if (!tree_fits_uhwi_p (t))
7257 return 1;
7258 else if (TREE_INT_CST_LOW (t) == u)
7259 return 0;
7260 else if (TREE_INT_CST_LOW (t) < u)
7261 return -1;
7262 else
7263 return 1;
7266 /* Return true if SIZE represents a constant size that is in bounds of
7267 what the middle-end and the backend accepts (covering not more than
7268 half of the address-space). */
7270 bool
7271 valid_constant_size_p (const_tree size)
7273 if (! tree_fits_uhwi_p (size)
7274 || TREE_OVERFLOW (size)
7275 || tree_int_cst_sign_bit (size) != 0)
7276 return false;
7277 return true;
7280 /* Return the precision of the type, or for a complex or vector type the
7281 precision of the type of its elements. */
7283 unsigned int
7284 element_precision (const_tree type)
7286 enum tree_code code = TREE_CODE (type);
7287 if (code == COMPLEX_TYPE || code == VECTOR_TYPE)
7288 type = TREE_TYPE (type);
7290 return TYPE_PRECISION (type);
7293 /* Return true if CODE represents an associative tree code. Otherwise
7294 return false. */
7295 bool
7296 associative_tree_code (enum tree_code code)
7298 switch (code)
7300 case BIT_IOR_EXPR:
7301 case BIT_AND_EXPR:
7302 case BIT_XOR_EXPR:
7303 case PLUS_EXPR:
7304 case MULT_EXPR:
7305 case MIN_EXPR:
7306 case MAX_EXPR:
7307 return true;
7309 default:
7310 break;
7312 return false;
7315 /* Return true if CODE represents a commutative tree code. Otherwise
7316 return false. */
7317 bool
7318 commutative_tree_code (enum tree_code code)
7320 switch (code)
7322 case PLUS_EXPR:
7323 case MULT_EXPR:
7324 case MULT_HIGHPART_EXPR:
7325 case MIN_EXPR:
7326 case MAX_EXPR:
7327 case BIT_IOR_EXPR:
7328 case BIT_XOR_EXPR:
7329 case BIT_AND_EXPR:
7330 case NE_EXPR:
7331 case EQ_EXPR:
7332 case UNORDERED_EXPR:
7333 case ORDERED_EXPR:
7334 case UNEQ_EXPR:
7335 case LTGT_EXPR:
7336 case TRUTH_AND_EXPR:
7337 case TRUTH_XOR_EXPR:
7338 case TRUTH_OR_EXPR:
7339 case WIDEN_MULT_EXPR:
7340 case VEC_WIDEN_MULT_HI_EXPR:
7341 case VEC_WIDEN_MULT_LO_EXPR:
7342 case VEC_WIDEN_MULT_EVEN_EXPR:
7343 case VEC_WIDEN_MULT_ODD_EXPR:
7344 return true;
7346 default:
7347 break;
7349 return false;
7352 /* Return true if CODE represents a ternary tree code for which the
7353 first two operands are commutative. Otherwise return false. */
7354 bool
7355 commutative_ternary_tree_code (enum tree_code code)
7357 switch (code)
7359 case WIDEN_MULT_PLUS_EXPR:
7360 case WIDEN_MULT_MINUS_EXPR:
7361 return true;
7363 default:
7364 break;
7366 return false;
7369 /* Generate a hash value for an expression. This can be used iteratively
7370 by passing a previous result as the VAL argument.
7372 This function is intended to produce the same hash for expressions which
7373 would compare equal using operand_equal_p. */
7375 hashval_t
7376 iterative_hash_expr (const_tree t, hashval_t val)
7378 int i;
7379 enum tree_code code;
7380 enum tree_code_class tclass;
7382 if (t == NULL_TREE)
7383 return iterative_hash_hashval_t (0, val);
7385 code = TREE_CODE (t);
7387 switch (code)
7389 /* Alas, constants aren't shared, so we can't rely on pointer
7390 identity. */
7391 case INTEGER_CST:
7392 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
7393 val = iterative_hash_host_wide_int (TREE_INT_CST_ELT (t, i), val);
7394 return val;
7395 case REAL_CST:
7397 unsigned int val2 = real_hash (TREE_REAL_CST_PTR (t));
7399 return iterative_hash_hashval_t (val2, val);
7401 case FIXED_CST:
7403 unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
7405 return iterative_hash_hashval_t (val2, val);
7407 case STRING_CST:
7408 return iterative_hash (TREE_STRING_POINTER (t),
7409 TREE_STRING_LENGTH (t), val);
7410 case COMPLEX_CST:
7411 val = iterative_hash_expr (TREE_REALPART (t), val);
7412 return iterative_hash_expr (TREE_IMAGPART (t), val);
7413 case VECTOR_CST:
7415 unsigned i;
7416 for (i = 0; i < VECTOR_CST_NELTS (t); ++i)
7417 val = iterative_hash_expr (VECTOR_CST_ELT (t, i), val);
7418 return val;
7420 case SSA_NAME:
7421 /* We can just compare by pointer. */
7422 return iterative_hash_host_wide_int (SSA_NAME_VERSION (t), val);
7423 case PLACEHOLDER_EXPR:
7424 /* The node itself doesn't matter. */
7425 return val;
7426 case TREE_LIST:
7427 /* A list of expressions, for a CALL_EXPR or as the elements of a
7428 VECTOR_CST. */
7429 for (; t; t = TREE_CHAIN (t))
7430 val = iterative_hash_expr (TREE_VALUE (t), val);
7431 return val;
7432 case CONSTRUCTOR:
7434 unsigned HOST_WIDE_INT idx;
7435 tree field, value;
7436 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
7438 val = iterative_hash_expr (field, val);
7439 val = iterative_hash_expr (value, val);
7441 return val;
7443 case FUNCTION_DECL:
7444 /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
7445 Otherwise nodes that compare equal according to operand_equal_p might
7446 get different hash codes. However, don't do this for machine specific
7447 or front end builtins, since the function code is overloaded in those
7448 cases. */
7449 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
7450 && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
7452 t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
7453 code = TREE_CODE (t);
7455 /* FALL THROUGH */
7456 default:
7457 tclass = TREE_CODE_CLASS (code);
7459 if (tclass == tcc_declaration)
7461 /* DECL's have a unique ID */
7462 val = iterative_hash_host_wide_int (DECL_UID (t), val);
7464 else
7466 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
7468 val = iterative_hash_object (code, val);
7470 /* Don't hash the type, that can lead to having nodes which
7471 compare equal according to operand_equal_p, but which
7472 have different hash codes. */
7473 if (CONVERT_EXPR_CODE_P (code)
7474 || code == NON_LVALUE_EXPR)
7476 /* Make sure to include signness in the hash computation. */
7477 val += TYPE_UNSIGNED (TREE_TYPE (t));
7478 val = iterative_hash_expr (TREE_OPERAND (t, 0), val);
7481 else if (commutative_tree_code (code))
7483 /* It's a commutative expression. We want to hash it the same
7484 however it appears. We do this by first hashing both operands
7485 and then rehashing based on the order of their independent
7486 hashes. */
7487 hashval_t one = iterative_hash_expr (TREE_OPERAND (t, 0), 0);
7488 hashval_t two = iterative_hash_expr (TREE_OPERAND (t, 1), 0);
7489 hashval_t t;
7491 if (one > two)
7492 t = one, one = two, two = t;
7494 val = iterative_hash_hashval_t (one, val);
7495 val = iterative_hash_hashval_t (two, val);
7497 else
7498 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
7499 val = iterative_hash_expr (TREE_OPERAND (t, i), val);
7501 return val;
7505 /* Constructors for pointer, array and function types.
7506 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
7507 constructed by language-dependent code, not here.) */
7509 /* Construct, lay out and return the type of pointers to TO_TYPE with
7510 mode MODE. If CAN_ALIAS_ALL is TRUE, indicate this type can
7511 reference all of memory. If such a type has already been
7512 constructed, reuse it. */
7514 tree
7515 build_pointer_type_for_mode (tree to_type, enum machine_mode mode,
7516 bool can_alias_all)
7518 tree t;
7520 if (to_type == error_mark_node)
7521 return error_mark_node;
7523 /* If the pointed-to type has the may_alias attribute set, force
7524 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7525 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7526 can_alias_all = true;
7528 /* In some cases, languages will have things that aren't a POINTER_TYPE
7529 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
7530 In that case, return that type without regard to the rest of our
7531 operands.
7533 ??? This is a kludge, but consistent with the way this function has
7534 always operated and there doesn't seem to be a good way to avoid this
7535 at the moment. */
7536 if (TYPE_POINTER_TO (to_type) != 0
7537 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
7538 return TYPE_POINTER_TO (to_type);
7540 /* First, if we already have a type for pointers to TO_TYPE and it's
7541 the proper mode, use it. */
7542 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
7543 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7544 return t;
7546 t = make_node (POINTER_TYPE);
7548 TREE_TYPE (t) = to_type;
7549 SET_TYPE_MODE (t, mode);
7550 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7551 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
7552 TYPE_POINTER_TO (to_type) = t;
7554 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7555 SET_TYPE_STRUCTURAL_EQUALITY (t);
7556 else if (TYPE_CANONICAL (to_type) != to_type)
7557 TYPE_CANONICAL (t)
7558 = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
7559 mode, can_alias_all);
7561 /* Lay out the type. This function has many callers that are concerned
7562 with expression-construction, and this simplifies them all. */
7563 layout_type (t);
7565 return t;
7568 /* By default build pointers in ptr_mode. */
7570 tree
7571 build_pointer_type (tree to_type)
7573 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7574 : TYPE_ADDR_SPACE (to_type);
7575 enum machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7576 return build_pointer_type_for_mode (to_type, pointer_mode, false);
7579 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
7581 tree
7582 build_reference_type_for_mode (tree to_type, enum machine_mode mode,
7583 bool can_alias_all)
7585 tree t;
7587 if (to_type == error_mark_node)
7588 return error_mark_node;
7590 /* If the pointed-to type has the may_alias attribute set, force
7591 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7592 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7593 can_alias_all = true;
7595 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
7596 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
7597 In that case, return that type without regard to the rest of our
7598 operands.
7600 ??? This is a kludge, but consistent with the way this function has
7601 always operated and there doesn't seem to be a good way to avoid this
7602 at the moment. */
7603 if (TYPE_REFERENCE_TO (to_type) != 0
7604 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
7605 return TYPE_REFERENCE_TO (to_type);
7607 /* First, if we already have a type for pointers to TO_TYPE and it's
7608 the proper mode, use it. */
7609 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
7610 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7611 return t;
7613 t = make_node (REFERENCE_TYPE);
7615 TREE_TYPE (t) = to_type;
7616 SET_TYPE_MODE (t, mode);
7617 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7618 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
7619 TYPE_REFERENCE_TO (to_type) = t;
7621 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7622 SET_TYPE_STRUCTURAL_EQUALITY (t);
7623 else if (TYPE_CANONICAL (to_type) != to_type)
7624 TYPE_CANONICAL (t)
7625 = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
7626 mode, can_alias_all);
7628 layout_type (t);
7630 return t;
7634 /* Build the node for the type of references-to-TO_TYPE by default
7635 in ptr_mode. */
7637 tree
7638 build_reference_type (tree to_type)
7640 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7641 : TYPE_ADDR_SPACE (to_type);
7642 enum machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7643 return build_reference_type_for_mode (to_type, pointer_mode, false);
7646 #define MAX_INT_CACHED_PREC \
7647 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
7648 static GTY(()) tree nonstandard_integer_type_cache[2 * MAX_INT_CACHED_PREC + 2];
7650 /* Builds a signed or unsigned integer type of precision PRECISION.
7651 Used for C bitfields whose precision does not match that of
7652 built-in target types. */
7653 tree
7654 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
7655 int unsignedp)
7657 tree itype, ret;
7659 if (unsignedp)
7660 unsignedp = MAX_INT_CACHED_PREC + 1;
7662 if (precision <= MAX_INT_CACHED_PREC)
7664 itype = nonstandard_integer_type_cache[precision + unsignedp];
7665 if (itype)
7666 return itype;
7669 itype = make_node (INTEGER_TYPE);
7670 TYPE_PRECISION (itype) = precision;
7672 if (unsignedp)
7673 fixup_unsigned_type (itype);
7674 else
7675 fixup_signed_type (itype);
7677 ret = itype;
7678 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (itype)))
7679 ret = type_hash_canon (tree_to_uhwi (TYPE_MAX_VALUE (itype)), itype);
7680 if (precision <= MAX_INT_CACHED_PREC)
7681 nonstandard_integer_type_cache[precision + unsignedp] = ret;
7683 return ret;
7686 /* Create a range of some discrete type TYPE (an INTEGER_TYPE, ENUMERAL_TYPE
7687 or BOOLEAN_TYPE) with low bound LOWVAL and high bound HIGHVAL. If SHARED
7688 is true, reuse such a type that has already been constructed. */
7690 static tree
7691 build_range_type_1 (tree type, tree lowval, tree highval, bool shared)
7693 tree itype = make_node (INTEGER_TYPE);
7694 hashval_t hashcode = 0;
7696 TREE_TYPE (itype) = type;
7698 TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
7699 TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
7701 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
7702 SET_TYPE_MODE (itype, TYPE_MODE (type));
7703 TYPE_SIZE (itype) = TYPE_SIZE (type);
7704 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
7705 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
7706 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
7708 if (!shared)
7709 return itype;
7711 if ((TYPE_MIN_VALUE (itype)
7712 && TREE_CODE (TYPE_MIN_VALUE (itype)) != INTEGER_CST)
7713 || (TYPE_MAX_VALUE (itype)
7714 && TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST))
7716 /* Since we cannot reliably merge this type, we need to compare it using
7717 structural equality checks. */
7718 SET_TYPE_STRUCTURAL_EQUALITY (itype);
7719 return itype;
7722 hashcode = iterative_hash_expr (TYPE_MIN_VALUE (itype), hashcode);
7723 hashcode = iterative_hash_expr (TYPE_MAX_VALUE (itype), hashcode);
7724 hashcode = iterative_hash_hashval_t (TYPE_HASH (type), hashcode);
7725 itype = type_hash_canon (hashcode, itype);
7727 return itype;
7730 /* Wrapper around build_range_type_1 with SHARED set to true. */
7732 tree
7733 build_range_type (tree type, tree lowval, tree highval)
7735 return build_range_type_1 (type, lowval, highval, true);
7738 /* Wrapper around build_range_type_1 with SHARED set to false. */
7740 tree
7741 build_nonshared_range_type (tree type, tree lowval, tree highval)
7743 return build_range_type_1 (type, lowval, highval, false);
7746 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
7747 MAXVAL should be the maximum value in the domain
7748 (one less than the length of the array).
7750 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
7751 We don't enforce this limit, that is up to caller (e.g. language front end).
7752 The limit exists because the result is a signed type and we don't handle
7753 sizes that use more than one HOST_WIDE_INT. */
7755 tree
7756 build_index_type (tree maxval)
7758 return build_range_type (sizetype, size_zero_node, maxval);
7761 /* Return true if the debug information for TYPE, a subtype, should be emitted
7762 as a subrange type. If so, set LOWVAL to the low bound and HIGHVAL to the
7763 high bound, respectively. Sometimes doing so unnecessarily obfuscates the
7764 debug info and doesn't reflect the source code. */
7766 bool
7767 subrange_type_for_debug_p (const_tree type, tree *lowval, tree *highval)
7769 tree base_type = TREE_TYPE (type), low, high;
7771 /* Subrange types have a base type which is an integral type. */
7772 if (!INTEGRAL_TYPE_P (base_type))
7773 return false;
7775 /* Get the real bounds of the subtype. */
7776 if (lang_hooks.types.get_subrange_bounds)
7777 lang_hooks.types.get_subrange_bounds (type, &low, &high);
7778 else
7780 low = TYPE_MIN_VALUE (type);
7781 high = TYPE_MAX_VALUE (type);
7784 /* If the type and its base type have the same representation and the same
7785 name, then the type is not a subrange but a copy of the base type. */
7786 if ((TREE_CODE (base_type) == INTEGER_TYPE
7787 || TREE_CODE (base_type) == BOOLEAN_TYPE)
7788 && int_size_in_bytes (type) == int_size_in_bytes (base_type)
7789 && tree_int_cst_equal (low, TYPE_MIN_VALUE (base_type))
7790 && tree_int_cst_equal (high, TYPE_MAX_VALUE (base_type))
7791 && TYPE_IDENTIFIER (type) == TYPE_IDENTIFIER (base_type))
7792 return false;
7794 if (lowval)
7795 *lowval = low;
7796 if (highval)
7797 *highval = high;
7798 return true;
7801 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
7802 and number of elements specified by the range of values of INDEX_TYPE.
7803 If SHARED is true, reuse such a type that has already been constructed. */
7805 static tree
7806 build_array_type_1 (tree elt_type, tree index_type, bool shared)
7808 tree t;
7810 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
7812 error ("arrays of functions are not meaningful");
7813 elt_type = integer_type_node;
7816 t = make_node (ARRAY_TYPE);
7817 TREE_TYPE (t) = elt_type;
7818 TYPE_DOMAIN (t) = index_type;
7819 TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (elt_type);
7820 layout_type (t);
7822 /* If the element type is incomplete at this point we get marked for
7823 structural equality. Do not record these types in the canonical
7824 type hashtable. */
7825 if (TYPE_STRUCTURAL_EQUALITY_P (t))
7826 return t;
7828 if (shared)
7830 hashval_t hashcode = iterative_hash_object (TYPE_HASH (elt_type), 0);
7831 if (index_type)
7832 hashcode = iterative_hash_object (TYPE_HASH (index_type), hashcode);
7833 t = type_hash_canon (hashcode, t);
7836 if (TYPE_CANONICAL (t) == t)
7838 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
7839 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
7840 SET_TYPE_STRUCTURAL_EQUALITY (t);
7841 else if (TYPE_CANONICAL (elt_type) != elt_type
7842 || (index_type && TYPE_CANONICAL (index_type) != index_type))
7843 TYPE_CANONICAL (t)
7844 = build_array_type_1 (TYPE_CANONICAL (elt_type),
7845 index_type
7846 ? TYPE_CANONICAL (index_type) : NULL_TREE,
7847 shared);
7850 return t;
7853 /* Wrapper around build_array_type_1 with SHARED set to true. */
7855 tree
7856 build_array_type (tree elt_type, tree index_type)
7858 return build_array_type_1 (elt_type, index_type, true);
7861 /* Wrapper around build_array_type_1 with SHARED set to false. */
7863 tree
7864 build_nonshared_array_type (tree elt_type, tree index_type)
7866 return build_array_type_1 (elt_type, index_type, false);
7869 /* Return a representation of ELT_TYPE[NELTS], using indices of type
7870 sizetype. */
7872 tree
7873 build_array_type_nelts (tree elt_type, unsigned HOST_WIDE_INT nelts)
7875 return build_array_type (elt_type, build_index_type (size_int (nelts - 1)));
7878 /* Recursively examines the array elements of TYPE, until a non-array
7879 element type is found. */
7881 tree
7882 strip_array_types (tree type)
7884 while (TREE_CODE (type) == ARRAY_TYPE)
7885 type = TREE_TYPE (type);
7887 return type;
7890 /* Computes the canonical argument types from the argument type list
7891 ARGTYPES.
7893 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
7894 on entry to this function, or if any of the ARGTYPES are
7895 structural.
7897 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
7898 true on entry to this function, or if any of the ARGTYPES are
7899 non-canonical.
7901 Returns a canonical argument list, which may be ARGTYPES when the
7902 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
7903 true) or would not differ from ARGTYPES. */
7905 static tree
7906 maybe_canonicalize_argtypes (tree argtypes,
7907 bool *any_structural_p,
7908 bool *any_noncanonical_p)
7910 tree arg;
7911 bool any_noncanonical_argtypes_p = false;
7913 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
7915 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
7916 /* Fail gracefully by stating that the type is structural. */
7917 *any_structural_p = true;
7918 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
7919 *any_structural_p = true;
7920 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
7921 || TREE_PURPOSE (arg))
7922 /* If the argument has a default argument, we consider it
7923 non-canonical even though the type itself is canonical.
7924 That way, different variants of function and method types
7925 with default arguments will all point to the variant with
7926 no defaults as their canonical type. */
7927 any_noncanonical_argtypes_p = true;
7930 if (*any_structural_p)
7931 return argtypes;
7933 if (any_noncanonical_argtypes_p)
7935 /* Build the canonical list of argument types. */
7936 tree canon_argtypes = NULL_TREE;
7937 bool is_void = false;
7939 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
7941 if (arg == void_list_node)
7942 is_void = true;
7943 else
7944 canon_argtypes = tree_cons (NULL_TREE,
7945 TYPE_CANONICAL (TREE_VALUE (arg)),
7946 canon_argtypes);
7949 canon_argtypes = nreverse (canon_argtypes);
7950 if (is_void)
7951 canon_argtypes = chainon (canon_argtypes, void_list_node);
7953 /* There is a non-canonical type. */
7954 *any_noncanonical_p = true;
7955 return canon_argtypes;
7958 /* The canonical argument types are the same as ARGTYPES. */
7959 return argtypes;
7962 /* Construct, lay out and return
7963 the type of functions returning type VALUE_TYPE
7964 given arguments of types ARG_TYPES.
7965 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
7966 are data type nodes for the arguments of the function.
7967 If such a type has already been constructed, reuse it. */
7969 tree
7970 build_function_type (tree value_type, tree arg_types)
7972 tree t;
7973 hashval_t hashcode = 0;
7974 bool any_structural_p, any_noncanonical_p;
7975 tree canon_argtypes;
7977 if (TREE_CODE (value_type) == FUNCTION_TYPE)
7979 error ("function return type cannot be function");
7980 value_type = integer_type_node;
7983 /* Make a node of the sort we want. */
7984 t = make_node (FUNCTION_TYPE);
7985 TREE_TYPE (t) = value_type;
7986 TYPE_ARG_TYPES (t) = arg_types;
7988 /* If we already have such a type, use the old one. */
7989 hashcode = iterative_hash_object (TYPE_HASH (value_type), hashcode);
7990 hashcode = type_hash_list (arg_types, hashcode);
7991 t = type_hash_canon (hashcode, t);
7993 /* Set up the canonical type. */
7994 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
7995 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
7996 canon_argtypes = maybe_canonicalize_argtypes (arg_types,
7997 &any_structural_p,
7998 &any_noncanonical_p);
7999 if (any_structural_p)
8000 SET_TYPE_STRUCTURAL_EQUALITY (t);
8001 else if (any_noncanonical_p)
8002 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
8003 canon_argtypes);
8005 if (!COMPLETE_TYPE_P (t))
8006 layout_type (t);
8007 return t;
8010 /* Build a function type. The RETURN_TYPE is the type returned by the
8011 function. If VAARGS is set, no void_type_node is appended to the
8012 the list. ARGP must be always be terminated be a NULL_TREE. */
8014 static tree
8015 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
8017 tree t, args, last;
8019 t = va_arg (argp, tree);
8020 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
8021 args = tree_cons (NULL_TREE, t, args);
8023 if (vaargs)
8025 last = args;
8026 if (args != NULL_TREE)
8027 args = nreverse (args);
8028 gcc_assert (last != void_list_node);
8030 else if (args == NULL_TREE)
8031 args = void_list_node;
8032 else
8034 last = args;
8035 args = nreverse (args);
8036 TREE_CHAIN (last) = void_list_node;
8038 args = build_function_type (return_type, args);
8040 return args;
8043 /* Build a function type. The RETURN_TYPE is the type returned by the
8044 function. If additional arguments are provided, they are
8045 additional argument types. The list of argument types must always
8046 be terminated by NULL_TREE. */
8048 tree
8049 build_function_type_list (tree return_type, ...)
8051 tree args;
8052 va_list p;
8054 va_start (p, return_type);
8055 args = build_function_type_list_1 (false, return_type, p);
8056 va_end (p);
8057 return args;
8060 /* Build a variable argument function type. The RETURN_TYPE is the
8061 type returned by the function. If additional arguments are provided,
8062 they are additional argument types. The list of argument types must
8063 always be terminated by NULL_TREE. */
8065 tree
8066 build_varargs_function_type_list (tree return_type, ...)
8068 tree args;
8069 va_list p;
8071 va_start (p, return_type);
8072 args = build_function_type_list_1 (true, return_type, p);
8073 va_end (p);
8075 return args;
8078 /* Build a function type. RETURN_TYPE is the type returned by the
8079 function; VAARGS indicates whether the function takes varargs. The
8080 function takes N named arguments, the types of which are provided in
8081 ARG_TYPES. */
8083 static tree
8084 build_function_type_array_1 (bool vaargs, tree return_type, int n,
8085 tree *arg_types)
8087 int i;
8088 tree t = vaargs ? NULL_TREE : void_list_node;
8090 for (i = n - 1; i >= 0; i--)
8091 t = tree_cons (NULL_TREE, arg_types[i], t);
8093 return build_function_type (return_type, t);
8096 /* Build a function type. RETURN_TYPE is the type returned by the
8097 function. The function takes N named arguments, the types of which
8098 are provided in ARG_TYPES. */
8100 tree
8101 build_function_type_array (tree return_type, int n, tree *arg_types)
8103 return build_function_type_array_1 (false, return_type, n, arg_types);
8106 /* Build a variable argument function type. RETURN_TYPE is the type
8107 returned by the function. The function takes N named arguments, the
8108 types of which are provided in ARG_TYPES. */
8110 tree
8111 build_varargs_function_type_array (tree return_type, int n, tree *arg_types)
8113 return build_function_type_array_1 (true, return_type, n, arg_types);
8116 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
8117 and ARGTYPES (a TREE_LIST) are the return type and arguments types
8118 for the method. An implicit additional parameter (of type
8119 pointer-to-BASETYPE) is added to the ARGTYPES. */
8121 tree
8122 build_method_type_directly (tree basetype,
8123 tree rettype,
8124 tree argtypes)
8126 tree t;
8127 tree ptype;
8128 int hashcode = 0;
8129 bool any_structural_p, any_noncanonical_p;
8130 tree canon_argtypes;
8132 /* Make a node of the sort we want. */
8133 t = make_node (METHOD_TYPE);
8135 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8136 TREE_TYPE (t) = rettype;
8137 ptype = build_pointer_type (basetype);
8139 /* The actual arglist for this function includes a "hidden" argument
8140 which is "this". Put it into the list of argument types. */
8141 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
8142 TYPE_ARG_TYPES (t) = argtypes;
8144 /* If we already have such a type, use the old one. */
8145 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
8146 hashcode = iterative_hash_object (TYPE_HASH (rettype), hashcode);
8147 hashcode = type_hash_list (argtypes, hashcode);
8148 t = type_hash_canon (hashcode, t);
8150 /* Set up the canonical type. */
8151 any_structural_p
8152 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8153 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
8154 any_noncanonical_p
8155 = (TYPE_CANONICAL (basetype) != basetype
8156 || TYPE_CANONICAL (rettype) != rettype);
8157 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
8158 &any_structural_p,
8159 &any_noncanonical_p);
8160 if (any_structural_p)
8161 SET_TYPE_STRUCTURAL_EQUALITY (t);
8162 else if (any_noncanonical_p)
8163 TYPE_CANONICAL (t)
8164 = build_method_type_directly (TYPE_CANONICAL (basetype),
8165 TYPE_CANONICAL (rettype),
8166 canon_argtypes);
8167 if (!COMPLETE_TYPE_P (t))
8168 layout_type (t);
8170 return t;
8173 /* Construct, lay out and return the type of methods belonging to class
8174 BASETYPE and whose arguments and values are described by TYPE.
8175 If that type exists already, reuse it.
8176 TYPE must be a FUNCTION_TYPE node. */
8178 tree
8179 build_method_type (tree basetype, tree type)
8181 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8183 return build_method_type_directly (basetype,
8184 TREE_TYPE (type),
8185 TYPE_ARG_TYPES (type));
8188 /* Construct, lay out and return the type of offsets to a value
8189 of type TYPE, within an object of type BASETYPE.
8190 If a suitable offset type exists already, reuse it. */
8192 tree
8193 build_offset_type (tree basetype, tree type)
8195 tree t;
8196 hashval_t hashcode = 0;
8198 /* Make a node of the sort we want. */
8199 t = make_node (OFFSET_TYPE);
8201 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8202 TREE_TYPE (t) = type;
8204 /* If we already have such a type, use the old one. */
8205 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
8206 hashcode = iterative_hash_object (TYPE_HASH (type), hashcode);
8207 t = type_hash_canon (hashcode, t);
8209 if (!COMPLETE_TYPE_P (t))
8210 layout_type (t);
8212 if (TYPE_CANONICAL (t) == t)
8214 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8215 || TYPE_STRUCTURAL_EQUALITY_P (type))
8216 SET_TYPE_STRUCTURAL_EQUALITY (t);
8217 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
8218 || TYPE_CANONICAL (type) != type)
8219 TYPE_CANONICAL (t)
8220 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
8221 TYPE_CANONICAL (type));
8224 return t;
8227 /* Create a complex type whose components are COMPONENT_TYPE. */
8229 tree
8230 build_complex_type (tree component_type)
8232 tree t;
8233 hashval_t hashcode;
8235 gcc_assert (INTEGRAL_TYPE_P (component_type)
8236 || SCALAR_FLOAT_TYPE_P (component_type)
8237 || FIXED_POINT_TYPE_P (component_type));
8239 /* Make a node of the sort we want. */
8240 t = make_node (COMPLEX_TYPE);
8242 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
8244 /* If we already have such a type, use the old one. */
8245 hashcode = iterative_hash_object (TYPE_HASH (component_type), 0);
8246 t = type_hash_canon (hashcode, t);
8248 if (!COMPLETE_TYPE_P (t))
8249 layout_type (t);
8251 if (TYPE_CANONICAL (t) == t)
8253 if (TYPE_STRUCTURAL_EQUALITY_P (component_type))
8254 SET_TYPE_STRUCTURAL_EQUALITY (t);
8255 else if (TYPE_CANONICAL (component_type) != component_type)
8256 TYPE_CANONICAL (t)
8257 = build_complex_type (TYPE_CANONICAL (component_type));
8260 /* We need to create a name, since complex is a fundamental type. */
8261 if (! TYPE_NAME (t))
8263 const char *name;
8264 if (component_type == char_type_node)
8265 name = "complex char";
8266 else if (component_type == signed_char_type_node)
8267 name = "complex signed char";
8268 else if (component_type == unsigned_char_type_node)
8269 name = "complex unsigned char";
8270 else if (component_type == short_integer_type_node)
8271 name = "complex short int";
8272 else if (component_type == short_unsigned_type_node)
8273 name = "complex short unsigned int";
8274 else if (component_type == integer_type_node)
8275 name = "complex int";
8276 else if (component_type == unsigned_type_node)
8277 name = "complex unsigned int";
8278 else if (component_type == long_integer_type_node)
8279 name = "complex long int";
8280 else if (component_type == long_unsigned_type_node)
8281 name = "complex long unsigned int";
8282 else if (component_type == long_long_integer_type_node)
8283 name = "complex long long int";
8284 else if (component_type == long_long_unsigned_type_node)
8285 name = "complex long long unsigned int";
8286 else
8287 name = 0;
8289 if (name != 0)
8290 TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
8291 get_identifier (name), t);
8294 return build_qualified_type (t, TYPE_QUALS (component_type));
8297 /* If TYPE is a real or complex floating-point type and the target
8298 does not directly support arithmetic on TYPE then return the wider
8299 type to be used for arithmetic on TYPE. Otherwise, return
8300 NULL_TREE. */
8302 tree
8303 excess_precision_type (tree type)
8305 if (flag_excess_precision != EXCESS_PRECISION_FAST)
8307 int flt_eval_method = TARGET_FLT_EVAL_METHOD;
8308 switch (TREE_CODE (type))
8310 case REAL_TYPE:
8311 switch (flt_eval_method)
8313 case 1:
8314 if (TYPE_MODE (type) == TYPE_MODE (float_type_node))
8315 return double_type_node;
8316 break;
8317 case 2:
8318 if (TYPE_MODE (type) == TYPE_MODE (float_type_node)
8319 || TYPE_MODE (type) == TYPE_MODE (double_type_node))
8320 return long_double_type_node;
8321 break;
8322 default:
8323 gcc_unreachable ();
8325 break;
8326 case COMPLEX_TYPE:
8327 if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
8328 return NULL_TREE;
8329 switch (flt_eval_method)
8331 case 1:
8332 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node))
8333 return complex_double_type_node;
8334 break;
8335 case 2:
8336 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node)
8337 || (TYPE_MODE (TREE_TYPE (type))
8338 == TYPE_MODE (double_type_node)))
8339 return complex_long_double_type_node;
8340 break;
8341 default:
8342 gcc_unreachable ();
8344 break;
8345 default:
8346 break;
8349 return NULL_TREE;
8352 /* Return OP, stripped of any conversions to wider types as much as is safe.
8353 Converting the value back to OP's type makes a value equivalent to OP.
8355 If FOR_TYPE is nonzero, we return a value which, if converted to
8356 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
8358 OP must have integer, real or enumeral type. Pointers are not allowed!
8360 There are some cases where the obvious value we could return
8361 would regenerate to OP if converted to OP's type,
8362 but would not extend like OP to wider types.
8363 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
8364 For example, if OP is (unsigned short)(signed char)-1,
8365 we avoid returning (signed char)-1 if FOR_TYPE is int,
8366 even though extending that to an unsigned short would regenerate OP,
8367 since the result of extending (signed char)-1 to (int)
8368 is different from (int) OP. */
8370 tree
8371 get_unwidened (tree op, tree for_type)
8373 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
8374 tree type = TREE_TYPE (op);
8375 unsigned final_prec
8376 = TYPE_PRECISION (for_type != 0 ? for_type : type);
8377 int uns
8378 = (for_type != 0 && for_type != type
8379 && final_prec > TYPE_PRECISION (type)
8380 && TYPE_UNSIGNED (type));
8381 tree win = op;
8383 while (CONVERT_EXPR_P (op))
8385 int bitschange;
8387 /* TYPE_PRECISION on vector types has different meaning
8388 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
8389 so avoid them here. */
8390 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
8391 break;
8393 bitschange = TYPE_PRECISION (TREE_TYPE (op))
8394 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
8396 /* Truncations are many-one so cannot be removed.
8397 Unless we are later going to truncate down even farther. */
8398 if (bitschange < 0
8399 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
8400 break;
8402 /* See what's inside this conversion. If we decide to strip it,
8403 we will set WIN. */
8404 op = TREE_OPERAND (op, 0);
8406 /* If we have not stripped any zero-extensions (uns is 0),
8407 we can strip any kind of extension.
8408 If we have previously stripped a zero-extension,
8409 only zero-extensions can safely be stripped.
8410 Any extension can be stripped if the bits it would produce
8411 are all going to be discarded later by truncating to FOR_TYPE. */
8413 if (bitschange > 0)
8415 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
8416 win = op;
8417 /* TYPE_UNSIGNED says whether this is a zero-extension.
8418 Let's avoid computing it if it does not affect WIN
8419 and if UNS will not be needed again. */
8420 if ((uns
8421 || CONVERT_EXPR_P (op))
8422 && TYPE_UNSIGNED (TREE_TYPE (op)))
8424 uns = 1;
8425 win = op;
8430 /* If we finally reach a constant see if it fits in for_type and
8431 in that case convert it. */
8432 if (for_type
8433 && TREE_CODE (win) == INTEGER_CST
8434 && TREE_TYPE (win) != for_type
8435 && int_fits_type_p (win, for_type))
8436 win = fold_convert (for_type, win);
8438 return win;
8441 /* Return OP or a simpler expression for a narrower value
8442 which can be sign-extended or zero-extended to give back OP.
8443 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
8444 or 0 if the value should be sign-extended. */
8446 tree
8447 get_narrower (tree op, int *unsignedp_ptr)
8449 int uns = 0;
8450 int first = 1;
8451 tree win = op;
8452 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
8454 while (TREE_CODE (op) == NOP_EXPR)
8456 int bitschange
8457 = (TYPE_PRECISION (TREE_TYPE (op))
8458 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
8460 /* Truncations are many-one so cannot be removed. */
8461 if (bitschange < 0)
8462 break;
8464 /* See what's inside this conversion. If we decide to strip it,
8465 we will set WIN. */
8467 if (bitschange > 0)
8469 op = TREE_OPERAND (op, 0);
8470 /* An extension: the outermost one can be stripped,
8471 but remember whether it is zero or sign extension. */
8472 if (first)
8473 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8474 /* Otherwise, if a sign extension has been stripped,
8475 only sign extensions can now be stripped;
8476 if a zero extension has been stripped, only zero-extensions. */
8477 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
8478 break;
8479 first = 0;
8481 else /* bitschange == 0 */
8483 /* A change in nominal type can always be stripped, but we must
8484 preserve the unsignedness. */
8485 if (first)
8486 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8487 first = 0;
8488 op = TREE_OPERAND (op, 0);
8489 /* Keep trying to narrow, but don't assign op to win if it
8490 would turn an integral type into something else. */
8491 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
8492 continue;
8495 win = op;
8498 if (TREE_CODE (op) == COMPONENT_REF
8499 /* Since type_for_size always gives an integer type. */
8500 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
8501 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
8502 /* Ensure field is laid out already. */
8503 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
8504 && tree_fits_uhwi_p (DECL_SIZE (TREE_OPERAND (op, 1))))
8506 unsigned HOST_WIDE_INT innerprec
8507 = tree_to_uhwi (DECL_SIZE (TREE_OPERAND (op, 1)));
8508 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
8509 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
8510 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
8512 /* We can get this structure field in a narrower type that fits it,
8513 but the resulting extension to its nominal type (a fullword type)
8514 must satisfy the same conditions as for other extensions.
8516 Do this only for fields that are aligned (not bit-fields),
8517 because when bit-field insns will be used there is no
8518 advantage in doing this. */
8520 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
8521 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
8522 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
8523 && type != 0)
8525 if (first)
8526 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
8527 win = fold_convert (type, op);
8531 *unsignedp_ptr = uns;
8532 return win;
8535 /* Returns true if integer constant C has a value that is permissible
8536 for type TYPE (an INTEGER_TYPE). */
8538 bool
8539 int_fits_type_p (const_tree c, const_tree type)
8541 tree type_low_bound, type_high_bound;
8542 bool ok_for_low_bound, ok_for_high_bound;
8543 signop sgn_c = TYPE_SIGN (TREE_TYPE (c));
8545 retry:
8546 type_low_bound = TYPE_MIN_VALUE (type);
8547 type_high_bound = TYPE_MAX_VALUE (type);
8549 /* If at least one bound of the type is a constant integer, we can check
8550 ourselves and maybe make a decision. If no such decision is possible, but
8551 this type is a subtype, try checking against that. Otherwise, use
8552 fits_to_tree_p, which checks against the precision.
8554 Compute the status for each possibly constant bound, and return if we see
8555 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
8556 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
8557 for "constant known to fit". */
8559 /* Check if c >= type_low_bound. */
8560 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
8562 if (tree_int_cst_lt (c, type_low_bound))
8563 return false;
8564 ok_for_low_bound = true;
8566 else
8567 ok_for_low_bound = false;
8569 /* Check if c <= type_high_bound. */
8570 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
8572 if (tree_int_cst_lt (type_high_bound, c))
8573 return false;
8574 ok_for_high_bound = true;
8576 else
8577 ok_for_high_bound = false;
8579 /* If the constant fits both bounds, the result is known. */
8580 if (ok_for_low_bound && ok_for_high_bound)
8581 return true;
8583 /* Perform some generic filtering which may allow making a decision
8584 even if the bounds are not constant. First, negative integers
8585 never fit in unsigned types, */
8586 if (TYPE_UNSIGNED (type) && sgn_c == SIGNED && wi::neg_p (c))
8587 return false;
8589 /* Second, narrower types always fit in wider ones. */
8590 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
8591 return true;
8593 /* Third, unsigned integers with top bit set never fit signed types. */
8594 if (!TYPE_UNSIGNED (type) && sgn_c == UNSIGNED)
8596 int prec = GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (c))) - 1;
8597 if (prec < TYPE_PRECISION (TREE_TYPE (c)))
8599 /* When a tree_cst is converted to a wide-int, the precision
8600 is taken from the type. However, if the precision of the
8601 mode underneath the type is smaller than that, it is
8602 possible that the value will not fit. The test below
8603 fails if any bit is set between the sign bit of the
8604 underlying mode and the top bit of the type. */
8605 if (wi::ne_p (wi::zext (c, prec - 1), c))
8606 return false;
8608 else if (wi::neg_p (c))
8609 return false;
8612 /* If we haven't been able to decide at this point, there nothing more we
8613 can check ourselves here. Look at the base type if we have one and it
8614 has the same precision. */
8615 if (TREE_CODE (type) == INTEGER_TYPE
8616 && TREE_TYPE (type) != 0
8617 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
8619 type = TREE_TYPE (type);
8620 goto retry;
8623 /* Or to fits_to_tree_p, if nothing else. */
8624 return wi::fits_to_tree_p (c, type);
8627 /* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
8628 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
8629 represented (assuming two's-complement arithmetic) within the bit
8630 precision of the type are returned instead. */
8632 void
8633 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
8635 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
8636 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
8637 wi::to_mpz (TYPE_MIN_VALUE (type), min, TYPE_SIGN (type));
8638 else
8640 if (TYPE_UNSIGNED (type))
8641 mpz_set_ui (min, 0);
8642 else
8644 wide_int mn = wi::min_value (TYPE_PRECISION (type), SIGNED);
8645 wi::to_mpz (mn, min, SIGNED);
8649 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
8650 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
8651 wi::to_mpz (TYPE_MAX_VALUE (type), max, TYPE_SIGN (type));
8652 else
8654 wide_int mn = wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
8655 wi::to_mpz (mn, max, TYPE_SIGN (type));
8659 /* Return true if VAR is an automatic variable defined in function FN. */
8661 bool
8662 auto_var_in_fn_p (const_tree var, const_tree fn)
8664 return (DECL_P (var) && DECL_CONTEXT (var) == fn
8665 && ((((TREE_CODE (var) == VAR_DECL && ! DECL_EXTERNAL (var))
8666 || TREE_CODE (var) == PARM_DECL)
8667 && ! TREE_STATIC (var))
8668 || TREE_CODE (var) == LABEL_DECL
8669 || TREE_CODE (var) == RESULT_DECL));
8672 /* Subprogram of following function. Called by walk_tree.
8674 Return *TP if it is an automatic variable or parameter of the
8675 function passed in as DATA. */
8677 static tree
8678 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
8680 tree fn = (tree) data;
8682 if (TYPE_P (*tp))
8683 *walk_subtrees = 0;
8685 else if (DECL_P (*tp)
8686 && auto_var_in_fn_p (*tp, fn))
8687 return *tp;
8689 return NULL_TREE;
8692 /* Returns true if T is, contains, or refers to a type with variable
8693 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
8694 arguments, but not the return type. If FN is nonzero, only return
8695 true if a modifier of the type or position of FN is a variable or
8696 parameter inside FN.
8698 This concept is more general than that of C99 'variably modified types':
8699 in C99, a struct type is never variably modified because a VLA may not
8700 appear as a structure member. However, in GNU C code like:
8702 struct S { int i[f()]; };
8704 is valid, and other languages may define similar constructs. */
8706 bool
8707 variably_modified_type_p (tree type, tree fn)
8709 tree t;
8711 /* Test if T is either variable (if FN is zero) or an expression containing
8712 a variable in FN. If TYPE isn't gimplified, return true also if
8713 gimplify_one_sizepos would gimplify the expression into a local
8714 variable. */
8715 #define RETURN_TRUE_IF_VAR(T) \
8716 do { tree _t = (T); \
8717 if (_t != NULL_TREE \
8718 && _t != error_mark_node \
8719 && TREE_CODE (_t) != INTEGER_CST \
8720 && TREE_CODE (_t) != PLACEHOLDER_EXPR \
8721 && (!fn \
8722 || (!TYPE_SIZES_GIMPLIFIED (type) \
8723 && !is_gimple_sizepos (_t)) \
8724 || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
8725 return true; } while (0)
8727 if (type == error_mark_node)
8728 return false;
8730 /* If TYPE itself has variable size, it is variably modified. */
8731 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
8732 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
8734 switch (TREE_CODE (type))
8736 case POINTER_TYPE:
8737 case REFERENCE_TYPE:
8738 case VECTOR_TYPE:
8739 if (variably_modified_type_p (TREE_TYPE (type), fn))
8740 return true;
8741 break;
8743 case FUNCTION_TYPE:
8744 case METHOD_TYPE:
8745 /* If TYPE is a function type, it is variably modified if the
8746 return type is variably modified. */
8747 if (variably_modified_type_p (TREE_TYPE (type), fn))
8748 return true;
8749 break;
8751 case INTEGER_TYPE:
8752 case REAL_TYPE:
8753 case FIXED_POINT_TYPE:
8754 case ENUMERAL_TYPE:
8755 case BOOLEAN_TYPE:
8756 /* Scalar types are variably modified if their end points
8757 aren't constant. */
8758 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
8759 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
8760 break;
8762 case RECORD_TYPE:
8763 case UNION_TYPE:
8764 case QUAL_UNION_TYPE:
8765 /* We can't see if any of the fields are variably-modified by the
8766 definition we normally use, since that would produce infinite
8767 recursion via pointers. */
8768 /* This is variably modified if some field's type is. */
8769 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
8770 if (TREE_CODE (t) == FIELD_DECL)
8772 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
8773 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
8774 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
8776 if (TREE_CODE (type) == QUAL_UNION_TYPE)
8777 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
8779 break;
8781 case ARRAY_TYPE:
8782 /* Do not call ourselves to avoid infinite recursion. This is
8783 variably modified if the element type is. */
8784 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
8785 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
8786 break;
8788 default:
8789 break;
8792 /* The current language may have other cases to check, but in general,
8793 all other types are not variably modified. */
8794 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
8796 #undef RETURN_TRUE_IF_VAR
8799 /* Given a DECL or TYPE, return the scope in which it was declared, or
8800 NULL_TREE if there is no containing scope. */
8802 tree
8803 get_containing_scope (const_tree t)
8805 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
8808 /* Return the innermost context enclosing DECL that is
8809 a FUNCTION_DECL, or zero if none. */
8811 tree
8812 decl_function_context (const_tree decl)
8814 tree context;
8816 if (TREE_CODE (decl) == ERROR_MARK)
8817 return 0;
8819 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
8820 where we look up the function at runtime. Such functions always take
8821 a first argument of type 'pointer to real context'.
8823 C++ should really be fixed to use DECL_CONTEXT for the real context,
8824 and use something else for the "virtual context". */
8825 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
8826 context
8827 = TYPE_MAIN_VARIANT
8828 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
8829 else
8830 context = DECL_CONTEXT (decl);
8832 while (context && TREE_CODE (context) != FUNCTION_DECL)
8834 if (TREE_CODE (context) == BLOCK)
8835 context = BLOCK_SUPERCONTEXT (context);
8836 else
8837 context = get_containing_scope (context);
8840 return context;
8843 /* Return the innermost context enclosing DECL that is
8844 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
8845 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
8847 tree
8848 decl_type_context (const_tree decl)
8850 tree context = DECL_CONTEXT (decl);
8852 while (context)
8853 switch (TREE_CODE (context))
8855 case NAMESPACE_DECL:
8856 case TRANSLATION_UNIT_DECL:
8857 return NULL_TREE;
8859 case RECORD_TYPE:
8860 case UNION_TYPE:
8861 case QUAL_UNION_TYPE:
8862 return context;
8864 case TYPE_DECL:
8865 case FUNCTION_DECL:
8866 context = DECL_CONTEXT (context);
8867 break;
8869 case BLOCK:
8870 context = BLOCK_SUPERCONTEXT (context);
8871 break;
8873 default:
8874 gcc_unreachable ();
8877 return NULL_TREE;
8880 /* CALL is a CALL_EXPR. Return the declaration for the function
8881 called, or NULL_TREE if the called function cannot be
8882 determined. */
8884 tree
8885 get_callee_fndecl (const_tree call)
8887 tree addr;
8889 if (call == error_mark_node)
8890 return error_mark_node;
8892 /* It's invalid to call this function with anything but a
8893 CALL_EXPR. */
8894 gcc_assert (TREE_CODE (call) == CALL_EXPR);
8896 /* The first operand to the CALL is the address of the function
8897 called. */
8898 addr = CALL_EXPR_FN (call);
8900 STRIP_NOPS (addr);
8902 /* If this is a readonly function pointer, extract its initial value. */
8903 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
8904 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
8905 && DECL_INITIAL (addr))
8906 addr = DECL_INITIAL (addr);
8908 /* If the address is just `&f' for some function `f', then we know
8909 that `f' is being called. */
8910 if (TREE_CODE (addr) == ADDR_EXPR
8911 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
8912 return TREE_OPERAND (addr, 0);
8914 /* We couldn't figure out what was being called. */
8915 return NULL_TREE;
8918 /* Print debugging information about tree nodes generated during the compile,
8919 and any language-specific information. */
8921 void
8922 dump_tree_statistics (void)
8924 if (GATHER_STATISTICS)
8926 int i;
8927 int total_nodes, total_bytes;
8928 fprintf (stderr, "Kind Nodes Bytes\n");
8929 fprintf (stderr, "---------------------------------------\n");
8930 total_nodes = total_bytes = 0;
8931 for (i = 0; i < (int) all_kinds; i++)
8933 fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
8934 tree_node_counts[i], tree_node_sizes[i]);
8935 total_nodes += tree_node_counts[i];
8936 total_bytes += tree_node_sizes[i];
8938 fprintf (stderr, "---------------------------------------\n");
8939 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
8940 fprintf (stderr, "---------------------------------------\n");
8941 fprintf (stderr, "Code Nodes\n");
8942 fprintf (stderr, "----------------------------\n");
8943 for (i = 0; i < (int) MAX_TREE_CODES; i++)
8944 fprintf (stderr, "%-20s %7d\n", get_tree_code_name ((enum tree_code) i),
8945 tree_code_counts[i]);
8946 fprintf (stderr, "----------------------------\n");
8947 ssanames_print_statistics ();
8948 phinodes_print_statistics ();
8950 else
8951 fprintf (stderr, "(No per-node statistics)\n");
8953 print_type_hash_statistics ();
8954 print_debug_expr_statistics ();
8955 print_value_expr_statistics ();
8956 lang_hooks.print_statistics ();
8959 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
8961 /* Generate a crc32 of a byte. */
8963 static unsigned
8964 crc32_unsigned_bits (unsigned chksum, unsigned value, unsigned bits)
8966 unsigned ix;
8968 for (ix = bits; ix--; value <<= 1)
8970 unsigned feedback;
8972 feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
8973 chksum <<= 1;
8974 chksum ^= feedback;
8976 return chksum;
8979 /* Generate a crc32 of a 32-bit unsigned. */
8981 unsigned
8982 crc32_unsigned (unsigned chksum, unsigned value)
8984 return crc32_unsigned_bits (chksum, value, 32);
8987 /* Generate a crc32 of a byte. */
8989 unsigned
8990 crc32_byte (unsigned chksum, char byte)
8992 return crc32_unsigned_bits (chksum, (unsigned) byte << 24, 8);
8995 /* Generate a crc32 of a string. */
8997 unsigned
8998 crc32_string (unsigned chksum, const char *string)
9002 chksum = crc32_byte (chksum, *string);
9004 while (*string++);
9005 return chksum;
9008 /* P is a string that will be used in a symbol. Mask out any characters
9009 that are not valid in that context. */
9011 void
9012 clean_symbol_name (char *p)
9014 for (; *p; p++)
9015 if (! (ISALNUM (*p)
9016 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
9017 || *p == '$'
9018 #endif
9019 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
9020 || *p == '.'
9021 #endif
9023 *p = '_';
9026 /* Generate a name for a special-purpose function.
9027 The generated name may need to be unique across the whole link.
9028 Changes to this function may also require corresponding changes to
9029 xstrdup_mask_random.
9030 TYPE is some string to identify the purpose of this function to the
9031 linker or collect2; it must start with an uppercase letter,
9032 one of:
9033 I - for constructors
9034 D - for destructors
9035 N - for C++ anonymous namespaces
9036 F - for DWARF unwind frame information. */
9038 tree
9039 get_file_function_name (const char *type)
9041 char *buf;
9042 const char *p;
9043 char *q;
9045 /* If we already have a name we know to be unique, just use that. */
9046 if (first_global_object_name)
9047 p = q = ASTRDUP (first_global_object_name);
9048 /* If the target is handling the constructors/destructors, they
9049 will be local to this file and the name is only necessary for
9050 debugging purposes.
9051 We also assign sub_I and sub_D sufixes to constructors called from
9052 the global static constructors. These are always local. */
9053 else if (((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
9054 || (strncmp (type, "sub_", 4) == 0
9055 && (type[4] == 'I' || type[4] == 'D')))
9057 const char *file = main_input_filename;
9058 if (! file)
9059 file = LOCATION_FILE (input_location);
9060 /* Just use the file's basename, because the full pathname
9061 might be quite long. */
9062 p = q = ASTRDUP (lbasename (file));
9064 else
9066 /* Otherwise, the name must be unique across the entire link.
9067 We don't have anything that we know to be unique to this translation
9068 unit, so use what we do have and throw in some randomness. */
9069 unsigned len;
9070 const char *name = weak_global_object_name;
9071 const char *file = main_input_filename;
9073 if (! name)
9074 name = "";
9075 if (! file)
9076 file = LOCATION_FILE (input_location);
9078 len = strlen (file);
9079 q = (char *) alloca (9 + 17 + len + 1);
9080 memcpy (q, file, len + 1);
9082 snprintf (q + len, 9 + 17 + 1, "_%08X_" HOST_WIDE_INT_PRINT_HEX,
9083 crc32_string (0, name), get_random_seed (false));
9085 p = q;
9088 clean_symbol_name (q);
9089 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
9090 + strlen (type));
9092 /* Set up the name of the file-level functions we may need.
9093 Use a global object (which is already required to be unique over
9094 the program) rather than the file name (which imposes extra
9095 constraints). */
9096 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
9098 return get_identifier (buf);
9101 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
9103 /* Complain that the tree code of NODE does not match the expected 0
9104 terminated list of trailing codes. The trailing code list can be
9105 empty, for a more vague error message. FILE, LINE, and FUNCTION
9106 are of the caller. */
9108 void
9109 tree_check_failed (const_tree node, const char *file,
9110 int line, const char *function, ...)
9112 va_list args;
9113 const char *buffer;
9114 unsigned length = 0;
9115 enum tree_code code;
9117 va_start (args, function);
9118 while ((code = (enum tree_code) va_arg (args, int)))
9119 length += 4 + strlen (get_tree_code_name (code));
9120 va_end (args);
9121 if (length)
9123 char *tmp;
9124 va_start (args, function);
9125 length += strlen ("expected ");
9126 buffer = tmp = (char *) alloca (length);
9127 length = 0;
9128 while ((code = (enum tree_code) va_arg (args, int)))
9130 const char *prefix = length ? " or " : "expected ";
9132 strcpy (tmp + length, prefix);
9133 length += strlen (prefix);
9134 strcpy (tmp + length, get_tree_code_name (code));
9135 length += strlen (get_tree_code_name (code));
9137 va_end (args);
9139 else
9140 buffer = "unexpected node";
9142 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9143 buffer, get_tree_code_name (TREE_CODE (node)),
9144 function, trim_filename (file), line);
9147 /* Complain that the tree code of NODE does match the expected 0
9148 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
9149 the caller. */
9151 void
9152 tree_not_check_failed (const_tree node, const char *file,
9153 int line, const char *function, ...)
9155 va_list args;
9156 char *buffer;
9157 unsigned length = 0;
9158 enum tree_code code;
9160 va_start (args, function);
9161 while ((code = (enum tree_code) va_arg (args, int)))
9162 length += 4 + strlen (get_tree_code_name (code));
9163 va_end (args);
9164 va_start (args, function);
9165 buffer = (char *) alloca (length);
9166 length = 0;
9167 while ((code = (enum tree_code) va_arg (args, int)))
9169 if (length)
9171 strcpy (buffer + length, " or ");
9172 length += 4;
9174 strcpy (buffer + length, get_tree_code_name (code));
9175 length += strlen (get_tree_code_name (code));
9177 va_end (args);
9179 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
9180 buffer, get_tree_code_name (TREE_CODE (node)),
9181 function, trim_filename (file), line);
9184 /* Similar to tree_check_failed, except that we check for a class of tree
9185 code, given in CL. */
9187 void
9188 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
9189 const char *file, int line, const char *function)
9191 internal_error
9192 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
9193 TREE_CODE_CLASS_STRING (cl),
9194 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9195 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9198 /* Similar to tree_check_failed, except that instead of specifying a
9199 dozen codes, use the knowledge that they're all sequential. */
9201 void
9202 tree_range_check_failed (const_tree node, const char *file, int line,
9203 const char *function, enum tree_code c1,
9204 enum tree_code c2)
9206 char *buffer;
9207 unsigned length = 0;
9208 unsigned int c;
9210 for (c = c1; c <= c2; ++c)
9211 length += 4 + strlen (get_tree_code_name ((enum tree_code) c));
9213 length += strlen ("expected ");
9214 buffer = (char *) alloca (length);
9215 length = 0;
9217 for (c = c1; c <= c2; ++c)
9219 const char *prefix = length ? " or " : "expected ";
9221 strcpy (buffer + length, prefix);
9222 length += strlen (prefix);
9223 strcpy (buffer + length, get_tree_code_name ((enum tree_code) c));
9224 length += strlen (get_tree_code_name ((enum tree_code) c));
9227 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9228 buffer, get_tree_code_name (TREE_CODE (node)),
9229 function, trim_filename (file), line);
9233 /* Similar to tree_check_failed, except that we check that a tree does
9234 not have the specified code, given in CL. */
9236 void
9237 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
9238 const char *file, int line, const char *function)
9240 internal_error
9241 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
9242 TREE_CODE_CLASS_STRING (cl),
9243 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9244 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9248 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
9250 void
9251 omp_clause_check_failed (const_tree node, const char *file, int line,
9252 const char *function, enum omp_clause_code code)
9254 internal_error ("tree check: expected omp_clause %s, have %s in %s, at %s:%d",
9255 omp_clause_code_name[code], get_tree_code_name (TREE_CODE (node)),
9256 function, trim_filename (file), line);
9260 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
9262 void
9263 omp_clause_range_check_failed (const_tree node, const char *file, int line,
9264 const char *function, enum omp_clause_code c1,
9265 enum omp_clause_code c2)
9267 char *buffer;
9268 unsigned length = 0;
9269 unsigned int c;
9271 for (c = c1; c <= c2; ++c)
9272 length += 4 + strlen (omp_clause_code_name[c]);
9274 length += strlen ("expected ");
9275 buffer = (char *) alloca (length);
9276 length = 0;
9278 for (c = c1; c <= c2; ++c)
9280 const char *prefix = length ? " or " : "expected ";
9282 strcpy (buffer + length, prefix);
9283 length += strlen (prefix);
9284 strcpy (buffer + length, omp_clause_code_name[c]);
9285 length += strlen (omp_clause_code_name[c]);
9288 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9289 buffer, omp_clause_code_name[TREE_CODE (node)],
9290 function, trim_filename (file), line);
9294 #undef DEFTREESTRUCT
9295 #define DEFTREESTRUCT(VAL, NAME) NAME,
9297 static const char *ts_enum_names[] = {
9298 #include "treestruct.def"
9300 #undef DEFTREESTRUCT
9302 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
9304 /* Similar to tree_class_check_failed, except that we check for
9305 whether CODE contains the tree structure identified by EN. */
9307 void
9308 tree_contains_struct_check_failed (const_tree node,
9309 const enum tree_node_structure_enum en,
9310 const char *file, int line,
9311 const char *function)
9313 internal_error
9314 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
9315 TS_ENUM_NAME (en),
9316 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9320 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9321 (dynamically sized) vector. */
9323 void
9324 tree_int_cst_elt_check_failed (int idx, int len, const char *file, int line,
9325 const char *function)
9327 internal_error
9328 ("tree check: accessed elt %d of tree_int_cst with %d elts in %s, at %s:%d",
9329 idx + 1, len, function, trim_filename (file), line);
9332 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9333 (dynamically sized) vector. */
9335 void
9336 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
9337 const char *function)
9339 internal_error
9340 ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
9341 idx + 1, len, function, trim_filename (file), line);
9344 /* Similar to above, except that the check is for the bounds of the operand
9345 vector of an expression node EXP. */
9347 void
9348 tree_operand_check_failed (int idx, const_tree exp, const char *file,
9349 int line, const char *function)
9351 enum tree_code code = TREE_CODE (exp);
9352 internal_error
9353 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
9354 idx + 1, get_tree_code_name (code), TREE_OPERAND_LENGTH (exp),
9355 function, trim_filename (file), line);
9358 /* Similar to above, except that the check is for the number of
9359 operands of an OMP_CLAUSE node. */
9361 void
9362 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
9363 int line, const char *function)
9365 internal_error
9366 ("tree check: accessed operand %d of omp_clause %s with %d operands "
9367 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
9368 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
9369 trim_filename (file), line);
9371 #endif /* ENABLE_TREE_CHECKING */
9373 /* Create a new vector type node holding SUBPARTS units of type INNERTYPE,
9374 and mapped to the machine mode MODE. Initialize its fields and build
9375 the information necessary for debugging output. */
9377 static tree
9378 make_vector_type (tree innertype, int nunits, enum machine_mode mode)
9380 tree t;
9381 hashval_t hashcode = 0;
9383 t = make_node (VECTOR_TYPE);
9384 TREE_TYPE (t) = TYPE_MAIN_VARIANT (innertype);
9385 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
9386 SET_TYPE_MODE (t, mode);
9388 if (TYPE_STRUCTURAL_EQUALITY_P (innertype))
9389 SET_TYPE_STRUCTURAL_EQUALITY (t);
9390 else if (TYPE_CANONICAL (innertype) != innertype
9391 || mode != VOIDmode)
9392 TYPE_CANONICAL (t)
9393 = make_vector_type (TYPE_CANONICAL (innertype), nunits, VOIDmode);
9395 layout_type (t);
9397 hashcode = iterative_hash_host_wide_int (VECTOR_TYPE, hashcode);
9398 hashcode = iterative_hash_host_wide_int (nunits, hashcode);
9399 hashcode = iterative_hash_host_wide_int (mode, hashcode);
9400 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (t)), hashcode);
9401 t = type_hash_canon (hashcode, t);
9403 /* We have built a main variant, based on the main variant of the
9404 inner type. Use it to build the variant we return. */
9405 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
9406 && TREE_TYPE (t) != innertype)
9407 return build_type_attribute_qual_variant (t,
9408 TYPE_ATTRIBUTES (innertype),
9409 TYPE_QUALS (innertype));
9411 return t;
9414 static tree
9415 make_or_reuse_type (unsigned size, int unsignedp)
9417 if (size == INT_TYPE_SIZE)
9418 return unsignedp ? unsigned_type_node : integer_type_node;
9419 if (size == CHAR_TYPE_SIZE)
9420 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
9421 if (size == SHORT_TYPE_SIZE)
9422 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
9423 if (size == LONG_TYPE_SIZE)
9424 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
9425 if (size == LONG_LONG_TYPE_SIZE)
9426 return (unsignedp ? long_long_unsigned_type_node
9427 : long_long_integer_type_node);
9428 if (size == 128 && int128_integer_type_node)
9429 return (unsignedp ? int128_unsigned_type_node
9430 : int128_integer_type_node);
9432 if (unsignedp)
9433 return make_unsigned_type (size);
9434 else
9435 return make_signed_type (size);
9438 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
9440 static tree
9441 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
9443 if (satp)
9445 if (size == SHORT_FRACT_TYPE_SIZE)
9446 return unsignedp ? sat_unsigned_short_fract_type_node
9447 : sat_short_fract_type_node;
9448 if (size == FRACT_TYPE_SIZE)
9449 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
9450 if (size == LONG_FRACT_TYPE_SIZE)
9451 return unsignedp ? sat_unsigned_long_fract_type_node
9452 : sat_long_fract_type_node;
9453 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9454 return unsignedp ? sat_unsigned_long_long_fract_type_node
9455 : sat_long_long_fract_type_node;
9457 else
9459 if (size == SHORT_FRACT_TYPE_SIZE)
9460 return unsignedp ? unsigned_short_fract_type_node
9461 : short_fract_type_node;
9462 if (size == FRACT_TYPE_SIZE)
9463 return unsignedp ? unsigned_fract_type_node : fract_type_node;
9464 if (size == LONG_FRACT_TYPE_SIZE)
9465 return unsignedp ? unsigned_long_fract_type_node
9466 : long_fract_type_node;
9467 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9468 return unsignedp ? unsigned_long_long_fract_type_node
9469 : long_long_fract_type_node;
9472 return make_fract_type (size, unsignedp, satp);
9475 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
9477 static tree
9478 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
9480 if (satp)
9482 if (size == SHORT_ACCUM_TYPE_SIZE)
9483 return unsignedp ? sat_unsigned_short_accum_type_node
9484 : sat_short_accum_type_node;
9485 if (size == ACCUM_TYPE_SIZE)
9486 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
9487 if (size == LONG_ACCUM_TYPE_SIZE)
9488 return unsignedp ? sat_unsigned_long_accum_type_node
9489 : sat_long_accum_type_node;
9490 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9491 return unsignedp ? sat_unsigned_long_long_accum_type_node
9492 : sat_long_long_accum_type_node;
9494 else
9496 if (size == SHORT_ACCUM_TYPE_SIZE)
9497 return unsignedp ? unsigned_short_accum_type_node
9498 : short_accum_type_node;
9499 if (size == ACCUM_TYPE_SIZE)
9500 return unsignedp ? unsigned_accum_type_node : accum_type_node;
9501 if (size == LONG_ACCUM_TYPE_SIZE)
9502 return unsignedp ? unsigned_long_accum_type_node
9503 : long_accum_type_node;
9504 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9505 return unsignedp ? unsigned_long_long_accum_type_node
9506 : long_long_accum_type_node;
9509 return make_accum_type (size, unsignedp, satp);
9513 /* Create an atomic variant node for TYPE. This routine is called
9514 during initialization of data types to create the 5 basic atomic
9515 types. The generic build_variant_type function requires these to
9516 already be set up in order to function properly, so cannot be
9517 called from there. If ALIGN is non-zero, then ensure alignment is
9518 overridden to this value. */
9520 static tree
9521 build_atomic_base (tree type, unsigned int align)
9523 tree t;
9525 /* Make sure its not already registered. */
9526 if ((t = get_qualified_type (type, TYPE_QUAL_ATOMIC)))
9527 return t;
9529 t = build_variant_type_copy (type);
9530 set_type_quals (t, TYPE_QUAL_ATOMIC);
9532 if (align)
9533 TYPE_ALIGN (t) = align;
9535 return t;
9538 /* Create nodes for all integer types (and error_mark_node) using the sizes
9539 of C datatypes. SIGNED_CHAR specifies whether char is signed,
9540 SHORT_DOUBLE specifies whether double should be of the same precision
9541 as float. */
9543 void
9544 build_common_tree_nodes (bool signed_char, bool short_double)
9546 error_mark_node = make_node (ERROR_MARK);
9547 TREE_TYPE (error_mark_node) = error_mark_node;
9549 initialize_sizetypes ();
9551 /* Define both `signed char' and `unsigned char'. */
9552 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
9553 TYPE_STRING_FLAG (signed_char_type_node) = 1;
9554 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
9555 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
9557 /* Define `char', which is like either `signed char' or `unsigned char'
9558 but not the same as either. */
9559 char_type_node
9560 = (signed_char
9561 ? make_signed_type (CHAR_TYPE_SIZE)
9562 : make_unsigned_type (CHAR_TYPE_SIZE));
9563 TYPE_STRING_FLAG (char_type_node) = 1;
9565 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
9566 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
9567 integer_type_node = make_signed_type (INT_TYPE_SIZE);
9568 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
9569 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
9570 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
9571 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
9572 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
9573 #if HOST_BITS_PER_WIDE_INT >= 64
9574 /* TODO: This isn't correct, but as logic depends at the moment on
9575 host's instead of target's wide-integer.
9576 If there is a target not supporting TImode, but has an 128-bit
9577 integer-scalar register, this target check needs to be adjusted. */
9578 if (targetm.scalar_mode_supported_p (TImode))
9580 int128_integer_type_node = make_signed_type (128);
9581 int128_unsigned_type_node = make_unsigned_type (128);
9583 #endif
9585 /* Define a boolean type. This type only represents boolean values but
9586 may be larger than char depending on the value of BOOL_TYPE_SIZE. */
9587 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
9588 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
9589 TYPE_PRECISION (boolean_type_node) = 1;
9590 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
9592 /* Define what type to use for size_t. */
9593 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
9594 size_type_node = unsigned_type_node;
9595 else if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
9596 size_type_node = long_unsigned_type_node;
9597 else if (strcmp (SIZE_TYPE, "long long unsigned int") == 0)
9598 size_type_node = long_long_unsigned_type_node;
9599 else if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
9600 size_type_node = short_unsigned_type_node;
9601 else
9602 gcc_unreachable ();
9604 /* Fill in the rest of the sized types. Reuse existing type nodes
9605 when possible. */
9606 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
9607 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
9608 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
9609 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
9610 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
9612 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
9613 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
9614 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
9615 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
9616 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
9618 /* Don't call build_qualified type for atomics. That routine does
9619 special processing for atomics, and until they are initialized
9620 it's better not to make that call.
9622 Check to see if there is a target override for atomic types. */
9624 atomicQI_type_node = build_atomic_base (unsigned_intQI_type_node,
9625 targetm.atomic_align_for_mode (QImode));
9626 atomicHI_type_node = build_atomic_base (unsigned_intHI_type_node,
9627 targetm.atomic_align_for_mode (HImode));
9628 atomicSI_type_node = build_atomic_base (unsigned_intSI_type_node,
9629 targetm.atomic_align_for_mode (SImode));
9630 atomicDI_type_node = build_atomic_base (unsigned_intDI_type_node,
9631 targetm.atomic_align_for_mode (DImode));
9632 atomicTI_type_node = build_atomic_base (unsigned_intTI_type_node,
9633 targetm.atomic_align_for_mode (TImode));
9635 access_public_node = get_identifier ("public");
9636 access_protected_node = get_identifier ("protected");
9637 access_private_node = get_identifier ("private");
9639 /* Define these next since types below may used them. */
9640 integer_zero_node = build_int_cst (integer_type_node, 0);
9641 integer_one_node = build_int_cst (integer_type_node, 1);
9642 integer_three_node = build_int_cst (integer_type_node, 3);
9643 integer_minus_one_node = build_int_cst (integer_type_node, -1);
9645 size_zero_node = size_int (0);
9646 size_one_node = size_int (1);
9647 bitsize_zero_node = bitsize_int (0);
9648 bitsize_one_node = bitsize_int (1);
9649 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
9651 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
9652 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
9654 void_type_node = make_node (VOID_TYPE);
9655 layout_type (void_type_node);
9657 /* We are not going to have real types in C with less than byte alignment,
9658 so we might as well not have any types that claim to have it. */
9659 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
9660 TYPE_USER_ALIGN (void_type_node) = 0;
9662 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
9663 layout_type (TREE_TYPE (null_pointer_node));
9665 ptr_type_node = build_pointer_type (void_type_node);
9666 const_ptr_type_node
9667 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
9668 fileptr_type_node = ptr_type_node;
9670 pointer_sized_int_node = build_nonstandard_integer_type (POINTER_SIZE, 1);
9672 float_type_node = make_node (REAL_TYPE);
9673 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
9674 layout_type (float_type_node);
9676 double_type_node = make_node (REAL_TYPE);
9677 if (short_double)
9678 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
9679 else
9680 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
9681 layout_type (double_type_node);
9683 long_double_type_node = make_node (REAL_TYPE);
9684 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
9685 layout_type (long_double_type_node);
9687 float_ptr_type_node = build_pointer_type (float_type_node);
9688 double_ptr_type_node = build_pointer_type (double_type_node);
9689 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
9690 integer_ptr_type_node = build_pointer_type (integer_type_node);
9692 /* Fixed size integer types. */
9693 uint16_type_node = build_nonstandard_integer_type (16, true);
9694 uint32_type_node = build_nonstandard_integer_type (32, true);
9695 uint64_type_node = build_nonstandard_integer_type (64, true);
9697 /* Decimal float types. */
9698 dfloat32_type_node = make_node (REAL_TYPE);
9699 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
9700 layout_type (dfloat32_type_node);
9701 SET_TYPE_MODE (dfloat32_type_node, SDmode);
9702 dfloat32_ptr_type_node = build_pointer_type (dfloat32_type_node);
9704 dfloat64_type_node = make_node (REAL_TYPE);
9705 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
9706 layout_type (dfloat64_type_node);
9707 SET_TYPE_MODE (dfloat64_type_node, DDmode);
9708 dfloat64_ptr_type_node = build_pointer_type (dfloat64_type_node);
9710 dfloat128_type_node = make_node (REAL_TYPE);
9711 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
9712 layout_type (dfloat128_type_node);
9713 SET_TYPE_MODE (dfloat128_type_node, TDmode);
9714 dfloat128_ptr_type_node = build_pointer_type (dfloat128_type_node);
9716 complex_integer_type_node = build_complex_type (integer_type_node);
9717 complex_float_type_node = build_complex_type (float_type_node);
9718 complex_double_type_node = build_complex_type (double_type_node);
9719 complex_long_double_type_node = build_complex_type (long_double_type_node);
9721 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
9722 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
9723 sat_ ## KIND ## _type_node = \
9724 make_sat_signed_ ## KIND ## _type (SIZE); \
9725 sat_unsigned_ ## KIND ## _type_node = \
9726 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9727 KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9728 unsigned_ ## KIND ## _type_node = \
9729 make_unsigned_ ## KIND ## _type (SIZE);
9731 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
9732 sat_ ## WIDTH ## KIND ## _type_node = \
9733 make_sat_signed_ ## KIND ## _type (SIZE); \
9734 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
9735 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9736 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9737 unsigned_ ## WIDTH ## KIND ## _type_node = \
9738 make_unsigned_ ## KIND ## _type (SIZE);
9740 /* Make fixed-point type nodes based on four different widths. */
9741 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
9742 MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
9743 MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
9744 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
9745 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
9747 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
9748 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
9749 NAME ## _type_node = \
9750 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
9751 u ## NAME ## _type_node = \
9752 make_or_reuse_unsigned_ ## KIND ## _type \
9753 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
9754 sat_ ## NAME ## _type_node = \
9755 make_or_reuse_sat_signed_ ## KIND ## _type \
9756 (GET_MODE_BITSIZE (MODE ## mode)); \
9757 sat_u ## NAME ## _type_node = \
9758 make_or_reuse_sat_unsigned_ ## KIND ## _type \
9759 (GET_MODE_BITSIZE (U ## MODE ## mode));
9761 /* Fixed-point type and mode nodes. */
9762 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
9763 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
9764 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
9765 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
9766 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
9767 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
9768 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
9769 MAKE_FIXED_MODE_NODE (accum, ha, HA)
9770 MAKE_FIXED_MODE_NODE (accum, sa, SA)
9771 MAKE_FIXED_MODE_NODE (accum, da, DA)
9772 MAKE_FIXED_MODE_NODE (accum, ta, TA)
9775 tree t = targetm.build_builtin_va_list ();
9777 /* Many back-ends define record types without setting TYPE_NAME.
9778 If we copied the record type here, we'd keep the original
9779 record type without a name. This breaks name mangling. So,
9780 don't copy record types and let c_common_nodes_and_builtins()
9781 declare the type to be __builtin_va_list. */
9782 if (TREE_CODE (t) != RECORD_TYPE)
9783 t = build_variant_type_copy (t);
9785 va_list_type_node = t;
9789 /* Modify DECL for given flags.
9790 TM_PURE attribute is set only on types, so the function will modify
9791 DECL's type when ECF_TM_PURE is used. */
9793 void
9794 set_call_expr_flags (tree decl, int flags)
9796 if (flags & ECF_NOTHROW)
9797 TREE_NOTHROW (decl) = 1;
9798 if (flags & ECF_CONST)
9799 TREE_READONLY (decl) = 1;
9800 if (flags & ECF_PURE)
9801 DECL_PURE_P (decl) = 1;
9802 if (flags & ECF_LOOPING_CONST_OR_PURE)
9803 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
9804 if (flags & ECF_NOVOPS)
9805 DECL_IS_NOVOPS (decl) = 1;
9806 if (flags & ECF_NORETURN)
9807 TREE_THIS_VOLATILE (decl) = 1;
9808 if (flags & ECF_MALLOC)
9809 DECL_IS_MALLOC (decl) = 1;
9810 if (flags & ECF_RETURNS_TWICE)
9811 DECL_IS_RETURNS_TWICE (decl) = 1;
9812 if (flags & ECF_LEAF)
9813 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("leaf"),
9814 NULL, DECL_ATTRIBUTES (decl));
9815 if ((flags & ECF_TM_PURE) && flag_tm)
9816 apply_tm_attr (decl, get_identifier ("transaction_pure"));
9817 /* Looping const or pure is implied by noreturn.
9818 There is currently no way to declare looping const or looping pure alone. */
9819 gcc_assert (!(flags & ECF_LOOPING_CONST_OR_PURE)
9820 || ((flags & ECF_NORETURN) && (flags & (ECF_CONST | ECF_PURE))));
9824 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
9826 static void
9827 local_define_builtin (const char *name, tree type, enum built_in_function code,
9828 const char *library_name, int ecf_flags)
9830 tree decl;
9832 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
9833 library_name, NULL_TREE);
9834 set_call_expr_flags (decl, ecf_flags);
9836 set_builtin_decl (code, decl, true);
9839 /* Call this function after instantiating all builtins that the language
9840 front end cares about. This will build the rest of the builtins that
9841 are relied upon by the tree optimizers and the middle-end. */
9843 void
9844 build_common_builtin_nodes (void)
9846 tree tmp, ftype;
9847 int ecf_flags;
9849 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE))
9851 ftype = build_function_type (void_type_node, void_list_node);
9852 local_define_builtin ("__builtin_unreachable", ftype, BUILT_IN_UNREACHABLE,
9853 "__builtin_unreachable",
9854 ECF_NOTHROW | ECF_LEAF | ECF_NORETURN
9855 | ECF_CONST | ECF_LEAF);
9858 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY)
9859 || !builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9861 ftype = build_function_type_list (ptr_type_node,
9862 ptr_type_node, const_ptr_type_node,
9863 size_type_node, NULL_TREE);
9865 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY))
9866 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
9867 "memcpy", ECF_NOTHROW | ECF_LEAF);
9868 if (!builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9869 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
9870 "memmove", ECF_NOTHROW | ECF_LEAF);
9873 if (!builtin_decl_explicit_p (BUILT_IN_MEMCMP))
9875 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
9876 const_ptr_type_node, size_type_node,
9877 NULL_TREE);
9878 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
9879 "memcmp", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9882 if (!builtin_decl_explicit_p (BUILT_IN_MEMSET))
9884 ftype = build_function_type_list (ptr_type_node,
9885 ptr_type_node, integer_type_node,
9886 size_type_node, NULL_TREE);
9887 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
9888 "memset", ECF_NOTHROW | ECF_LEAF);
9891 if (!builtin_decl_explicit_p (BUILT_IN_ALLOCA))
9893 ftype = build_function_type_list (ptr_type_node,
9894 size_type_node, NULL_TREE);
9895 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
9896 "alloca", ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
9899 ftype = build_function_type_list (ptr_type_node, size_type_node,
9900 size_type_node, NULL_TREE);
9901 local_define_builtin ("__builtin_alloca_with_align", ftype,
9902 BUILT_IN_ALLOCA_WITH_ALIGN, "alloca",
9903 ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
9905 /* If we're checking the stack, `alloca' can throw. */
9906 if (flag_stack_check)
9908 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA)) = 0;
9909 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN)) = 0;
9912 ftype = build_function_type_list (void_type_node,
9913 ptr_type_node, ptr_type_node,
9914 ptr_type_node, NULL_TREE);
9915 local_define_builtin ("__builtin_init_trampoline", ftype,
9916 BUILT_IN_INIT_TRAMPOLINE,
9917 "__builtin_init_trampoline", ECF_NOTHROW | ECF_LEAF);
9918 local_define_builtin ("__builtin_init_heap_trampoline", ftype,
9919 BUILT_IN_INIT_HEAP_TRAMPOLINE,
9920 "__builtin_init_heap_trampoline",
9921 ECF_NOTHROW | ECF_LEAF);
9923 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
9924 local_define_builtin ("__builtin_adjust_trampoline", ftype,
9925 BUILT_IN_ADJUST_TRAMPOLINE,
9926 "__builtin_adjust_trampoline",
9927 ECF_CONST | ECF_NOTHROW);
9929 ftype = build_function_type_list (void_type_node,
9930 ptr_type_node, ptr_type_node, NULL_TREE);
9931 local_define_builtin ("__builtin_nonlocal_goto", ftype,
9932 BUILT_IN_NONLOCAL_GOTO,
9933 "__builtin_nonlocal_goto",
9934 ECF_NORETURN | ECF_NOTHROW);
9936 ftype = build_function_type_list (void_type_node,
9937 ptr_type_node, ptr_type_node, NULL_TREE);
9938 local_define_builtin ("__builtin_setjmp_setup", ftype,
9939 BUILT_IN_SETJMP_SETUP,
9940 "__builtin_setjmp_setup", ECF_NOTHROW);
9942 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9943 local_define_builtin ("__builtin_setjmp_receiver", ftype,
9944 BUILT_IN_SETJMP_RECEIVER,
9945 "__builtin_setjmp_receiver", ECF_NOTHROW | ECF_LEAF);
9947 ftype = build_function_type_list (ptr_type_node, NULL_TREE);
9948 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
9949 "__builtin_stack_save", ECF_NOTHROW | ECF_LEAF);
9951 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9952 local_define_builtin ("__builtin_stack_restore", ftype,
9953 BUILT_IN_STACK_RESTORE,
9954 "__builtin_stack_restore", ECF_NOTHROW | ECF_LEAF);
9956 /* If there's a possibility that we might use the ARM EABI, build the
9957 alternate __cxa_end_cleanup node used to resume from C++ and Java. */
9958 if (targetm.arm_eabi_unwinder)
9960 ftype = build_function_type_list (void_type_node, NULL_TREE);
9961 local_define_builtin ("__builtin_cxa_end_cleanup", ftype,
9962 BUILT_IN_CXA_END_CLEANUP,
9963 "__cxa_end_cleanup", ECF_NORETURN | ECF_LEAF);
9966 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9967 local_define_builtin ("__builtin_unwind_resume", ftype,
9968 BUILT_IN_UNWIND_RESUME,
9969 ((targetm_common.except_unwind_info (&global_options)
9970 == UI_SJLJ)
9971 ? "_Unwind_SjLj_Resume" : "_Unwind_Resume"),
9972 ECF_NORETURN);
9974 if (builtin_decl_explicit (BUILT_IN_RETURN_ADDRESS) == NULL_TREE)
9976 ftype = build_function_type_list (ptr_type_node, integer_type_node,
9977 NULL_TREE);
9978 local_define_builtin ("__builtin_return_address", ftype,
9979 BUILT_IN_RETURN_ADDRESS,
9980 "__builtin_return_address",
9981 ECF_NOTHROW);
9984 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER)
9985 || !builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
9987 ftype = build_function_type_list (void_type_node, ptr_type_node,
9988 ptr_type_node, NULL_TREE);
9989 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER))
9990 local_define_builtin ("__cyg_profile_func_enter", ftype,
9991 BUILT_IN_PROFILE_FUNC_ENTER,
9992 "__cyg_profile_func_enter", 0);
9993 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
9994 local_define_builtin ("__cyg_profile_func_exit", ftype,
9995 BUILT_IN_PROFILE_FUNC_EXIT,
9996 "__cyg_profile_func_exit", 0);
9999 /* The exception object and filter values from the runtime. The argument
10000 must be zero before exception lowering, i.e. from the front end. After
10001 exception lowering, it will be the region number for the exception
10002 landing pad. These functions are PURE instead of CONST to prevent
10003 them from being hoisted past the exception edge that will initialize
10004 its value in the landing pad. */
10005 ftype = build_function_type_list (ptr_type_node,
10006 integer_type_node, NULL_TREE);
10007 ecf_flags = ECF_PURE | ECF_NOTHROW | ECF_LEAF;
10008 /* Only use TM_PURE if we we have TM language support. */
10009 if (builtin_decl_explicit_p (BUILT_IN_TM_LOAD_1))
10010 ecf_flags |= ECF_TM_PURE;
10011 local_define_builtin ("__builtin_eh_pointer", ftype, BUILT_IN_EH_POINTER,
10012 "__builtin_eh_pointer", ecf_flags);
10014 tmp = lang_hooks.types.type_for_mode (targetm.eh_return_filter_mode (), 0);
10015 ftype = build_function_type_list (tmp, integer_type_node, NULL_TREE);
10016 local_define_builtin ("__builtin_eh_filter", ftype, BUILT_IN_EH_FILTER,
10017 "__builtin_eh_filter", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10019 ftype = build_function_type_list (void_type_node,
10020 integer_type_node, integer_type_node,
10021 NULL_TREE);
10022 local_define_builtin ("__builtin_eh_copy_values", ftype,
10023 BUILT_IN_EH_COPY_VALUES,
10024 "__builtin_eh_copy_values", ECF_NOTHROW);
10026 /* Complex multiplication and division. These are handled as builtins
10027 rather than optabs because emit_library_call_value doesn't support
10028 complex. Further, we can do slightly better with folding these
10029 beasties if the real and complex parts of the arguments are separate. */
10031 int mode;
10033 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
10035 char mode_name_buf[4], *q;
10036 const char *p;
10037 enum built_in_function mcode, dcode;
10038 tree type, inner_type;
10039 const char *prefix = "__";
10041 if (targetm.libfunc_gnu_prefix)
10042 prefix = "__gnu_";
10044 type = lang_hooks.types.type_for_mode ((enum machine_mode) mode, 0);
10045 if (type == NULL)
10046 continue;
10047 inner_type = TREE_TYPE (type);
10049 ftype = build_function_type_list (type, inner_type, inner_type,
10050 inner_type, inner_type, NULL_TREE);
10052 mcode = ((enum built_in_function)
10053 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10054 dcode = ((enum built_in_function)
10055 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10057 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
10058 *q = TOLOWER (*p);
10059 *q = '\0';
10061 built_in_names[mcode] = concat (prefix, "mul", mode_name_buf, "3",
10062 NULL);
10063 local_define_builtin (built_in_names[mcode], ftype, mcode,
10064 built_in_names[mcode],
10065 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
10067 built_in_names[dcode] = concat (prefix, "div", mode_name_buf, "3",
10068 NULL);
10069 local_define_builtin (built_in_names[dcode], ftype, dcode,
10070 built_in_names[dcode],
10071 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
10076 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
10077 better way.
10079 If we requested a pointer to a vector, build up the pointers that
10080 we stripped off while looking for the inner type. Similarly for
10081 return values from functions.
10083 The argument TYPE is the top of the chain, and BOTTOM is the
10084 new type which we will point to. */
10086 tree
10087 reconstruct_complex_type (tree type, tree bottom)
10089 tree inner, outer;
10091 if (TREE_CODE (type) == POINTER_TYPE)
10093 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10094 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
10095 TYPE_REF_CAN_ALIAS_ALL (type));
10097 else if (TREE_CODE (type) == REFERENCE_TYPE)
10099 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10100 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
10101 TYPE_REF_CAN_ALIAS_ALL (type));
10103 else if (TREE_CODE (type) == ARRAY_TYPE)
10105 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10106 outer = build_array_type (inner, TYPE_DOMAIN (type));
10108 else if (TREE_CODE (type) == FUNCTION_TYPE)
10110 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10111 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
10113 else if (TREE_CODE (type) == METHOD_TYPE)
10115 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10116 /* The build_method_type_directly() routine prepends 'this' to argument list,
10117 so we must compensate by getting rid of it. */
10118 outer
10119 = build_method_type_directly
10120 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
10121 inner,
10122 TREE_CHAIN (TYPE_ARG_TYPES (type)));
10124 else if (TREE_CODE (type) == OFFSET_TYPE)
10126 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10127 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
10129 else
10130 return bottom;
10132 return build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
10133 TYPE_QUALS (type));
10136 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
10137 the inner type. */
10138 tree
10139 build_vector_type_for_mode (tree innertype, enum machine_mode mode)
10141 int nunits;
10143 switch (GET_MODE_CLASS (mode))
10145 case MODE_VECTOR_INT:
10146 case MODE_VECTOR_FLOAT:
10147 case MODE_VECTOR_FRACT:
10148 case MODE_VECTOR_UFRACT:
10149 case MODE_VECTOR_ACCUM:
10150 case MODE_VECTOR_UACCUM:
10151 nunits = GET_MODE_NUNITS (mode);
10152 break;
10154 case MODE_INT:
10155 /* Check that there are no leftover bits. */
10156 gcc_assert (GET_MODE_BITSIZE (mode)
10157 % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
10159 nunits = GET_MODE_BITSIZE (mode)
10160 / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
10161 break;
10163 default:
10164 gcc_unreachable ();
10167 return make_vector_type (innertype, nunits, mode);
10170 /* Similarly, but takes the inner type and number of units, which must be
10171 a power of two. */
10173 tree
10174 build_vector_type (tree innertype, int nunits)
10176 return make_vector_type (innertype, nunits, VOIDmode);
10179 /* Similarly, but builds a variant type with TYPE_VECTOR_OPAQUE set. */
10181 tree
10182 build_opaque_vector_type (tree innertype, int nunits)
10184 tree t = make_vector_type (innertype, nunits, VOIDmode);
10185 tree cand;
10186 /* We always build the non-opaque variant before the opaque one,
10187 so if it already exists, it is TYPE_NEXT_VARIANT of this one. */
10188 cand = TYPE_NEXT_VARIANT (t);
10189 if (cand
10190 && TYPE_VECTOR_OPAQUE (cand)
10191 && check_qualified_type (cand, t, TYPE_QUALS (t)))
10192 return cand;
10193 /* Othewise build a variant type and make sure to queue it after
10194 the non-opaque type. */
10195 cand = build_distinct_type_copy (t);
10196 TYPE_VECTOR_OPAQUE (cand) = true;
10197 TYPE_CANONICAL (cand) = TYPE_CANONICAL (t);
10198 TYPE_NEXT_VARIANT (cand) = TYPE_NEXT_VARIANT (t);
10199 TYPE_NEXT_VARIANT (t) = cand;
10200 TYPE_MAIN_VARIANT (cand) = TYPE_MAIN_VARIANT (t);
10201 return cand;
10205 /* Given an initializer INIT, return TRUE if INIT is zero or some
10206 aggregate of zeros. Otherwise return FALSE. */
10207 bool
10208 initializer_zerop (const_tree init)
10210 tree elt;
10212 STRIP_NOPS (init);
10214 switch (TREE_CODE (init))
10216 case INTEGER_CST:
10217 return integer_zerop (init);
10219 case REAL_CST:
10220 /* ??? Note that this is not correct for C4X float formats. There,
10221 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
10222 negative exponent. */
10223 return real_zerop (init)
10224 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
10226 case FIXED_CST:
10227 return fixed_zerop (init);
10229 case COMPLEX_CST:
10230 return integer_zerop (init)
10231 || (real_zerop (init)
10232 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
10233 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
10235 case VECTOR_CST:
10237 unsigned i;
10238 for (i = 0; i < VECTOR_CST_NELTS (init); ++i)
10239 if (!initializer_zerop (VECTOR_CST_ELT (init, i)))
10240 return false;
10241 return true;
10244 case CONSTRUCTOR:
10246 unsigned HOST_WIDE_INT idx;
10248 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
10249 if (!initializer_zerop (elt))
10250 return false;
10251 return true;
10254 case STRING_CST:
10256 int i;
10258 /* We need to loop through all elements to handle cases like
10259 "\0" and "\0foobar". */
10260 for (i = 0; i < TREE_STRING_LENGTH (init); ++i)
10261 if (TREE_STRING_POINTER (init)[i] != '\0')
10262 return false;
10264 return true;
10267 default:
10268 return false;
10272 /* Check if vector VEC consists of all the equal elements and
10273 that the number of elements corresponds to the type of VEC.
10274 The function returns first element of the vector
10275 or NULL_TREE if the vector is not uniform. */
10276 tree
10277 uniform_vector_p (const_tree vec)
10279 tree first, t;
10280 unsigned i;
10282 if (vec == NULL_TREE)
10283 return NULL_TREE;
10285 gcc_assert (VECTOR_TYPE_P (TREE_TYPE (vec)));
10287 if (TREE_CODE (vec) == VECTOR_CST)
10289 first = VECTOR_CST_ELT (vec, 0);
10290 for (i = 1; i < VECTOR_CST_NELTS (vec); ++i)
10291 if (!operand_equal_p (first, VECTOR_CST_ELT (vec, i), 0))
10292 return NULL_TREE;
10294 return first;
10297 else if (TREE_CODE (vec) == CONSTRUCTOR)
10299 first = error_mark_node;
10301 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (vec), i, t)
10303 if (i == 0)
10305 first = t;
10306 continue;
10308 if (!operand_equal_p (first, t, 0))
10309 return NULL_TREE;
10311 if (i != TYPE_VECTOR_SUBPARTS (TREE_TYPE (vec)))
10312 return NULL_TREE;
10314 return first;
10317 return NULL_TREE;
10320 /* Build an empty statement at location LOC. */
10322 tree
10323 build_empty_stmt (location_t loc)
10325 tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
10326 SET_EXPR_LOCATION (t, loc);
10327 return t;
10331 /* Build an OpenMP clause with code CODE. LOC is the location of the
10332 clause. */
10334 tree
10335 build_omp_clause (location_t loc, enum omp_clause_code code)
10337 tree t;
10338 int size, length;
10340 length = omp_clause_num_ops[code];
10341 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
10343 record_node_allocation_statistics (OMP_CLAUSE, size);
10345 t = (tree) ggc_internal_alloc (size);
10346 memset (t, 0, size);
10347 TREE_SET_CODE (t, OMP_CLAUSE);
10348 OMP_CLAUSE_SET_CODE (t, code);
10349 OMP_CLAUSE_LOCATION (t) = loc;
10351 return t;
10354 /* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
10355 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
10356 Except for the CODE and operand count field, other storage for the
10357 object is initialized to zeros. */
10359 tree
10360 build_vl_exp_stat (enum tree_code code, int len MEM_STAT_DECL)
10362 tree t;
10363 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
10365 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
10366 gcc_assert (len >= 1);
10368 record_node_allocation_statistics (code, length);
10370 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
10372 TREE_SET_CODE (t, code);
10374 /* Can't use TREE_OPERAND to store the length because if checking is
10375 enabled, it will try to check the length before we store it. :-P */
10376 t->exp.operands[0] = build_int_cst (sizetype, len);
10378 return t;
10381 /* Helper function for build_call_* functions; build a CALL_EXPR with
10382 indicated RETURN_TYPE, FN, and NARGS, but do not initialize any of
10383 the argument slots. */
10385 static tree
10386 build_call_1 (tree return_type, tree fn, int nargs)
10388 tree t;
10390 t = build_vl_exp (CALL_EXPR, nargs + 3);
10391 TREE_TYPE (t) = return_type;
10392 CALL_EXPR_FN (t) = fn;
10393 CALL_EXPR_STATIC_CHAIN (t) = NULL;
10395 return t;
10398 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10399 FN and a null static chain slot. NARGS is the number of call arguments
10400 which are specified as "..." arguments. */
10402 tree
10403 build_call_nary (tree return_type, tree fn, int nargs, ...)
10405 tree ret;
10406 va_list args;
10407 va_start (args, nargs);
10408 ret = build_call_valist (return_type, fn, nargs, args);
10409 va_end (args);
10410 return ret;
10413 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10414 FN and a null static chain slot. NARGS is the number of call arguments
10415 which are specified as a va_list ARGS. */
10417 tree
10418 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
10420 tree t;
10421 int i;
10423 t = build_call_1 (return_type, fn, nargs);
10424 for (i = 0; i < nargs; i++)
10425 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
10426 process_call_operands (t);
10427 return t;
10430 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10431 FN and a null static chain slot. NARGS is the number of call arguments
10432 which are specified as a tree array ARGS. */
10434 tree
10435 build_call_array_loc (location_t loc, tree return_type, tree fn,
10436 int nargs, const tree *args)
10438 tree t;
10439 int i;
10441 t = build_call_1 (return_type, fn, nargs);
10442 for (i = 0; i < nargs; i++)
10443 CALL_EXPR_ARG (t, i) = args[i];
10444 process_call_operands (t);
10445 SET_EXPR_LOCATION (t, loc);
10446 return t;
10449 /* Like build_call_array, but takes a vec. */
10451 tree
10452 build_call_vec (tree return_type, tree fn, vec<tree, va_gc> *args)
10454 tree ret, t;
10455 unsigned int ix;
10457 ret = build_call_1 (return_type, fn, vec_safe_length (args));
10458 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
10459 CALL_EXPR_ARG (ret, ix) = t;
10460 process_call_operands (ret);
10461 return ret;
10464 /* Return true if T (assumed to be a DECL) must be assigned a memory
10465 location. */
10467 bool
10468 needs_to_live_in_memory (const_tree t)
10470 return (TREE_ADDRESSABLE (t)
10471 || is_global_var (t)
10472 || (TREE_CODE (t) == RESULT_DECL
10473 && !DECL_BY_REFERENCE (t)
10474 && aggregate_value_p (t, current_function_decl)));
10477 /* Return value of a constant X and sign-extend it. */
10479 HOST_WIDE_INT
10480 int_cst_value (const_tree x)
10482 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10483 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
10485 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10486 gcc_assert (cst_and_fits_in_hwi (x));
10488 if (bits < HOST_BITS_PER_WIDE_INT)
10490 bool negative = ((val >> (bits - 1)) & 1) != 0;
10491 if (negative)
10492 val |= (~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1;
10493 else
10494 val &= ~((~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1);
10497 return val;
10500 /* Return value of a constant X and sign-extend it. */
10502 HOST_WIDEST_INT
10503 widest_int_cst_value (const_tree x)
10505 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10506 unsigned HOST_WIDEST_INT val = TREE_INT_CST_LOW (x);
10508 #if HOST_BITS_PER_WIDEST_INT > HOST_BITS_PER_WIDE_INT
10509 gcc_assert (HOST_BITS_PER_WIDEST_INT >= HOST_BITS_PER_DOUBLE_INT);
10510 gcc_assert (TREE_INT_CST_NUNITS (x) == 2);
10512 if (TREE_INT_CST_NUNITS (x) == 1)
10513 val = HOST_WIDE_INT (val);
10514 else
10515 val |= (((unsigned HOST_WIDEST_INT) TREE_INT_CST_ELT (x, 1))
10516 << HOST_BITS_PER_WIDE_INT);
10517 #else
10518 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10519 gcc_assert (TREE_INT_CST_NUNITS (x) == 1);
10520 #endif
10522 if (bits < HOST_BITS_PER_WIDEST_INT)
10524 bool negative = ((val >> (bits - 1)) & 1) != 0;
10525 if (negative)
10526 val |= (~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1;
10527 else
10528 val &= ~((~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1);
10531 return val;
10534 /* If TYPE is an integral or pointer type, return an integer type with
10535 the same precision which is unsigned iff UNSIGNEDP is true, or itself
10536 if TYPE is already an integer type of signedness UNSIGNEDP. */
10538 tree
10539 signed_or_unsigned_type_for (int unsignedp, tree type)
10541 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type) == unsignedp)
10542 return type;
10544 if (TREE_CODE (type) == VECTOR_TYPE)
10546 tree inner = TREE_TYPE (type);
10547 tree inner2 = signed_or_unsigned_type_for (unsignedp, inner);
10548 if (!inner2)
10549 return NULL_TREE;
10550 if (inner == inner2)
10551 return type;
10552 return build_vector_type (inner2, TYPE_VECTOR_SUBPARTS (type));
10555 if (!INTEGRAL_TYPE_P (type)
10556 && !POINTER_TYPE_P (type)
10557 && TREE_CODE (type) != OFFSET_TYPE)
10558 return NULL_TREE;
10560 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
10563 /* If TYPE is an integral or pointer type, return an integer type with
10564 the same precision which is unsigned, or itself if TYPE is already an
10565 unsigned integer type. */
10567 tree
10568 unsigned_type_for (tree type)
10570 return signed_or_unsigned_type_for (1, type);
10573 /* If TYPE is an integral or pointer type, return an integer type with
10574 the same precision which is signed, or itself if TYPE is already a
10575 signed integer type. */
10577 tree
10578 signed_type_for (tree type)
10580 return signed_or_unsigned_type_for (0, type);
10583 /* If TYPE is a vector type, return a signed integer vector type with the
10584 same width and number of subparts. Otherwise return boolean_type_node. */
10586 tree
10587 truth_type_for (tree type)
10589 if (TREE_CODE (type) == VECTOR_TYPE)
10591 tree elem = lang_hooks.types.type_for_size
10592 (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))), 0);
10593 return build_opaque_vector_type (elem, TYPE_VECTOR_SUBPARTS (type));
10595 else
10596 return boolean_type_node;
10599 /* Returns the largest value obtainable by casting something in INNER type to
10600 OUTER type. */
10602 tree
10603 upper_bound_in_type (tree outer, tree inner)
10605 unsigned int det = 0;
10606 unsigned oprec = TYPE_PRECISION (outer);
10607 unsigned iprec = TYPE_PRECISION (inner);
10608 unsigned prec;
10610 /* Compute a unique number for every combination. */
10611 det |= (oprec > iprec) ? 4 : 0;
10612 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
10613 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
10615 /* Determine the exponent to use. */
10616 switch (det)
10618 case 0:
10619 case 1:
10620 /* oprec <= iprec, outer: signed, inner: don't care. */
10621 prec = oprec - 1;
10622 break;
10623 case 2:
10624 case 3:
10625 /* oprec <= iprec, outer: unsigned, inner: don't care. */
10626 prec = oprec;
10627 break;
10628 case 4:
10629 /* oprec > iprec, outer: signed, inner: signed. */
10630 prec = iprec - 1;
10631 break;
10632 case 5:
10633 /* oprec > iprec, outer: signed, inner: unsigned. */
10634 prec = iprec;
10635 break;
10636 case 6:
10637 /* oprec > iprec, outer: unsigned, inner: signed. */
10638 prec = oprec;
10639 break;
10640 case 7:
10641 /* oprec > iprec, outer: unsigned, inner: unsigned. */
10642 prec = iprec;
10643 break;
10644 default:
10645 gcc_unreachable ();
10648 return wide_int_to_tree (outer,
10649 wi::mask (prec, false, TYPE_PRECISION (outer)));
10652 /* Returns the smallest value obtainable by casting something in INNER type to
10653 OUTER type. */
10655 tree
10656 lower_bound_in_type (tree outer, tree inner)
10658 unsigned oprec = TYPE_PRECISION (outer);
10659 unsigned iprec = TYPE_PRECISION (inner);
10661 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
10662 and obtain 0. */
10663 if (TYPE_UNSIGNED (outer)
10664 /* If we are widening something of an unsigned type, OUTER type
10665 contains all values of INNER type. In particular, both INNER
10666 and OUTER types have zero in common. */
10667 || (oprec > iprec && TYPE_UNSIGNED (inner)))
10668 return build_int_cst (outer, 0);
10669 else
10671 /* If we are widening a signed type to another signed type, we
10672 want to obtain -2^^(iprec-1). If we are keeping the
10673 precision or narrowing to a signed type, we want to obtain
10674 -2^(oprec-1). */
10675 unsigned prec = oprec > iprec ? iprec : oprec;
10676 return wide_int_to_tree (outer,
10677 wi::mask (prec - 1, true,
10678 TYPE_PRECISION (outer)));
10682 /* Return nonzero if two operands that are suitable for PHI nodes are
10683 necessarily equal. Specifically, both ARG0 and ARG1 must be either
10684 SSA_NAME or invariant. Note that this is strictly an optimization.
10685 That is, callers of this function can directly call operand_equal_p
10686 and get the same result, only slower. */
10689 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
10691 if (arg0 == arg1)
10692 return 1;
10693 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
10694 return 0;
10695 return operand_equal_p (arg0, arg1, 0);
10698 /* Returns number of zeros at the end of binary representation of X. */
10700 tree
10701 num_ending_zeros (const_tree x)
10703 return build_int_cst (TREE_TYPE (x), wi::ctz (x));
10707 #define WALK_SUBTREE(NODE) \
10708 do \
10710 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
10711 if (result) \
10712 return result; \
10714 while (0)
10716 /* This is a subroutine of walk_tree that walks field of TYPE that are to
10717 be walked whenever a type is seen in the tree. Rest of operands and return
10718 value are as for walk_tree. */
10720 static tree
10721 walk_type_fields (tree type, walk_tree_fn func, void *data,
10722 struct pointer_set_t *pset, walk_tree_lh lh)
10724 tree result = NULL_TREE;
10726 switch (TREE_CODE (type))
10728 case POINTER_TYPE:
10729 case REFERENCE_TYPE:
10730 case VECTOR_TYPE:
10731 /* We have to worry about mutually recursive pointers. These can't
10732 be written in C. They can in Ada. It's pathological, but
10733 there's an ACATS test (c38102a) that checks it. Deal with this
10734 by checking if we're pointing to another pointer, that one
10735 points to another pointer, that one does too, and we have no htab.
10736 If so, get a hash table. We check three levels deep to avoid
10737 the cost of the hash table if we don't need one. */
10738 if (POINTER_TYPE_P (TREE_TYPE (type))
10739 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
10740 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
10741 && !pset)
10743 result = walk_tree_without_duplicates (&TREE_TYPE (type),
10744 func, data);
10745 if (result)
10746 return result;
10748 break;
10751 /* ... fall through ... */
10753 case COMPLEX_TYPE:
10754 WALK_SUBTREE (TREE_TYPE (type));
10755 break;
10757 case METHOD_TYPE:
10758 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
10760 /* Fall through. */
10762 case FUNCTION_TYPE:
10763 WALK_SUBTREE (TREE_TYPE (type));
10765 tree arg;
10767 /* We never want to walk into default arguments. */
10768 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
10769 WALK_SUBTREE (TREE_VALUE (arg));
10771 break;
10773 case ARRAY_TYPE:
10774 /* Don't follow this nodes's type if a pointer for fear that
10775 we'll have infinite recursion. If we have a PSET, then we
10776 need not fear. */
10777 if (pset
10778 || (!POINTER_TYPE_P (TREE_TYPE (type))
10779 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
10780 WALK_SUBTREE (TREE_TYPE (type));
10781 WALK_SUBTREE (TYPE_DOMAIN (type));
10782 break;
10784 case OFFSET_TYPE:
10785 WALK_SUBTREE (TREE_TYPE (type));
10786 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
10787 break;
10789 default:
10790 break;
10793 return NULL_TREE;
10796 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
10797 called with the DATA and the address of each sub-tree. If FUNC returns a
10798 non-NULL value, the traversal is stopped, and the value returned by FUNC
10799 is returned. If PSET is non-NULL it is used to record the nodes visited,
10800 and to avoid visiting a node more than once. */
10802 tree
10803 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
10804 struct pointer_set_t *pset, walk_tree_lh lh)
10806 enum tree_code code;
10807 int walk_subtrees;
10808 tree result;
10810 #define WALK_SUBTREE_TAIL(NODE) \
10811 do \
10813 tp = & (NODE); \
10814 goto tail_recurse; \
10816 while (0)
10818 tail_recurse:
10819 /* Skip empty subtrees. */
10820 if (!*tp)
10821 return NULL_TREE;
10823 /* Don't walk the same tree twice, if the user has requested
10824 that we avoid doing so. */
10825 if (pset && pointer_set_insert (pset, *tp))
10826 return NULL_TREE;
10828 /* Call the function. */
10829 walk_subtrees = 1;
10830 result = (*func) (tp, &walk_subtrees, data);
10832 /* If we found something, return it. */
10833 if (result)
10834 return result;
10836 code = TREE_CODE (*tp);
10838 /* Even if we didn't, FUNC may have decided that there was nothing
10839 interesting below this point in the tree. */
10840 if (!walk_subtrees)
10842 /* But we still need to check our siblings. */
10843 if (code == TREE_LIST)
10844 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
10845 else if (code == OMP_CLAUSE)
10846 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10847 else
10848 return NULL_TREE;
10851 if (lh)
10853 result = (*lh) (tp, &walk_subtrees, func, data, pset);
10854 if (result || !walk_subtrees)
10855 return result;
10858 switch (code)
10860 case ERROR_MARK:
10861 case IDENTIFIER_NODE:
10862 case INTEGER_CST:
10863 case REAL_CST:
10864 case FIXED_CST:
10865 case VECTOR_CST:
10866 case STRING_CST:
10867 case BLOCK:
10868 case PLACEHOLDER_EXPR:
10869 case SSA_NAME:
10870 case FIELD_DECL:
10871 case RESULT_DECL:
10872 /* None of these have subtrees other than those already walked
10873 above. */
10874 break;
10876 case TREE_LIST:
10877 WALK_SUBTREE (TREE_VALUE (*tp));
10878 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
10879 break;
10881 case TREE_VEC:
10883 int len = TREE_VEC_LENGTH (*tp);
10885 if (len == 0)
10886 break;
10888 /* Walk all elements but the first. */
10889 while (--len)
10890 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
10892 /* Now walk the first one as a tail call. */
10893 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
10896 case COMPLEX_CST:
10897 WALK_SUBTREE (TREE_REALPART (*tp));
10898 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
10900 case CONSTRUCTOR:
10902 unsigned HOST_WIDE_INT idx;
10903 constructor_elt *ce;
10905 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (*tp), idx, &ce);
10906 idx++)
10907 WALK_SUBTREE (ce->value);
10909 break;
10911 case SAVE_EXPR:
10912 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
10914 case BIND_EXPR:
10916 tree decl;
10917 for (decl = BIND_EXPR_VARS (*tp); decl; decl = DECL_CHAIN (decl))
10919 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
10920 into declarations that are just mentioned, rather than
10921 declared; they don't really belong to this part of the tree.
10922 And, we can see cycles: the initializer for a declaration
10923 can refer to the declaration itself. */
10924 WALK_SUBTREE (DECL_INITIAL (decl));
10925 WALK_SUBTREE (DECL_SIZE (decl));
10926 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
10928 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
10931 case STATEMENT_LIST:
10933 tree_stmt_iterator i;
10934 for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
10935 WALK_SUBTREE (*tsi_stmt_ptr (i));
10937 break;
10939 case OMP_CLAUSE:
10940 switch (OMP_CLAUSE_CODE (*tp))
10942 case OMP_CLAUSE_HOST:
10943 case OMP_CLAUSE_OACC_DEVICE:
10944 case OMP_CLAUSE_DEVICE_RESIDENT:
10945 case OMP_CLAUSE_USE_DEVICE:
10946 case OMP_CLAUSE_GANG:
10947 case OMP_CLAUSE_ASYNC:
10948 case OMP_CLAUSE_WAIT:
10949 case OMP_NO_CLAUSE_CACHE:
10950 case OMP_CLAUSE_WORKER:
10951 case OMP_CLAUSE_VECTOR:
10952 case OMP_CLAUSE_NUM_GANGS:
10953 case OMP_CLAUSE_NUM_WORKERS:
10954 case OMP_CLAUSE_VECTOR_LENGTH:
10955 case OMP_CLAUSE_PRIVATE:
10956 case OMP_CLAUSE_SHARED:
10957 case OMP_CLAUSE_FIRSTPRIVATE:
10958 case OMP_CLAUSE_COPYIN:
10959 case OMP_CLAUSE_COPYPRIVATE:
10960 case OMP_CLAUSE_FINAL:
10961 case OMP_CLAUSE_IF:
10962 case OMP_CLAUSE_NUM_THREADS:
10963 case OMP_CLAUSE_SCHEDULE:
10964 case OMP_CLAUSE_UNIFORM:
10965 case OMP_CLAUSE_DEPEND:
10966 case OMP_CLAUSE_NUM_TEAMS:
10967 case OMP_CLAUSE_THREAD_LIMIT:
10968 case OMP_CLAUSE_DEVICE:
10969 case OMP_CLAUSE_DIST_SCHEDULE:
10970 case OMP_CLAUSE_SAFELEN:
10971 case OMP_CLAUSE_SIMDLEN:
10972 case OMP_CLAUSE__LOOPTEMP_:
10973 case OMP_CLAUSE__SIMDUID_:
10974 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0));
10975 /* FALLTHRU */
10977 case OMP_CLAUSE_INDEPENDENT:
10978 case OMP_CLAUSE_NOWAIT:
10979 case OMP_CLAUSE_ORDERED:
10980 case OMP_CLAUSE_DEFAULT:
10981 case OMP_CLAUSE_UNTIED:
10982 case OMP_CLAUSE_MERGEABLE:
10983 case OMP_CLAUSE_PROC_BIND:
10984 case OMP_CLAUSE_INBRANCH:
10985 case OMP_CLAUSE_NOTINBRANCH:
10986 case OMP_CLAUSE_FOR:
10987 case OMP_CLAUSE_PARALLEL:
10988 case OMP_CLAUSE_SECTIONS:
10989 case OMP_CLAUSE_TASKGROUP:
10990 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10992 case OMP_CLAUSE_LASTPRIVATE:
10993 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
10994 WALK_SUBTREE (OMP_CLAUSE_LASTPRIVATE_STMT (*tp));
10995 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10997 case OMP_CLAUSE_COLLAPSE:
10999 int i;
11000 for (i = 0; i < 3; i++)
11001 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
11002 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11005 case OMP_CLAUSE_LINEAR:
11006 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11007 WALK_SUBTREE (OMP_CLAUSE_LINEAR_STEP (*tp));
11008 WALK_SUBTREE (OMP_CLAUSE_LINEAR_STMT (*tp));
11009 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11011 case OMP_CLAUSE_ALIGNED:
11012 case OMP_CLAUSE_FROM:
11013 case OMP_CLAUSE_TO:
11014 case OMP_CLAUSE_MAP:
11015 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11016 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 1));
11017 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11019 case OMP_CLAUSE_REDUCTION:
11021 int i;
11022 for (i = 0; i < 4; i++)
11023 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
11024 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11027 default:
11028 gcc_unreachable ();
11030 break;
11032 case TARGET_EXPR:
11034 int i, len;
11036 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
11037 But, we only want to walk once. */
11038 len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
11039 for (i = 0; i < len; ++i)
11040 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11041 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
11044 case DECL_EXPR:
11045 /* If this is a TYPE_DECL, walk into the fields of the type that it's
11046 defining. We only want to walk into these fields of a type in this
11047 case and not in the general case of a mere reference to the type.
11049 The criterion is as follows: if the field can be an expression, it
11050 must be walked only here. This should be in keeping with the fields
11051 that are directly gimplified in gimplify_type_sizes in order for the
11052 mark/copy-if-shared/unmark machinery of the gimplifier to work with
11053 variable-sized types.
11055 Note that DECLs get walked as part of processing the BIND_EXPR. */
11056 if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
11058 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
11059 if (TREE_CODE (*type_p) == ERROR_MARK)
11060 return NULL_TREE;
11062 /* Call the function for the type. See if it returns anything or
11063 doesn't want us to continue. If we are to continue, walk both
11064 the normal fields and those for the declaration case. */
11065 result = (*func) (type_p, &walk_subtrees, data);
11066 if (result || !walk_subtrees)
11067 return result;
11069 /* But do not walk a pointed-to type since it may itself need to
11070 be walked in the declaration case if it isn't anonymous. */
11071 if (!POINTER_TYPE_P (*type_p))
11073 result = walk_type_fields (*type_p, func, data, pset, lh);
11074 if (result)
11075 return result;
11078 /* If this is a record type, also walk the fields. */
11079 if (RECORD_OR_UNION_TYPE_P (*type_p))
11081 tree field;
11083 for (field = TYPE_FIELDS (*type_p); field;
11084 field = DECL_CHAIN (field))
11086 /* We'd like to look at the type of the field, but we can
11087 easily get infinite recursion. So assume it's pointed
11088 to elsewhere in the tree. Also, ignore things that
11089 aren't fields. */
11090 if (TREE_CODE (field) != FIELD_DECL)
11091 continue;
11093 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
11094 WALK_SUBTREE (DECL_SIZE (field));
11095 WALK_SUBTREE (DECL_SIZE_UNIT (field));
11096 if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
11097 WALK_SUBTREE (DECL_QUALIFIER (field));
11101 /* Same for scalar types. */
11102 else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
11103 || TREE_CODE (*type_p) == ENUMERAL_TYPE
11104 || TREE_CODE (*type_p) == INTEGER_TYPE
11105 || TREE_CODE (*type_p) == FIXED_POINT_TYPE
11106 || TREE_CODE (*type_p) == REAL_TYPE)
11108 WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
11109 WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
11112 WALK_SUBTREE (TYPE_SIZE (*type_p));
11113 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
11115 /* FALLTHRU */
11117 default:
11118 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
11120 int i, len;
11122 /* Walk over all the sub-trees of this operand. */
11123 len = TREE_OPERAND_LENGTH (*tp);
11125 /* Go through the subtrees. We need to do this in forward order so
11126 that the scope of a FOR_EXPR is handled properly. */
11127 if (len)
11129 for (i = 0; i < len - 1; ++i)
11130 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11131 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
11134 /* If this is a type, walk the needed fields in the type. */
11135 else if (TYPE_P (*tp))
11136 return walk_type_fields (*tp, func, data, pset, lh);
11137 break;
11140 /* We didn't find what we were looking for. */
11141 return NULL_TREE;
11143 #undef WALK_SUBTREE_TAIL
11145 #undef WALK_SUBTREE
11147 /* Like walk_tree, but does not walk duplicate nodes more than once. */
11149 tree
11150 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
11151 walk_tree_lh lh)
11153 tree result;
11154 struct pointer_set_t *pset;
11156 pset = pointer_set_create ();
11157 result = walk_tree_1 (tp, func, data, pset, lh);
11158 pointer_set_destroy (pset);
11159 return result;
11163 tree
11164 tree_block (tree t)
11166 const enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
11168 if (IS_EXPR_CODE_CLASS (c))
11169 return LOCATION_BLOCK (t->exp.locus);
11170 gcc_unreachable ();
11171 return NULL;
11174 void
11175 tree_set_block (tree t, tree b)
11177 const enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
11179 if (IS_EXPR_CODE_CLASS (c))
11181 if (b)
11182 t->exp.locus = COMBINE_LOCATION_DATA (line_table, t->exp.locus, b);
11183 else
11184 t->exp.locus = LOCATION_LOCUS (t->exp.locus);
11186 else
11187 gcc_unreachable ();
11190 /* Create a nameless artificial label and put it in the current
11191 function context. The label has a location of LOC. Returns the
11192 newly created label. */
11194 tree
11195 create_artificial_label (location_t loc)
11197 tree lab = build_decl (loc,
11198 LABEL_DECL, NULL_TREE, void_type_node);
11200 DECL_ARTIFICIAL (lab) = 1;
11201 DECL_IGNORED_P (lab) = 1;
11202 DECL_CONTEXT (lab) = current_function_decl;
11203 return lab;
11206 /* Given a tree, try to return a useful variable name that we can use
11207 to prefix a temporary that is being assigned the value of the tree.
11208 I.E. given <temp> = &A, return A. */
11210 const char *
11211 get_name (tree t)
11213 tree stripped_decl;
11215 stripped_decl = t;
11216 STRIP_NOPS (stripped_decl);
11217 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
11218 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
11219 else if (TREE_CODE (stripped_decl) == SSA_NAME)
11221 tree name = SSA_NAME_IDENTIFIER (stripped_decl);
11222 if (!name)
11223 return NULL;
11224 return IDENTIFIER_POINTER (name);
11226 else
11228 switch (TREE_CODE (stripped_decl))
11230 case ADDR_EXPR:
11231 return get_name (TREE_OPERAND (stripped_decl, 0));
11232 default:
11233 return NULL;
11238 /* Return true if TYPE has a variable argument list. */
11240 bool
11241 stdarg_p (const_tree fntype)
11243 function_args_iterator args_iter;
11244 tree n = NULL_TREE, t;
11246 if (!fntype)
11247 return false;
11249 FOREACH_FUNCTION_ARGS (fntype, t, args_iter)
11251 n = t;
11254 return n != NULL_TREE && n != void_type_node;
11257 /* Return true if TYPE has a prototype. */
11259 bool
11260 prototype_p (tree fntype)
11262 tree t;
11264 gcc_assert (fntype != NULL_TREE);
11266 t = TYPE_ARG_TYPES (fntype);
11267 return (t != NULL_TREE);
11270 /* If BLOCK is inlined from an __attribute__((__artificial__))
11271 routine, return pointer to location from where it has been
11272 called. */
11273 location_t *
11274 block_nonartificial_location (tree block)
11276 location_t *ret = NULL;
11278 while (block && TREE_CODE (block) == BLOCK
11279 && BLOCK_ABSTRACT_ORIGIN (block))
11281 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
11283 while (TREE_CODE (ao) == BLOCK
11284 && BLOCK_ABSTRACT_ORIGIN (ao)
11285 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
11286 ao = BLOCK_ABSTRACT_ORIGIN (ao);
11288 if (TREE_CODE (ao) == FUNCTION_DECL)
11290 /* If AO is an artificial inline, point RET to the
11291 call site locus at which it has been inlined and continue
11292 the loop, in case AO's caller is also an artificial
11293 inline. */
11294 if (DECL_DECLARED_INLINE_P (ao)
11295 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
11296 ret = &BLOCK_SOURCE_LOCATION (block);
11297 else
11298 break;
11300 else if (TREE_CODE (ao) != BLOCK)
11301 break;
11303 block = BLOCK_SUPERCONTEXT (block);
11305 return ret;
11309 /* If EXP is inlined from an __attribute__((__artificial__))
11310 function, return the location of the original call expression. */
11312 location_t
11313 tree_nonartificial_location (tree exp)
11315 location_t *loc = block_nonartificial_location (TREE_BLOCK (exp));
11317 if (loc)
11318 return *loc;
11319 else
11320 return EXPR_LOCATION (exp);
11324 /* These are the hash table functions for the hash table of OPTIMIZATION_NODEq
11325 nodes. */
11327 /* Return the hash code code X, an OPTIMIZATION_NODE or TARGET_OPTION code. */
11329 static hashval_t
11330 cl_option_hash_hash (const void *x)
11332 const_tree const t = (const_tree) x;
11333 const char *p;
11334 size_t i;
11335 size_t len = 0;
11336 hashval_t hash = 0;
11338 if (TREE_CODE (t) == OPTIMIZATION_NODE)
11340 p = (const char *)TREE_OPTIMIZATION (t);
11341 len = sizeof (struct cl_optimization);
11344 else if (TREE_CODE (t) == TARGET_OPTION_NODE)
11346 p = (const char *)TREE_TARGET_OPTION (t);
11347 len = sizeof (struct cl_target_option);
11350 else
11351 gcc_unreachable ();
11353 /* assume most opt flags are just 0/1, some are 2-3, and a few might be
11354 something else. */
11355 for (i = 0; i < len; i++)
11356 if (p[i])
11357 hash = (hash << 4) ^ ((i << 2) | p[i]);
11359 return hash;
11362 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
11363 TARGET_OPTION tree node) is the same as that given by *Y, which is the
11364 same. */
11366 static int
11367 cl_option_hash_eq (const void *x, const void *y)
11369 const_tree const xt = (const_tree) x;
11370 const_tree const yt = (const_tree) y;
11371 const char *xp;
11372 const char *yp;
11373 size_t len;
11375 if (TREE_CODE (xt) != TREE_CODE (yt))
11376 return 0;
11378 if (TREE_CODE (xt) == OPTIMIZATION_NODE)
11380 xp = (const char *)TREE_OPTIMIZATION (xt);
11381 yp = (const char *)TREE_OPTIMIZATION (yt);
11382 len = sizeof (struct cl_optimization);
11385 else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
11387 xp = (const char *)TREE_TARGET_OPTION (xt);
11388 yp = (const char *)TREE_TARGET_OPTION (yt);
11389 len = sizeof (struct cl_target_option);
11392 else
11393 gcc_unreachable ();
11395 return (memcmp (xp, yp, len) == 0);
11398 /* Build an OPTIMIZATION_NODE based on the options in OPTS. */
11400 tree
11401 build_optimization_node (struct gcc_options *opts)
11403 tree t;
11404 void **slot;
11406 /* Use the cache of optimization nodes. */
11408 cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node),
11409 opts);
11411 slot = htab_find_slot (cl_option_hash_table, cl_optimization_node, INSERT);
11412 t = (tree) *slot;
11413 if (!t)
11415 /* Insert this one into the hash table. */
11416 t = cl_optimization_node;
11417 *slot = t;
11419 /* Make a new node for next time round. */
11420 cl_optimization_node = make_node (OPTIMIZATION_NODE);
11423 return t;
11426 /* Build a TARGET_OPTION_NODE based on the options in OPTS. */
11428 tree
11429 build_target_option_node (struct gcc_options *opts)
11431 tree t;
11432 void **slot;
11434 /* Use the cache of optimization nodes. */
11436 cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node),
11437 opts);
11439 slot = htab_find_slot (cl_option_hash_table, cl_target_option_node, INSERT);
11440 t = (tree) *slot;
11441 if (!t)
11443 /* Insert this one into the hash table. */
11444 t = cl_target_option_node;
11445 *slot = t;
11447 /* Make a new node for next time round. */
11448 cl_target_option_node = make_node (TARGET_OPTION_NODE);
11451 return t;
11454 /* Reset TREE_TARGET_GLOBALS cache for TARGET_OPTION_NODE.
11455 Called through htab_traverse. */
11457 static int
11458 prepare_target_option_node_for_pch (void **slot, void *)
11460 tree node = (tree) *slot;
11461 if (TREE_CODE (node) == TARGET_OPTION_NODE)
11462 TREE_TARGET_GLOBALS (node) = NULL;
11463 return 1;
11466 /* Clear TREE_TARGET_GLOBALS of all TARGET_OPTION_NODE trees,
11467 so that they aren't saved during PCH writing. */
11469 void
11470 prepare_target_option_nodes_for_pch (void)
11472 htab_traverse (cl_option_hash_table, prepare_target_option_node_for_pch,
11473 NULL);
11476 /* Determine the "ultimate origin" of a block. The block may be an inlined
11477 instance of an inlined instance of a block which is local to an inline
11478 function, so we have to trace all of the way back through the origin chain
11479 to find out what sort of node actually served as the original seed for the
11480 given block. */
11482 tree
11483 block_ultimate_origin (const_tree block)
11485 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
11487 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
11488 nodes in the function to point to themselves; ignore that if
11489 we're trying to output the abstract instance of this function. */
11490 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
11491 return NULL_TREE;
11493 if (immediate_origin == NULL_TREE)
11494 return NULL_TREE;
11495 else
11497 tree ret_val;
11498 tree lookahead = immediate_origin;
11502 ret_val = lookahead;
11503 lookahead = (TREE_CODE (ret_val) == BLOCK
11504 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
11506 while (lookahead != NULL && lookahead != ret_val);
11508 /* The block's abstract origin chain may not be the *ultimate* origin of
11509 the block. It could lead to a DECL that has an abstract origin set.
11510 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
11511 will give us if it has one). Note that DECL's abstract origins are
11512 supposed to be the most distant ancestor (or so decl_ultimate_origin
11513 claims), so we don't need to loop following the DECL origins. */
11514 if (DECL_P (ret_val))
11515 return DECL_ORIGIN (ret_val);
11517 return ret_val;
11521 /* Return true iff conversion in EXP generates no instruction. Mark
11522 it inline so that we fully inline into the stripping functions even
11523 though we have two uses of this function. */
11525 static inline bool
11526 tree_nop_conversion (const_tree exp)
11528 tree outer_type, inner_type;
11530 if (!CONVERT_EXPR_P (exp)
11531 && TREE_CODE (exp) != NON_LVALUE_EXPR)
11532 return false;
11533 if (TREE_OPERAND (exp, 0) == error_mark_node)
11534 return false;
11536 outer_type = TREE_TYPE (exp);
11537 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11539 if (!inner_type)
11540 return false;
11542 /* Use precision rather then machine mode when we can, which gives
11543 the correct answer even for submode (bit-field) types. */
11544 if ((INTEGRAL_TYPE_P (outer_type)
11545 || POINTER_TYPE_P (outer_type)
11546 || TREE_CODE (outer_type) == OFFSET_TYPE)
11547 && (INTEGRAL_TYPE_P (inner_type)
11548 || POINTER_TYPE_P (inner_type)
11549 || TREE_CODE (inner_type) == OFFSET_TYPE))
11550 return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
11552 /* Otherwise fall back on comparing machine modes (e.g. for
11553 aggregate types, floats). */
11554 return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
11557 /* Return true iff conversion in EXP generates no instruction. Don't
11558 consider conversions changing the signedness. */
11560 static bool
11561 tree_sign_nop_conversion (const_tree exp)
11563 tree outer_type, inner_type;
11565 if (!tree_nop_conversion (exp))
11566 return false;
11568 outer_type = TREE_TYPE (exp);
11569 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11571 return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
11572 && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
11575 /* Strip conversions from EXP according to tree_nop_conversion and
11576 return the resulting expression. */
11578 tree
11579 tree_strip_nop_conversions (tree exp)
11581 while (tree_nop_conversion (exp))
11582 exp = TREE_OPERAND (exp, 0);
11583 return exp;
11586 /* Strip conversions from EXP according to tree_sign_nop_conversion
11587 and return the resulting expression. */
11589 tree
11590 tree_strip_sign_nop_conversions (tree exp)
11592 while (tree_sign_nop_conversion (exp))
11593 exp = TREE_OPERAND (exp, 0);
11594 return exp;
11597 /* Avoid any floating point extensions from EXP. */
11598 tree
11599 strip_float_extensions (tree exp)
11601 tree sub, expt, subt;
11603 /* For floating point constant look up the narrowest type that can hold
11604 it properly and handle it like (type)(narrowest_type)constant.
11605 This way we can optimize for instance a=a*2.0 where "a" is float
11606 but 2.0 is double constant. */
11607 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
11609 REAL_VALUE_TYPE orig;
11610 tree type = NULL;
11612 orig = TREE_REAL_CST (exp);
11613 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
11614 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
11615 type = float_type_node;
11616 else if (TYPE_PRECISION (TREE_TYPE (exp))
11617 > TYPE_PRECISION (double_type_node)
11618 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
11619 type = double_type_node;
11620 if (type)
11621 return build_real (type, real_value_truncate (TYPE_MODE (type), orig));
11624 if (!CONVERT_EXPR_P (exp))
11625 return exp;
11627 sub = TREE_OPERAND (exp, 0);
11628 subt = TREE_TYPE (sub);
11629 expt = TREE_TYPE (exp);
11631 if (!FLOAT_TYPE_P (subt))
11632 return exp;
11634 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
11635 return exp;
11637 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
11638 return exp;
11640 return strip_float_extensions (sub);
11643 /* Strip out all handled components that produce invariant
11644 offsets. */
11646 const_tree
11647 strip_invariant_refs (const_tree op)
11649 while (handled_component_p (op))
11651 switch (TREE_CODE (op))
11653 case ARRAY_REF:
11654 case ARRAY_RANGE_REF:
11655 if (!is_gimple_constant (TREE_OPERAND (op, 1))
11656 || TREE_OPERAND (op, 2) != NULL_TREE
11657 || TREE_OPERAND (op, 3) != NULL_TREE)
11658 return NULL;
11659 break;
11661 case COMPONENT_REF:
11662 if (TREE_OPERAND (op, 2) != NULL_TREE)
11663 return NULL;
11664 break;
11666 default:;
11668 op = TREE_OPERAND (op, 0);
11671 return op;
11674 static GTY(()) tree gcc_eh_personality_decl;
11676 /* Return the GCC personality function decl. */
11678 tree
11679 lhd_gcc_personality (void)
11681 if (!gcc_eh_personality_decl)
11682 gcc_eh_personality_decl = build_personality_function ("gcc");
11683 return gcc_eh_personality_decl;
11686 /* For languages with One Definition Rule, work out if
11687 trees are actually the same even if the tree representation
11688 differs. This handles only decls appearing in TYPE_NAME
11689 and TYPE_CONTEXT. That is NAMESPACE_DECL, TYPE_DECL,
11690 RECORD_TYPE and IDENTIFIER_NODE. */
11692 static bool
11693 same_for_odr (tree t1, tree t2)
11695 if (t1 == t2)
11696 return true;
11697 if (!t1 || !t2)
11698 return false;
11699 /* C and C++ FEs differ by using IDENTIFIER_NODE and TYPE_DECL. */
11700 if (TREE_CODE (t1) == IDENTIFIER_NODE
11701 && TREE_CODE (t2) == TYPE_DECL
11702 && DECL_FILE_SCOPE_P (t1))
11704 t2 = DECL_NAME (t2);
11705 gcc_assert (TREE_CODE (t2) == IDENTIFIER_NODE);
11707 if (TREE_CODE (t2) == IDENTIFIER_NODE
11708 && TREE_CODE (t1) == TYPE_DECL
11709 && DECL_FILE_SCOPE_P (t2))
11711 t1 = DECL_NAME (t1);
11712 gcc_assert (TREE_CODE (t1) == IDENTIFIER_NODE);
11714 if (TREE_CODE (t1) != TREE_CODE (t2))
11715 return false;
11716 if (TYPE_P (t1))
11717 return types_same_for_odr (t1, t2);
11718 if (DECL_P (t1))
11719 return decls_same_for_odr (t1, t2);
11720 return false;
11723 /* For languages with One Definition Rule, work out if
11724 decls are actually the same even if the tree representation
11725 differs. This handles only decls appearing in TYPE_NAME
11726 and TYPE_CONTEXT. That is NAMESPACE_DECL, TYPE_DECL,
11727 RECORD_TYPE and IDENTIFIER_NODE. */
11729 static bool
11730 decls_same_for_odr (tree decl1, tree decl2)
11732 if (decl1 && TREE_CODE (decl1) == TYPE_DECL
11733 && DECL_ORIGINAL_TYPE (decl1))
11734 decl1 = DECL_ORIGINAL_TYPE (decl1);
11735 if (decl2 && TREE_CODE (decl2) == TYPE_DECL
11736 && DECL_ORIGINAL_TYPE (decl2))
11737 decl2 = DECL_ORIGINAL_TYPE (decl2);
11738 if (decl1 == decl2)
11739 return true;
11740 if (!decl1 || !decl2)
11741 return false;
11742 gcc_checking_assert (DECL_P (decl1) && DECL_P (decl2));
11743 if (TREE_CODE (decl1) != TREE_CODE (decl2))
11744 return false;
11745 if (TREE_CODE (decl1) == TRANSLATION_UNIT_DECL)
11746 return true;
11747 if (TREE_CODE (decl1) != NAMESPACE_DECL
11748 && TREE_CODE (decl1) != TYPE_DECL)
11749 return false;
11750 if (!DECL_NAME (decl1))
11751 return false;
11752 gcc_checking_assert (TREE_CODE (DECL_NAME (decl1)) == IDENTIFIER_NODE);
11753 gcc_checking_assert (!DECL_NAME (decl2)
11754 || TREE_CODE (DECL_NAME (decl2)) == IDENTIFIER_NODE);
11755 if (DECL_NAME (decl1) != DECL_NAME (decl2))
11756 return false;
11757 return same_for_odr (DECL_CONTEXT (decl1),
11758 DECL_CONTEXT (decl2));
11761 /* For languages with One Definition Rule, work out if
11762 types are same even if the tree representation differs.
11763 This is non-trivial for LTO where minnor differences in
11764 the type representation may have prevented type merging
11765 to merge two copies of otherwise equivalent type. */
11767 bool
11768 types_same_for_odr (tree type1, tree type2)
11770 gcc_checking_assert (TYPE_P (type1) && TYPE_P (type2));
11771 type1 = TYPE_MAIN_VARIANT (type1);
11772 type2 = TYPE_MAIN_VARIANT (type2);
11773 if (type1 == type2)
11774 return true;
11776 #ifndef ENABLE_CHECKING
11777 if (!in_lto_p)
11778 return false;
11779 #endif
11781 /* Check for anonymous namespaces. Those have !TREE_PUBLIC
11782 on the corresponding TYPE_STUB_DECL. */
11783 if (type_in_anonymous_namespace_p (type1)
11784 || type_in_anonymous_namespace_p (type2))
11785 return false;
11786 /* When assembler name of virtual table is available, it is
11787 easy to compare types for equivalence. */
11788 if (TYPE_BINFO (type1) && TYPE_BINFO (type2)
11789 && BINFO_VTABLE (TYPE_BINFO (type1))
11790 && BINFO_VTABLE (TYPE_BINFO (type2)))
11792 tree v1 = BINFO_VTABLE (TYPE_BINFO (type1));
11793 tree v2 = BINFO_VTABLE (TYPE_BINFO (type2));
11795 if (TREE_CODE (v1) == POINTER_PLUS_EXPR)
11797 if (TREE_CODE (v2) != POINTER_PLUS_EXPR
11798 || !operand_equal_p (TREE_OPERAND (v1, 1),
11799 TREE_OPERAND (v2, 1), 0))
11800 return false;
11801 v1 = TREE_OPERAND (TREE_OPERAND (v1, 0), 0);
11802 v2 = TREE_OPERAND (TREE_OPERAND (v2, 0), 0);
11804 v1 = DECL_ASSEMBLER_NAME (v1);
11805 v2 = DECL_ASSEMBLER_NAME (v2);
11806 return (v1 == v2);
11809 /* FIXME: the code comparing type names consider all instantiations of the
11810 same template to have same name. This is because we have no access
11811 to template parameters. For types with no virtual method tables
11812 we thus can return false positives. At the moment we do not need
11813 to compare types in other scenarios than devirtualization. */
11815 /* If types are not structuraly same, do not bother to contnue.
11816 Match in the remainder of code would mean ODR violation. */
11817 if (!types_compatible_p (type1, type2))
11818 return false;
11819 if (!TYPE_NAME (type1))
11820 return false;
11821 if (!decls_same_for_odr (TYPE_NAME (type1), TYPE_NAME (type2)))
11822 return false;
11823 if (!same_for_odr (TYPE_CONTEXT (type1), TYPE_CONTEXT (type2)))
11824 return false;
11825 /* When not in LTO the MAIN_VARIANT check should be the same. */
11826 gcc_assert (in_lto_p);
11828 return true;
11831 /* TARGET is a call target of GIMPLE call statement
11832 (obtained by gimple_call_fn). Return true if it is
11833 OBJ_TYPE_REF representing an virtual call of C++ method.
11834 (As opposed to OBJ_TYPE_REF representing objc calls
11835 through a cast where middle-end devirtualization machinery
11836 can't apply.) */
11838 bool
11839 virtual_method_call_p (tree target)
11841 if (TREE_CODE (target) != OBJ_TYPE_REF)
11842 return false;
11843 target = TREE_TYPE (target);
11844 gcc_checking_assert (TREE_CODE (target) == POINTER_TYPE);
11845 target = TREE_TYPE (target);
11846 if (TREE_CODE (target) == FUNCTION_TYPE)
11847 return false;
11848 gcc_checking_assert (TREE_CODE (target) == METHOD_TYPE);
11849 return true;
11852 /* REF is OBJ_TYPE_REF, return the class the ref corresponds to. */
11854 tree
11855 obj_type_ref_class (tree ref)
11857 gcc_checking_assert (TREE_CODE (ref) == OBJ_TYPE_REF);
11858 ref = TREE_TYPE (ref);
11859 gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
11860 ref = TREE_TYPE (ref);
11861 /* We look for type THIS points to. ObjC also builds
11862 OBJ_TYPE_REF with non-method calls, Their first parameter
11863 ID however also corresponds to class type. */
11864 gcc_checking_assert (TREE_CODE (ref) == METHOD_TYPE
11865 || TREE_CODE (ref) == FUNCTION_TYPE);
11866 ref = TREE_VALUE (TYPE_ARG_TYPES (ref));
11867 gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
11868 return TREE_TYPE (ref);
11871 /* Return true if T is in anonymous namespace. */
11873 bool
11874 type_in_anonymous_namespace_p (tree t)
11876 return (TYPE_STUB_DECL (t) && !TREE_PUBLIC (TYPE_STUB_DECL (t)));
11879 /* Try to find a base info of BINFO that would have its field decl at offset
11880 OFFSET within the BINFO type and which is of EXPECTED_TYPE. If it can be
11881 found, return, otherwise return NULL_TREE. */
11883 tree
11884 get_binfo_at_offset (tree binfo, HOST_WIDE_INT offset, tree expected_type)
11886 tree type = BINFO_TYPE (binfo);
11888 while (true)
11890 HOST_WIDE_INT pos, size;
11891 tree fld;
11892 int i;
11894 if (types_same_for_odr (type, expected_type))
11895 return binfo;
11896 if (offset < 0)
11897 return NULL_TREE;
11899 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
11901 if (TREE_CODE (fld) != FIELD_DECL)
11902 continue;
11904 pos = int_bit_position (fld);
11905 size = tree_to_uhwi (DECL_SIZE (fld));
11906 if (pos <= offset && (pos + size) > offset)
11907 break;
11909 if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE)
11910 return NULL_TREE;
11912 if (!DECL_ARTIFICIAL (fld))
11914 binfo = TYPE_BINFO (TREE_TYPE (fld));
11915 if (!binfo)
11916 return NULL_TREE;
11918 /* Offset 0 indicates the primary base, whose vtable contents are
11919 represented in the binfo for the derived class. */
11920 else if (offset != 0)
11922 tree base_binfo, binfo2 = binfo;
11924 /* Find BINFO corresponding to FLD. This is bit harder
11925 by a fact that in virtual inheritance we may need to walk down
11926 the non-virtual inheritance chain. */
11927 while (true)
11929 tree containing_binfo = NULL, found_binfo = NULL;
11930 for (i = 0; BINFO_BASE_ITERATE (binfo2, i, base_binfo); i++)
11931 if (types_same_for_odr (TREE_TYPE (base_binfo), TREE_TYPE (fld)))
11933 found_binfo = base_binfo;
11934 break;
11936 else
11937 if ((tree_to_shwi (BINFO_OFFSET (base_binfo))
11938 - tree_to_shwi (BINFO_OFFSET (binfo)))
11939 * BITS_PER_UNIT < pos
11940 /* Rule out types with no virtual methods or we can get confused
11941 here by zero sized bases. */
11942 && BINFO_VTABLE (TYPE_BINFO (BINFO_TYPE (base_binfo)))
11943 && (!containing_binfo
11944 || (tree_to_shwi (BINFO_OFFSET (containing_binfo))
11945 < tree_to_shwi (BINFO_OFFSET (base_binfo)))))
11946 containing_binfo = base_binfo;
11947 if (found_binfo)
11949 binfo = found_binfo;
11950 break;
11952 if (!containing_binfo)
11953 return NULL_TREE;
11954 binfo2 = containing_binfo;
11958 type = TREE_TYPE (fld);
11959 offset -= pos;
11963 /* Returns true if X is a typedef decl. */
11965 bool
11966 is_typedef_decl (tree x)
11968 return (x && TREE_CODE (x) == TYPE_DECL
11969 && DECL_ORIGINAL_TYPE (x) != NULL_TREE);
11972 /* Returns true iff TYPE is a type variant created for a typedef. */
11974 bool
11975 typedef_variant_p (tree type)
11977 return is_typedef_decl (TYPE_NAME (type));
11980 /* Warn about a use of an identifier which was marked deprecated. */
11981 void
11982 warn_deprecated_use (tree node, tree attr)
11984 const char *msg;
11986 if (node == 0 || !warn_deprecated_decl)
11987 return;
11989 if (!attr)
11991 if (DECL_P (node))
11992 attr = DECL_ATTRIBUTES (node);
11993 else if (TYPE_P (node))
11995 tree decl = TYPE_STUB_DECL (node);
11996 if (decl)
11997 attr = lookup_attribute ("deprecated",
11998 TYPE_ATTRIBUTES (TREE_TYPE (decl)));
12002 if (attr)
12003 attr = lookup_attribute ("deprecated", attr);
12005 if (attr)
12006 msg = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr)));
12007 else
12008 msg = NULL;
12010 if (DECL_P (node))
12012 expanded_location xloc = expand_location (DECL_SOURCE_LOCATION (node));
12013 if (msg)
12014 warning (OPT_Wdeprecated_declarations,
12015 "%qD is deprecated (declared at %r%s:%d%R): %s",
12016 node, "locus", xloc.file, xloc.line, msg);
12017 else
12018 warning (OPT_Wdeprecated_declarations,
12019 "%qD is deprecated (declared at %r%s:%d%R)",
12020 node, "locus", xloc.file, xloc.line);
12022 else if (TYPE_P (node))
12024 tree what = NULL_TREE;
12025 tree decl = TYPE_STUB_DECL (node);
12027 if (TYPE_NAME (node))
12029 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
12030 what = TYPE_NAME (node);
12031 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
12032 && DECL_NAME (TYPE_NAME (node)))
12033 what = DECL_NAME (TYPE_NAME (node));
12036 if (decl)
12038 expanded_location xloc
12039 = expand_location (DECL_SOURCE_LOCATION (decl));
12040 if (what)
12042 if (msg)
12043 warning (OPT_Wdeprecated_declarations,
12044 "%qE is deprecated (declared at %r%s:%d%R): %s",
12045 what, "locus", xloc.file, xloc.line, msg);
12046 else
12047 warning (OPT_Wdeprecated_declarations,
12048 "%qE is deprecated (declared at %r%s:%d%R)",
12049 what, "locus", xloc.file, xloc.line);
12051 else
12053 if (msg)
12054 warning (OPT_Wdeprecated_declarations,
12055 "type is deprecated (declared at %r%s:%d%R): %s",
12056 "locus", xloc.file, xloc.line, msg);
12057 else
12058 warning (OPT_Wdeprecated_declarations,
12059 "type is deprecated (declared at %r%s:%d%R)",
12060 "locus", xloc.file, xloc.line);
12063 else
12065 if (what)
12067 if (msg)
12068 warning (OPT_Wdeprecated_declarations, "%qE is deprecated: %s",
12069 what, msg);
12070 else
12071 warning (OPT_Wdeprecated_declarations, "%qE is deprecated", what);
12073 else
12075 if (msg)
12076 warning (OPT_Wdeprecated_declarations, "type is deprecated: %s",
12077 msg);
12078 else
12079 warning (OPT_Wdeprecated_declarations, "type is deprecated");
12085 /* Return true if REF has a COMPONENT_REF with a bit-field field declaration
12086 somewhere in it. */
12088 bool
12089 contains_bitfld_component_ref_p (const_tree ref)
12091 while (handled_component_p (ref))
12093 if (TREE_CODE (ref) == COMPONENT_REF
12094 && DECL_BIT_FIELD (TREE_OPERAND (ref, 1)))
12095 return true;
12096 ref = TREE_OPERAND (ref, 0);
12099 return false;
12102 /* Try to determine whether a TRY_CATCH expression can fall through.
12103 This is a subroutine of block_may_fallthru. */
12105 static bool
12106 try_catch_may_fallthru (const_tree stmt)
12108 tree_stmt_iterator i;
12110 /* If the TRY block can fall through, the whole TRY_CATCH can
12111 fall through. */
12112 if (block_may_fallthru (TREE_OPERAND (stmt, 0)))
12113 return true;
12115 i = tsi_start (TREE_OPERAND (stmt, 1));
12116 switch (TREE_CODE (tsi_stmt (i)))
12118 case CATCH_EXPR:
12119 /* We expect to see a sequence of CATCH_EXPR trees, each with a
12120 catch expression and a body. The whole TRY_CATCH may fall
12121 through iff any of the catch bodies falls through. */
12122 for (; !tsi_end_p (i); tsi_next (&i))
12124 if (block_may_fallthru (CATCH_BODY (tsi_stmt (i))))
12125 return true;
12127 return false;
12129 case EH_FILTER_EXPR:
12130 /* The exception filter expression only matters if there is an
12131 exception. If the exception does not match EH_FILTER_TYPES,
12132 we will execute EH_FILTER_FAILURE, and we will fall through
12133 if that falls through. If the exception does match
12134 EH_FILTER_TYPES, the stack unwinder will continue up the
12135 stack, so we will not fall through. We don't know whether we
12136 will throw an exception which matches EH_FILTER_TYPES or not,
12137 so we just ignore EH_FILTER_TYPES and assume that we might
12138 throw an exception which doesn't match. */
12139 return block_may_fallthru (EH_FILTER_FAILURE (tsi_stmt (i)));
12141 default:
12142 /* This case represents statements to be executed when an
12143 exception occurs. Those statements are implicitly followed
12144 by a RESX statement to resume execution after the exception.
12145 So in this case the TRY_CATCH never falls through. */
12146 return false;
12150 /* Try to determine if we can fall out of the bottom of BLOCK. This guess
12151 need not be 100% accurate; simply be conservative and return true if we
12152 don't know. This is used only to avoid stupidly generating extra code.
12153 If we're wrong, we'll just delete the extra code later. */
12155 bool
12156 block_may_fallthru (const_tree block)
12158 /* This CONST_CAST is okay because expr_last returns its argument
12159 unmodified and we assign it to a const_tree. */
12160 const_tree stmt = expr_last (CONST_CAST_TREE (block));
12162 switch (stmt ? TREE_CODE (stmt) : ERROR_MARK)
12164 case GOTO_EXPR:
12165 case RETURN_EXPR:
12166 /* Easy cases. If the last statement of the block implies
12167 control transfer, then we can't fall through. */
12168 return false;
12170 case SWITCH_EXPR:
12171 /* If SWITCH_LABELS is set, this is lowered, and represents a
12172 branch to a selected label and hence can not fall through.
12173 Otherwise SWITCH_BODY is set, and the switch can fall
12174 through. */
12175 return SWITCH_LABELS (stmt) == NULL_TREE;
12177 case COND_EXPR:
12178 if (block_may_fallthru (COND_EXPR_THEN (stmt)))
12179 return true;
12180 return block_may_fallthru (COND_EXPR_ELSE (stmt));
12182 case BIND_EXPR:
12183 return block_may_fallthru (BIND_EXPR_BODY (stmt));
12185 case TRY_CATCH_EXPR:
12186 return try_catch_may_fallthru (stmt);
12188 case TRY_FINALLY_EXPR:
12189 /* The finally clause is always executed after the try clause,
12190 so if it does not fall through, then the try-finally will not
12191 fall through. Otherwise, if the try clause does not fall
12192 through, then when the finally clause falls through it will
12193 resume execution wherever the try clause was going. So the
12194 whole try-finally will only fall through if both the try
12195 clause and the finally clause fall through. */
12196 return (block_may_fallthru (TREE_OPERAND (stmt, 0))
12197 && block_may_fallthru (TREE_OPERAND (stmt, 1)));
12199 case MODIFY_EXPR:
12200 if (TREE_CODE (TREE_OPERAND (stmt, 1)) == CALL_EXPR)
12201 stmt = TREE_OPERAND (stmt, 1);
12202 else
12203 return true;
12204 /* FALLTHRU */
12206 case CALL_EXPR:
12207 /* Functions that do not return do not fall through. */
12208 return (call_expr_flags (stmt) & ECF_NORETURN) == 0;
12210 case CLEANUP_POINT_EXPR:
12211 return block_may_fallthru (TREE_OPERAND (stmt, 0));
12213 case TARGET_EXPR:
12214 return block_may_fallthru (TREE_OPERAND (stmt, 1));
12216 case ERROR_MARK:
12217 return true;
12219 default:
12220 return lang_hooks.block_may_fallthru (stmt);
12224 /* True if we are using EH to handle cleanups. */
12225 static bool using_eh_for_cleanups_flag = false;
12227 /* This routine is called from front ends to indicate eh should be used for
12228 cleanups. */
12229 void
12230 using_eh_for_cleanups (void)
12232 using_eh_for_cleanups_flag = true;
12235 /* Query whether EH is used for cleanups. */
12236 bool
12237 using_eh_for_cleanups_p (void)
12239 return using_eh_for_cleanups_flag;
12242 /* Wrapper for tree_code_name to ensure that tree code is valid */
12243 const char *
12244 get_tree_code_name (enum tree_code code)
12246 const char *invalid = "<invalid tree code>";
12248 if (code >= MAX_TREE_CODES)
12249 return invalid;
12251 return tree_code_name[code];
12254 /* Drops the TREE_OVERFLOW flag from T. */
12256 tree
12257 drop_tree_overflow (tree t)
12259 gcc_checking_assert (TREE_OVERFLOW (t));
12261 /* For tree codes with a sharing machinery re-build the result. */
12262 if (TREE_CODE (t) == INTEGER_CST)
12263 return wide_int_to_tree (TREE_TYPE (t), t);
12265 /* Otherwise, as all tcc_constants are possibly shared, copy the node
12266 and drop the flag. */
12267 t = copy_node (t);
12268 TREE_OVERFLOW (t) = 0;
12269 return t;
12272 /* Given a memory reference expression T, return its base address.
12273 The base address of a memory reference expression is the main
12274 object being referenced. For instance, the base address for
12275 'array[i].fld[j]' is 'array'. You can think of this as stripping
12276 away the offset part from a memory address.
12278 This function calls handled_component_p to strip away all the inner
12279 parts of the memory reference until it reaches the base object. */
12281 tree
12282 get_base_address (tree t)
12284 while (handled_component_p (t))
12285 t = TREE_OPERAND (t, 0);
12287 if ((TREE_CODE (t) == MEM_REF
12288 || TREE_CODE (t) == TARGET_MEM_REF)
12289 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR)
12290 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
12292 /* ??? Either the alias oracle or all callers need to properly deal
12293 with WITH_SIZE_EXPRs before we can look through those. */
12294 if (TREE_CODE (t) == WITH_SIZE_EXPR)
12295 return NULL_TREE;
12297 return t;
12300 #include "gt-tree.h"