rs6000.md (fseldfsf4): Add TARGET_SINGLE_FLOAT condition.
[gcc.git] / gcc / tree.c
1 /* Language-independent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 /* This file contains the low level primitives for operating on tree nodes,
23 including allocation, list operations, interning of identifiers,
24 construction of data type nodes and statement nodes,
25 and construction of type conversion nodes. It also contains
26 tables index by tree code that describe how to take apart
27 nodes of that code.
28
29 It is intended to be language-independent, but occasionally
30 calls language-dependent routines defined (for C) in typecheck.c. */
31
32 #include "config.h"
33 #include "system.h"
34 #include "coretypes.h"
35 #include "tm.h"
36 #include "flags.h"
37 #include "tree.h"
38 #include "real.h"
39 #include "tm_p.h"
40 #include "function.h"
41 #include "obstack.h"
42 #include "toplev.h"
43 #include "ggc.h"
44 #include "hashtab.h"
45 #include "output.h"
46 #include "target.h"
47 #include "langhooks.h"
48 #include "tree-iterator.h"
49 #include "basic-block.h"
50 #include "tree-flow.h"
51 #include "params.h"
52 #include "pointer-set.h"
53 #include "fixed-value.h"
54
55 /* Tree code classes. */
56
57 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
58 #define END_OF_BASE_TREE_CODES tcc_exceptional,
59
60 const enum tree_code_class tree_code_type[] = {
61 #include "all-tree.def"
62 };
63
64 #undef DEFTREECODE
65 #undef END_OF_BASE_TREE_CODES
66
67 /* Table indexed by tree code giving number of expression
68 operands beyond the fixed part of the node structure.
69 Not used for types or decls. */
70
71 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
72 #define END_OF_BASE_TREE_CODES 0,
73
74 const unsigned char tree_code_length[] = {
75 #include "all-tree.def"
76 };
77
78 #undef DEFTREECODE
79 #undef END_OF_BASE_TREE_CODES
80
81 /* Names of tree components.
82 Used for printing out the tree and error messages. */
83 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
84 #define END_OF_BASE_TREE_CODES "@dummy",
85
86 const char *const tree_code_name[] = {
87 #include "all-tree.def"
88 };
89
90 #undef DEFTREECODE
91 #undef END_OF_BASE_TREE_CODES
92
93 /* Each tree code class has an associated string representation.
94 These must correspond to the tree_code_class entries. */
95
96 const char *const tree_code_class_strings[] =
97 {
98 "exceptional",
99 "constant",
100 "type",
101 "declaration",
102 "reference",
103 "comparison",
104 "unary",
105 "binary",
106 "statement",
107 "vl_exp",
108 "expression"
109 };
110
111 /* obstack.[ch] explicitly declined to prototype this. */
112 extern int _obstack_allocated_p (struct obstack *h, void *obj);
113
114 #ifdef GATHER_STATISTICS
115 /* Statistics-gathering stuff. */
116
117 int tree_node_counts[(int) all_kinds];
118 int tree_node_sizes[(int) all_kinds];
119
120 /* Keep in sync with tree.h:enum tree_node_kind. */
121 static const char * const tree_node_kind_names[] = {
122 "decls",
123 "types",
124 "blocks",
125 "stmts",
126 "refs",
127 "exprs",
128 "constants",
129 "identifiers",
130 "perm_tree_lists",
131 "temp_tree_lists",
132 "vecs",
133 "binfos",
134 "ssa names",
135 "constructors",
136 "random kinds",
137 "lang_decl kinds",
138 "lang_type kinds",
139 "omp clauses",
140 };
141 #endif /* GATHER_STATISTICS */
142
143 /* Unique id for next decl created. */
144 static GTY(()) int next_decl_uid;
145 /* Unique id for next type created. */
146 static GTY(()) int next_type_uid = 1;
147
148 /* Since we cannot rehash a type after it is in the table, we have to
149 keep the hash code. */
150
151 struct type_hash GTY(())
152 {
153 unsigned long hash;
154 tree type;
155 };
156
157 /* Initial size of the hash table (rounded to next prime). */
158 #define TYPE_HASH_INITIAL_SIZE 1000
159
160 /* Now here is the hash table. When recording a type, it is added to
161 the slot whose index is the hash code. Note that the hash table is
162 used for several kinds of types (function types, array types and
163 array index range types, for now). While all these live in the
164 same table, they are completely independent, and the hash code is
165 computed differently for each of these. */
166
167 static GTY ((if_marked ("type_hash_marked_p"), param_is (struct type_hash)))
168 htab_t type_hash_table;
169
170 /* Hash table and temporary node for larger integer const values. */
171 static GTY (()) tree int_cst_node;
172 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node)))
173 htab_t int_cst_hash_table;
174
175 /* Hash table for optimization flags and target option flags. Use the same
176 hash table for both sets of options. Nodes for building the current
177 optimization and target option nodes. The assumption is most of the time
178 the options created will already be in the hash table, so we avoid
179 allocating and freeing up a node repeatably. */
180 static GTY (()) tree cl_optimization_node;
181 static GTY (()) tree cl_target_option_node;
182 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node)))
183 htab_t cl_option_hash_table;
184
185 /* General tree->tree mapping structure for use in hash tables. */
186
187
188 static GTY ((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
189 htab_t debug_expr_for_decl;
190
191 static GTY ((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
192 htab_t value_expr_for_decl;
193
194 static GTY ((if_marked ("tree_priority_map_marked_p"),
195 param_is (struct tree_priority_map)))
196 htab_t init_priority_for_decl;
197
198 static GTY ((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
199 htab_t restrict_base_for_decl;
200
201 static void set_type_quals (tree, int);
202 static int type_hash_eq (const void *, const void *);
203 static hashval_t type_hash_hash (const void *);
204 static hashval_t int_cst_hash_hash (const void *);
205 static int int_cst_hash_eq (const void *, const void *);
206 static hashval_t cl_option_hash_hash (const void *);
207 static int cl_option_hash_eq (const void *, const void *);
208 static void print_type_hash_statistics (void);
209 static void print_debug_expr_statistics (void);
210 static void print_value_expr_statistics (void);
211 static int type_hash_marked_p (const void *);
212 static unsigned int type_hash_list (const_tree, hashval_t);
213 static unsigned int attribute_hash_list (const_tree, hashval_t);
214
215 tree global_trees[TI_MAX];
216 tree integer_types[itk_none];
217
218 unsigned char tree_contains_struct[MAX_TREE_CODES][64];
219
220 /* Number of operands for each OpenMP clause. */
221 unsigned const char omp_clause_num_ops[] =
222 {
223 0, /* OMP_CLAUSE_ERROR */
224 1, /* OMP_CLAUSE_PRIVATE */
225 1, /* OMP_CLAUSE_SHARED */
226 1, /* OMP_CLAUSE_FIRSTPRIVATE */
227 2, /* OMP_CLAUSE_LASTPRIVATE */
228 4, /* OMP_CLAUSE_REDUCTION */
229 1, /* OMP_CLAUSE_COPYIN */
230 1, /* OMP_CLAUSE_COPYPRIVATE */
231 1, /* OMP_CLAUSE_IF */
232 1, /* OMP_CLAUSE_NUM_THREADS */
233 1, /* OMP_CLAUSE_SCHEDULE */
234 0, /* OMP_CLAUSE_NOWAIT */
235 0, /* OMP_CLAUSE_ORDERED */
236 0, /* OMP_CLAUSE_DEFAULT */
237 3, /* OMP_CLAUSE_COLLAPSE */
238 0 /* OMP_CLAUSE_UNTIED */
239 };
240
241 const char * const omp_clause_code_name[] =
242 {
243 "error_clause",
244 "private",
245 "shared",
246 "firstprivate",
247 "lastprivate",
248 "reduction",
249 "copyin",
250 "copyprivate",
251 "if",
252 "num_threads",
253 "schedule",
254 "nowait",
255 "ordered",
256 "default",
257 "collapse",
258 "untied"
259 };
260 \f
261 /* Init tree.c. */
262
263 void
264 init_ttree (void)
265 {
266 /* Initialize the hash table of types. */
267 type_hash_table = htab_create_ggc (TYPE_HASH_INITIAL_SIZE, type_hash_hash,
268 type_hash_eq, 0);
269
270 debug_expr_for_decl = htab_create_ggc (512, tree_map_hash,
271 tree_map_eq, 0);
272
273 value_expr_for_decl = htab_create_ggc (512, tree_map_hash,
274 tree_map_eq, 0);
275 init_priority_for_decl = htab_create_ggc (512, tree_priority_map_hash,
276 tree_priority_map_eq, 0);
277 restrict_base_for_decl = htab_create_ggc (256, tree_map_hash,
278 tree_map_eq, 0);
279
280 int_cst_hash_table = htab_create_ggc (1024, int_cst_hash_hash,
281 int_cst_hash_eq, NULL);
282
283 int_cst_node = make_node (INTEGER_CST);
284
285 cl_option_hash_table = htab_create_ggc (64, cl_option_hash_hash,
286 cl_option_hash_eq, NULL);
287
288 cl_optimization_node = make_node (OPTIMIZATION_NODE);
289 cl_target_option_node = make_node (TARGET_OPTION_NODE);
290
291 tree_contains_struct[FUNCTION_DECL][TS_DECL_NON_COMMON] = 1;
292 tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_NON_COMMON] = 1;
293 tree_contains_struct[TYPE_DECL][TS_DECL_NON_COMMON] = 1;
294
295
296 tree_contains_struct[CONST_DECL][TS_DECL_COMMON] = 1;
297 tree_contains_struct[VAR_DECL][TS_DECL_COMMON] = 1;
298 tree_contains_struct[PARM_DECL][TS_DECL_COMMON] = 1;
299 tree_contains_struct[RESULT_DECL][TS_DECL_COMMON] = 1;
300 tree_contains_struct[FUNCTION_DECL][TS_DECL_COMMON] = 1;
301 tree_contains_struct[TYPE_DECL][TS_DECL_COMMON] = 1;
302 tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_COMMON] = 1;
303 tree_contains_struct[LABEL_DECL][TS_DECL_COMMON] = 1;
304 tree_contains_struct[FIELD_DECL][TS_DECL_COMMON] = 1;
305
306
307 tree_contains_struct[CONST_DECL][TS_DECL_WRTL] = 1;
308 tree_contains_struct[VAR_DECL][TS_DECL_WRTL] = 1;
309 tree_contains_struct[PARM_DECL][TS_DECL_WRTL] = 1;
310 tree_contains_struct[RESULT_DECL][TS_DECL_WRTL] = 1;
311 tree_contains_struct[FUNCTION_DECL][TS_DECL_WRTL] = 1;
312 tree_contains_struct[LABEL_DECL][TS_DECL_WRTL] = 1;
313
314 tree_contains_struct[CONST_DECL][TS_DECL_MINIMAL] = 1;
315 tree_contains_struct[VAR_DECL][TS_DECL_MINIMAL] = 1;
316 tree_contains_struct[PARM_DECL][TS_DECL_MINIMAL] = 1;
317 tree_contains_struct[RESULT_DECL][TS_DECL_MINIMAL] = 1;
318 tree_contains_struct[FUNCTION_DECL][TS_DECL_MINIMAL] = 1;
319 tree_contains_struct[TYPE_DECL][TS_DECL_MINIMAL] = 1;
320 tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_MINIMAL] = 1;
321 tree_contains_struct[LABEL_DECL][TS_DECL_MINIMAL] = 1;
322 tree_contains_struct[FIELD_DECL][TS_DECL_MINIMAL] = 1;
323 tree_contains_struct[NAME_MEMORY_TAG][TS_DECL_MINIMAL] = 1;
324 tree_contains_struct[SYMBOL_MEMORY_TAG][TS_DECL_MINIMAL] = 1;
325 tree_contains_struct[MEMORY_PARTITION_TAG][TS_DECL_MINIMAL] = 1;
326
327 tree_contains_struct[NAME_MEMORY_TAG][TS_MEMORY_TAG] = 1;
328 tree_contains_struct[SYMBOL_MEMORY_TAG][TS_MEMORY_TAG] = 1;
329 tree_contains_struct[MEMORY_PARTITION_TAG][TS_MEMORY_TAG] = 1;
330
331 tree_contains_struct[MEMORY_PARTITION_TAG][TS_MEMORY_PARTITION_TAG] = 1;
332
333 tree_contains_struct[VAR_DECL][TS_DECL_WITH_VIS] = 1;
334 tree_contains_struct[FUNCTION_DECL][TS_DECL_WITH_VIS] = 1;
335 tree_contains_struct[TYPE_DECL][TS_DECL_WITH_VIS] = 1;
336 tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_WITH_VIS] = 1;
337
338 tree_contains_struct[VAR_DECL][TS_VAR_DECL] = 1;
339 tree_contains_struct[FIELD_DECL][TS_FIELD_DECL] = 1;
340 tree_contains_struct[PARM_DECL][TS_PARM_DECL] = 1;
341 tree_contains_struct[LABEL_DECL][TS_LABEL_DECL] = 1;
342 tree_contains_struct[RESULT_DECL][TS_RESULT_DECL] = 1;
343 tree_contains_struct[CONST_DECL][TS_CONST_DECL] = 1;
344 tree_contains_struct[TYPE_DECL][TS_TYPE_DECL] = 1;
345 tree_contains_struct[FUNCTION_DECL][TS_FUNCTION_DECL] = 1;
346
347 lang_hooks.init_ts ();
348 }
349
350 \f
351 /* The name of the object as the assembler will see it (but before any
352 translations made by ASM_OUTPUT_LABELREF). Often this is the same
353 as DECL_NAME. It is an IDENTIFIER_NODE. */
354 tree
355 decl_assembler_name (tree decl)
356 {
357 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
358 lang_hooks.set_decl_assembler_name (decl);
359 return DECL_WITH_VIS_CHECK (decl)->decl_with_vis.assembler_name;
360 }
361
362 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
363
364 bool
365 decl_assembler_name_equal (tree decl, const_tree asmname)
366 {
367 tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
368 const char *decl_str;
369 const char *asmname_str;
370 bool test = false;
371
372 if (decl_asmname == asmname)
373 return true;
374
375 decl_str = IDENTIFIER_POINTER (decl_asmname);
376 asmname_str = IDENTIFIER_POINTER (asmname);
377
378
379 /* If the target assembler name was set by the user, things are trickier.
380 We have a leading '*' to begin with. After that, it's arguable what
381 is the correct thing to do with -fleading-underscore. Arguably, we've
382 historically been doing the wrong thing in assemble_alias by always
383 printing the leading underscore. Since we're not changing that, make
384 sure user_label_prefix follows the '*' before matching. */
385 if (decl_str[0] == '*')
386 {
387 size_t ulp_len = strlen (user_label_prefix);
388
389 decl_str ++;
390
391 if (ulp_len == 0)
392 test = true;
393 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
394 decl_str += ulp_len, test=true;
395 else
396 decl_str --;
397 }
398 if (asmname_str[0] == '*')
399 {
400 size_t ulp_len = strlen (user_label_prefix);
401
402 asmname_str ++;
403
404 if (ulp_len == 0)
405 test = true;
406 else if (strncmp (asmname_str, user_label_prefix, ulp_len) == 0)
407 asmname_str += ulp_len, test=true;
408 else
409 asmname_str --;
410 }
411
412 if (!test)
413 return false;
414 return strcmp (decl_str, asmname_str) == 0;
415 }
416
417 /* Hash asmnames ignoring the user specified marks. */
418
419 hashval_t
420 decl_assembler_name_hash (const_tree asmname)
421 {
422 if (IDENTIFIER_POINTER (asmname)[0] == '*')
423 {
424 const char *decl_str = IDENTIFIER_POINTER (asmname) + 1;
425 size_t ulp_len = strlen (user_label_prefix);
426
427 if (ulp_len == 0)
428 ;
429 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
430 decl_str += ulp_len;
431
432 return htab_hash_string (decl_str);
433 }
434
435 return htab_hash_string (IDENTIFIER_POINTER (asmname));
436 }
437
438 /* Compute the number of bytes occupied by a tree with code CODE.
439 This function cannot be used for nodes that have variable sizes,
440 including TREE_VEC, STRING_CST, and CALL_EXPR. */
441 size_t
442 tree_code_size (enum tree_code code)
443 {
444 switch (TREE_CODE_CLASS (code))
445 {
446 case tcc_declaration: /* A decl node */
447 {
448 switch (code)
449 {
450 case FIELD_DECL:
451 return sizeof (struct tree_field_decl);
452 case PARM_DECL:
453 return sizeof (struct tree_parm_decl);
454 case VAR_DECL:
455 return sizeof (struct tree_var_decl);
456 case LABEL_DECL:
457 return sizeof (struct tree_label_decl);
458 case RESULT_DECL:
459 return sizeof (struct tree_result_decl);
460 case CONST_DECL:
461 return sizeof (struct tree_const_decl);
462 case TYPE_DECL:
463 return sizeof (struct tree_type_decl);
464 case FUNCTION_DECL:
465 return sizeof (struct tree_function_decl);
466 case NAME_MEMORY_TAG:
467 case SYMBOL_MEMORY_TAG:
468 return sizeof (struct tree_memory_tag);
469 case MEMORY_PARTITION_TAG:
470 return sizeof (struct tree_memory_partition_tag);
471 default:
472 return sizeof (struct tree_decl_non_common);
473 }
474 }
475
476 case tcc_type: /* a type node */
477 return sizeof (struct tree_type);
478
479 case tcc_reference: /* a reference */
480 case tcc_expression: /* an expression */
481 case tcc_statement: /* an expression with side effects */
482 case tcc_comparison: /* a comparison expression */
483 case tcc_unary: /* a unary arithmetic expression */
484 case tcc_binary: /* a binary arithmetic expression */
485 return (sizeof (struct tree_exp)
486 + (TREE_CODE_LENGTH (code) - 1) * sizeof (tree));
487
488 case tcc_constant: /* a constant */
489 switch (code)
490 {
491 case INTEGER_CST: return sizeof (struct tree_int_cst);
492 case REAL_CST: return sizeof (struct tree_real_cst);
493 case FIXED_CST: return sizeof (struct tree_fixed_cst);
494 case COMPLEX_CST: return sizeof (struct tree_complex);
495 case VECTOR_CST: return sizeof (struct tree_vector);
496 case STRING_CST: gcc_unreachable ();
497 default:
498 return lang_hooks.tree_size (code);
499 }
500
501 case tcc_exceptional: /* something random, like an identifier. */
502 switch (code)
503 {
504 case IDENTIFIER_NODE: return lang_hooks.identifier_size;
505 case TREE_LIST: return sizeof (struct tree_list);
506
507 case ERROR_MARK:
508 case PLACEHOLDER_EXPR: return sizeof (struct tree_common);
509
510 case TREE_VEC:
511 case OMP_CLAUSE: gcc_unreachable ();
512
513 case SSA_NAME: return sizeof (struct tree_ssa_name);
514
515 case STATEMENT_LIST: return sizeof (struct tree_statement_list);
516 case BLOCK: return sizeof (struct tree_block);
517 case CONSTRUCTOR: return sizeof (struct tree_constructor);
518 case OPTIMIZATION_NODE: return sizeof (struct tree_optimization_option);
519 case TARGET_OPTION_NODE: return sizeof (struct tree_target_option);
520
521 default:
522 return lang_hooks.tree_size (code);
523 }
524
525 default:
526 gcc_unreachable ();
527 }
528 }
529
530 /* Compute the number of bytes occupied by NODE. This routine only
531 looks at TREE_CODE, except for those nodes that have variable sizes. */
532 size_t
533 tree_size (const_tree node)
534 {
535 const enum tree_code code = TREE_CODE (node);
536 switch (code)
537 {
538 case TREE_BINFO:
539 return (offsetof (struct tree_binfo, base_binfos)
540 + VEC_embedded_size (tree, BINFO_N_BASE_BINFOS (node)));
541
542 case TREE_VEC:
543 return (sizeof (struct tree_vec)
544 + (TREE_VEC_LENGTH (node) - 1) * sizeof (tree));
545
546 case STRING_CST:
547 return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
548
549 case OMP_CLAUSE:
550 return (sizeof (struct tree_omp_clause)
551 + (omp_clause_num_ops[OMP_CLAUSE_CODE (node)] - 1)
552 * sizeof (tree));
553
554 default:
555 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
556 return (sizeof (struct tree_exp)
557 + (VL_EXP_OPERAND_LENGTH (node) - 1) * sizeof (tree));
558 else
559 return tree_code_size (code);
560 }
561 }
562
563 /* Return a newly allocated node of code CODE. For decl and type
564 nodes, some other fields are initialized. The rest of the node is
565 initialized to zero. This function cannot be used for TREE_VEC or
566 OMP_CLAUSE nodes, which is enforced by asserts in tree_code_size.
567
568 Achoo! I got a code in the node. */
569
570 tree
571 make_node_stat (enum tree_code code MEM_STAT_DECL)
572 {
573 tree t;
574 enum tree_code_class type = TREE_CODE_CLASS (code);
575 size_t length = tree_code_size (code);
576 #ifdef GATHER_STATISTICS
577 tree_node_kind kind;
578
579 switch (type)
580 {
581 case tcc_declaration: /* A decl node */
582 kind = d_kind;
583 break;
584
585 case tcc_type: /* a type node */
586 kind = t_kind;
587 break;
588
589 case tcc_statement: /* an expression with side effects */
590 kind = s_kind;
591 break;
592
593 case tcc_reference: /* a reference */
594 kind = r_kind;
595 break;
596
597 case tcc_expression: /* an expression */
598 case tcc_comparison: /* a comparison expression */
599 case tcc_unary: /* a unary arithmetic expression */
600 case tcc_binary: /* a binary arithmetic expression */
601 kind = e_kind;
602 break;
603
604 case tcc_constant: /* a constant */
605 kind = c_kind;
606 break;
607
608 case tcc_exceptional: /* something random, like an identifier. */
609 switch (code)
610 {
611 case IDENTIFIER_NODE:
612 kind = id_kind;
613 break;
614
615 case TREE_VEC:
616 kind = vec_kind;
617 break;
618
619 case TREE_BINFO:
620 kind = binfo_kind;
621 break;
622
623 case SSA_NAME:
624 kind = ssa_name_kind;
625 break;
626
627 case BLOCK:
628 kind = b_kind;
629 break;
630
631 case CONSTRUCTOR:
632 kind = constr_kind;
633 break;
634
635 default:
636 kind = x_kind;
637 break;
638 }
639 break;
640
641 default:
642 gcc_unreachable ();
643 }
644
645 tree_node_counts[(int) kind]++;
646 tree_node_sizes[(int) kind] += length;
647 #endif
648
649 if (code == IDENTIFIER_NODE)
650 t = (tree) ggc_alloc_zone_pass_stat (length, &tree_id_zone);
651 else
652 t = (tree) ggc_alloc_zone_pass_stat (length, &tree_zone);
653
654 memset (t, 0, length);
655
656 TREE_SET_CODE (t, code);
657
658 switch (type)
659 {
660 case tcc_statement:
661 TREE_SIDE_EFFECTS (t) = 1;
662 break;
663
664 case tcc_declaration:
665 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
666 {
667 if (code == FUNCTION_DECL)
668 {
669 DECL_ALIGN (t) = FUNCTION_BOUNDARY;
670 DECL_MODE (t) = FUNCTION_MODE;
671 }
672 else
673 DECL_ALIGN (t) = 1;
674 /* We have not yet computed the alias set for this declaration. */
675 DECL_POINTER_ALIAS_SET (t) = -1;
676 }
677 DECL_SOURCE_LOCATION (t) = input_location;
678 DECL_UID (t) = next_decl_uid++;
679
680 break;
681
682 case tcc_type:
683 TYPE_UID (t) = next_type_uid++;
684 TYPE_ALIGN (t) = BITS_PER_UNIT;
685 TYPE_USER_ALIGN (t) = 0;
686 TYPE_MAIN_VARIANT (t) = t;
687 TYPE_CANONICAL (t) = t;
688
689 /* Default to no attributes for type, but let target change that. */
690 TYPE_ATTRIBUTES (t) = NULL_TREE;
691 targetm.set_default_type_attributes (t);
692
693 /* We have not yet computed the alias set for this type. */
694 TYPE_ALIAS_SET (t) = -1;
695 break;
696
697 case tcc_constant:
698 TREE_CONSTANT (t) = 1;
699 break;
700
701 case tcc_expression:
702 switch (code)
703 {
704 case INIT_EXPR:
705 case MODIFY_EXPR:
706 case VA_ARG_EXPR:
707 case PREDECREMENT_EXPR:
708 case PREINCREMENT_EXPR:
709 case POSTDECREMENT_EXPR:
710 case POSTINCREMENT_EXPR:
711 /* All of these have side-effects, no matter what their
712 operands are. */
713 TREE_SIDE_EFFECTS (t) = 1;
714 break;
715
716 default:
717 break;
718 }
719 break;
720
721 default:
722 /* Other classes need no special treatment. */
723 break;
724 }
725
726 return t;
727 }
728 \f
729 /* Return a new node with the same contents as NODE except that its
730 TREE_CHAIN is zero and it has a fresh uid. */
731
732 tree
733 copy_node_stat (tree node MEM_STAT_DECL)
734 {
735 tree t;
736 enum tree_code code = TREE_CODE (node);
737 size_t length;
738
739 gcc_assert (code != STATEMENT_LIST);
740
741 length = tree_size (node);
742 t = (tree) ggc_alloc_zone_pass_stat (length, &tree_zone);
743 memcpy (t, node, length);
744
745 TREE_CHAIN (t) = 0;
746 TREE_ASM_WRITTEN (t) = 0;
747 TREE_VISITED (t) = 0;
748 t->base.ann = 0;
749
750 if (TREE_CODE_CLASS (code) == tcc_declaration)
751 {
752 DECL_UID (t) = next_decl_uid++;
753 if ((TREE_CODE (node) == PARM_DECL || TREE_CODE (node) == VAR_DECL)
754 && DECL_HAS_VALUE_EXPR_P (node))
755 {
756 SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (node));
757 DECL_HAS_VALUE_EXPR_P (t) = 1;
758 }
759 if (TREE_CODE (node) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (node))
760 {
761 SET_DECL_INIT_PRIORITY (t, DECL_INIT_PRIORITY (node));
762 DECL_HAS_INIT_PRIORITY_P (t) = 1;
763 }
764 if (TREE_CODE (node) == VAR_DECL && DECL_BASED_ON_RESTRICT_P (node))
765 {
766 SET_DECL_RESTRICT_BASE (t, DECL_GET_RESTRICT_BASE (node));
767 DECL_BASED_ON_RESTRICT_P (t) = 1;
768 }
769 }
770 else if (TREE_CODE_CLASS (code) == tcc_type)
771 {
772 TYPE_UID (t) = next_type_uid++;
773 /* The following is so that the debug code for
774 the copy is different from the original type.
775 The two statements usually duplicate each other
776 (because they clear fields of the same union),
777 but the optimizer should catch that. */
778 TYPE_SYMTAB_POINTER (t) = 0;
779 TYPE_SYMTAB_ADDRESS (t) = 0;
780
781 /* Do not copy the values cache. */
782 if (TYPE_CACHED_VALUES_P(t))
783 {
784 TYPE_CACHED_VALUES_P (t) = 0;
785 TYPE_CACHED_VALUES (t) = NULL_TREE;
786 }
787 }
788
789 return t;
790 }
791
792 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
793 For example, this can copy a list made of TREE_LIST nodes. */
794
795 tree
796 copy_list (tree list)
797 {
798 tree head;
799 tree prev, next;
800
801 if (list == 0)
802 return 0;
803
804 head = prev = copy_node (list);
805 next = TREE_CHAIN (list);
806 while (next)
807 {
808 TREE_CHAIN (prev) = copy_node (next);
809 prev = TREE_CHAIN (prev);
810 next = TREE_CHAIN (next);
811 }
812 return head;
813 }
814
815 \f
816 /* Create an INT_CST node with a LOW value sign extended. */
817
818 tree
819 build_int_cst (tree type, HOST_WIDE_INT low)
820 {
821 /* Support legacy code. */
822 if (!type)
823 type = integer_type_node;
824
825 return build_int_cst_wide (type, low, low < 0 ? -1 : 0);
826 }
827
828 /* Create an INT_CST node with a LOW value zero extended. */
829
830 tree
831 build_int_cstu (tree type, unsigned HOST_WIDE_INT low)
832 {
833 return build_int_cst_wide (type, low, 0);
834 }
835
836 /* Create an INT_CST node with a LOW value in TYPE. The value is sign extended
837 if it is negative. This function is similar to build_int_cst, but
838 the extra bits outside of the type precision are cleared. Constants
839 with these extra bits may confuse the fold so that it detects overflows
840 even in cases when they do not occur, and in general should be avoided.
841 We cannot however make this a default behavior of build_int_cst without
842 more intrusive changes, since there are parts of gcc that rely on the extra
843 precision of the integer constants. */
844
845 tree
846 build_int_cst_type (tree type, HOST_WIDE_INT low)
847 {
848 unsigned HOST_WIDE_INT low1;
849 HOST_WIDE_INT hi;
850
851 gcc_assert (type);
852
853 fit_double_type (low, low < 0 ? -1 : 0, &low1, &hi, type);
854
855 return build_int_cst_wide (type, low1, hi);
856 }
857
858 /* Create an INT_CST node of TYPE and value HI:LOW. The value is truncated
859 and sign extended according to the value range of TYPE. */
860
861 tree
862 build_int_cst_wide_type (tree type,
863 unsigned HOST_WIDE_INT low, HOST_WIDE_INT high)
864 {
865 fit_double_type (low, high, &low, &high, type);
866 return build_int_cst_wide (type, low, high);
867 }
868
869 /* These are the hash table functions for the hash table of INTEGER_CST
870 nodes of a sizetype. */
871
872 /* Return the hash code code X, an INTEGER_CST. */
873
874 static hashval_t
875 int_cst_hash_hash (const void *x)
876 {
877 const_tree const t = (const_tree) x;
878
879 return (TREE_INT_CST_HIGH (t) ^ TREE_INT_CST_LOW (t)
880 ^ htab_hash_pointer (TREE_TYPE (t)));
881 }
882
883 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
884 is the same as that given by *Y, which is the same. */
885
886 static int
887 int_cst_hash_eq (const void *x, const void *y)
888 {
889 const_tree const xt = (const_tree) x;
890 const_tree const yt = (const_tree) y;
891
892 return (TREE_TYPE (xt) == TREE_TYPE (yt)
893 && TREE_INT_CST_HIGH (xt) == TREE_INT_CST_HIGH (yt)
894 && TREE_INT_CST_LOW (xt) == TREE_INT_CST_LOW (yt));
895 }
896
897 /* Create an INT_CST node of TYPE and value HI:LOW.
898 The returned node is always shared. For small integers we use a
899 per-type vector cache, for larger ones we use a single hash table. */
900
901 tree
902 build_int_cst_wide (tree type, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
903 {
904 tree t;
905 int ix = -1;
906 int limit = 0;
907
908 gcc_assert (type);
909
910 switch (TREE_CODE (type))
911 {
912 case POINTER_TYPE:
913 case REFERENCE_TYPE:
914 /* Cache NULL pointer. */
915 if (!hi && !low)
916 {
917 limit = 1;
918 ix = 0;
919 }
920 break;
921
922 case BOOLEAN_TYPE:
923 /* Cache false or true. */
924 limit = 2;
925 if (!hi && low < 2)
926 ix = low;
927 break;
928
929 case INTEGER_TYPE:
930 case OFFSET_TYPE:
931 if (TYPE_UNSIGNED (type))
932 {
933 /* Cache 0..N */
934 limit = INTEGER_SHARE_LIMIT;
935 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
936 ix = low;
937 }
938 else
939 {
940 /* Cache -1..N */
941 limit = INTEGER_SHARE_LIMIT + 1;
942 if (!hi && low < (unsigned HOST_WIDE_INT)INTEGER_SHARE_LIMIT)
943 ix = low + 1;
944 else if (hi == -1 && low == -(unsigned HOST_WIDE_INT)1)
945 ix = 0;
946 }
947 break;
948
949 case ENUMERAL_TYPE:
950 break;
951
952 default:
953 gcc_unreachable ();
954 }
955
956 if (ix >= 0)
957 {
958 /* Look for it in the type's vector of small shared ints. */
959 if (!TYPE_CACHED_VALUES_P (type))
960 {
961 TYPE_CACHED_VALUES_P (type) = 1;
962 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
963 }
964
965 t = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix);
966 if (t)
967 {
968 /* Make sure no one is clobbering the shared constant. */
969 gcc_assert (TREE_TYPE (t) == type);
970 gcc_assert (TREE_INT_CST_LOW (t) == low);
971 gcc_assert (TREE_INT_CST_HIGH (t) == hi);
972 }
973 else
974 {
975 /* Create a new shared int. */
976 t = make_node (INTEGER_CST);
977
978 TREE_INT_CST_LOW (t) = low;
979 TREE_INT_CST_HIGH (t) = hi;
980 TREE_TYPE (t) = type;
981
982 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
983 }
984 }
985 else
986 {
987 /* Use the cache of larger shared ints. */
988 void **slot;
989
990 TREE_INT_CST_LOW (int_cst_node) = low;
991 TREE_INT_CST_HIGH (int_cst_node) = hi;
992 TREE_TYPE (int_cst_node) = type;
993
994 slot = htab_find_slot (int_cst_hash_table, int_cst_node, INSERT);
995 t = (tree) *slot;
996 if (!t)
997 {
998 /* Insert this one into the hash table. */
999 t = int_cst_node;
1000 *slot = t;
1001 /* Make a new node for next time round. */
1002 int_cst_node = make_node (INTEGER_CST);
1003 }
1004 }
1005
1006 return t;
1007 }
1008
1009 /* Builds an integer constant in TYPE such that lowest BITS bits are ones
1010 and the rest are zeros. */
1011
1012 tree
1013 build_low_bits_mask (tree type, unsigned bits)
1014 {
1015 unsigned HOST_WIDE_INT low;
1016 HOST_WIDE_INT high;
1017 unsigned HOST_WIDE_INT all_ones = ~(unsigned HOST_WIDE_INT) 0;
1018
1019 gcc_assert (bits <= TYPE_PRECISION (type));
1020
1021 if (bits == TYPE_PRECISION (type)
1022 && !TYPE_UNSIGNED (type))
1023 {
1024 /* Sign extended all-ones mask. */
1025 low = all_ones;
1026 high = -1;
1027 }
1028 else if (bits <= HOST_BITS_PER_WIDE_INT)
1029 {
1030 low = all_ones >> (HOST_BITS_PER_WIDE_INT - bits);
1031 high = 0;
1032 }
1033 else
1034 {
1035 bits -= HOST_BITS_PER_WIDE_INT;
1036 low = all_ones;
1037 high = all_ones >> (HOST_BITS_PER_WIDE_INT - bits);
1038 }
1039
1040 return build_int_cst_wide (type, low, high);
1041 }
1042
1043 /* Checks that X is integer constant that can be expressed in (unsigned)
1044 HOST_WIDE_INT without loss of precision. */
1045
1046 bool
1047 cst_and_fits_in_hwi (const_tree x)
1048 {
1049 if (TREE_CODE (x) != INTEGER_CST)
1050 return false;
1051
1052 if (TYPE_PRECISION (TREE_TYPE (x)) > HOST_BITS_PER_WIDE_INT)
1053 return false;
1054
1055 return (TREE_INT_CST_HIGH (x) == 0
1056 || TREE_INT_CST_HIGH (x) == -1);
1057 }
1058
1059 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1060 are in a list pointed to by VALS. */
1061
1062 tree
1063 build_vector (tree type, tree vals)
1064 {
1065 tree v = make_node (VECTOR_CST);
1066 int over = 0;
1067 tree link;
1068
1069 TREE_VECTOR_CST_ELTS (v) = vals;
1070 TREE_TYPE (v) = type;
1071
1072 /* Iterate through elements and check for overflow. */
1073 for (link = vals; link; link = TREE_CHAIN (link))
1074 {
1075 tree value = TREE_VALUE (link);
1076
1077 /* Don't crash if we get an address constant. */
1078 if (!CONSTANT_CLASS_P (value))
1079 continue;
1080
1081 over |= TREE_OVERFLOW (value);
1082 }
1083
1084 TREE_OVERFLOW (v) = over;
1085 return v;
1086 }
1087
1088 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1089 are extracted from V, a vector of CONSTRUCTOR_ELT. */
1090
1091 tree
1092 build_vector_from_ctor (tree type, VEC(constructor_elt,gc) *v)
1093 {
1094 tree list = NULL_TREE;
1095 unsigned HOST_WIDE_INT idx;
1096 tree value;
1097
1098 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1099 list = tree_cons (NULL_TREE, value, list);
1100 return build_vector (type, nreverse (list));
1101 }
1102
1103 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1104 are in the VEC pointed to by VALS. */
1105 tree
1106 build_constructor (tree type, VEC(constructor_elt,gc) *vals)
1107 {
1108 tree c = make_node (CONSTRUCTOR);
1109 TREE_TYPE (c) = type;
1110 CONSTRUCTOR_ELTS (c) = vals;
1111 return c;
1112 }
1113
1114 /* Build a CONSTRUCTOR node made of a single initializer, with the specified
1115 INDEX and VALUE. */
1116 tree
1117 build_constructor_single (tree type, tree index, tree value)
1118 {
1119 VEC(constructor_elt,gc) *v;
1120 constructor_elt *elt;
1121 tree t;
1122
1123 v = VEC_alloc (constructor_elt, gc, 1);
1124 elt = VEC_quick_push (constructor_elt, v, NULL);
1125 elt->index = index;
1126 elt->value = value;
1127
1128 t = build_constructor (type, v);
1129 TREE_CONSTANT (t) = TREE_CONSTANT (value);
1130 return t;
1131 }
1132
1133
1134 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1135 are in a list pointed to by VALS. */
1136 tree
1137 build_constructor_from_list (tree type, tree vals)
1138 {
1139 tree t, val;
1140 VEC(constructor_elt,gc) *v = NULL;
1141 bool constant_p = true;
1142
1143 if (vals)
1144 {
1145 v = VEC_alloc (constructor_elt, gc, list_length (vals));
1146 for (t = vals; t; t = TREE_CHAIN (t))
1147 {
1148 constructor_elt *elt = VEC_quick_push (constructor_elt, v, NULL);
1149 val = TREE_VALUE (t);
1150 elt->index = TREE_PURPOSE (t);
1151 elt->value = val;
1152 if (!TREE_CONSTANT (val))
1153 constant_p = false;
1154 }
1155 }
1156
1157 t = build_constructor (type, v);
1158 TREE_CONSTANT (t) = constant_p;
1159 return t;
1160 }
1161
1162 /* Return a new FIXED_CST node whose type is TYPE and value is F. */
1163
1164 tree
1165 build_fixed (tree type, FIXED_VALUE_TYPE f)
1166 {
1167 tree v;
1168 FIXED_VALUE_TYPE *fp;
1169
1170 v = make_node (FIXED_CST);
1171 fp = GGC_NEW (FIXED_VALUE_TYPE);
1172 memcpy (fp, &f, sizeof (FIXED_VALUE_TYPE));
1173
1174 TREE_TYPE (v) = type;
1175 TREE_FIXED_CST_PTR (v) = fp;
1176 return v;
1177 }
1178
1179 /* Return a new REAL_CST node whose type is TYPE and value is D. */
1180
1181 tree
1182 build_real (tree type, REAL_VALUE_TYPE d)
1183 {
1184 tree v;
1185 REAL_VALUE_TYPE *dp;
1186 int overflow = 0;
1187
1188 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
1189 Consider doing it via real_convert now. */
1190
1191 v = make_node (REAL_CST);
1192 dp = GGC_NEW (REAL_VALUE_TYPE);
1193 memcpy (dp, &d, sizeof (REAL_VALUE_TYPE));
1194
1195 TREE_TYPE (v) = type;
1196 TREE_REAL_CST_PTR (v) = dp;
1197 TREE_OVERFLOW (v) = overflow;
1198 return v;
1199 }
1200
1201 /* Return a new REAL_CST node whose type is TYPE
1202 and whose value is the integer value of the INTEGER_CST node I. */
1203
1204 REAL_VALUE_TYPE
1205 real_value_from_int_cst (const_tree type, const_tree i)
1206 {
1207 REAL_VALUE_TYPE d;
1208
1209 /* Clear all bits of the real value type so that we can later do
1210 bitwise comparisons to see if two values are the same. */
1211 memset (&d, 0, sizeof d);
1212
1213 real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode,
1214 TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i),
1215 TYPE_UNSIGNED (TREE_TYPE (i)));
1216 return d;
1217 }
1218
1219 /* Given a tree representing an integer constant I, return a tree
1220 representing the same value as a floating-point constant of type TYPE. */
1221
1222 tree
1223 build_real_from_int_cst (tree type, const_tree i)
1224 {
1225 tree v;
1226 int overflow = TREE_OVERFLOW (i);
1227
1228 v = build_real (type, real_value_from_int_cst (type, i));
1229
1230 TREE_OVERFLOW (v) |= overflow;
1231 return v;
1232 }
1233
1234 /* Return a newly constructed STRING_CST node whose value is
1235 the LEN characters at STR.
1236 The TREE_TYPE is not initialized. */
1237
1238 tree
1239 build_string (int len, const char *str)
1240 {
1241 tree s;
1242 size_t length;
1243
1244 /* Do not waste bytes provided by padding of struct tree_string. */
1245 length = len + offsetof (struct tree_string, str) + 1;
1246
1247 #ifdef GATHER_STATISTICS
1248 tree_node_counts[(int) c_kind]++;
1249 tree_node_sizes[(int) c_kind] += length;
1250 #endif
1251
1252 s = ggc_alloc_tree (length);
1253
1254 memset (s, 0, sizeof (struct tree_common));
1255 TREE_SET_CODE (s, STRING_CST);
1256 TREE_CONSTANT (s) = 1;
1257 TREE_STRING_LENGTH (s) = len;
1258 memcpy (s->string.str, str, len);
1259 s->string.str[len] = '\0';
1260
1261 return s;
1262 }
1263
1264 /* Return a newly constructed COMPLEX_CST node whose value is
1265 specified by the real and imaginary parts REAL and IMAG.
1266 Both REAL and IMAG should be constant nodes. TYPE, if specified,
1267 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
1268
1269 tree
1270 build_complex (tree type, tree real, tree imag)
1271 {
1272 tree t = make_node (COMPLEX_CST);
1273
1274 TREE_REALPART (t) = real;
1275 TREE_IMAGPART (t) = imag;
1276 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
1277 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
1278 return t;
1279 }
1280
1281 /* Return a constant of arithmetic type TYPE which is the
1282 multiplicative identity of the set TYPE. */
1283
1284 tree
1285 build_one_cst (tree type)
1286 {
1287 switch (TREE_CODE (type))
1288 {
1289 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1290 case POINTER_TYPE: case REFERENCE_TYPE:
1291 case OFFSET_TYPE:
1292 return build_int_cst (type, 1);
1293
1294 case REAL_TYPE:
1295 return build_real (type, dconst1);
1296
1297 case FIXED_POINT_TYPE:
1298 /* We can only generate 1 for accum types. */
1299 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
1300 return build_fixed (type, FCONST1(TYPE_MODE (type)));
1301
1302 case VECTOR_TYPE:
1303 {
1304 tree scalar, cst;
1305 int i;
1306
1307 scalar = build_one_cst (TREE_TYPE (type));
1308
1309 /* Create 'vect_cst_ = {cst,cst,...,cst}' */
1310 cst = NULL_TREE;
1311 for (i = TYPE_VECTOR_SUBPARTS (type); --i >= 0; )
1312 cst = tree_cons (NULL_TREE, scalar, cst);
1313
1314 return build_vector (type, cst);
1315 }
1316
1317 case COMPLEX_TYPE:
1318 return build_complex (type,
1319 build_one_cst (TREE_TYPE (type)),
1320 fold_convert (TREE_TYPE (type), integer_zero_node));
1321
1322 default:
1323 gcc_unreachable ();
1324 }
1325 }
1326
1327 /* Build a BINFO with LEN language slots. */
1328
1329 tree
1330 make_tree_binfo_stat (unsigned base_binfos MEM_STAT_DECL)
1331 {
1332 tree t;
1333 size_t length = (offsetof (struct tree_binfo, base_binfos)
1334 + VEC_embedded_size (tree, base_binfos));
1335
1336 #ifdef GATHER_STATISTICS
1337 tree_node_counts[(int) binfo_kind]++;
1338 tree_node_sizes[(int) binfo_kind] += length;
1339 #endif
1340
1341 t = (tree) ggc_alloc_zone_pass_stat (length, &tree_zone);
1342
1343 memset (t, 0, offsetof (struct tree_binfo, base_binfos));
1344
1345 TREE_SET_CODE (t, TREE_BINFO);
1346
1347 VEC_embedded_init (tree, BINFO_BASE_BINFOS (t), base_binfos);
1348
1349 return t;
1350 }
1351
1352
1353 /* Build a newly constructed TREE_VEC node of length LEN. */
1354
1355 tree
1356 make_tree_vec_stat (int len MEM_STAT_DECL)
1357 {
1358 tree t;
1359 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
1360
1361 #ifdef GATHER_STATISTICS
1362 tree_node_counts[(int) vec_kind]++;
1363 tree_node_sizes[(int) vec_kind] += length;
1364 #endif
1365
1366 t = (tree) ggc_alloc_zone_pass_stat (length, &tree_zone);
1367
1368 memset (t, 0, length);
1369
1370 TREE_SET_CODE (t, TREE_VEC);
1371 TREE_VEC_LENGTH (t) = len;
1372
1373 return t;
1374 }
1375 \f
1376 /* Return 1 if EXPR is the integer constant zero or a complex constant
1377 of zero. */
1378
1379 int
1380 integer_zerop (const_tree expr)
1381 {
1382 STRIP_NOPS (expr);
1383
1384 return ((TREE_CODE (expr) == INTEGER_CST
1385 && TREE_INT_CST_LOW (expr) == 0
1386 && TREE_INT_CST_HIGH (expr) == 0)
1387 || (TREE_CODE (expr) == COMPLEX_CST
1388 && integer_zerop (TREE_REALPART (expr))
1389 && integer_zerop (TREE_IMAGPART (expr))));
1390 }
1391
1392 /* Return 1 if EXPR is the integer constant one or the corresponding
1393 complex constant. */
1394
1395 int
1396 integer_onep (const_tree expr)
1397 {
1398 STRIP_NOPS (expr);
1399
1400 return ((TREE_CODE (expr) == INTEGER_CST
1401 && TREE_INT_CST_LOW (expr) == 1
1402 && TREE_INT_CST_HIGH (expr) == 0)
1403 || (TREE_CODE (expr) == COMPLEX_CST
1404 && integer_onep (TREE_REALPART (expr))
1405 && integer_zerop (TREE_IMAGPART (expr))));
1406 }
1407
1408 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
1409 it contains. Likewise for the corresponding complex constant. */
1410
1411 int
1412 integer_all_onesp (const_tree expr)
1413 {
1414 int prec;
1415 int uns;
1416
1417 STRIP_NOPS (expr);
1418
1419 if (TREE_CODE (expr) == COMPLEX_CST
1420 && integer_all_onesp (TREE_REALPART (expr))
1421 && integer_zerop (TREE_IMAGPART (expr)))
1422 return 1;
1423
1424 else if (TREE_CODE (expr) != INTEGER_CST)
1425 return 0;
1426
1427 uns = TYPE_UNSIGNED (TREE_TYPE (expr));
1428 if (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
1429 && TREE_INT_CST_HIGH (expr) == -1)
1430 return 1;
1431 if (!uns)
1432 return 0;
1433
1434 /* Note that using TYPE_PRECISION here is wrong. We care about the
1435 actual bits, not the (arbitrary) range of the type. */
1436 prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (expr)));
1437 if (prec >= HOST_BITS_PER_WIDE_INT)
1438 {
1439 HOST_WIDE_INT high_value;
1440 int shift_amount;
1441
1442 shift_amount = prec - HOST_BITS_PER_WIDE_INT;
1443
1444 /* Can not handle precisions greater than twice the host int size. */
1445 gcc_assert (shift_amount <= HOST_BITS_PER_WIDE_INT);
1446 if (shift_amount == HOST_BITS_PER_WIDE_INT)
1447 /* Shifting by the host word size is undefined according to the ANSI
1448 standard, so we must handle this as a special case. */
1449 high_value = -1;
1450 else
1451 high_value = ((HOST_WIDE_INT) 1 << shift_amount) - 1;
1452
1453 return (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
1454 && TREE_INT_CST_HIGH (expr) == high_value);
1455 }
1456 else
1457 return TREE_INT_CST_LOW (expr) == ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
1458 }
1459
1460 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
1461 one bit on). */
1462
1463 int
1464 integer_pow2p (const_tree expr)
1465 {
1466 int prec;
1467 HOST_WIDE_INT high, low;
1468
1469 STRIP_NOPS (expr);
1470
1471 if (TREE_CODE (expr) == COMPLEX_CST
1472 && integer_pow2p (TREE_REALPART (expr))
1473 && integer_zerop (TREE_IMAGPART (expr)))
1474 return 1;
1475
1476 if (TREE_CODE (expr) != INTEGER_CST)
1477 return 0;
1478
1479 prec = (POINTER_TYPE_P (TREE_TYPE (expr))
1480 ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
1481 high = TREE_INT_CST_HIGH (expr);
1482 low = TREE_INT_CST_LOW (expr);
1483
1484 /* First clear all bits that are beyond the type's precision in case
1485 we've been sign extended. */
1486
1487 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
1488 ;
1489 else if (prec > HOST_BITS_PER_WIDE_INT)
1490 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
1491 else
1492 {
1493 high = 0;
1494 if (prec < HOST_BITS_PER_WIDE_INT)
1495 low &= ~((HOST_WIDE_INT) (-1) << prec);
1496 }
1497
1498 if (high == 0 && low == 0)
1499 return 0;
1500
1501 return ((high == 0 && (low & (low - 1)) == 0)
1502 || (low == 0 && (high & (high - 1)) == 0));
1503 }
1504
1505 /* Return 1 if EXPR is an integer constant other than zero or a
1506 complex constant other than zero. */
1507
1508 int
1509 integer_nonzerop (const_tree expr)
1510 {
1511 STRIP_NOPS (expr);
1512
1513 return ((TREE_CODE (expr) == INTEGER_CST
1514 && (TREE_INT_CST_LOW (expr) != 0
1515 || TREE_INT_CST_HIGH (expr) != 0))
1516 || (TREE_CODE (expr) == COMPLEX_CST
1517 && (integer_nonzerop (TREE_REALPART (expr))
1518 || integer_nonzerop (TREE_IMAGPART (expr)))));
1519 }
1520
1521 /* Return 1 if EXPR is the fixed-point constant zero. */
1522
1523 int
1524 fixed_zerop (const_tree expr)
1525 {
1526 return (TREE_CODE (expr) == FIXED_CST
1527 && double_int_zero_p (TREE_FIXED_CST (expr).data));
1528 }
1529
1530 /* Return the power of two represented by a tree node known to be a
1531 power of two. */
1532
1533 int
1534 tree_log2 (const_tree expr)
1535 {
1536 int prec;
1537 HOST_WIDE_INT high, low;
1538
1539 STRIP_NOPS (expr);
1540
1541 if (TREE_CODE (expr) == COMPLEX_CST)
1542 return tree_log2 (TREE_REALPART (expr));
1543
1544 prec = (POINTER_TYPE_P (TREE_TYPE (expr))
1545 ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
1546
1547 high = TREE_INT_CST_HIGH (expr);
1548 low = TREE_INT_CST_LOW (expr);
1549
1550 /* First clear all bits that are beyond the type's precision in case
1551 we've been sign extended. */
1552
1553 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
1554 ;
1555 else if (prec > HOST_BITS_PER_WIDE_INT)
1556 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
1557 else
1558 {
1559 high = 0;
1560 if (prec < HOST_BITS_PER_WIDE_INT)
1561 low &= ~((HOST_WIDE_INT) (-1) << prec);
1562 }
1563
1564 return (high != 0 ? HOST_BITS_PER_WIDE_INT + exact_log2 (high)
1565 : exact_log2 (low));
1566 }
1567
1568 /* Similar, but return the largest integer Y such that 2 ** Y is less
1569 than or equal to EXPR. */
1570
1571 int
1572 tree_floor_log2 (const_tree expr)
1573 {
1574 int prec;
1575 HOST_WIDE_INT high, low;
1576
1577 STRIP_NOPS (expr);
1578
1579 if (TREE_CODE (expr) == COMPLEX_CST)
1580 return tree_log2 (TREE_REALPART (expr));
1581
1582 prec = (POINTER_TYPE_P (TREE_TYPE (expr))
1583 ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
1584
1585 high = TREE_INT_CST_HIGH (expr);
1586 low = TREE_INT_CST_LOW (expr);
1587
1588 /* First clear all bits that are beyond the type's precision in case
1589 we've been sign extended. Ignore if type's precision hasn't been set
1590 since what we are doing is setting it. */
1591
1592 if (prec == 2 * HOST_BITS_PER_WIDE_INT || prec == 0)
1593 ;
1594 else if (prec > HOST_BITS_PER_WIDE_INT)
1595 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
1596 else
1597 {
1598 high = 0;
1599 if (prec < HOST_BITS_PER_WIDE_INT)
1600 low &= ~((HOST_WIDE_INT) (-1) << prec);
1601 }
1602
1603 return (high != 0 ? HOST_BITS_PER_WIDE_INT + floor_log2 (high)
1604 : floor_log2 (low));
1605 }
1606
1607 /* Return 1 if EXPR is the real constant zero. */
1608
1609 int
1610 real_zerop (const_tree expr)
1611 {
1612 STRIP_NOPS (expr);
1613
1614 return ((TREE_CODE (expr) == REAL_CST
1615 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0))
1616 || (TREE_CODE (expr) == COMPLEX_CST
1617 && real_zerop (TREE_REALPART (expr))
1618 && real_zerop (TREE_IMAGPART (expr))));
1619 }
1620
1621 /* Return 1 if EXPR is the real constant one in real or complex form. */
1622
1623 int
1624 real_onep (const_tree expr)
1625 {
1626 STRIP_NOPS (expr);
1627
1628 return ((TREE_CODE (expr) == REAL_CST
1629 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1))
1630 || (TREE_CODE (expr) == COMPLEX_CST
1631 && real_onep (TREE_REALPART (expr))
1632 && real_zerop (TREE_IMAGPART (expr))));
1633 }
1634
1635 /* Return 1 if EXPR is the real constant two. */
1636
1637 int
1638 real_twop (const_tree expr)
1639 {
1640 STRIP_NOPS (expr);
1641
1642 return ((TREE_CODE (expr) == REAL_CST
1643 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst2))
1644 || (TREE_CODE (expr) == COMPLEX_CST
1645 && real_twop (TREE_REALPART (expr))
1646 && real_zerop (TREE_IMAGPART (expr))));
1647 }
1648
1649 /* Return 1 if EXPR is the real constant minus one. */
1650
1651 int
1652 real_minus_onep (const_tree expr)
1653 {
1654 STRIP_NOPS (expr);
1655
1656 return ((TREE_CODE (expr) == REAL_CST
1657 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconstm1))
1658 || (TREE_CODE (expr) == COMPLEX_CST
1659 && real_minus_onep (TREE_REALPART (expr))
1660 && real_zerop (TREE_IMAGPART (expr))));
1661 }
1662
1663 /* Nonzero if EXP is a constant or a cast of a constant. */
1664
1665 int
1666 really_constant_p (const_tree exp)
1667 {
1668 /* This is not quite the same as STRIP_NOPS. It does more. */
1669 while (CONVERT_EXPR_P (exp)
1670 || TREE_CODE (exp) == NON_LVALUE_EXPR)
1671 exp = TREE_OPERAND (exp, 0);
1672 return TREE_CONSTANT (exp);
1673 }
1674 \f
1675 /* Return first list element whose TREE_VALUE is ELEM.
1676 Return 0 if ELEM is not in LIST. */
1677
1678 tree
1679 value_member (tree elem, tree list)
1680 {
1681 while (list)
1682 {
1683 if (elem == TREE_VALUE (list))
1684 return list;
1685 list = TREE_CHAIN (list);
1686 }
1687 return NULL_TREE;
1688 }
1689
1690 /* Return first list element whose TREE_PURPOSE is ELEM.
1691 Return 0 if ELEM is not in LIST. */
1692
1693 tree
1694 purpose_member (const_tree elem, tree list)
1695 {
1696 while (list)
1697 {
1698 if (elem == TREE_PURPOSE (list))
1699 return list;
1700 list = TREE_CHAIN (list);
1701 }
1702 return NULL_TREE;
1703 }
1704
1705 /* Return nonzero if ELEM is part of the chain CHAIN. */
1706
1707 int
1708 chain_member (const_tree elem, const_tree chain)
1709 {
1710 while (chain)
1711 {
1712 if (elem == chain)
1713 return 1;
1714 chain = TREE_CHAIN (chain);
1715 }
1716
1717 return 0;
1718 }
1719
1720 /* Return the length of a chain of nodes chained through TREE_CHAIN.
1721 We expect a null pointer to mark the end of the chain.
1722 This is the Lisp primitive `length'. */
1723
1724 int
1725 list_length (const_tree t)
1726 {
1727 const_tree p = t;
1728 #ifdef ENABLE_TREE_CHECKING
1729 const_tree q = t;
1730 #endif
1731 int len = 0;
1732
1733 while (p)
1734 {
1735 p = TREE_CHAIN (p);
1736 #ifdef ENABLE_TREE_CHECKING
1737 if (len % 2)
1738 q = TREE_CHAIN (q);
1739 gcc_assert (p != q);
1740 #endif
1741 len++;
1742 }
1743
1744 return len;
1745 }
1746
1747 /* Returns the number of FIELD_DECLs in TYPE. */
1748
1749 int
1750 fields_length (const_tree type)
1751 {
1752 tree t = TYPE_FIELDS (type);
1753 int count = 0;
1754
1755 for (; t; t = TREE_CHAIN (t))
1756 if (TREE_CODE (t) == FIELD_DECL)
1757 ++count;
1758
1759 return count;
1760 }
1761
1762 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
1763 by modifying the last node in chain 1 to point to chain 2.
1764 This is the Lisp primitive `nconc'. */
1765
1766 tree
1767 chainon (tree op1, tree op2)
1768 {
1769 tree t1;
1770
1771 if (!op1)
1772 return op2;
1773 if (!op2)
1774 return op1;
1775
1776 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
1777 continue;
1778 TREE_CHAIN (t1) = op2;
1779
1780 #ifdef ENABLE_TREE_CHECKING
1781 {
1782 tree t2;
1783 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
1784 gcc_assert (t2 != t1);
1785 }
1786 #endif
1787
1788 return op1;
1789 }
1790
1791 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
1792
1793 tree
1794 tree_last (tree chain)
1795 {
1796 tree next;
1797 if (chain)
1798 while ((next = TREE_CHAIN (chain)))
1799 chain = next;
1800 return chain;
1801 }
1802
1803 /* Reverse the order of elements in the chain T,
1804 and return the new head of the chain (old last element). */
1805
1806 tree
1807 nreverse (tree t)
1808 {
1809 tree prev = 0, decl, next;
1810 for (decl = t; decl; decl = next)
1811 {
1812 next = TREE_CHAIN (decl);
1813 TREE_CHAIN (decl) = prev;
1814 prev = decl;
1815 }
1816 return prev;
1817 }
1818 \f
1819 /* Return a newly created TREE_LIST node whose
1820 purpose and value fields are PARM and VALUE. */
1821
1822 tree
1823 build_tree_list_stat (tree parm, tree value MEM_STAT_DECL)
1824 {
1825 tree t = make_node_stat (TREE_LIST PASS_MEM_STAT);
1826 TREE_PURPOSE (t) = parm;
1827 TREE_VALUE (t) = value;
1828 return t;
1829 }
1830
1831 /* Return a newly created TREE_LIST node whose
1832 purpose and value fields are PURPOSE and VALUE
1833 and whose TREE_CHAIN is CHAIN. */
1834
1835 tree
1836 tree_cons_stat (tree purpose, tree value, tree chain MEM_STAT_DECL)
1837 {
1838 tree node;
1839
1840 node = (tree) ggc_alloc_zone_pass_stat (sizeof (struct tree_list), &tree_zone);
1841
1842 memset (node, 0, sizeof (struct tree_common));
1843
1844 #ifdef GATHER_STATISTICS
1845 tree_node_counts[(int) x_kind]++;
1846 tree_node_sizes[(int) x_kind] += sizeof (struct tree_list);
1847 #endif
1848
1849 TREE_SET_CODE (node, TREE_LIST);
1850 TREE_CHAIN (node) = chain;
1851 TREE_PURPOSE (node) = purpose;
1852 TREE_VALUE (node) = value;
1853 return node;
1854 }
1855
1856 /* Return the elements of a CONSTRUCTOR as a TREE_LIST. */
1857
1858 tree
1859 ctor_to_list (tree ctor)
1860 {
1861 tree list = NULL_TREE;
1862 tree *p = &list;
1863 unsigned ix;
1864 tree purpose, val;
1865
1866 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), ix, purpose, val)
1867 {
1868 *p = build_tree_list (purpose, val);
1869 p = &TREE_CHAIN (*p);
1870 }
1871
1872 return list;
1873 }
1874 \f
1875 /* Return the size nominally occupied by an object of type TYPE
1876 when it resides in memory. The value is measured in units of bytes,
1877 and its data type is that normally used for type sizes
1878 (which is the first type created by make_signed_type or
1879 make_unsigned_type). */
1880
1881 tree
1882 size_in_bytes (const_tree type)
1883 {
1884 tree t;
1885
1886 if (type == error_mark_node)
1887 return integer_zero_node;
1888
1889 type = TYPE_MAIN_VARIANT (type);
1890 t = TYPE_SIZE_UNIT (type);
1891
1892 if (t == 0)
1893 {
1894 lang_hooks.types.incomplete_type_error (NULL_TREE, type);
1895 return size_zero_node;
1896 }
1897
1898 return t;
1899 }
1900
1901 /* Return the size of TYPE (in bytes) as a wide integer
1902 or return -1 if the size can vary or is larger than an integer. */
1903
1904 HOST_WIDE_INT
1905 int_size_in_bytes (const_tree type)
1906 {
1907 tree t;
1908
1909 if (type == error_mark_node)
1910 return 0;
1911
1912 type = TYPE_MAIN_VARIANT (type);
1913 t = TYPE_SIZE_UNIT (type);
1914 if (t == 0
1915 || TREE_CODE (t) != INTEGER_CST
1916 || TREE_INT_CST_HIGH (t) != 0
1917 /* If the result would appear negative, it's too big to represent. */
1918 || (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0)
1919 return -1;
1920
1921 return TREE_INT_CST_LOW (t);
1922 }
1923
1924 /* Return the maximum size of TYPE (in bytes) as a wide integer
1925 or return -1 if the size can vary or is larger than an integer. */
1926
1927 HOST_WIDE_INT
1928 max_int_size_in_bytes (const_tree type)
1929 {
1930 HOST_WIDE_INT size = -1;
1931 tree size_tree;
1932
1933 /* If this is an array type, check for a possible MAX_SIZE attached. */
1934
1935 if (TREE_CODE (type) == ARRAY_TYPE)
1936 {
1937 size_tree = TYPE_ARRAY_MAX_SIZE (type);
1938
1939 if (size_tree && host_integerp (size_tree, 1))
1940 size = tree_low_cst (size_tree, 1);
1941 }
1942
1943 /* If we still haven't been able to get a size, see if the language
1944 can compute a maximum size. */
1945
1946 if (size == -1)
1947 {
1948 size_tree = lang_hooks.types.max_size (type);
1949
1950 if (size_tree && host_integerp (size_tree, 1))
1951 size = tree_low_cst (size_tree, 1);
1952 }
1953
1954 return size;
1955 }
1956 \f
1957 /* Return the bit position of FIELD, in bits from the start of the record.
1958 This is a tree of type bitsizetype. */
1959
1960 tree
1961 bit_position (const_tree field)
1962 {
1963 return bit_from_pos (DECL_FIELD_OFFSET (field),
1964 DECL_FIELD_BIT_OFFSET (field));
1965 }
1966
1967 /* Likewise, but return as an integer. It must be representable in
1968 that way (since it could be a signed value, we don't have the
1969 option of returning -1 like int_size_in_byte can. */
1970
1971 HOST_WIDE_INT
1972 int_bit_position (const_tree field)
1973 {
1974 return tree_low_cst (bit_position (field), 0);
1975 }
1976 \f
1977 /* Return the byte position of FIELD, in bytes from the start of the record.
1978 This is a tree of type sizetype. */
1979
1980 tree
1981 byte_position (const_tree field)
1982 {
1983 return byte_from_pos (DECL_FIELD_OFFSET (field),
1984 DECL_FIELD_BIT_OFFSET (field));
1985 }
1986
1987 /* Likewise, but return as an integer. It must be representable in
1988 that way (since it could be a signed value, we don't have the
1989 option of returning -1 like int_size_in_byte can. */
1990
1991 HOST_WIDE_INT
1992 int_byte_position (const_tree field)
1993 {
1994 return tree_low_cst (byte_position (field), 0);
1995 }
1996 \f
1997 /* Return the strictest alignment, in bits, that T is known to have. */
1998
1999 unsigned int
2000 expr_align (const_tree t)
2001 {
2002 unsigned int align0, align1;
2003
2004 switch (TREE_CODE (t))
2005 {
2006 CASE_CONVERT: case NON_LVALUE_EXPR:
2007 /* If we have conversions, we know that the alignment of the
2008 object must meet each of the alignments of the types. */
2009 align0 = expr_align (TREE_OPERAND (t, 0));
2010 align1 = TYPE_ALIGN (TREE_TYPE (t));
2011 return MAX (align0, align1);
2012
2013 case SAVE_EXPR: case COMPOUND_EXPR: case MODIFY_EXPR:
2014 case INIT_EXPR: case TARGET_EXPR: case WITH_CLEANUP_EXPR:
2015 case CLEANUP_POINT_EXPR:
2016 /* These don't change the alignment of an object. */
2017 return expr_align (TREE_OPERAND (t, 0));
2018
2019 case COND_EXPR:
2020 /* The best we can do is say that the alignment is the least aligned
2021 of the two arms. */
2022 align0 = expr_align (TREE_OPERAND (t, 1));
2023 align1 = expr_align (TREE_OPERAND (t, 2));
2024 return MIN (align0, align1);
2025
2026 /* FIXME: LABEL_DECL and CONST_DECL never have DECL_ALIGN set
2027 meaningfully, it's always 1. */
2028 case LABEL_DECL: case CONST_DECL:
2029 case VAR_DECL: case PARM_DECL: case RESULT_DECL:
2030 case FUNCTION_DECL:
2031 gcc_assert (DECL_ALIGN (t) != 0);
2032 return DECL_ALIGN (t);
2033
2034 default:
2035 break;
2036 }
2037
2038 /* Otherwise take the alignment from that of the type. */
2039 return TYPE_ALIGN (TREE_TYPE (t));
2040 }
2041 \f
2042 /* Return, as a tree node, the number of elements for TYPE (which is an
2043 ARRAY_TYPE) minus one. This counts only elements of the top array. */
2044
2045 tree
2046 array_type_nelts (const_tree type)
2047 {
2048 tree index_type, min, max;
2049
2050 /* If they did it with unspecified bounds, then we should have already
2051 given an error about it before we got here. */
2052 if (! TYPE_DOMAIN (type))
2053 return error_mark_node;
2054
2055 index_type = TYPE_DOMAIN (type);
2056 min = TYPE_MIN_VALUE (index_type);
2057 max = TYPE_MAX_VALUE (index_type);
2058
2059 return (integer_zerop (min)
2060 ? max
2061 : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
2062 }
2063 \f
2064 /* If arg is static -- a reference to an object in static storage -- then
2065 return the object. This is not the same as the C meaning of `static'.
2066 If arg isn't static, return NULL. */
2067
2068 tree
2069 staticp (tree arg)
2070 {
2071 switch (TREE_CODE (arg))
2072 {
2073 case FUNCTION_DECL:
2074 /* Nested functions are static, even though taking their address will
2075 involve a trampoline as we unnest the nested function and create
2076 the trampoline on the tree level. */
2077 return arg;
2078
2079 case VAR_DECL:
2080 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2081 && ! DECL_THREAD_LOCAL_P (arg)
2082 && ! DECL_DLLIMPORT_P (arg)
2083 ? arg : NULL);
2084
2085 case CONST_DECL:
2086 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2087 ? arg : NULL);
2088
2089 case CONSTRUCTOR:
2090 return TREE_STATIC (arg) ? arg : NULL;
2091
2092 case LABEL_DECL:
2093 case STRING_CST:
2094 return arg;
2095
2096 case COMPONENT_REF:
2097 /* If the thing being referenced is not a field, then it is
2098 something language specific. */
2099 if (TREE_CODE (TREE_OPERAND (arg, 1)) != FIELD_DECL)
2100 return (*lang_hooks.staticp) (arg);
2101
2102 /* If we are referencing a bitfield, we can't evaluate an
2103 ADDR_EXPR at compile time and so it isn't a constant. */
2104 if (DECL_BIT_FIELD (TREE_OPERAND (arg, 1)))
2105 return NULL;
2106
2107 return staticp (TREE_OPERAND (arg, 0));
2108
2109 case BIT_FIELD_REF:
2110 return NULL;
2111
2112 case MISALIGNED_INDIRECT_REF:
2113 case ALIGN_INDIRECT_REF:
2114 case INDIRECT_REF:
2115 return TREE_CONSTANT (TREE_OPERAND (arg, 0)) ? arg : NULL;
2116
2117 case ARRAY_REF:
2118 case ARRAY_RANGE_REF:
2119 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
2120 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
2121 return staticp (TREE_OPERAND (arg, 0));
2122 else
2123 return false;
2124
2125 default:
2126 if ((unsigned int) TREE_CODE (arg)
2127 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
2128 return lang_hooks.staticp (arg);
2129 else
2130 return NULL;
2131 }
2132 }
2133
2134 \f
2135
2136
2137 /* Return whether OP is a DECL whose address is function-invariant. */
2138
2139 bool
2140 decl_address_invariant_p (const_tree op)
2141 {
2142 /* The conditions below are slightly less strict than the one in
2143 staticp. */
2144
2145 switch (TREE_CODE (op))
2146 {
2147 case PARM_DECL:
2148 case RESULT_DECL:
2149 case LABEL_DECL:
2150 case FUNCTION_DECL:
2151 return true;
2152
2153 case VAR_DECL:
2154 if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
2155 && !DECL_DLLIMPORT_P (op))
2156 || DECL_THREAD_LOCAL_P (op)
2157 || DECL_CONTEXT (op) == current_function_decl
2158 || decl_function_context (op) == current_function_decl)
2159 return true;
2160 break;
2161
2162 case CONST_DECL:
2163 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
2164 || decl_function_context (op) == current_function_decl)
2165 return true;
2166 break;
2167
2168 default:
2169 break;
2170 }
2171
2172 return false;
2173 }
2174
2175 /* Return whether OP is a DECL whose address is interprocedural-invariant. */
2176
2177 bool
2178 decl_address_ip_invariant_p (const_tree op)
2179 {
2180 /* The conditions below are slightly less strict than the one in
2181 staticp. */
2182
2183 switch (TREE_CODE (op))
2184 {
2185 case LABEL_DECL:
2186 case FUNCTION_DECL:
2187 case STRING_CST:
2188 return true;
2189
2190 case VAR_DECL:
2191 if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
2192 && !DECL_DLLIMPORT_P (op))
2193 || DECL_THREAD_LOCAL_P (op))
2194 return true;
2195 break;
2196
2197 case CONST_DECL:
2198 if ((TREE_STATIC (op) || DECL_EXTERNAL (op)))
2199 return true;
2200 break;
2201
2202 default:
2203 break;
2204 }
2205
2206 return false;
2207 }
2208
2209
2210 /* Return true if T is function-invariant (internal function, does
2211 not handle arithmetic; that's handled in skip_simple_arithmetic and
2212 tree_invariant_p). */
2213
2214 static bool tree_invariant_p (tree t);
2215
2216 static bool
2217 tree_invariant_p_1 (tree t)
2218 {
2219 tree op;
2220
2221 if (TREE_CONSTANT (t)
2222 || (TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t)))
2223 return true;
2224
2225 switch (TREE_CODE (t))
2226 {
2227 case SAVE_EXPR:
2228 return true;
2229
2230 case ADDR_EXPR:
2231 op = TREE_OPERAND (t, 0);
2232 while (handled_component_p (op))
2233 {
2234 switch (TREE_CODE (op))
2235 {
2236 case ARRAY_REF:
2237 case ARRAY_RANGE_REF:
2238 if (!tree_invariant_p (TREE_OPERAND (op, 1))
2239 || TREE_OPERAND (op, 2) != NULL_TREE
2240 || TREE_OPERAND (op, 3) != NULL_TREE)
2241 return false;
2242 break;
2243
2244 case COMPONENT_REF:
2245 if (TREE_OPERAND (op, 2) != NULL_TREE)
2246 return false;
2247 break;
2248
2249 default:;
2250 }
2251 op = TREE_OPERAND (op, 0);
2252 }
2253
2254 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
2255
2256 default:
2257 break;
2258 }
2259
2260 return false;
2261 }
2262
2263 /* Return true if T is function-invariant. */
2264
2265 static bool
2266 tree_invariant_p (tree t)
2267 {
2268 tree inner = skip_simple_arithmetic (t);
2269 return tree_invariant_p_1 (inner);
2270 }
2271
2272 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
2273 Do this to any expression which may be used in more than one place,
2274 but must be evaluated only once.
2275
2276 Normally, expand_expr would reevaluate the expression each time.
2277 Calling save_expr produces something that is evaluated and recorded
2278 the first time expand_expr is called on it. Subsequent calls to
2279 expand_expr just reuse the recorded value.
2280
2281 The call to expand_expr that generates code that actually computes
2282 the value is the first call *at compile time*. Subsequent calls
2283 *at compile time* generate code to use the saved value.
2284 This produces correct result provided that *at run time* control
2285 always flows through the insns made by the first expand_expr
2286 before reaching the other places where the save_expr was evaluated.
2287 You, the caller of save_expr, must make sure this is so.
2288
2289 Constants, and certain read-only nodes, are returned with no
2290 SAVE_EXPR because that is safe. Expressions containing placeholders
2291 are not touched; see tree.def for an explanation of what these
2292 are used for. */
2293
2294 tree
2295 save_expr (tree expr)
2296 {
2297 tree t = fold (expr);
2298 tree inner;
2299
2300 /* If the tree evaluates to a constant, then we don't want to hide that
2301 fact (i.e. this allows further folding, and direct checks for constants).
2302 However, a read-only object that has side effects cannot be bypassed.
2303 Since it is no problem to reevaluate literals, we just return the
2304 literal node. */
2305 inner = skip_simple_arithmetic (t);
2306 if (TREE_CODE (inner) == ERROR_MARK)
2307 return inner;
2308
2309 if (tree_invariant_p_1 (inner))
2310 return t;
2311
2312 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
2313 it means that the size or offset of some field of an object depends on
2314 the value within another field.
2315
2316 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
2317 and some variable since it would then need to be both evaluated once and
2318 evaluated more than once. Front-ends must assure this case cannot
2319 happen by surrounding any such subexpressions in their own SAVE_EXPR
2320 and forcing evaluation at the proper time. */
2321 if (contains_placeholder_p (inner))
2322 return t;
2323
2324 t = build1 (SAVE_EXPR, TREE_TYPE (expr), t);
2325
2326 /* This expression might be placed ahead of a jump to ensure that the
2327 value was computed on both sides of the jump. So make sure it isn't
2328 eliminated as dead. */
2329 TREE_SIDE_EFFECTS (t) = 1;
2330 return t;
2331 }
2332
2333 /* Look inside EXPR and into any simple arithmetic operations. Return
2334 the innermost non-arithmetic node. */
2335
2336 tree
2337 skip_simple_arithmetic (tree expr)
2338 {
2339 tree inner;
2340
2341 /* We don't care about whether this can be used as an lvalue in this
2342 context. */
2343 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
2344 expr = TREE_OPERAND (expr, 0);
2345
2346 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
2347 a constant, it will be more efficient to not make another SAVE_EXPR since
2348 it will allow better simplification and GCSE will be able to merge the
2349 computations if they actually occur. */
2350 inner = expr;
2351 while (1)
2352 {
2353 if (UNARY_CLASS_P (inner))
2354 inner = TREE_OPERAND (inner, 0);
2355 else if (BINARY_CLASS_P (inner))
2356 {
2357 if (tree_invariant_p (TREE_OPERAND (inner, 1)))
2358 inner = TREE_OPERAND (inner, 0);
2359 else if (tree_invariant_p (TREE_OPERAND (inner, 0)))
2360 inner = TREE_OPERAND (inner, 1);
2361 else
2362 break;
2363 }
2364 else
2365 break;
2366 }
2367
2368 return inner;
2369 }
2370
2371 /* Return which tree structure is used by T. */
2372
2373 enum tree_node_structure_enum
2374 tree_node_structure (const_tree t)
2375 {
2376 const enum tree_code code = TREE_CODE (t);
2377
2378 switch (TREE_CODE_CLASS (code))
2379 {
2380 case tcc_declaration:
2381 {
2382 switch (code)
2383 {
2384 case FIELD_DECL:
2385 return TS_FIELD_DECL;
2386 case PARM_DECL:
2387 return TS_PARM_DECL;
2388 case VAR_DECL:
2389 return TS_VAR_DECL;
2390 case LABEL_DECL:
2391 return TS_LABEL_DECL;
2392 case RESULT_DECL:
2393 return TS_RESULT_DECL;
2394 case CONST_DECL:
2395 return TS_CONST_DECL;
2396 case TYPE_DECL:
2397 return TS_TYPE_DECL;
2398 case FUNCTION_DECL:
2399 return TS_FUNCTION_DECL;
2400 case SYMBOL_MEMORY_TAG:
2401 case NAME_MEMORY_TAG:
2402 case MEMORY_PARTITION_TAG:
2403 return TS_MEMORY_TAG;
2404 default:
2405 return TS_DECL_NON_COMMON;
2406 }
2407 }
2408 case tcc_type:
2409 return TS_TYPE;
2410 case tcc_reference:
2411 case tcc_comparison:
2412 case tcc_unary:
2413 case tcc_binary:
2414 case tcc_expression:
2415 case tcc_statement:
2416 case tcc_vl_exp:
2417 return TS_EXP;
2418 default: /* tcc_constant and tcc_exceptional */
2419 break;
2420 }
2421 switch (code)
2422 {
2423 /* tcc_constant cases. */
2424 case INTEGER_CST: return TS_INT_CST;
2425 case REAL_CST: return TS_REAL_CST;
2426 case FIXED_CST: return TS_FIXED_CST;
2427 case COMPLEX_CST: return TS_COMPLEX;
2428 case VECTOR_CST: return TS_VECTOR;
2429 case STRING_CST: return TS_STRING;
2430 /* tcc_exceptional cases. */
2431 case ERROR_MARK: return TS_COMMON;
2432 case IDENTIFIER_NODE: return TS_IDENTIFIER;
2433 case TREE_LIST: return TS_LIST;
2434 case TREE_VEC: return TS_VEC;
2435 case SSA_NAME: return TS_SSA_NAME;
2436 case PLACEHOLDER_EXPR: return TS_COMMON;
2437 case STATEMENT_LIST: return TS_STATEMENT_LIST;
2438 case BLOCK: return TS_BLOCK;
2439 case CONSTRUCTOR: return TS_CONSTRUCTOR;
2440 case TREE_BINFO: return TS_BINFO;
2441 case OMP_CLAUSE: return TS_OMP_CLAUSE;
2442 case OPTIMIZATION_NODE: return TS_OPTIMIZATION;
2443 case TARGET_OPTION_NODE: return TS_TARGET_OPTION;
2444
2445 default:
2446 gcc_unreachable ();
2447 }
2448 }
2449 \f
2450 /* Return 1 if EXP contains a PLACEHOLDER_EXPR; i.e., if it represents a size
2451 or offset that depends on a field within a record. */
2452
2453 bool
2454 contains_placeholder_p (const_tree exp)
2455 {
2456 enum tree_code code;
2457
2458 if (!exp)
2459 return 0;
2460
2461 code = TREE_CODE (exp);
2462 if (code == PLACEHOLDER_EXPR)
2463 return 1;
2464
2465 switch (TREE_CODE_CLASS (code))
2466 {
2467 case tcc_reference:
2468 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
2469 position computations since they will be converted into a
2470 WITH_RECORD_EXPR involving the reference, which will assume
2471 here will be valid. */
2472 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
2473
2474 case tcc_exceptional:
2475 if (code == TREE_LIST)
2476 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
2477 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
2478 break;
2479
2480 case tcc_unary:
2481 case tcc_binary:
2482 case tcc_comparison:
2483 case tcc_expression:
2484 switch (code)
2485 {
2486 case COMPOUND_EXPR:
2487 /* Ignoring the first operand isn't quite right, but works best. */
2488 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
2489
2490 case COND_EXPR:
2491 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
2492 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
2493 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
2494
2495 case SAVE_EXPR:
2496 /* The save_expr function never wraps anything containing
2497 a PLACEHOLDER_EXPR. */
2498 return 0;
2499
2500 default:
2501 break;
2502 }
2503
2504 switch (TREE_CODE_LENGTH (code))
2505 {
2506 case 1:
2507 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
2508 case 2:
2509 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
2510 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
2511 default:
2512 return 0;
2513 }
2514
2515 case tcc_vl_exp:
2516 switch (code)
2517 {
2518 case CALL_EXPR:
2519 {
2520 const_tree arg;
2521 const_call_expr_arg_iterator iter;
2522 FOR_EACH_CONST_CALL_EXPR_ARG (arg, iter, exp)
2523 if (CONTAINS_PLACEHOLDER_P (arg))
2524 return 1;
2525 return 0;
2526 }
2527 default:
2528 return 0;
2529 }
2530
2531 default:
2532 return 0;
2533 }
2534 return 0;
2535 }
2536
2537 /* Return true if any part of the computation of TYPE involves a
2538 PLACEHOLDER_EXPR. This includes size, bounds, qualifiers
2539 (for QUAL_UNION_TYPE) and field positions. */
2540
2541 static bool
2542 type_contains_placeholder_1 (const_tree type)
2543 {
2544 /* If the size contains a placeholder or the parent type (component type in
2545 the case of arrays) type involves a placeholder, this type does. */
2546 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
2547 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
2548 || (TREE_TYPE (type) != 0
2549 && type_contains_placeholder_p (TREE_TYPE (type))))
2550 return true;
2551
2552 /* Now do type-specific checks. Note that the last part of the check above
2553 greatly limits what we have to do below. */
2554 switch (TREE_CODE (type))
2555 {
2556 case VOID_TYPE:
2557 case COMPLEX_TYPE:
2558 case ENUMERAL_TYPE:
2559 case BOOLEAN_TYPE:
2560 case POINTER_TYPE:
2561 case OFFSET_TYPE:
2562 case REFERENCE_TYPE:
2563 case METHOD_TYPE:
2564 case FUNCTION_TYPE:
2565 case VECTOR_TYPE:
2566 return false;
2567
2568 case INTEGER_TYPE:
2569 case REAL_TYPE:
2570 case FIXED_POINT_TYPE:
2571 /* Here we just check the bounds. */
2572 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
2573 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
2574
2575 case ARRAY_TYPE:
2576 /* We're already checked the component type (TREE_TYPE), so just check
2577 the index type. */
2578 return type_contains_placeholder_p (TYPE_DOMAIN (type));
2579
2580 case RECORD_TYPE:
2581 case UNION_TYPE:
2582 case QUAL_UNION_TYPE:
2583 {
2584 tree field;
2585
2586 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2587 if (TREE_CODE (field) == FIELD_DECL
2588 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
2589 || (TREE_CODE (type) == QUAL_UNION_TYPE
2590 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
2591 || type_contains_placeholder_p (TREE_TYPE (field))))
2592 return true;
2593
2594 return false;
2595 }
2596
2597 default:
2598 gcc_unreachable ();
2599 }
2600 }
2601
2602 bool
2603 type_contains_placeholder_p (tree type)
2604 {
2605 bool result;
2606
2607 /* If the contains_placeholder_bits field has been initialized,
2608 then we know the answer. */
2609 if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
2610 return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
2611
2612 /* Indicate that we've seen this type node, and the answer is false.
2613 This is what we want to return if we run into recursion via fields. */
2614 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
2615
2616 /* Compute the real value. */
2617 result = type_contains_placeholder_1 (type);
2618
2619 /* Store the real value. */
2620 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
2621
2622 return result;
2623 }
2624 \f
2625 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
2626 return a tree with all occurrences of references to F in a
2627 PLACEHOLDER_EXPR replaced by R. Note that we assume here that EXP
2628 contains only arithmetic expressions or a CALL_EXPR with a
2629 PLACEHOLDER_EXPR occurring only in its arglist. */
2630
2631 tree
2632 substitute_in_expr (tree exp, tree f, tree r)
2633 {
2634 enum tree_code code = TREE_CODE (exp);
2635 tree op0, op1, op2, op3;
2636 tree new_tree, inner;
2637
2638 /* We handle TREE_LIST and COMPONENT_REF separately. */
2639 if (code == TREE_LIST)
2640 {
2641 op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
2642 op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
2643 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
2644 return exp;
2645
2646 return tree_cons (TREE_PURPOSE (exp), op1, op0);
2647 }
2648 else if (code == COMPONENT_REF)
2649 {
2650 /* If this expression is getting a value from a PLACEHOLDER_EXPR
2651 and it is the right field, replace it with R. */
2652 for (inner = TREE_OPERAND (exp, 0);
2653 REFERENCE_CLASS_P (inner);
2654 inner = TREE_OPERAND (inner, 0))
2655 ;
2656 if (TREE_CODE (inner) == PLACEHOLDER_EXPR
2657 && TREE_OPERAND (exp, 1) == f)
2658 return r;
2659
2660 /* If this expression hasn't been completed let, leave it alone. */
2661 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && TREE_TYPE (inner) == 0)
2662 return exp;
2663
2664 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2665 if (op0 == TREE_OPERAND (exp, 0))
2666 return exp;
2667
2668 new_tree = fold_build3 (COMPONENT_REF, TREE_TYPE (exp),
2669 op0, TREE_OPERAND (exp, 1), NULL_TREE);
2670 }
2671 else
2672 switch (TREE_CODE_CLASS (code))
2673 {
2674 case tcc_constant:
2675 case tcc_declaration:
2676 return exp;
2677
2678 case tcc_exceptional:
2679 case tcc_unary:
2680 case tcc_binary:
2681 case tcc_comparison:
2682 case tcc_expression:
2683 case tcc_reference:
2684 switch (TREE_CODE_LENGTH (code))
2685 {
2686 case 0:
2687 return exp;
2688
2689 case 1:
2690 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2691 if (op0 == TREE_OPERAND (exp, 0))
2692 return exp;
2693
2694 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
2695 break;
2696
2697 case 2:
2698 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2699 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
2700
2701 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
2702 return exp;
2703
2704 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
2705 break;
2706
2707 case 3:
2708 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2709 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
2710 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
2711
2712 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2713 && op2 == TREE_OPERAND (exp, 2))
2714 return exp;
2715
2716 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
2717 break;
2718
2719 case 4:
2720 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2721 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
2722 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
2723 op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
2724
2725 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2726 && op2 == TREE_OPERAND (exp, 2)
2727 && op3 == TREE_OPERAND (exp, 3))
2728 return exp;
2729
2730 new_tree = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
2731 break;
2732
2733 default:
2734 gcc_unreachable ();
2735 }
2736 break;
2737
2738 case tcc_vl_exp:
2739 {
2740 tree copy = NULL_TREE;
2741 int i;
2742
2743 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
2744 {
2745 tree op = TREE_OPERAND (exp, i);
2746 tree new_op = SUBSTITUTE_IN_EXPR (op, f, r);
2747 if (new_op != op)
2748 {
2749 if (!copy)
2750 copy = copy_node (exp);
2751 TREE_OPERAND (copy, i) = new_op;
2752 }
2753 }
2754
2755 if (copy)
2756 new_tree = fold (copy);
2757 else
2758 return exp;
2759 }
2760 break;
2761
2762 default:
2763 gcc_unreachable ();
2764 }
2765
2766 TREE_READONLY (new_tree) = TREE_READONLY (exp);
2767 return new_tree;
2768 }
2769
2770 /* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
2771 for it within OBJ, a tree that is an object or a chain of references. */
2772
2773 tree
2774 substitute_placeholder_in_expr (tree exp, tree obj)
2775 {
2776 enum tree_code code = TREE_CODE (exp);
2777 tree op0, op1, op2, op3;
2778
2779 /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
2780 in the chain of OBJ. */
2781 if (code == PLACEHOLDER_EXPR)
2782 {
2783 tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
2784 tree elt;
2785
2786 for (elt = obj; elt != 0;
2787 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
2788 || TREE_CODE (elt) == COND_EXPR)
2789 ? TREE_OPERAND (elt, 1)
2790 : (REFERENCE_CLASS_P (elt)
2791 || UNARY_CLASS_P (elt)
2792 || BINARY_CLASS_P (elt)
2793 || VL_EXP_CLASS_P (elt)
2794 || EXPRESSION_CLASS_P (elt))
2795 ? TREE_OPERAND (elt, 0) : 0))
2796 if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
2797 return elt;
2798
2799 for (elt = obj; elt != 0;
2800 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
2801 || TREE_CODE (elt) == COND_EXPR)
2802 ? TREE_OPERAND (elt, 1)
2803 : (REFERENCE_CLASS_P (elt)
2804 || UNARY_CLASS_P (elt)
2805 || BINARY_CLASS_P (elt)
2806 || VL_EXP_CLASS_P (elt)
2807 || EXPRESSION_CLASS_P (elt))
2808 ? TREE_OPERAND (elt, 0) : 0))
2809 if (POINTER_TYPE_P (TREE_TYPE (elt))
2810 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
2811 == need_type))
2812 return fold_build1 (INDIRECT_REF, need_type, elt);
2813
2814 /* If we didn't find it, return the original PLACEHOLDER_EXPR. If it
2815 survives until RTL generation, there will be an error. */
2816 return exp;
2817 }
2818
2819 /* TREE_LIST is special because we need to look at TREE_VALUE
2820 and TREE_CHAIN, not TREE_OPERANDS. */
2821 else if (code == TREE_LIST)
2822 {
2823 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
2824 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
2825 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
2826 return exp;
2827
2828 return tree_cons (TREE_PURPOSE (exp), op1, op0);
2829 }
2830 else
2831 switch (TREE_CODE_CLASS (code))
2832 {
2833 case tcc_constant:
2834 case tcc_declaration:
2835 return exp;
2836
2837 case tcc_exceptional:
2838 case tcc_unary:
2839 case tcc_binary:
2840 case tcc_comparison:
2841 case tcc_expression:
2842 case tcc_reference:
2843 case tcc_statement:
2844 switch (TREE_CODE_LENGTH (code))
2845 {
2846 case 0:
2847 return exp;
2848
2849 case 1:
2850 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
2851 if (op0 == TREE_OPERAND (exp, 0))
2852 return exp;
2853 else
2854 return fold_build1 (code, TREE_TYPE (exp), op0);
2855
2856 case 2:
2857 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
2858 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
2859
2860 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
2861 return exp;
2862 else
2863 return fold_build2 (code, TREE_TYPE (exp), op0, op1);
2864
2865 case 3:
2866 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
2867 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
2868 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
2869
2870 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2871 && op2 == TREE_OPERAND (exp, 2))
2872 return exp;
2873 else
2874 return fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
2875
2876 case 4:
2877 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
2878 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
2879 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
2880 op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
2881
2882 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2883 && op2 == TREE_OPERAND (exp, 2)
2884 && op3 == TREE_OPERAND (exp, 3))
2885 return exp;
2886 else
2887 return fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
2888
2889 default:
2890 gcc_unreachable ();
2891 }
2892 break;
2893
2894 case tcc_vl_exp:
2895 {
2896 tree copy = NULL_TREE;
2897 int i;
2898
2899 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
2900 {
2901 tree op = TREE_OPERAND (exp, i);
2902 tree new_op = SUBSTITUTE_PLACEHOLDER_IN_EXPR (op, obj);
2903 if (new_op != op)
2904 {
2905 if (!copy)
2906 copy = copy_node (exp);
2907 TREE_OPERAND (copy, i) = new_op;
2908 }
2909 }
2910
2911 if (copy)
2912 return fold (copy);
2913 else
2914 return exp;
2915 }
2916
2917 default:
2918 gcc_unreachable ();
2919 }
2920 }
2921 \f
2922 /* Stabilize a reference so that we can use it any number of times
2923 without causing its operands to be evaluated more than once.
2924 Returns the stabilized reference. This works by means of save_expr,
2925 so see the caveats in the comments about save_expr.
2926
2927 Also allows conversion expressions whose operands are references.
2928 Any other kind of expression is returned unchanged. */
2929
2930 tree
2931 stabilize_reference (tree ref)
2932 {
2933 tree result;
2934 enum tree_code code = TREE_CODE (ref);
2935
2936 switch (code)
2937 {
2938 case VAR_DECL:
2939 case PARM_DECL:
2940 case RESULT_DECL:
2941 /* No action is needed in this case. */
2942 return ref;
2943
2944 CASE_CONVERT:
2945 case FLOAT_EXPR:
2946 case FIX_TRUNC_EXPR:
2947 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
2948 break;
2949
2950 case INDIRECT_REF:
2951 result = build_nt (INDIRECT_REF,
2952 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
2953 break;
2954
2955 case COMPONENT_REF:
2956 result = build_nt (COMPONENT_REF,
2957 stabilize_reference (TREE_OPERAND (ref, 0)),
2958 TREE_OPERAND (ref, 1), NULL_TREE);
2959 break;
2960
2961 case BIT_FIELD_REF:
2962 result = build_nt (BIT_FIELD_REF,
2963 stabilize_reference (TREE_OPERAND (ref, 0)),
2964 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
2965 stabilize_reference_1 (TREE_OPERAND (ref, 2)));
2966 break;
2967
2968 case ARRAY_REF:
2969 result = build_nt (ARRAY_REF,
2970 stabilize_reference (TREE_OPERAND (ref, 0)),
2971 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
2972 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
2973 break;
2974
2975 case ARRAY_RANGE_REF:
2976 result = build_nt (ARRAY_RANGE_REF,
2977 stabilize_reference (TREE_OPERAND (ref, 0)),
2978 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
2979 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
2980 break;
2981
2982 case COMPOUND_EXPR:
2983 /* We cannot wrap the first expression in a SAVE_EXPR, as then
2984 it wouldn't be ignored. This matters when dealing with
2985 volatiles. */
2986 return stabilize_reference_1 (ref);
2987
2988 /* If arg isn't a kind of lvalue we recognize, make no change.
2989 Caller should recognize the error for an invalid lvalue. */
2990 default:
2991 return ref;
2992
2993 case ERROR_MARK:
2994 return error_mark_node;
2995 }
2996
2997 TREE_TYPE (result) = TREE_TYPE (ref);
2998 TREE_READONLY (result) = TREE_READONLY (ref);
2999 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
3000 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
3001
3002 return result;
3003 }
3004
3005 /* Subroutine of stabilize_reference; this is called for subtrees of
3006 references. Any expression with side-effects must be put in a SAVE_EXPR
3007 to ensure that it is only evaluated once.
3008
3009 We don't put SAVE_EXPR nodes around everything, because assigning very
3010 simple expressions to temporaries causes us to miss good opportunities
3011 for optimizations. Among other things, the opportunity to fold in the
3012 addition of a constant into an addressing mode often gets lost, e.g.
3013 "y[i+1] += x;". In general, we take the approach that we should not make
3014 an assignment unless we are forced into it - i.e., that any non-side effect
3015 operator should be allowed, and that cse should take care of coalescing
3016 multiple utterances of the same expression should that prove fruitful. */
3017
3018 tree
3019 stabilize_reference_1 (tree e)
3020 {
3021 tree result;
3022 enum tree_code code = TREE_CODE (e);
3023
3024 /* We cannot ignore const expressions because it might be a reference
3025 to a const array but whose index contains side-effects. But we can
3026 ignore things that are actual constant or that already have been
3027 handled by this function. */
3028
3029 if (tree_invariant_p (e))
3030 return e;
3031
3032 switch (TREE_CODE_CLASS (code))
3033 {
3034 case tcc_exceptional:
3035 case tcc_type:
3036 case tcc_declaration:
3037 case tcc_comparison:
3038 case tcc_statement:
3039 case tcc_expression:
3040 case tcc_reference:
3041 case tcc_vl_exp:
3042 /* If the expression has side-effects, then encase it in a SAVE_EXPR
3043 so that it will only be evaluated once. */
3044 /* The reference (r) and comparison (<) classes could be handled as
3045 below, but it is generally faster to only evaluate them once. */
3046 if (TREE_SIDE_EFFECTS (e))
3047 return save_expr (e);
3048 return e;
3049
3050 case tcc_constant:
3051 /* Constants need no processing. In fact, we should never reach
3052 here. */
3053 return e;
3054
3055 case tcc_binary:
3056 /* Division is slow and tends to be compiled with jumps,
3057 especially the division by powers of 2 that is often
3058 found inside of an array reference. So do it just once. */
3059 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
3060 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
3061 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
3062 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
3063 return save_expr (e);
3064 /* Recursively stabilize each operand. */
3065 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
3066 stabilize_reference_1 (TREE_OPERAND (e, 1)));
3067 break;
3068
3069 case tcc_unary:
3070 /* Recursively stabilize each operand. */
3071 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
3072 break;
3073
3074 default:
3075 gcc_unreachable ();
3076 }
3077
3078 TREE_TYPE (result) = TREE_TYPE (e);
3079 TREE_READONLY (result) = TREE_READONLY (e);
3080 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
3081 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
3082
3083 return result;
3084 }
3085 \f
3086 /* Low-level constructors for expressions. */
3087
3088 /* A helper function for build1 and constant folders. Set TREE_CONSTANT,
3089 and TREE_SIDE_EFFECTS for an ADDR_EXPR. */
3090
3091 void
3092 recompute_tree_invariant_for_addr_expr (tree t)
3093 {
3094 tree node;
3095 bool tc = true, se = false;
3096
3097 /* We started out assuming this address is both invariant and constant, but
3098 does not have side effects. Now go down any handled components and see if
3099 any of them involve offsets that are either non-constant or non-invariant.
3100 Also check for side-effects.
3101
3102 ??? Note that this code makes no attempt to deal with the case where
3103 taking the address of something causes a copy due to misalignment. */
3104
3105 #define UPDATE_FLAGS(NODE) \
3106 do { tree _node = (NODE); \
3107 if (_node && !TREE_CONSTANT (_node)) tc = false; \
3108 if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
3109
3110 for (node = TREE_OPERAND (t, 0); handled_component_p (node);
3111 node = TREE_OPERAND (node, 0))
3112 {
3113 /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
3114 array reference (probably made temporarily by the G++ front end),
3115 so ignore all the operands. */
3116 if ((TREE_CODE (node) == ARRAY_REF
3117 || TREE_CODE (node) == ARRAY_RANGE_REF)
3118 && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
3119 {
3120 UPDATE_FLAGS (TREE_OPERAND (node, 1));
3121 if (TREE_OPERAND (node, 2))
3122 UPDATE_FLAGS (TREE_OPERAND (node, 2));
3123 if (TREE_OPERAND (node, 3))
3124 UPDATE_FLAGS (TREE_OPERAND (node, 3));
3125 }
3126 /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
3127 FIELD_DECL, apparently. The G++ front end can put something else
3128 there, at least temporarily. */
3129 else if (TREE_CODE (node) == COMPONENT_REF
3130 && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
3131 {
3132 if (TREE_OPERAND (node, 2))
3133 UPDATE_FLAGS (TREE_OPERAND (node, 2));
3134 }
3135 else if (TREE_CODE (node) == BIT_FIELD_REF)
3136 UPDATE_FLAGS (TREE_OPERAND (node, 2));
3137 }
3138
3139 node = lang_hooks.expr_to_decl (node, &tc, &se);
3140
3141 /* Now see what's inside. If it's an INDIRECT_REF, copy our properties from
3142 the address, since &(*a)->b is a form of addition. If it's a constant, the
3143 address is constant too. If it's a decl, its address is constant if the
3144 decl is static. Everything else is not constant and, furthermore,
3145 taking the address of a volatile variable is not volatile. */
3146 if (TREE_CODE (node) == INDIRECT_REF)
3147 UPDATE_FLAGS (TREE_OPERAND (node, 0));
3148 else if (CONSTANT_CLASS_P (node))
3149 ;
3150 else if (DECL_P (node))
3151 tc &= (staticp (node) != NULL_TREE);
3152 else
3153 {
3154 tc = false;
3155 se |= TREE_SIDE_EFFECTS (node);
3156 }
3157
3158
3159 TREE_CONSTANT (t) = tc;
3160 TREE_SIDE_EFFECTS (t) = se;
3161 #undef UPDATE_FLAGS
3162 }
3163
3164 /* Build an expression of code CODE, data type TYPE, and operands as
3165 specified. Expressions and reference nodes can be created this way.
3166 Constants, decls, types and misc nodes cannot be.
3167
3168 We define 5 non-variadic functions, from 0 to 4 arguments. This is
3169 enough for all extant tree codes. */
3170
3171 tree
3172 build0_stat (enum tree_code code, tree tt MEM_STAT_DECL)
3173 {
3174 tree t;
3175
3176 gcc_assert (TREE_CODE_LENGTH (code) == 0);
3177
3178 t = make_node_stat (code PASS_MEM_STAT);
3179 TREE_TYPE (t) = tt;
3180
3181 return t;
3182 }
3183
3184 tree
3185 build1_stat (enum tree_code code, tree type, tree node MEM_STAT_DECL)
3186 {
3187 int length = sizeof (struct tree_exp);
3188 #ifdef GATHER_STATISTICS
3189 tree_node_kind kind;
3190 #endif
3191 tree t;
3192
3193 #ifdef GATHER_STATISTICS
3194 switch (TREE_CODE_CLASS (code))
3195 {
3196 case tcc_statement: /* an expression with side effects */
3197 kind = s_kind;
3198 break;
3199 case tcc_reference: /* a reference */
3200 kind = r_kind;
3201 break;
3202 default:
3203 kind = e_kind;
3204 break;
3205 }
3206
3207 tree_node_counts[(int) kind]++;
3208 tree_node_sizes[(int) kind] += length;
3209 #endif
3210
3211 gcc_assert (TREE_CODE_LENGTH (code) == 1);
3212
3213 t = (tree) ggc_alloc_zone_pass_stat (length, &tree_zone);
3214
3215 memset (t, 0, sizeof (struct tree_common));
3216
3217 TREE_SET_CODE (t, code);
3218
3219 TREE_TYPE (t) = type;
3220 SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
3221 TREE_OPERAND (t, 0) = node;
3222 TREE_BLOCK (t) = NULL_TREE;
3223 if (node && !TYPE_P (node))
3224 {
3225 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
3226 TREE_READONLY (t) = TREE_READONLY (node);
3227 }
3228
3229 if (TREE_CODE_CLASS (code) == tcc_statement)
3230 TREE_SIDE_EFFECTS (t) = 1;
3231 else switch (code)
3232 {
3233 case VA_ARG_EXPR:
3234 /* All of these have side-effects, no matter what their
3235 operands are. */
3236 TREE_SIDE_EFFECTS (t) = 1;
3237 TREE_READONLY (t) = 0;
3238 break;
3239
3240 case MISALIGNED_INDIRECT_REF:
3241 case ALIGN_INDIRECT_REF:
3242 case INDIRECT_REF:
3243 /* Whether a dereference is readonly has nothing to do with whether
3244 its operand is readonly. */
3245 TREE_READONLY (t) = 0;
3246 break;
3247
3248 case ADDR_EXPR:
3249 if (node)
3250 recompute_tree_invariant_for_addr_expr (t);
3251 break;
3252
3253 default:
3254 if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
3255 && node && !TYPE_P (node)
3256 && TREE_CONSTANT (node))
3257 TREE_CONSTANT (t) = 1;
3258 if (TREE_CODE_CLASS (code) == tcc_reference
3259 && node && TREE_THIS_VOLATILE (node))
3260 TREE_THIS_VOLATILE (t) = 1;
3261 break;
3262 }
3263
3264 return t;
3265 }
3266
3267 #define PROCESS_ARG(N) \
3268 do { \
3269 TREE_OPERAND (t, N) = arg##N; \
3270 if (arg##N &&!TYPE_P (arg##N)) \
3271 { \
3272 if (TREE_SIDE_EFFECTS (arg##N)) \
3273 side_effects = 1; \
3274 if (!TREE_READONLY (arg##N)) \
3275 read_only = 0; \
3276 if (!TREE_CONSTANT (arg##N)) \
3277 constant = 0; \
3278 } \
3279 } while (0)
3280
3281 tree
3282 build2_stat (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
3283 {
3284 bool constant, read_only, side_effects;
3285 tree t;
3286
3287 gcc_assert (TREE_CODE_LENGTH (code) == 2);
3288
3289 if ((code == MINUS_EXPR || code == PLUS_EXPR || code == MULT_EXPR)
3290 && arg0 && arg1 && tt && POINTER_TYPE_P (tt))
3291 gcc_assert (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST);
3292
3293 if (code == POINTER_PLUS_EXPR && arg0 && arg1 && tt)
3294 gcc_assert (POINTER_TYPE_P (tt) && POINTER_TYPE_P (TREE_TYPE (arg0))
3295 && INTEGRAL_TYPE_P (TREE_TYPE (arg1))
3296 && useless_type_conversion_p (sizetype, TREE_TYPE (arg1)));
3297
3298 t = make_node_stat (code PASS_MEM_STAT);
3299 TREE_TYPE (t) = tt;
3300
3301 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
3302 result based on those same flags for the arguments. But if the
3303 arguments aren't really even `tree' expressions, we shouldn't be trying
3304 to do this. */
3305
3306 /* Expressions without side effects may be constant if their
3307 arguments are as well. */
3308 constant = (TREE_CODE_CLASS (code) == tcc_comparison
3309 || TREE_CODE_CLASS (code) == tcc_binary);
3310 read_only = 1;
3311 side_effects = TREE_SIDE_EFFECTS (t);
3312
3313 PROCESS_ARG(0);
3314 PROCESS_ARG(1);
3315
3316 TREE_READONLY (t) = read_only;
3317 TREE_CONSTANT (t) = constant;
3318 TREE_SIDE_EFFECTS (t) = side_effects;
3319 TREE_THIS_VOLATILE (t)
3320 = (TREE_CODE_CLASS (code) == tcc_reference
3321 && arg0 && TREE_THIS_VOLATILE (arg0));
3322
3323 return t;
3324 }
3325
3326
3327 tree
3328 build3_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3329 tree arg2 MEM_STAT_DECL)
3330 {
3331 bool constant, read_only, side_effects;
3332 tree t;
3333
3334 gcc_assert (TREE_CODE_LENGTH (code) == 3);
3335 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3336
3337 t = make_node_stat (code PASS_MEM_STAT);
3338 TREE_TYPE (t) = tt;
3339
3340 /* As a special exception, if COND_EXPR has NULL branches, we
3341 assume that it is a gimple statement and always consider
3342 it to have side effects. */
3343 if (code == COND_EXPR
3344 && tt == void_type_node
3345 && arg1 == NULL_TREE
3346 && arg2 == NULL_TREE)
3347 side_effects = true;
3348 else
3349 side_effects = TREE_SIDE_EFFECTS (t);
3350
3351 PROCESS_ARG(0);
3352 PROCESS_ARG(1);
3353 PROCESS_ARG(2);
3354
3355 TREE_SIDE_EFFECTS (t) = side_effects;
3356 TREE_THIS_VOLATILE (t)
3357 = (TREE_CODE_CLASS (code) == tcc_reference
3358 && arg0 && TREE_THIS_VOLATILE (arg0));
3359
3360 return t;
3361 }
3362
3363 tree
3364 build4_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3365 tree arg2, tree arg3 MEM_STAT_DECL)
3366 {
3367 bool constant, read_only, side_effects;
3368 tree t;
3369
3370 gcc_assert (TREE_CODE_LENGTH (code) == 4);
3371
3372 t = make_node_stat (code PASS_MEM_STAT);
3373 TREE_TYPE (t) = tt;
3374
3375 side_effects = TREE_SIDE_EFFECTS (t);
3376
3377 PROCESS_ARG(0);
3378 PROCESS_ARG(1);
3379 PROCESS_ARG(2);
3380 PROCESS_ARG(3);
3381
3382 TREE_SIDE_EFFECTS (t) = side_effects;
3383 TREE_THIS_VOLATILE (t)
3384 = (TREE_CODE_CLASS (code) == tcc_reference
3385 && arg0 && TREE_THIS_VOLATILE (arg0));
3386
3387 return t;
3388 }
3389
3390 tree
3391 build5_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3392 tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
3393 {
3394 bool constant, read_only, side_effects;
3395 tree t;
3396
3397 gcc_assert (TREE_CODE_LENGTH (code) == 5);
3398
3399 t = make_node_stat (code PASS_MEM_STAT);
3400 TREE_TYPE (t) = tt;
3401
3402 side_effects = TREE_SIDE_EFFECTS (t);
3403
3404 PROCESS_ARG(0);
3405 PROCESS_ARG(1);
3406 PROCESS_ARG(2);
3407 PROCESS_ARG(3);
3408 PROCESS_ARG(4);
3409
3410 TREE_SIDE_EFFECTS (t) = side_effects;
3411 TREE_THIS_VOLATILE (t)
3412 = (TREE_CODE_CLASS (code) == tcc_reference
3413 && arg0 && TREE_THIS_VOLATILE (arg0));
3414
3415 return t;
3416 }
3417
3418 tree
3419 build7_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3420 tree arg2, tree arg3, tree arg4, tree arg5,
3421 tree arg6 MEM_STAT_DECL)
3422 {
3423 bool constant, read_only, side_effects;
3424 tree t;
3425
3426 gcc_assert (code == TARGET_MEM_REF);
3427
3428 t = make_node_stat (code PASS_MEM_STAT);
3429 TREE_TYPE (t) = tt;
3430
3431 side_effects = TREE_SIDE_EFFECTS (t);
3432
3433 PROCESS_ARG(0);
3434 PROCESS_ARG(1);
3435 PROCESS_ARG(2);
3436 PROCESS_ARG(3);
3437 PROCESS_ARG(4);
3438 PROCESS_ARG(5);
3439 PROCESS_ARG(6);
3440
3441 TREE_SIDE_EFFECTS (t) = side_effects;
3442 TREE_THIS_VOLATILE (t) = 0;
3443
3444 return t;
3445 }
3446
3447 /* Similar except don't specify the TREE_TYPE
3448 and leave the TREE_SIDE_EFFECTS as 0.
3449 It is permissible for arguments to be null,
3450 or even garbage if their values do not matter. */
3451
3452 tree
3453 build_nt (enum tree_code code, ...)
3454 {
3455 tree t;
3456 int length;
3457 int i;
3458 va_list p;
3459
3460 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3461
3462 va_start (p, code);
3463
3464 t = make_node (code);
3465 length = TREE_CODE_LENGTH (code);
3466
3467 for (i = 0; i < length; i++)
3468 TREE_OPERAND (t, i) = va_arg (p, tree);
3469
3470 va_end (p);
3471 return t;
3472 }
3473
3474 /* Similar to build_nt, but for creating a CALL_EXPR object with
3475 ARGLIST passed as a list. */
3476
3477 tree
3478 build_nt_call_list (tree fn, tree arglist)
3479 {
3480 tree t;
3481 int i;
3482
3483 t = build_vl_exp (CALL_EXPR, list_length (arglist) + 3);
3484 CALL_EXPR_FN (t) = fn;
3485 CALL_EXPR_STATIC_CHAIN (t) = NULL_TREE;
3486 for (i = 0; arglist; arglist = TREE_CHAIN (arglist), i++)
3487 CALL_EXPR_ARG (t, i) = TREE_VALUE (arglist);
3488 return t;
3489 }
3490 \f
3491 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
3492 We do NOT enter this node in any sort of symbol table.
3493
3494 layout_decl is used to set up the decl's storage layout.
3495 Other slots are initialized to 0 or null pointers. */
3496
3497 tree
3498 build_decl_stat (enum tree_code code, tree name, tree type MEM_STAT_DECL)
3499 {
3500 tree t;
3501
3502 t = make_node_stat (code PASS_MEM_STAT);
3503
3504 /* if (type == error_mark_node)
3505 type = integer_type_node; */
3506 /* That is not done, deliberately, so that having error_mark_node
3507 as the type can suppress useless errors in the use of this variable. */
3508
3509 DECL_NAME (t) = name;
3510 TREE_TYPE (t) = type;
3511
3512 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
3513 layout_decl (t, 0);
3514
3515 return t;
3516 }
3517
3518 /* Builds and returns function declaration with NAME and TYPE. */
3519
3520 tree
3521 build_fn_decl (const char *name, tree type)
3522 {
3523 tree id = get_identifier (name);
3524 tree decl = build_decl (FUNCTION_DECL, id, type);
3525
3526 DECL_EXTERNAL (decl) = 1;
3527 TREE_PUBLIC (decl) = 1;
3528 DECL_ARTIFICIAL (decl) = 1;
3529 TREE_NOTHROW (decl) = 1;
3530
3531 return decl;
3532 }
3533
3534 \f
3535 /* BLOCK nodes are used to represent the structure of binding contours
3536 and declarations, once those contours have been exited and their contents
3537 compiled. This information is used for outputting debugging info. */
3538
3539 tree
3540 build_block (tree vars, tree subblocks, tree supercontext, tree chain)
3541 {
3542 tree block = make_node (BLOCK);
3543
3544 BLOCK_VARS (block) = vars;
3545 BLOCK_SUBBLOCKS (block) = subblocks;
3546 BLOCK_SUPERCONTEXT (block) = supercontext;
3547 BLOCK_CHAIN (block) = chain;
3548 return block;
3549 }
3550
3551 expanded_location
3552 expand_location (source_location loc)
3553 {
3554 expanded_location xloc;
3555 if (loc == 0)
3556 {
3557 xloc.file = NULL;
3558 xloc.line = 0;
3559 xloc.column = 0;
3560 xloc.sysp = 0;
3561 }
3562 else
3563 {
3564 const struct line_map *map = linemap_lookup (line_table, loc);
3565 xloc.file = map->to_file;
3566 xloc.line = SOURCE_LINE (map, loc);
3567 xloc.column = SOURCE_COLUMN (map, loc);
3568 xloc.sysp = map->sysp != 0;
3569 };
3570 return xloc;
3571 }
3572
3573 \f
3574 /* Source location accessor functions. */
3575
3576
3577 void
3578 set_expr_locus (tree node, source_location *loc)
3579 {
3580 if (loc == NULL)
3581 EXPR_CHECK (node)->exp.locus = UNKNOWN_LOCATION;
3582 else
3583 EXPR_CHECK (node)->exp.locus = *loc;
3584 }
3585
3586 /* Like SET_EXPR_LOCATION, but make sure the tree can have a location.
3587
3588 LOC is the location to use in tree T. */
3589
3590 void protected_set_expr_location (tree t, location_t loc)
3591 {
3592 if (t && CAN_HAVE_LOCATION_P (t))
3593 SET_EXPR_LOCATION (t, loc);
3594 }
3595 \f
3596 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
3597 is ATTRIBUTE. */
3598
3599 tree
3600 build_decl_attribute_variant (tree ddecl, tree attribute)
3601 {
3602 DECL_ATTRIBUTES (ddecl) = attribute;
3603 return ddecl;
3604 }
3605
3606 /* Borrowed from hashtab.c iterative_hash implementation. */
3607 #define mix(a,b,c) \
3608 { \
3609 a -= b; a -= c; a ^= (c>>13); \
3610 b -= c; b -= a; b ^= (a<< 8); \
3611 c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \
3612 a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \
3613 b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \
3614 c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \
3615 a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \
3616 b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \
3617 c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \
3618 }
3619
3620
3621 /* Produce good hash value combining VAL and VAL2. */
3622 hashval_t
3623 iterative_hash_hashval_t (hashval_t val, hashval_t val2)
3624 {
3625 /* the golden ratio; an arbitrary value. */
3626 hashval_t a = 0x9e3779b9;
3627
3628 mix (a, val, val2);
3629 return val2;
3630 }
3631
3632 /* Produce good hash value combining PTR and VAL2. */
3633 static inline hashval_t
3634 iterative_hash_pointer (const void *ptr, hashval_t val2)
3635 {
3636 if (sizeof (ptr) == sizeof (hashval_t))
3637 return iterative_hash_hashval_t ((size_t) ptr, val2);
3638 else
3639 {
3640 hashval_t a = (hashval_t) (size_t) ptr;
3641 /* Avoid warnings about shifting of more than the width of the type on
3642 hosts that won't execute this path. */
3643 int zero = 0;
3644 hashval_t b = (hashval_t) ((size_t) ptr >> (sizeof (hashval_t) * 8 + zero));
3645 mix (a, b, val2);
3646 return val2;
3647 }
3648 }
3649
3650 /* Produce good hash value combining VAL and VAL2. */
3651 static inline hashval_t
3652 iterative_hash_host_wide_int (HOST_WIDE_INT val, hashval_t val2)
3653 {
3654 if (sizeof (HOST_WIDE_INT) == sizeof (hashval_t))
3655 return iterative_hash_hashval_t (val, val2);
3656 else
3657 {
3658 hashval_t a = (hashval_t) val;
3659 /* Avoid warnings about shifting of more than the width of the type on
3660 hosts that won't execute this path. */
3661 int zero = 0;
3662 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 8 + zero));
3663 mix (a, b, val2);
3664 if (sizeof (HOST_WIDE_INT) > 2 * sizeof (hashval_t))
3665 {
3666 hashval_t a = (hashval_t) (val >> (sizeof (hashval_t) * 16 + zero));
3667 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 24 + zero));
3668 mix (a, b, val2);
3669 }
3670 return val2;
3671 }
3672 }
3673
3674 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
3675 is ATTRIBUTE and its qualifiers are QUALS.
3676
3677 Record such modified types already made so we don't make duplicates. */
3678
3679 static tree
3680 build_type_attribute_qual_variant (tree ttype, tree attribute, int quals)
3681 {
3682 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
3683 {
3684 hashval_t hashcode = 0;
3685 tree ntype;
3686 enum tree_code code = TREE_CODE (ttype);
3687
3688 /* Building a distinct copy of a tagged type is inappropriate; it
3689 causes breakage in code that expects there to be a one-to-one
3690 relationship between a struct and its fields.
3691 build_duplicate_type is another solution (as used in
3692 handle_transparent_union_attribute), but that doesn't play well
3693 with the stronger C++ type identity model. */
3694 if (TREE_CODE (ttype) == RECORD_TYPE
3695 || TREE_CODE (ttype) == UNION_TYPE
3696 || TREE_CODE (ttype) == QUAL_UNION_TYPE
3697 || TREE_CODE (ttype) == ENUMERAL_TYPE)
3698 {
3699 warning (OPT_Wattributes,
3700 "ignoring attributes applied to %qT after definition",
3701 TYPE_MAIN_VARIANT (ttype));
3702 return build_qualified_type (ttype, quals);
3703 }
3704
3705 ntype = build_distinct_type_copy (ttype);
3706
3707 TYPE_ATTRIBUTES (ntype) = attribute;
3708 set_type_quals (ntype, TYPE_UNQUALIFIED);
3709
3710 hashcode = iterative_hash_object (code, hashcode);
3711 if (TREE_TYPE (ntype))
3712 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (ntype)),
3713 hashcode);
3714 hashcode = attribute_hash_list (attribute, hashcode);
3715
3716 switch (TREE_CODE (ntype))
3717 {
3718 case FUNCTION_TYPE:
3719 hashcode = type_hash_list (TYPE_ARG_TYPES (ntype), hashcode);
3720 break;
3721 case ARRAY_TYPE:
3722 if (TYPE_DOMAIN (ntype))
3723 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (ntype)),
3724 hashcode);
3725 break;
3726 case INTEGER_TYPE:
3727 hashcode = iterative_hash_object
3728 (TREE_INT_CST_LOW (TYPE_MAX_VALUE (ntype)), hashcode);
3729 hashcode = iterative_hash_object
3730 (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (ntype)), hashcode);
3731 break;
3732 case REAL_TYPE:
3733 case FIXED_POINT_TYPE:
3734 {
3735 unsigned int precision = TYPE_PRECISION (ntype);
3736 hashcode = iterative_hash_object (precision, hashcode);
3737 }
3738 break;
3739 default:
3740 break;
3741 }
3742
3743 ntype = type_hash_canon (hashcode, ntype);
3744
3745 /* If the target-dependent attributes make NTYPE different from
3746 its canonical type, we will need to use structural equality
3747 checks for this qualified type. */
3748 ttype = build_qualified_type (ttype, TYPE_UNQUALIFIED);
3749 if (TYPE_STRUCTURAL_EQUALITY_P (ttype)
3750 || !targetm.comp_type_attributes (ntype, ttype))
3751 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
3752 else
3753 TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype);
3754
3755 ttype = build_qualified_type (ntype, quals);
3756 }
3757 else if (TYPE_QUALS (ttype) != quals)
3758 ttype = build_qualified_type (ttype, quals);
3759
3760 return ttype;
3761 }
3762
3763
3764 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
3765 is ATTRIBUTE.
3766
3767 Record such modified types already made so we don't make duplicates. */
3768
3769 tree
3770 build_type_attribute_variant (tree ttype, tree attribute)
3771 {
3772 return build_type_attribute_qual_variant (ttype, attribute,
3773 TYPE_QUALS (ttype));
3774 }
3775
3776 /* Return nonzero if IDENT is a valid name for attribute ATTR,
3777 or zero if not.
3778
3779 We try both `text' and `__text__', ATTR may be either one. */
3780 /* ??? It might be a reasonable simplification to require ATTR to be only
3781 `text'. One might then also require attribute lists to be stored in
3782 their canonicalized form. */
3783
3784 static int
3785 is_attribute_with_length_p (const char *attr, int attr_len, const_tree ident)
3786 {
3787 int ident_len;
3788 const char *p;
3789
3790 if (TREE_CODE (ident) != IDENTIFIER_NODE)
3791 return 0;
3792
3793 p = IDENTIFIER_POINTER (ident);
3794 ident_len = IDENTIFIER_LENGTH (ident);
3795
3796 if (ident_len == attr_len
3797 && strcmp (attr, p) == 0)
3798 return 1;
3799
3800 /* If ATTR is `__text__', IDENT must be `text'; and vice versa. */
3801 if (attr[0] == '_')
3802 {
3803 gcc_assert (attr[1] == '_');
3804 gcc_assert (attr[attr_len - 2] == '_');
3805 gcc_assert (attr[attr_len - 1] == '_');
3806 if (ident_len == attr_len - 4
3807 && strncmp (attr + 2, p, attr_len - 4) == 0)
3808 return 1;
3809 }
3810 else
3811 {
3812 if (ident_len == attr_len + 4
3813 && p[0] == '_' && p[1] == '_'
3814 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
3815 && strncmp (attr, p + 2, attr_len) == 0)
3816 return 1;
3817 }
3818
3819 return 0;
3820 }
3821
3822 /* Return nonzero if IDENT is a valid name for attribute ATTR,
3823 or zero if not.
3824
3825 We try both `text' and `__text__', ATTR may be either one. */
3826
3827 int
3828 is_attribute_p (const char *attr, const_tree ident)
3829 {
3830 return is_attribute_with_length_p (attr, strlen (attr), ident);
3831 }
3832
3833 /* Given an attribute name and a list of attributes, return a pointer to the
3834 attribute's list element if the attribute is part of the list, or NULL_TREE
3835 if not found. If the attribute appears more than once, this only
3836 returns the first occurrence; the TREE_CHAIN of the return value should
3837 be passed back in if further occurrences are wanted. */
3838
3839 tree
3840 lookup_attribute (const char *attr_name, tree list)
3841 {
3842 tree l;
3843 size_t attr_len = strlen (attr_name);
3844
3845 for (l = list; l; l = TREE_CHAIN (l))
3846 {
3847 gcc_assert (TREE_CODE (TREE_PURPOSE (l)) == IDENTIFIER_NODE);
3848 if (is_attribute_with_length_p (attr_name, attr_len, TREE_PURPOSE (l)))
3849 return l;
3850 }
3851 return NULL_TREE;
3852 }
3853
3854 /* Remove any instances of attribute ATTR_NAME in LIST and return the
3855 modified list. */
3856
3857 tree
3858 remove_attribute (const char *attr_name, tree list)
3859 {
3860 tree *p;
3861 size_t attr_len = strlen (attr_name);
3862
3863 for (p = &list; *p; )
3864 {
3865 tree l = *p;
3866 gcc_assert (TREE_CODE (TREE_PURPOSE (l)) == IDENTIFIER_NODE);
3867 if (is_attribute_with_length_p (attr_name, attr_len, TREE_PURPOSE (l)))
3868 *p = TREE_CHAIN (l);
3869 else
3870 p = &TREE_CHAIN (l);
3871 }
3872
3873 return list;
3874 }
3875
3876 /* Return an attribute list that is the union of a1 and a2. */
3877
3878 tree
3879 merge_attributes (tree a1, tree a2)
3880 {
3881 tree attributes;
3882
3883 /* Either one unset? Take the set one. */
3884
3885 if ((attributes = a1) == 0)
3886 attributes = a2;
3887
3888 /* One that completely contains the other? Take it. */
3889
3890 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
3891 {
3892 if (attribute_list_contained (a2, a1))
3893 attributes = a2;
3894 else
3895 {
3896 /* Pick the longest list, and hang on the other list. */
3897
3898 if (list_length (a1) < list_length (a2))
3899 attributes = a2, a2 = a1;
3900
3901 for (; a2 != 0; a2 = TREE_CHAIN (a2))
3902 {
3903 tree a;
3904 for (a = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
3905 attributes);
3906 a != NULL_TREE;
3907 a = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
3908 TREE_CHAIN (a)))
3909 {
3910 if (TREE_VALUE (a) != NULL
3911 && TREE_CODE (TREE_VALUE (a)) == TREE_LIST
3912 && TREE_VALUE (a2) != NULL
3913 && TREE_CODE (TREE_VALUE (a2)) == TREE_LIST)
3914 {
3915 if (simple_cst_list_equal (TREE_VALUE (a),
3916 TREE_VALUE (a2)) == 1)
3917 break;
3918 }
3919 else if (simple_cst_equal (TREE_VALUE (a),
3920 TREE_VALUE (a2)) == 1)
3921 break;
3922 }
3923 if (a == NULL_TREE)
3924 {
3925 a1 = copy_node (a2);
3926 TREE_CHAIN (a1) = attributes;
3927 attributes = a1;
3928 }
3929 }
3930 }
3931 }
3932 return attributes;
3933 }
3934
3935 /* Given types T1 and T2, merge their attributes and return
3936 the result. */
3937
3938 tree
3939 merge_type_attributes (tree t1, tree t2)
3940 {
3941 return merge_attributes (TYPE_ATTRIBUTES (t1),
3942 TYPE_ATTRIBUTES (t2));
3943 }
3944
3945 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
3946 the result. */
3947
3948 tree
3949 merge_decl_attributes (tree olddecl, tree newdecl)
3950 {
3951 return merge_attributes (DECL_ATTRIBUTES (olddecl),
3952 DECL_ATTRIBUTES (newdecl));
3953 }
3954
3955 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
3956
3957 /* Specialization of merge_decl_attributes for various Windows targets.
3958
3959 This handles the following situation:
3960
3961 __declspec (dllimport) int foo;
3962 int foo;
3963
3964 The second instance of `foo' nullifies the dllimport. */
3965
3966 tree
3967 merge_dllimport_decl_attributes (tree old, tree new_tree)
3968 {
3969 tree a;
3970 int delete_dllimport_p = 1;
3971
3972 /* What we need to do here is remove from `old' dllimport if it doesn't
3973 appear in `new'. dllimport behaves like extern: if a declaration is
3974 marked dllimport and a definition appears later, then the object
3975 is not dllimport'd. We also remove a `new' dllimport if the old list
3976 contains dllexport: dllexport always overrides dllimport, regardless
3977 of the order of declaration. */
3978 if (!VAR_OR_FUNCTION_DECL_P (new_tree))
3979 delete_dllimport_p = 0;
3980 else if (DECL_DLLIMPORT_P (new_tree)
3981 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
3982 {
3983 DECL_DLLIMPORT_P (new_tree) = 0;
3984 warning (OPT_Wattributes, "%q+D already declared with dllexport attribute: "
3985 "dllimport ignored", new_tree);
3986 }
3987 else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new_tree))
3988 {
3989 /* Warn about overriding a symbol that has already been used, e.g.:
3990 extern int __attribute__ ((dllimport)) foo;
3991 int* bar () {return &foo;}
3992 int foo;
3993 */
3994 if (TREE_USED (old))
3995 {
3996 warning (0, "%q+D redeclared without dllimport attribute "
3997 "after being referenced with dll linkage", new_tree);
3998 /* If we have used a variable's address with dllimport linkage,
3999 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
4000 decl may already have had TREE_CONSTANT computed.
4001 We still remove the attribute so that assembler code refers
4002 to '&foo rather than '_imp__foo'. */
4003 if (TREE_CODE (old) == VAR_DECL && TREE_ADDRESSABLE (old))
4004 DECL_DLLIMPORT_P (new_tree) = 1;
4005 }
4006
4007 /* Let an inline definition silently override the external reference,
4008 but otherwise warn about attribute inconsistency. */
4009 else if (TREE_CODE (new_tree) == VAR_DECL
4010 || !DECL_DECLARED_INLINE_P (new_tree))
4011 warning (OPT_Wattributes, "%q+D redeclared without dllimport attribute: "
4012 "previous dllimport ignored", new_tree);
4013 }
4014 else
4015 delete_dllimport_p = 0;
4016
4017 a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new_tree));
4018
4019 if (delete_dllimport_p)
4020 {
4021 tree prev, t;
4022 const size_t attr_len = strlen ("dllimport");
4023
4024 /* Scan the list for dllimport and delete it. */
4025 for (prev = NULL_TREE, t = a; t; prev = t, t = TREE_CHAIN (t))
4026 {
4027 if (is_attribute_with_length_p ("dllimport", attr_len,
4028 TREE_PURPOSE (t)))
4029 {
4030 if (prev == NULL_TREE)
4031 a = TREE_CHAIN (a);
4032 else
4033 TREE_CHAIN (prev) = TREE_CHAIN (t);
4034 break;
4035 }
4036 }
4037 }
4038
4039 return a;
4040 }
4041
4042 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
4043 struct attribute_spec.handler. */
4044
4045 tree
4046 handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
4047 bool *no_add_attrs)
4048 {
4049 tree node = *pnode;
4050
4051 /* These attributes may apply to structure and union types being created,
4052 but otherwise should pass to the declaration involved. */
4053 if (!DECL_P (node))
4054 {
4055 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
4056 | (int) ATTR_FLAG_ARRAY_NEXT))
4057 {
4058 *no_add_attrs = true;
4059 return tree_cons (name, args, NULL_TREE);
4060 }
4061 if (TREE_CODE (node) == RECORD_TYPE
4062 || TREE_CODE (node) == UNION_TYPE)
4063 {
4064 node = TYPE_NAME (node);
4065 if (!node)
4066 return NULL_TREE;
4067 }
4068 else
4069 {
4070 warning (OPT_Wattributes, "%qs attribute ignored",
4071 IDENTIFIER_POINTER (name));
4072 *no_add_attrs = true;
4073 return NULL_TREE;
4074 }
4075 }
4076
4077 if (TREE_CODE (node) != FUNCTION_DECL
4078 && TREE_CODE (node) != VAR_DECL
4079 && TREE_CODE (node) != TYPE_DECL)
4080 {
4081 *no_add_attrs = true;
4082 warning (OPT_Wattributes, "%qs attribute ignored",
4083 IDENTIFIER_POINTER (name));
4084 return NULL_TREE;
4085 }
4086
4087 if (TREE_CODE (node) == TYPE_DECL
4088 && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE
4089 && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE)
4090 {
4091 *no_add_attrs = true;
4092 warning (OPT_Wattributes, "%qs attribute ignored",
4093 IDENTIFIER_POINTER (name));
4094 return NULL_TREE;
4095 }
4096
4097 /* Report error on dllimport ambiguities seen now before they cause
4098 any damage. */
4099 else if (is_attribute_p ("dllimport", name))
4100 {
4101 /* Honor any target-specific overrides. */
4102 if (!targetm.valid_dllimport_attribute_p (node))
4103 *no_add_attrs = true;
4104
4105 else if (TREE_CODE (node) == FUNCTION_DECL
4106 && DECL_DECLARED_INLINE_P (node))
4107 {
4108 warning (OPT_Wattributes, "inline function %q+D declared as "
4109 " dllimport: attribute ignored", node);
4110 *no_add_attrs = true;
4111 }
4112 /* Like MS, treat definition of dllimported variables and
4113 non-inlined functions on declaration as syntax errors. */
4114 else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
4115 {
4116 error ("function %q+D definition is marked dllimport", node);
4117 *no_add_attrs = true;
4118 }
4119
4120 else if (TREE_CODE (node) == VAR_DECL)
4121 {
4122 if (DECL_INITIAL (node))
4123 {
4124 error ("variable %q+D definition is marked dllimport",
4125 node);
4126 *no_add_attrs = true;
4127 }
4128
4129 /* `extern' needn't be specified with dllimport.
4130 Specify `extern' now and hope for the best. Sigh. */
4131 DECL_EXTERNAL (node) = 1;
4132 /* Also, implicitly give dllimport'd variables declared within
4133 a function global scope, unless declared static. */
4134 if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
4135 TREE_PUBLIC (node) = 1;
4136 }
4137
4138 if (*no_add_attrs == false)
4139 DECL_DLLIMPORT_P (node) = 1;
4140 }
4141
4142 /* Report error if symbol is not accessible at global scope. */
4143 if (!TREE_PUBLIC (node)
4144 && (TREE_CODE (node) == VAR_DECL
4145 || TREE_CODE (node) == FUNCTION_DECL))
4146 {
4147 error ("external linkage required for symbol %q+D because of "
4148 "%qs attribute", node, IDENTIFIER_POINTER (name));
4149 *no_add_attrs = true;
4150 }
4151
4152 /* A dllexport'd entity must have default visibility so that other
4153 program units (shared libraries or the main executable) can see
4154 it. A dllimport'd entity must have default visibility so that
4155 the linker knows that undefined references within this program
4156 unit can be resolved by the dynamic linker. */
4157 if (!*no_add_attrs)
4158 {
4159 if (DECL_VISIBILITY_SPECIFIED (node)
4160 && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT)
4161 error ("%qs implies default visibility, but %qD has already "
4162 "been declared with a different visibility",
4163 IDENTIFIER_POINTER (name), node);
4164 DECL_VISIBILITY (node) = VISIBILITY_DEFAULT;
4165 DECL_VISIBILITY_SPECIFIED (node) = 1;
4166 }
4167
4168 return NULL_TREE;
4169 }
4170
4171 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
4172 \f
4173 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
4174 of the various TYPE_QUAL values. */
4175
4176 static void
4177 set_type_quals (tree type, int type_quals)
4178 {
4179 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
4180 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
4181 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
4182 }
4183
4184 /* Returns true iff CAND is equivalent to BASE with TYPE_QUALS. */
4185
4186 bool
4187 check_qualified_type (const_tree cand, const_tree base, int type_quals)
4188 {
4189 return (TYPE_QUALS (cand) == type_quals
4190 && TYPE_NAME (cand) == TYPE_NAME (base)
4191 /* Apparently this is needed for Objective-C. */
4192 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
4193 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
4194 TYPE_ATTRIBUTES (base)));
4195 }
4196
4197 /* Return a version of the TYPE, qualified as indicated by the
4198 TYPE_QUALS, if one exists. If no qualified version exists yet,
4199 return NULL_TREE. */
4200
4201 tree
4202 get_qualified_type (tree type, int type_quals)
4203 {
4204 tree t;
4205
4206 if (TYPE_QUALS (type) == type_quals)
4207 return type;
4208
4209 /* Search the chain of variants to see if there is already one there just
4210 like the one we need to have. If so, use that existing one. We must
4211 preserve the TYPE_NAME, since there is code that depends on this. */
4212 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
4213 if (check_qualified_type (t, type, type_quals))
4214 return t;
4215
4216 return NULL_TREE;
4217 }
4218
4219 /* Like get_qualified_type, but creates the type if it does not
4220 exist. This function never returns NULL_TREE. */
4221
4222 tree
4223 build_qualified_type (tree type, int type_quals)
4224 {
4225 tree t;
4226
4227 /* See if we already have the appropriate qualified variant. */
4228 t = get_qualified_type (type, type_quals);
4229
4230 /* If not, build it. */
4231 if (!t)
4232 {
4233 t = build_variant_type_copy (type);
4234 set_type_quals (t, type_quals);
4235
4236 if (TYPE_STRUCTURAL_EQUALITY_P (type))
4237 /* Propagate structural equality. */
4238 SET_TYPE_STRUCTURAL_EQUALITY (t);
4239 else if (TYPE_CANONICAL (type) != type)
4240 /* Build the underlying canonical type, since it is different
4241 from TYPE. */
4242 TYPE_CANONICAL (t) = build_qualified_type (TYPE_CANONICAL (type),
4243 type_quals);
4244 else
4245 /* T is its own canonical type. */
4246 TYPE_CANONICAL (t) = t;
4247
4248 }
4249
4250 return t;
4251 }
4252
4253 /* Create a new distinct copy of TYPE. The new type is made its own
4254 MAIN_VARIANT. If TYPE requires structural equality checks, the
4255 resulting type requires structural equality checks; otherwise, its
4256 TYPE_CANONICAL points to itself. */
4257
4258 tree
4259 build_distinct_type_copy (tree type)
4260 {
4261 tree t = copy_node (type);
4262
4263 TYPE_POINTER_TO (t) = 0;
4264 TYPE_REFERENCE_TO (t) = 0;
4265
4266 /* Set the canonical type either to a new equivalence class, or
4267 propagate the need for structural equality checks. */
4268 if (TYPE_STRUCTURAL_EQUALITY_P (type))
4269 SET_TYPE_STRUCTURAL_EQUALITY (t);
4270 else
4271 TYPE_CANONICAL (t) = t;
4272
4273 /* Make it its own variant. */
4274 TYPE_MAIN_VARIANT (t) = t;
4275 TYPE_NEXT_VARIANT (t) = 0;
4276
4277 /* Note that it is now possible for TYPE_MIN_VALUE to be a value
4278 whose TREE_TYPE is not t. This can also happen in the Ada
4279 frontend when using subtypes. */
4280
4281 return t;
4282 }
4283
4284 /* Create a new variant of TYPE, equivalent but distinct. This is so
4285 the caller can modify it. TYPE_CANONICAL for the return type will
4286 be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
4287 are considered equal by the language itself (or that both types
4288 require structural equality checks). */
4289
4290 tree
4291 build_variant_type_copy (tree type)
4292 {
4293 tree t, m = TYPE_MAIN_VARIANT (type);
4294
4295 t = build_distinct_type_copy (type);
4296
4297 /* Since we're building a variant, assume that it is a non-semantic
4298 variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
4299 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
4300
4301 /* Add the new type to the chain of variants of TYPE. */
4302 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
4303 TYPE_NEXT_VARIANT (m) = t;
4304 TYPE_MAIN_VARIANT (t) = m;
4305
4306 return t;
4307 }
4308 \f
4309 /* Return true if the from tree in both tree maps are equal. */
4310
4311 int
4312 tree_map_base_eq (const void *va, const void *vb)
4313 {
4314 const struct tree_map_base *const a = (const struct tree_map_base *) va,
4315 *const b = (const struct tree_map_base *) vb;
4316 return (a->from == b->from);
4317 }
4318
4319 /* Hash a from tree in a tree_map. */
4320
4321 unsigned int
4322 tree_map_base_hash (const void *item)
4323 {
4324 return htab_hash_pointer (((const struct tree_map_base *)item)->from);
4325 }
4326
4327 /* Return true if this tree map structure is marked for garbage collection
4328 purposes. We simply return true if the from tree is marked, so that this
4329 structure goes away when the from tree goes away. */
4330
4331 int
4332 tree_map_base_marked_p (const void *p)
4333 {
4334 return ggc_marked_p (((const struct tree_map_base *) p)->from);
4335 }
4336
4337 unsigned int
4338 tree_map_hash (const void *item)
4339 {
4340 return (((const struct tree_map *) item)->hash);
4341 }
4342
4343 /* Return the initialization priority for DECL. */
4344
4345 priority_type
4346 decl_init_priority_lookup (tree decl)
4347 {
4348 struct tree_priority_map *h;
4349 struct tree_map_base in;
4350
4351 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
4352 in.from = decl;
4353 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
4354 return h ? h->init : DEFAULT_INIT_PRIORITY;
4355 }
4356
4357 /* Return the finalization priority for DECL. */
4358
4359 priority_type
4360 decl_fini_priority_lookup (tree decl)
4361 {
4362 struct tree_priority_map *h;
4363 struct tree_map_base in;
4364
4365 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4366 in.from = decl;
4367 h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
4368 return h ? h->fini : DEFAULT_INIT_PRIORITY;
4369 }
4370
4371 /* Return the initialization and finalization priority information for
4372 DECL. If there is no previous priority information, a freshly
4373 allocated structure is returned. */
4374
4375 static struct tree_priority_map *
4376 decl_priority_info (tree decl)
4377 {
4378 struct tree_priority_map in;
4379 struct tree_priority_map *h;
4380 void **loc;
4381
4382 in.base.from = decl;
4383 loc = htab_find_slot (init_priority_for_decl, &in, INSERT);
4384 h = (struct tree_priority_map *) *loc;
4385 if (!h)
4386 {
4387 h = GGC_CNEW (struct tree_priority_map);
4388 *loc = h;
4389 h->base.from = decl;
4390 h->init = DEFAULT_INIT_PRIORITY;
4391 h->fini = DEFAULT_INIT_PRIORITY;
4392 }
4393
4394 return h;
4395 }
4396
4397 /* Set the initialization priority for DECL to PRIORITY. */
4398
4399 void
4400 decl_init_priority_insert (tree decl, priority_type priority)
4401 {
4402 struct tree_priority_map *h;
4403
4404 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
4405 h = decl_priority_info (decl);
4406 h->init = priority;
4407 }
4408
4409 /* Set the finalization priority for DECL to PRIORITY. */
4410
4411 void
4412 decl_fini_priority_insert (tree decl, priority_type priority)
4413 {
4414 struct tree_priority_map *h;
4415
4416 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4417 h = decl_priority_info (decl);
4418 h->fini = priority;
4419 }
4420
4421 /* Look up a restrict qualified base decl for FROM. */
4422
4423 tree
4424 decl_restrict_base_lookup (tree from)
4425 {
4426 struct tree_map *h;
4427 struct tree_map in;
4428
4429 in.base.from = from;
4430 h = (struct tree_map *) htab_find_with_hash (restrict_base_for_decl, &in,
4431 htab_hash_pointer (from));
4432 return h ? h->to : NULL_TREE;
4433 }
4434
4435 /* Record the restrict qualified base TO for FROM. */
4436
4437 void
4438 decl_restrict_base_insert (tree from, tree to)
4439 {
4440 struct tree_map *h;
4441 void **loc;
4442
4443 h = GGC_NEW (struct tree_map);
4444 h->hash = htab_hash_pointer (from);
4445 h->base.from = from;
4446 h->to = to;
4447 loc = htab_find_slot_with_hash (restrict_base_for_decl, h, h->hash, INSERT);
4448 *(struct tree_map **) loc = h;
4449 }
4450
4451 /* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
4452
4453 static void
4454 print_debug_expr_statistics (void)
4455 {
4456 fprintf (stderr, "DECL_DEBUG_EXPR hash: size %ld, %ld elements, %f collisions\n",
4457 (long) htab_size (debug_expr_for_decl),
4458 (long) htab_elements (debug_expr_for_decl),
4459 htab_collisions (debug_expr_for_decl));
4460 }
4461
4462 /* Print out the statistics for the DECL_VALUE_EXPR hash table. */
4463
4464 static void
4465 print_value_expr_statistics (void)
4466 {
4467 fprintf (stderr, "DECL_VALUE_EXPR hash: size %ld, %ld elements, %f collisions\n",
4468 (long) htab_size (value_expr_for_decl),
4469 (long) htab_elements (value_expr_for_decl),
4470 htab_collisions (value_expr_for_decl));
4471 }
4472
4473 /* Print out statistics for the RESTRICT_BASE_FOR_DECL hash table, but
4474 don't print anything if the table is empty. */
4475
4476 static void
4477 print_restrict_base_statistics (void)
4478 {
4479 if (htab_elements (restrict_base_for_decl) != 0)
4480 fprintf (stderr,
4481 "RESTRICT_BASE hash: size %ld, %ld elements, %f collisions\n",
4482 (long) htab_size (restrict_base_for_decl),
4483 (long) htab_elements (restrict_base_for_decl),
4484 htab_collisions (restrict_base_for_decl));
4485 }
4486
4487 /* Lookup a debug expression for FROM, and return it if we find one. */
4488
4489 tree
4490 decl_debug_expr_lookup (tree from)
4491 {
4492 struct tree_map *h, in;
4493 in.base.from = from;
4494
4495 h = (struct tree_map *) htab_find_with_hash (debug_expr_for_decl, &in,
4496 htab_hash_pointer (from));
4497 if (h)
4498 return h->to;
4499 return NULL_TREE;
4500 }
4501
4502 /* Insert a mapping FROM->TO in the debug expression hashtable. */
4503
4504 void
4505 decl_debug_expr_insert (tree from, tree to)
4506 {
4507 struct tree_map *h;
4508 void **loc;
4509
4510 h = GGC_NEW (struct tree_map);
4511 h->hash = htab_hash_pointer (from);
4512 h->base.from = from;
4513 h->to = to;
4514 loc = htab_find_slot_with_hash (debug_expr_for_decl, h, h->hash, INSERT);
4515 *(struct tree_map **) loc = h;
4516 }
4517
4518 /* Lookup a value expression for FROM, and return it if we find one. */
4519
4520 tree
4521 decl_value_expr_lookup (tree from)
4522 {
4523 struct tree_map *h, in;
4524 in.base.from = from;
4525
4526 h = (struct tree_map *) htab_find_with_hash (value_expr_for_decl, &in,
4527 htab_hash_pointer (from));
4528 if (h)
4529 return h->to;
4530 return NULL_TREE;
4531 }
4532
4533 /* Insert a mapping FROM->TO in the value expression hashtable. */
4534
4535 void
4536 decl_value_expr_insert (tree from, tree to)
4537 {
4538 struct tree_map *h;
4539 void **loc;
4540
4541 h = GGC_NEW (struct tree_map);
4542 h->hash = htab_hash_pointer (from);
4543 h->base.from = from;
4544 h->to = to;
4545 loc = htab_find_slot_with_hash (value_expr_for_decl, h, h->hash, INSERT);
4546 *(struct tree_map **) loc = h;
4547 }
4548
4549 /* Hashing of types so that we don't make duplicates.
4550 The entry point is `type_hash_canon'. */
4551
4552 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
4553 with types in the TREE_VALUE slots), by adding the hash codes
4554 of the individual types. */
4555
4556 static unsigned int
4557 type_hash_list (const_tree list, hashval_t hashcode)
4558 {
4559 const_tree tail;
4560
4561 for (tail = list; tail; tail = TREE_CHAIN (tail))
4562 if (TREE_VALUE (tail) != error_mark_node)
4563 hashcode = iterative_hash_object (TYPE_HASH (TREE_VALUE (tail)),
4564 hashcode);
4565
4566 return hashcode;
4567 }
4568
4569 /* These are the Hashtable callback functions. */
4570
4571 /* Returns true iff the types are equivalent. */
4572
4573 static int
4574 type_hash_eq (const void *va, const void *vb)
4575 {
4576 const struct type_hash *const a = (const struct type_hash *) va,
4577 *const b = (const struct type_hash *) vb;
4578
4579 /* First test the things that are the same for all types. */
4580 if (a->hash != b->hash
4581 || TREE_CODE (a->type) != TREE_CODE (b->type)
4582 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
4583 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
4584 TYPE_ATTRIBUTES (b->type))
4585 || TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
4586 || TYPE_MODE (a->type) != TYPE_MODE (b->type))
4587 return 0;
4588
4589 switch (TREE_CODE (a->type))
4590 {
4591 case VOID_TYPE:
4592 case COMPLEX_TYPE:
4593 case POINTER_TYPE:
4594 case REFERENCE_TYPE:
4595 return 1;
4596
4597 case VECTOR_TYPE:
4598 return TYPE_VECTOR_SUBPARTS (a->type) == TYPE_VECTOR_SUBPARTS (b->type);
4599
4600 case ENUMERAL_TYPE:
4601 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
4602 && !(TYPE_VALUES (a->type)
4603 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
4604 && TYPE_VALUES (b->type)
4605 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
4606 && type_list_equal (TYPE_VALUES (a->type),
4607 TYPE_VALUES (b->type))))
4608 return 0;
4609
4610 /* ... fall through ... */
4611
4612 case INTEGER_TYPE:
4613 case REAL_TYPE:
4614 case BOOLEAN_TYPE:
4615 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
4616 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
4617 TYPE_MAX_VALUE (b->type)))
4618 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
4619 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
4620 TYPE_MIN_VALUE (b->type))));
4621
4622 case FIXED_POINT_TYPE:
4623 return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
4624
4625 case OFFSET_TYPE:
4626 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
4627
4628 case METHOD_TYPE:
4629 return (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
4630 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
4631 || (TYPE_ARG_TYPES (a->type)
4632 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
4633 && TYPE_ARG_TYPES (b->type)
4634 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
4635 && type_list_equal (TYPE_ARG_TYPES (a->type),
4636 TYPE_ARG_TYPES (b->type)))));
4637
4638 case ARRAY_TYPE:
4639 return TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type);
4640
4641 case RECORD_TYPE:
4642 case UNION_TYPE:
4643 case QUAL_UNION_TYPE:
4644 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
4645 || (TYPE_FIELDS (a->type)
4646 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
4647 && TYPE_FIELDS (b->type)
4648 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
4649 && type_list_equal (TYPE_FIELDS (a->type),
4650 TYPE_FIELDS (b->type))));
4651
4652 case FUNCTION_TYPE:
4653 if (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
4654 || (TYPE_ARG_TYPES (a->type)
4655 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
4656 && TYPE_ARG_TYPES (b->type)
4657 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
4658 && type_list_equal (TYPE_ARG_TYPES (a->type),
4659 TYPE_ARG_TYPES (b->type))))
4660 break;
4661 return 0;
4662
4663 default:
4664 return 0;
4665 }
4666
4667 if (lang_hooks.types.type_hash_eq != NULL)
4668 return lang_hooks.types.type_hash_eq (a->type, b->type);
4669
4670 return 1;
4671 }
4672
4673 /* Return the cached hash value. */
4674
4675 static hashval_t
4676 type_hash_hash (const void *item)
4677 {
4678 return ((const struct type_hash *) item)->hash;
4679 }
4680
4681 /* Look in the type hash table for a type isomorphic to TYPE.
4682 If one is found, return it. Otherwise return 0. */
4683
4684 tree
4685 type_hash_lookup (hashval_t hashcode, tree type)
4686 {
4687 struct type_hash *h, in;
4688
4689 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
4690 must call that routine before comparing TYPE_ALIGNs. */
4691 layout_type (type);
4692
4693 in.hash = hashcode;
4694 in.type = type;
4695
4696 h = (struct type_hash *) htab_find_with_hash (type_hash_table, &in,
4697 hashcode);
4698 if (h)
4699 return h->type;
4700 return NULL_TREE;
4701 }
4702
4703 /* Add an entry to the type-hash-table
4704 for a type TYPE whose hash code is HASHCODE. */
4705
4706 void
4707 type_hash_add (hashval_t hashcode, tree type)
4708 {
4709 struct type_hash *h;
4710 void **loc;
4711
4712 h = GGC_NEW (struct type_hash);
4713 h->hash = hashcode;
4714 h->type = type;
4715 loc = htab_find_slot_with_hash (type_hash_table, h, hashcode, INSERT);
4716 *loc = (void *)h;
4717 }
4718
4719 /* Given TYPE, and HASHCODE its hash code, return the canonical
4720 object for an identical type if one already exists.
4721 Otherwise, return TYPE, and record it as the canonical object.
4722
4723 To use this function, first create a type of the sort you want.
4724 Then compute its hash code from the fields of the type that
4725 make it different from other similar types.
4726 Then call this function and use the value. */
4727
4728 tree
4729 type_hash_canon (unsigned int hashcode, tree type)
4730 {
4731 tree t1;
4732
4733 /* The hash table only contains main variants, so ensure that's what we're
4734 being passed. */
4735 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
4736
4737 if (!lang_hooks.types.hash_types)
4738 return type;
4739
4740 /* See if the type is in the hash table already. If so, return it.
4741 Otherwise, add the type. */
4742 t1 = type_hash_lookup (hashcode, type);
4743 if (t1 != 0)
4744 {
4745 #ifdef GATHER_STATISTICS
4746 tree_node_counts[(int) t_kind]--;
4747 tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type);
4748 #endif
4749 return t1;
4750 }
4751 else
4752 {
4753 type_hash_add (hashcode, type);
4754 return type;
4755 }
4756 }
4757
4758 /* See if the data pointed to by the type hash table is marked. We consider
4759 it marked if the type is marked or if a debug type number or symbol
4760 table entry has been made for the type. This reduces the amount of
4761 debugging output and eliminates that dependency of the debug output on
4762 the number of garbage collections. */
4763
4764 static int
4765 type_hash_marked_p (const void *p)
4766 {
4767 const_tree const type = ((const struct type_hash *) p)->type;
4768
4769 return ggc_marked_p (type) || TYPE_SYMTAB_POINTER (type);
4770 }
4771
4772 static void
4773 print_type_hash_statistics (void)
4774 {
4775 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
4776 (long) htab_size (type_hash_table),
4777 (long) htab_elements (type_hash_table),
4778 htab_collisions (type_hash_table));
4779 }
4780
4781 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
4782 with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
4783 by adding the hash codes of the individual attributes. */
4784
4785 static unsigned int
4786 attribute_hash_list (const_tree list, hashval_t hashcode)
4787 {
4788 const_tree tail;
4789
4790 for (tail = list; tail; tail = TREE_CHAIN (tail))
4791 /* ??? Do we want to add in TREE_VALUE too? */
4792 hashcode = iterative_hash_object
4793 (IDENTIFIER_HASH_VALUE (TREE_PURPOSE (tail)), hashcode);
4794 return hashcode;
4795 }
4796
4797 /* Given two lists of attributes, return true if list l2 is
4798 equivalent to l1. */
4799
4800 int
4801 attribute_list_equal (const_tree l1, const_tree l2)
4802 {
4803 return attribute_list_contained (l1, l2)
4804 && attribute_list_contained (l2, l1);
4805 }
4806
4807 /* Given two lists of attributes, return true if list L2 is
4808 completely contained within L1. */
4809 /* ??? This would be faster if attribute names were stored in a canonicalized
4810 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
4811 must be used to show these elements are equivalent (which they are). */
4812 /* ??? It's not clear that attributes with arguments will always be handled
4813 correctly. */
4814
4815 int
4816 attribute_list_contained (const_tree l1, const_tree l2)
4817 {
4818 const_tree t1, t2;
4819
4820 /* First check the obvious, maybe the lists are identical. */
4821 if (l1 == l2)
4822 return 1;
4823
4824 /* Maybe the lists are similar. */
4825 for (t1 = l1, t2 = l2;
4826 t1 != 0 && t2 != 0
4827 && TREE_PURPOSE (t1) == TREE_PURPOSE (t2)
4828 && TREE_VALUE (t1) == TREE_VALUE (t2);
4829 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2));
4830
4831 /* Maybe the lists are equal. */
4832 if (t1 == 0 && t2 == 0)
4833 return 1;
4834
4835 for (; t2 != 0; t2 = TREE_CHAIN (t2))
4836 {
4837 const_tree attr;
4838 /* This CONST_CAST is okay because lookup_attribute does not
4839 modify its argument and the return value is assigned to a
4840 const_tree. */
4841 for (attr = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (t2)),
4842 CONST_CAST_TREE(l1));
4843 attr != NULL_TREE;
4844 attr = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (t2)),
4845 TREE_CHAIN (attr)))
4846 {
4847 if (TREE_VALUE (t2) != NULL
4848 && TREE_CODE (TREE_VALUE (t2)) == TREE_LIST
4849 && TREE_VALUE (attr) != NULL
4850 && TREE_CODE (TREE_VALUE (attr)) == TREE_LIST)
4851 {
4852 if (simple_cst_list_equal (TREE_VALUE (t2),
4853 TREE_VALUE (attr)) == 1)
4854 break;
4855 }
4856 else if (simple_cst_equal (TREE_VALUE (t2), TREE_VALUE (attr)) == 1)
4857 break;
4858 }
4859
4860 if (attr == 0)
4861 return 0;
4862 }
4863
4864 return 1;
4865 }
4866
4867 /* Given two lists of types
4868 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
4869 return 1 if the lists contain the same types in the same order.
4870 Also, the TREE_PURPOSEs must match. */
4871
4872 int
4873 type_list_equal (const_tree l1, const_tree l2)
4874 {
4875 const_tree t1, t2;
4876
4877 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
4878 if (TREE_VALUE (t1) != TREE_VALUE (t2)
4879 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
4880 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
4881 && (TREE_TYPE (TREE_PURPOSE (t1))
4882 == TREE_TYPE (TREE_PURPOSE (t2))))))
4883 return 0;
4884
4885 return t1 == t2;
4886 }
4887
4888 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
4889 given by TYPE. If the argument list accepts variable arguments,
4890 then this function counts only the ordinary arguments. */
4891
4892 int
4893 type_num_arguments (const_tree type)
4894 {
4895 int i = 0;
4896 tree t;
4897
4898 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
4899 /* If the function does not take a variable number of arguments,
4900 the last element in the list will have type `void'. */
4901 if (VOID_TYPE_P (TREE_VALUE (t)))
4902 break;
4903 else
4904 ++i;
4905
4906 return i;
4907 }
4908
4909 /* Nonzero if integer constants T1 and T2
4910 represent the same constant value. */
4911
4912 int
4913 tree_int_cst_equal (const_tree t1, const_tree t2)
4914 {
4915 if (t1 == t2)
4916 return 1;
4917
4918 if (t1 == 0 || t2 == 0)
4919 return 0;
4920
4921 if (TREE_CODE (t1) == INTEGER_CST
4922 && TREE_CODE (t2) == INTEGER_CST
4923 && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
4924 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
4925 return 1;
4926
4927 return 0;
4928 }
4929
4930 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
4931 The precise way of comparison depends on their data type. */
4932
4933 int
4934 tree_int_cst_lt (const_tree t1, const_tree t2)
4935 {
4936 if (t1 == t2)
4937 return 0;
4938
4939 if (TYPE_UNSIGNED (TREE_TYPE (t1)) != TYPE_UNSIGNED (TREE_TYPE (t2)))
4940 {
4941 int t1_sgn = tree_int_cst_sgn (t1);
4942 int t2_sgn = tree_int_cst_sgn (t2);
4943
4944 if (t1_sgn < t2_sgn)
4945 return 1;
4946 else if (t1_sgn > t2_sgn)
4947 return 0;
4948 /* Otherwise, both are non-negative, so we compare them as
4949 unsigned just in case one of them would overflow a signed
4950 type. */
4951 }
4952 else if (!TYPE_UNSIGNED (TREE_TYPE (t1)))
4953 return INT_CST_LT (t1, t2);
4954
4955 return INT_CST_LT_UNSIGNED (t1, t2);
4956 }
4957
4958 /* Returns -1 if T1 < T2, 0 if T1 == T2, and 1 if T1 > T2. */
4959
4960 int
4961 tree_int_cst_compare (const_tree t1, const_tree t2)
4962 {
4963 if (tree_int_cst_lt (t1, t2))
4964 return -1;
4965 else if (tree_int_cst_lt (t2, t1))
4966 return 1;
4967 else
4968 return 0;
4969 }
4970
4971 /* Return 1 if T is an INTEGER_CST that can be manipulated efficiently on
4972 the host. If POS is zero, the value can be represented in a single
4973 HOST_WIDE_INT. If POS is nonzero, the value must be non-negative and can
4974 be represented in a single unsigned HOST_WIDE_INT. */
4975
4976 int
4977 host_integerp (const_tree t, int pos)
4978 {
4979 return (TREE_CODE (t) == INTEGER_CST
4980 && ((TREE_INT_CST_HIGH (t) == 0
4981 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) >= 0)
4982 || (! pos && TREE_INT_CST_HIGH (t) == -1
4983 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0
4984 && (!TYPE_UNSIGNED (TREE_TYPE (t))
4985 || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
4986 && TYPE_IS_SIZETYPE (TREE_TYPE (t)))))
4987 || (pos && TREE_INT_CST_HIGH (t) == 0)));
4988 }
4989
4990 /* Return the HOST_WIDE_INT least significant bits of T if it is an
4991 INTEGER_CST and there is no overflow. POS is nonzero if the result must
4992 be non-negative. We must be able to satisfy the above conditions. */
4993
4994 HOST_WIDE_INT
4995 tree_low_cst (const_tree t, int pos)
4996 {
4997 gcc_assert (host_integerp (t, pos));
4998 return TREE_INT_CST_LOW (t);
4999 }
5000
5001 /* Return the most significant bit of the integer constant T. */
5002
5003 int
5004 tree_int_cst_msb (const_tree t)
5005 {
5006 int prec;
5007 HOST_WIDE_INT h;
5008 unsigned HOST_WIDE_INT l;
5009
5010 /* Note that using TYPE_PRECISION here is wrong. We care about the
5011 actual bits, not the (arbitrary) range of the type. */
5012 prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (t))) - 1;
5013 rshift_double (TREE_INT_CST_LOW (t), TREE_INT_CST_HIGH (t), prec,
5014 2 * HOST_BITS_PER_WIDE_INT, &l, &h, 0);
5015 return (l & 1) == 1;
5016 }
5017
5018 /* Return an indication of the sign of the integer constant T.
5019 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
5020 Note that -1 will never be returned if T's type is unsigned. */
5021
5022 int
5023 tree_int_cst_sgn (const_tree t)
5024 {
5025 if (TREE_INT_CST_LOW (t) == 0 && TREE_INT_CST_HIGH (t) == 0)
5026 return 0;
5027 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
5028 return 1;
5029 else if (TREE_INT_CST_HIGH (t) < 0)
5030 return -1;
5031 else
5032 return 1;
5033 }
5034
5035 /* Compare two constructor-element-type constants. Return 1 if the lists
5036 are known to be equal; otherwise return 0. */
5037
5038 int
5039 simple_cst_list_equal (const_tree l1, const_tree l2)
5040 {
5041 while (l1 != NULL_TREE && l2 != NULL_TREE)
5042 {
5043 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
5044 return 0;
5045
5046 l1 = TREE_CHAIN (l1);
5047 l2 = TREE_CHAIN (l2);
5048 }
5049
5050 return l1 == l2;
5051 }
5052
5053 /* Return truthvalue of whether T1 is the same tree structure as T2.
5054 Return 1 if they are the same.
5055 Return 0 if they are understandably different.
5056 Return -1 if either contains tree structure not understood by
5057 this function. */
5058
5059 int
5060 simple_cst_equal (const_tree t1, const_tree t2)
5061 {
5062 enum tree_code code1, code2;
5063 int cmp;
5064 int i;
5065
5066 if (t1 == t2)
5067 return 1;
5068 if (t1 == 0 || t2 == 0)
5069 return 0;
5070
5071 code1 = TREE_CODE (t1);
5072 code2 = TREE_CODE (t2);
5073
5074 if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
5075 {
5076 if (CONVERT_EXPR_CODE_P (code2)
5077 || code2 == NON_LVALUE_EXPR)
5078 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
5079 else
5080 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
5081 }
5082
5083 else if (CONVERT_EXPR_CODE_P (code2)
5084 || code2 == NON_LVALUE_EXPR)
5085 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
5086
5087 if (code1 != code2)
5088 return 0;
5089
5090 switch (code1)
5091 {
5092 case INTEGER_CST:
5093 return (TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
5094 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2));
5095
5096 case REAL_CST:
5097 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
5098
5099 case FIXED_CST:
5100 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
5101
5102 case STRING_CST:
5103 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
5104 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
5105 TREE_STRING_LENGTH (t1)));
5106
5107 case CONSTRUCTOR:
5108 {
5109 unsigned HOST_WIDE_INT idx;
5110 VEC(constructor_elt, gc) *v1 = CONSTRUCTOR_ELTS (t1);
5111 VEC(constructor_elt, gc) *v2 = CONSTRUCTOR_ELTS (t2);
5112
5113 if (VEC_length (constructor_elt, v1) != VEC_length (constructor_elt, v2))
5114 return false;
5115
5116 for (idx = 0; idx < VEC_length (constructor_elt, v1); ++idx)
5117 /* ??? Should we handle also fields here? */
5118 if (!simple_cst_equal (VEC_index (constructor_elt, v1, idx)->value,
5119 VEC_index (constructor_elt, v2, idx)->value))
5120 return false;
5121 return true;
5122 }
5123
5124 case SAVE_EXPR:
5125 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
5126
5127 case CALL_EXPR:
5128 cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
5129 if (cmp <= 0)
5130 return cmp;
5131 if (call_expr_nargs (t1) != call_expr_nargs (t2))
5132 return 0;
5133 {
5134 const_tree arg1, arg2;
5135 const_call_expr_arg_iterator iter1, iter2;
5136 for (arg1 = first_const_call_expr_arg (t1, &iter1),
5137 arg2 = first_const_call_expr_arg (t2, &iter2);
5138 arg1 && arg2;
5139 arg1 = next_const_call_expr_arg (&iter1),
5140 arg2 = next_const_call_expr_arg (&iter2))
5141 {
5142 cmp = simple_cst_equal (arg1, arg2);
5143 if (cmp <= 0)
5144 return cmp;
5145 }
5146 return arg1 == arg2;
5147 }
5148
5149 case TARGET_EXPR:
5150 /* Special case: if either target is an unallocated VAR_DECL,
5151 it means that it's going to be unified with whatever the
5152 TARGET_EXPR is really supposed to initialize, so treat it
5153 as being equivalent to anything. */
5154 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
5155 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
5156 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
5157 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
5158 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
5159 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
5160 cmp = 1;
5161 else
5162 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
5163
5164 if (cmp <= 0)
5165 return cmp;
5166
5167 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
5168
5169 case WITH_CLEANUP_EXPR:
5170 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
5171 if (cmp <= 0)
5172 return cmp;
5173
5174 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
5175
5176 case COMPONENT_REF:
5177 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
5178 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
5179
5180 return 0;
5181
5182 case VAR_DECL:
5183 case PARM_DECL:
5184 case CONST_DECL:
5185 case FUNCTION_DECL:
5186 return 0;
5187
5188 default:
5189 break;
5190 }
5191
5192 /* This general rule works for most tree codes. All exceptions should be
5193 handled above. If this is a language-specific tree code, we can't
5194 trust what might be in the operand, so say we don't know
5195 the situation. */
5196 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
5197 return -1;
5198
5199 switch (TREE_CODE_CLASS (code1))
5200 {
5201 case tcc_unary:
5202 case tcc_binary:
5203 case tcc_comparison:
5204 case tcc_expression:
5205 case tcc_reference:
5206 case tcc_statement:
5207 cmp = 1;
5208 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
5209 {
5210 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
5211 if (cmp <= 0)
5212 return cmp;
5213 }
5214
5215 return cmp;
5216
5217 default:
5218 return -1;
5219 }
5220 }
5221
5222 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
5223 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
5224 than U, respectively. */
5225
5226 int
5227 compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
5228 {
5229 if (tree_int_cst_sgn (t) < 0)
5230 return -1;
5231 else if (TREE_INT_CST_HIGH (t) != 0)
5232 return 1;
5233 else if (TREE_INT_CST_LOW (t) == u)
5234 return 0;
5235 else if (TREE_INT_CST_LOW (t) < u)
5236 return -1;
5237 else
5238 return 1;
5239 }
5240
5241 /* Return true if CODE represents an associative tree code. Otherwise
5242 return false. */
5243 bool
5244 associative_tree_code (enum tree_code code)
5245 {
5246 switch (code)
5247 {
5248 case BIT_IOR_EXPR:
5249 case BIT_AND_EXPR:
5250 case BIT_XOR_EXPR:
5251 case PLUS_EXPR:
5252 case MULT_EXPR:
5253 case MIN_EXPR:
5254 case MAX_EXPR:
5255 return true;
5256
5257 default:
5258 break;
5259 }
5260 return false;
5261 }
5262
5263 /* Return true if CODE represents a commutative tree code. Otherwise
5264 return false. */
5265 bool
5266 commutative_tree_code (enum tree_code code)
5267 {
5268 switch (code)
5269 {
5270 case PLUS_EXPR:
5271 case MULT_EXPR:
5272 case MIN_EXPR:
5273 case MAX_EXPR:
5274 case BIT_IOR_EXPR:
5275 case BIT_XOR_EXPR:
5276 case BIT_AND_EXPR:
5277 case NE_EXPR:
5278 case EQ_EXPR:
5279 case UNORDERED_EXPR:
5280 case ORDERED_EXPR:
5281 case UNEQ_EXPR:
5282 case LTGT_EXPR:
5283 case TRUTH_AND_EXPR:
5284 case TRUTH_XOR_EXPR:
5285 case TRUTH_OR_EXPR:
5286 return true;
5287
5288 default:
5289 break;
5290 }
5291 return false;
5292 }
5293
5294 /* Generate a hash value for an expression. This can be used iteratively
5295 by passing a previous result as the VAL argument.
5296
5297 This function is intended to produce the same hash for expressions which
5298 would compare equal using operand_equal_p. */
5299
5300 hashval_t
5301 iterative_hash_expr (const_tree t, hashval_t val)
5302 {
5303 int i;
5304 enum tree_code code;
5305 char tclass;
5306
5307 if (t == NULL_TREE)
5308 return iterative_hash_pointer (t, val);
5309
5310 code = TREE_CODE (t);
5311
5312 switch (code)
5313 {
5314 /* Alas, constants aren't shared, so we can't rely on pointer
5315 identity. */
5316 case INTEGER_CST:
5317 val = iterative_hash_host_wide_int (TREE_INT_CST_LOW (t), val);
5318 return iterative_hash_host_wide_int (TREE_INT_CST_HIGH (t), val);
5319 case REAL_CST:
5320 {
5321 unsigned int val2 = real_hash (TREE_REAL_CST_PTR (t));
5322
5323 return iterative_hash_hashval_t (val2, val);
5324 }
5325 case FIXED_CST:
5326 {
5327 unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
5328
5329 return iterative_hash_hashval_t (val2, val);
5330 }
5331 case STRING_CST:
5332 return iterative_hash (TREE_STRING_POINTER (t),
5333 TREE_STRING_LENGTH (t), val);
5334 case COMPLEX_CST:
5335 val = iterative_hash_expr (TREE_REALPART (t), val);
5336 return iterative_hash_expr (TREE_IMAGPART (t), val);
5337 case VECTOR_CST:
5338 return iterative_hash_expr (TREE_VECTOR_CST_ELTS (t), val);
5339
5340 case SSA_NAME:
5341 /* we can just compare by pointer. */
5342 return iterative_hash_pointer (t, val);
5343
5344 case TREE_LIST:
5345 /* A list of expressions, for a CALL_EXPR or as the elements of a
5346 VECTOR_CST. */
5347 for (; t; t = TREE_CHAIN (t))
5348 val = iterative_hash_expr (TREE_VALUE (t), val);
5349 return val;
5350 case CONSTRUCTOR:
5351 {
5352 unsigned HOST_WIDE_INT idx;
5353 tree field, value;
5354 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
5355 {
5356 val = iterative_hash_expr (field, val);
5357 val = iterative_hash_expr (value, val);
5358 }
5359 return val;
5360 }
5361 case FUNCTION_DECL:
5362 /* When referring to a built-in FUNCTION_DECL, use the
5363 __builtin__ form. Otherwise nodes that compare equal
5364 according to operand_equal_p might get different
5365 hash codes. */
5366 if (DECL_BUILT_IN (t))
5367 {
5368 val = iterative_hash_pointer (built_in_decls[DECL_FUNCTION_CODE (t)],
5369 val);
5370 return val;
5371 }
5372 /* else FALL THROUGH */
5373 default:
5374 tclass = TREE_CODE_CLASS (code);
5375
5376 if (tclass == tcc_declaration)
5377 {
5378 /* DECL's have a unique ID */
5379 val = iterative_hash_host_wide_int (DECL_UID (t), val);
5380 }
5381 else
5382 {
5383 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
5384
5385 val = iterative_hash_object (code, val);
5386
5387 /* Don't hash the type, that can lead to having nodes which
5388 compare equal according to operand_equal_p, but which
5389 have different hash codes. */
5390 if (CONVERT_EXPR_CODE_P (code)
5391 || code == NON_LVALUE_EXPR)
5392 {
5393 /* Make sure to include signness in the hash computation. */
5394 val += TYPE_UNSIGNED (TREE_TYPE (t));
5395 val = iterative_hash_expr (TREE_OPERAND (t, 0), val);
5396 }
5397
5398 else if (commutative_tree_code (code))
5399 {
5400 /* It's a commutative expression. We want to hash it the same
5401 however it appears. We do this by first hashing both operands
5402 and then rehashing based on the order of their independent
5403 hashes. */
5404 hashval_t one = iterative_hash_expr (TREE_OPERAND (t, 0), 0);
5405 hashval_t two = iterative_hash_expr (TREE_OPERAND (t, 1), 0);
5406 hashval_t t;
5407
5408 if (one > two)
5409 t = one, one = two, two = t;
5410
5411 val = iterative_hash_hashval_t (one, val);
5412 val = iterative_hash_hashval_t (two, val);
5413 }
5414 else
5415 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
5416 val = iterative_hash_expr (TREE_OPERAND (t, i), val);
5417 }
5418 return val;
5419 break;
5420 }
5421 }
5422
5423 /* Generate a hash value for a pair of expressions. This can be used
5424 iteratively by passing a previous result as the VAL argument.
5425
5426 The same hash value is always returned for a given pair of expressions,
5427 regardless of the order in which they are presented. This is useful in
5428 hashing the operands of commutative functions. */
5429
5430 hashval_t
5431 iterative_hash_exprs_commutative (const_tree t1,
5432 const_tree t2, hashval_t val)
5433 {
5434 hashval_t one = iterative_hash_expr (t1, 0);
5435 hashval_t two = iterative_hash_expr (t2, 0);
5436 hashval_t t;
5437
5438 if (one > two)
5439 t = one, one = two, two = t;
5440 val = iterative_hash_hashval_t (one, val);
5441 val = iterative_hash_hashval_t (two, val);
5442
5443 return val;
5444 }
5445 \f
5446 /* Constructors for pointer, array and function types.
5447 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
5448 constructed by language-dependent code, not here.) */
5449
5450 /* Construct, lay out and return the type of pointers to TO_TYPE with
5451 mode MODE. If CAN_ALIAS_ALL is TRUE, indicate this type can
5452 reference all of memory. If such a type has already been
5453 constructed, reuse it. */
5454
5455 tree
5456 build_pointer_type_for_mode (tree to_type, enum machine_mode mode,
5457 bool can_alias_all)
5458 {
5459 tree t;
5460
5461 if (to_type == error_mark_node)
5462 return error_mark_node;
5463
5464 /* If the pointed-to type has the may_alias attribute set, force
5465 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
5466 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
5467 can_alias_all = true;
5468
5469 /* In some cases, languages will have things that aren't a POINTER_TYPE
5470 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
5471 In that case, return that type without regard to the rest of our
5472 operands.
5473
5474 ??? This is a kludge, but consistent with the way this function has
5475 always operated and there doesn't seem to be a good way to avoid this
5476 at the moment. */
5477 if (TYPE_POINTER_TO (to_type) != 0
5478 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
5479 return TYPE_POINTER_TO (to_type);
5480
5481 /* First, if we already have a type for pointers to TO_TYPE and it's
5482 the proper mode, use it. */
5483 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
5484 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
5485 return t;
5486
5487 t = make_node (POINTER_TYPE);
5488
5489 TREE_TYPE (t) = to_type;
5490 TYPE_MODE (t) = mode;
5491 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
5492 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
5493 TYPE_POINTER_TO (to_type) = t;
5494
5495 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
5496 SET_TYPE_STRUCTURAL_EQUALITY (t);
5497 else if (TYPE_CANONICAL (to_type) != to_type)
5498 TYPE_CANONICAL (t)
5499 = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
5500 mode, can_alias_all);
5501
5502 /* Lay out the type. This function has many callers that are concerned
5503 with expression-construction, and this simplifies them all. */
5504 layout_type (t);
5505
5506 return t;
5507 }
5508
5509 /* By default build pointers in ptr_mode. */
5510
5511 tree
5512 build_pointer_type (tree to_type)
5513 {
5514 return build_pointer_type_for_mode (to_type, ptr_mode, false);
5515 }
5516
5517 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
5518
5519 tree
5520 build_reference_type_for_mode (tree to_type, enum machine_mode mode,
5521 bool can_alias_all)
5522 {
5523 tree t;
5524
5525 if (to_type == error_mark_node)
5526 return error_mark_node;
5527
5528 /* If the pointed-to type has the may_alias attribute set, force
5529 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
5530 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
5531 can_alias_all = true;
5532
5533 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
5534 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
5535 In that case, return that type without regard to the rest of our
5536 operands.
5537
5538 ??? This is a kludge, but consistent with the way this function has
5539 always operated and there doesn't seem to be a good way to avoid this
5540 at the moment. */
5541 if (TYPE_REFERENCE_TO (to_type) != 0
5542 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
5543 return TYPE_REFERENCE_TO (to_type);
5544
5545 /* First, if we already have a type for pointers to TO_TYPE and it's
5546 the proper mode, use it. */
5547 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
5548 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
5549 return t;
5550
5551 t = make_node (REFERENCE_TYPE);
5552
5553 TREE_TYPE (t) = to_type;
5554 TYPE_MODE (t) = mode;
5555 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
5556 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
5557 TYPE_REFERENCE_TO (to_type) = t;
5558
5559 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
5560 SET_TYPE_STRUCTURAL_EQUALITY (t);
5561 else if (TYPE_CANONICAL (to_type) != to_type)
5562 TYPE_CANONICAL (t)
5563 = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
5564 mode, can_alias_all);
5565
5566 layout_type (t);
5567
5568 return t;
5569 }
5570
5571
5572 /* Build the node for the type of references-to-TO_TYPE by default
5573 in ptr_mode. */
5574
5575 tree
5576 build_reference_type (tree to_type)
5577 {
5578 return build_reference_type_for_mode (to_type, ptr_mode, false);
5579 }
5580
5581 /* Build a type that is compatible with t but has no cv quals anywhere
5582 in its type, thus
5583
5584 const char *const *const * -> char ***. */
5585
5586 tree
5587 build_type_no_quals (tree t)
5588 {
5589 switch (TREE_CODE (t))
5590 {
5591 case POINTER_TYPE:
5592 return build_pointer_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
5593 TYPE_MODE (t),
5594 TYPE_REF_CAN_ALIAS_ALL (t));
5595 case REFERENCE_TYPE:
5596 return
5597 build_reference_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
5598 TYPE_MODE (t),
5599 TYPE_REF_CAN_ALIAS_ALL (t));
5600 default:
5601 return TYPE_MAIN_VARIANT (t);
5602 }
5603 }
5604
5605 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
5606 MAXVAL should be the maximum value in the domain
5607 (one less than the length of the array).
5608
5609 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
5610 We don't enforce this limit, that is up to caller (e.g. language front end).
5611 The limit exists because the result is a signed type and we don't handle
5612 sizes that use more than one HOST_WIDE_INT. */
5613
5614 tree
5615 build_index_type (tree maxval)
5616 {
5617 tree itype = make_node (INTEGER_TYPE);
5618
5619 TREE_TYPE (itype) = sizetype;
5620 TYPE_PRECISION (itype) = TYPE_PRECISION (sizetype);
5621 TYPE_MIN_VALUE (itype) = size_zero_node;
5622 TYPE_MAX_VALUE (itype) = fold_convert (sizetype, maxval);
5623 TYPE_MODE (itype) = TYPE_MODE (sizetype);
5624 TYPE_SIZE (itype) = TYPE_SIZE (sizetype);
5625 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (sizetype);
5626 TYPE_ALIGN (itype) = TYPE_ALIGN (sizetype);
5627 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (sizetype);
5628
5629 if (host_integerp (maxval, 1))
5630 return type_hash_canon (tree_low_cst (maxval, 1), itype);
5631 else
5632 {
5633 /* Since we cannot hash this type, we need to compare it using
5634 structural equality checks. */
5635 SET_TYPE_STRUCTURAL_EQUALITY (itype);
5636 return itype;
5637 }
5638 }
5639
5640 /* Builds a signed or unsigned integer type of precision PRECISION.
5641 Used for C bitfields whose precision does not match that of
5642 built-in target types. */
5643 tree
5644 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
5645 int unsignedp)
5646 {
5647 tree itype = make_node (INTEGER_TYPE);
5648
5649 TYPE_PRECISION (itype) = precision;
5650
5651 if (unsignedp)
5652 fixup_unsigned_type (itype);
5653 else
5654 fixup_signed_type (itype);
5655
5656 if (host_integerp (TYPE_MAX_VALUE (itype), 1))
5657 return type_hash_canon (tree_low_cst (TYPE_MAX_VALUE (itype), 1), itype);
5658
5659 return itype;
5660 }
5661
5662 /* Create a range of some discrete type TYPE (an INTEGER_TYPE,
5663 ENUMERAL_TYPE or BOOLEAN_TYPE), with low bound LOWVAL and
5664 high bound HIGHVAL. If TYPE is NULL, sizetype is used. */
5665
5666 tree
5667 build_range_type (tree type, tree lowval, tree highval)
5668 {
5669 tree itype = make_node (INTEGER_TYPE);
5670
5671 TREE_TYPE (itype) = type;
5672 if (type == NULL_TREE)
5673 type = sizetype;
5674
5675 TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
5676 TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
5677
5678 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
5679 TYPE_MODE (itype) = TYPE_MODE (type);
5680 TYPE_SIZE (itype) = TYPE_SIZE (type);
5681 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
5682 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
5683 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
5684
5685 if (host_integerp (lowval, 0) && highval != 0 && host_integerp (highval, 0))
5686 return type_hash_canon (tree_low_cst (highval, 0)
5687 - tree_low_cst (lowval, 0),
5688 itype);
5689 else
5690 return itype;
5691 }
5692
5693 /* Just like build_index_type, but takes lowval and highval instead
5694 of just highval (maxval). */
5695
5696 tree
5697 build_index_2_type (tree lowval, tree highval)
5698 {
5699 return build_range_type (sizetype, lowval, highval);
5700 }
5701
5702 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
5703 and number of elements specified by the range of values of INDEX_TYPE.
5704 If such a type has already been constructed, reuse it. */
5705
5706 tree
5707 build_array_type (tree elt_type, tree index_type)
5708 {
5709 tree t;
5710 hashval_t hashcode = 0;
5711
5712 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
5713 {
5714 error ("arrays of functions are not meaningful");
5715 elt_type = integer_type_node;
5716 }
5717
5718 t = make_node (ARRAY_TYPE);
5719 TREE_TYPE (t) = elt_type;
5720 TYPE_DOMAIN (t) = index_type;
5721
5722 if (index_type == 0)
5723 {
5724 tree save = t;
5725 hashcode = iterative_hash_object (TYPE_HASH (elt_type), hashcode);
5726 t = type_hash_canon (hashcode, t);
5727 if (save == t)
5728 layout_type (t);
5729
5730 if (TYPE_CANONICAL (t) == t)
5731 {
5732 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type))
5733 SET_TYPE_STRUCTURAL_EQUALITY (t);
5734 else if (TYPE_CANONICAL (elt_type) != elt_type)
5735 TYPE_CANONICAL (t)
5736 = build_array_type (TYPE_CANONICAL (elt_type), index_type);
5737 }
5738
5739 return t;
5740 }
5741
5742 hashcode = iterative_hash_object (TYPE_HASH (elt_type), hashcode);
5743 hashcode = iterative_hash_object (TYPE_HASH (index_type), hashcode);
5744 t = type_hash_canon (hashcode, t);
5745
5746 if (!COMPLETE_TYPE_P (t))
5747 layout_type (t);
5748
5749 if (TYPE_CANONICAL (t) == t)
5750 {
5751 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
5752 || TYPE_STRUCTURAL_EQUALITY_P (index_type))
5753 SET_TYPE_STRUCTURAL_EQUALITY (t);
5754 else if (TYPE_CANONICAL (elt_type) != elt_type
5755 || TYPE_CANONICAL (index_type) != index_type)
5756 TYPE_CANONICAL (t)
5757 = build_array_type (TYPE_CANONICAL (elt_type),
5758 TYPE_CANONICAL (index_type));
5759 }
5760
5761 return t;
5762 }
5763
5764 /* Recursively examines the array elements of TYPE, until a non-array
5765 element type is found. */
5766
5767 tree
5768 strip_array_types (tree type)
5769 {
5770 while (TREE_CODE (type) == ARRAY_TYPE)
5771 type = TREE_TYPE (type);
5772
5773 return type;
5774 }
5775
5776 /* Computes the canonical argument types from the argument type list
5777 ARGTYPES.
5778
5779 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
5780 on entry to this function, or if any of the ARGTYPES are
5781 structural.
5782
5783 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
5784 true on entry to this function, or if any of the ARGTYPES are
5785 non-canonical.
5786
5787 Returns a canonical argument list, which may be ARGTYPES when the
5788 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
5789 true) or would not differ from ARGTYPES. */
5790
5791 static tree
5792 maybe_canonicalize_argtypes(tree argtypes,
5793 bool *any_structural_p,
5794 bool *any_noncanonical_p)
5795 {
5796 tree arg;
5797 bool any_noncanonical_argtypes_p = false;
5798
5799 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
5800 {
5801 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
5802 /* Fail gracefully by stating that the type is structural. */
5803 *any_structural_p = true;
5804 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
5805 *any_structural_p = true;
5806 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
5807 || TREE_PURPOSE (arg))
5808 /* If the argument has a default argument, we consider it
5809 non-canonical even though the type itself is canonical.
5810 That way, different variants of function and method types
5811 with default arguments will all point to the variant with
5812 no defaults as their canonical type. */
5813 any_noncanonical_argtypes_p = true;
5814 }
5815
5816 if (*any_structural_p)
5817 return argtypes;
5818
5819 if (any_noncanonical_argtypes_p)
5820 {
5821 /* Build the canonical list of argument types. */
5822 tree canon_argtypes = NULL_TREE;
5823 bool is_void = false;
5824
5825 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
5826 {
5827 if (arg == void_list_node)
5828 is_void = true;
5829 else
5830 canon_argtypes = tree_cons (NULL_TREE,
5831 TYPE_CANONICAL (TREE_VALUE (arg)),
5832 canon_argtypes);
5833 }
5834
5835 canon_argtypes = nreverse (canon_argtypes);
5836 if (is_void)
5837 canon_argtypes = chainon (canon_argtypes, void_list_node);
5838
5839 /* There is a non-canonical type. */
5840 *any_noncanonical_p = true;
5841 return canon_argtypes;
5842 }
5843
5844 /* The canonical argument types are the same as ARGTYPES. */
5845 return argtypes;
5846 }
5847
5848 /* Construct, lay out and return
5849 the type of functions returning type VALUE_TYPE
5850 given arguments of types ARG_TYPES.
5851 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
5852 are data type nodes for the arguments of the function.
5853 If such a type has already been constructed, reuse it. */
5854
5855 tree
5856 build_function_type (tree value_type, tree arg_types)
5857 {
5858 tree t;
5859 hashval_t hashcode = 0;
5860 bool any_structural_p, any_noncanonical_p;
5861 tree canon_argtypes;
5862
5863 if (TREE_CODE (value_type) == FUNCTION_TYPE)
5864 {
5865 error ("function return type cannot be function");
5866 value_type = integer_type_node;
5867 }
5868
5869 /* Make a node of the sort we want. */
5870 t = make_node (FUNCTION_TYPE);
5871 TREE_TYPE (t) = value_type;
5872 TYPE_ARG_TYPES (t) = arg_types;
5873
5874 /* If we already have such a type, use the old one. */
5875 hashcode = iterative_hash_object (TYPE_HASH (value_type), hashcode);
5876 hashcode = type_hash_list (arg_types, hashcode);
5877 t = type_hash_canon (hashcode, t);
5878
5879 /* Set up the canonical type. */
5880 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
5881 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
5882 canon_argtypes = maybe_canonicalize_argtypes (arg_types,
5883 &any_structural_p,
5884 &any_noncanonical_p);
5885 if (any_structural_p)
5886 SET_TYPE_STRUCTURAL_EQUALITY (t);
5887 else if (any_noncanonical_p)
5888 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
5889 canon_argtypes);
5890
5891 if (!COMPLETE_TYPE_P (t))
5892 layout_type (t);
5893 return t;
5894 }
5895
5896 /* Build variant of function type ORIG_TYPE skipping ARGS_TO_SKIP. */
5897
5898 tree
5899 build_function_type_skip_args (tree orig_type, bitmap args_to_skip)
5900 {
5901 tree new_type = NULL;
5902 tree args, new_args = NULL, t;
5903 tree new_reversed;
5904 int i = 0;
5905
5906 for (args = TYPE_ARG_TYPES (orig_type); args && args != void_list_node;
5907 args = TREE_CHAIN (args), i++)
5908 if (!bitmap_bit_p (args_to_skip, i))
5909 new_args = tree_cons (NULL_TREE, TREE_VALUE (args), new_args);
5910
5911 new_reversed = nreverse (new_args);
5912 if (args)
5913 {
5914 if (new_reversed)
5915 TREE_CHAIN (new_args) = void_list_node;
5916 else
5917 new_reversed = void_list_node;
5918 }
5919 gcc_assert (new_reversed);
5920
5921 /* Use copy_node to preserve as much as possible from original type
5922 (debug info, attribute lists etc.)
5923 Exception is METHOD_TYPEs must have THIS argument.
5924 When we are asked to remove it, we need to build new FUNCTION_TYPE
5925 instead. */
5926 if (TREE_CODE (orig_type) != METHOD_TYPE
5927 || !bitmap_bit_p (args_to_skip, 0))
5928 {
5929 new_type = copy_node (orig_type);
5930 TYPE_ARG_TYPES (new_type) = new_reversed;
5931 }
5932 else
5933 {
5934 new_type
5935 = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type),
5936 new_reversed));
5937 TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
5938 }
5939
5940 /* This is a new type, not a copy of an old type. Need to reassociate
5941 variants. We can handle everything except the main variant lazily. */
5942 t = TYPE_MAIN_VARIANT (orig_type);
5943 if (orig_type != t)
5944 {
5945 TYPE_MAIN_VARIANT (new_type) = t;
5946 TYPE_NEXT_VARIANT (new_type) = TYPE_NEXT_VARIANT (t);
5947 TYPE_NEXT_VARIANT (t) = new_type;
5948 }
5949 else
5950 {
5951 TYPE_MAIN_VARIANT (new_type) = new_type;
5952 TYPE_NEXT_VARIANT (new_type) = NULL;
5953 }
5954 return new_type;
5955 }
5956
5957 /* Build variant of function type ORIG_TYPE skipping ARGS_TO_SKIP.
5958
5959 Arguments from DECL_ARGUMENTS list can't be removed now, since they are
5960 linked by TREE_CHAIN directly. It is caller responsibility to eliminate
5961 them when they are being duplicated (i.e. copy_arguments_for_versioning). */
5962
5963 tree
5964 build_function_decl_skip_args (tree orig_decl, bitmap args_to_skip)
5965 {
5966 tree new_decl = copy_node (orig_decl);
5967 tree new_type;
5968
5969 new_type = TREE_TYPE (orig_decl);
5970 if (prototype_p (new_type))
5971 new_type = build_function_type_skip_args (new_type, args_to_skip);
5972 TREE_TYPE (new_decl) = new_type;
5973
5974 /* For declarations setting DECL_VINDEX (i.e. methods)
5975 we expect first argument to be THIS pointer. */
5976 if (bitmap_bit_p (args_to_skip, 0))
5977 DECL_VINDEX (new_decl) = NULL_TREE;
5978 return new_decl;
5979 }
5980
5981 /* Build a function type. The RETURN_TYPE is the type returned by the
5982 function. If VAARGS is set, no void_type_node is appended to the
5983 the list. ARGP muse be alway be terminated be a NULL_TREE. */
5984
5985 static tree
5986 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
5987 {
5988 tree t, args, last;
5989
5990 t = va_arg (argp, tree);
5991 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
5992 args = tree_cons (NULL_TREE, t, args);
5993
5994 if (vaargs)
5995 {
5996 last = args;
5997 if (args != NULL_TREE)
5998 args = nreverse (args);
5999 gcc_assert (args != NULL_TREE && last != void_list_node);
6000 }
6001 else if (args == NULL_TREE)
6002 args = void_list_node;
6003 else
6004 {
6005 last = args;
6006 args = nreverse (args);
6007 TREE_CHAIN (last) = void_list_node;
6008 }
6009 args = build_function_type (return_type, args);
6010
6011 return args;
6012 }
6013
6014 /* Build a function type. The RETURN_TYPE is the type returned by the
6015 function. If additional arguments are provided, they are
6016 additional argument types. The list of argument types must always
6017 be terminated by NULL_TREE. */
6018
6019 tree
6020 build_function_type_list (tree return_type, ...)
6021 {
6022 tree args;
6023 va_list p;
6024
6025 va_start (p, return_type);
6026 args = build_function_type_list_1 (false, return_type, p);
6027 va_end (p);
6028 return args;
6029 }
6030
6031 /* Build a variable argument function type. The RETURN_TYPE is the
6032 type returned by the function. If additional arguments are provided,
6033 they are additional argument types. The list of argument types must
6034 always be terminated by NULL_TREE. */
6035
6036 tree
6037 build_varargs_function_type_list (tree return_type, ...)
6038 {
6039 tree args;
6040 va_list p;
6041
6042 va_start (p, return_type);
6043 args = build_function_type_list_1 (true, return_type, p);
6044 va_end (p);
6045
6046 return args;
6047 }
6048
6049 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
6050 and ARGTYPES (a TREE_LIST) are the return type and arguments types
6051 for the method. An implicit additional parameter (of type
6052 pointer-to-BASETYPE) is added to the ARGTYPES. */
6053
6054 tree
6055 build_method_type_directly (tree basetype,
6056 tree rettype,
6057 tree argtypes)
6058 {
6059 tree t;
6060 tree ptype;
6061 int hashcode = 0;
6062 bool any_structural_p, any_noncanonical_p;
6063 tree canon_argtypes;
6064
6065 /* Make a node of the sort we want. */
6066 t = make_node (METHOD_TYPE);
6067
6068 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
6069 TREE_TYPE (t) = rettype;
6070 ptype = build_pointer_type (basetype);
6071
6072 /* The actual arglist for this function includes a "hidden" argument
6073 which is "this". Put it into the list of argument types. */
6074 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
6075 TYPE_ARG_TYPES (t) = argtypes;
6076
6077 /* If we already have such a type, use the old one. */
6078 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
6079 hashcode = iterative_hash_object (TYPE_HASH (rettype), hashcode);
6080 hashcode = type_hash_list (argtypes, hashcode);
6081 t = type_hash_canon (hashcode, t);
6082
6083 /* Set up the canonical type. */
6084 any_structural_p
6085 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
6086 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
6087 any_noncanonical_p
6088 = (TYPE_CANONICAL (basetype) != basetype
6089 || TYPE_CANONICAL (rettype) != rettype);
6090 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
6091 &any_structural_p,
6092 &any_noncanonical_p);
6093 if (any_structural_p)
6094 SET_TYPE_STRUCTURAL_EQUALITY (t);
6095 else if (any_noncanonical_p)
6096 TYPE_CANONICAL (t)
6097 = build_method_type_directly (TYPE_CANONICAL (basetype),
6098 TYPE_CANONICAL (rettype),
6099 canon_argtypes);
6100 if (!COMPLETE_TYPE_P (t))
6101 layout_type (t);
6102
6103 return t;
6104 }
6105
6106 /* Construct, lay out and return the type of methods belonging to class
6107 BASETYPE and whose arguments and values are described by TYPE.
6108 If that type exists already, reuse it.
6109 TYPE must be a FUNCTION_TYPE node. */
6110
6111 tree
6112 build_method_type (tree basetype, tree type)
6113 {
6114 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
6115
6116 return build_method_type_directly (basetype,
6117 TREE_TYPE (type),
6118 TYPE_ARG_TYPES (type));
6119 }
6120
6121 /* Construct, lay out and return the type of offsets to a value
6122 of type TYPE, within an object of type BASETYPE.
6123 If a suitable offset type exists already, reuse it. */
6124
6125 tree
6126 build_offset_type (tree basetype, tree type)
6127 {
6128 tree t;
6129 hashval_t hashcode = 0;
6130
6131 /* Make a node of the sort we want. */
6132 t = make_node (OFFSET_TYPE);
6133
6134 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
6135 TREE_TYPE (t) = type;
6136
6137 /* If we already have such a type, use the old one. */
6138 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
6139 hashcode = iterative_hash_object (TYPE_HASH (type), hashcode);
6140 t = type_hash_canon (hashcode, t);
6141
6142 if (!COMPLETE_TYPE_P (t))
6143 layout_type (t);
6144
6145 if (TYPE_CANONICAL (t) == t)
6146 {
6147 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
6148 || TYPE_STRUCTURAL_EQUALITY_P (type))
6149 SET_TYPE_STRUCTURAL_EQUALITY (t);
6150 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
6151 || TYPE_CANONICAL (type) != type)
6152 TYPE_CANONICAL (t)
6153 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
6154 TYPE_CANONICAL (type));
6155 }
6156
6157 return t;
6158 }
6159
6160 /* Create a complex type whose components are COMPONENT_TYPE. */
6161
6162 tree
6163 build_complex_type (tree component_type)
6164 {
6165 tree t;
6166 hashval_t hashcode;
6167
6168 /* Make a node of the sort we want. */
6169 t = make_node (COMPLEX_TYPE);
6170
6171 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
6172
6173 /* If we already have such a type, use the old one. */
6174 hashcode = iterative_hash_object (TYPE_HASH (component_type), 0);
6175 t = type_hash_canon (hashcode, t);
6176
6177 if (!COMPLETE_TYPE_P (t))
6178 layout_type (t);
6179
6180 if (TYPE_CANONICAL (t) == t)
6181 {
6182 if (TYPE_STRUCTURAL_EQUALITY_P (component_type))
6183 SET_TYPE_STRUCTURAL_EQUALITY (t);
6184 else if (TYPE_CANONICAL (component_type) != component_type)
6185 TYPE_CANONICAL (t)
6186 = build_complex_type (TYPE_CANONICAL (component_type));
6187 }
6188
6189 /* We need to create a name, since complex is a fundamental type. */
6190 if (! TYPE_NAME (t))
6191 {
6192 const char *name;
6193 if (component_type == char_type_node)
6194 name = "complex char";
6195 else if (component_type == signed_char_type_node)
6196 name = "complex signed char";
6197 else if (component_type == unsigned_char_type_node)
6198 name = "complex unsigned char";
6199 else if (component_type == short_integer_type_node)
6200 name = "complex short int";
6201 else if (component_type == short_unsigned_type_node)
6202 name = "complex short unsigned int";
6203 else if (component_type == integer_type_node)
6204 name = "complex int";
6205 else if (component_type == unsigned_type_node)
6206 name = "complex unsigned int";
6207 else if (component_type == long_integer_type_node)
6208 name = "complex long int";
6209 else if (component_type == long_unsigned_type_node)
6210 name = "complex long unsigned int";
6211 else if (component_type == long_long_integer_type_node)
6212 name = "complex long long int";
6213 else if (component_type == long_long_unsigned_type_node)
6214 name = "complex long long unsigned int";
6215 else
6216 name = 0;
6217
6218 if (name != 0)
6219 TYPE_NAME (t) = build_decl (TYPE_DECL, get_identifier (name), t);
6220 }
6221
6222 return build_qualified_type (t, TYPE_QUALS (component_type));
6223 }
6224 \f
6225 /* Return OP, stripped of any conversions to wider types as much as is safe.
6226 Converting the value back to OP's type makes a value equivalent to OP.
6227
6228 If FOR_TYPE is nonzero, we return a value which, if converted to
6229 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
6230
6231 OP must have integer, real or enumeral type. Pointers are not allowed!
6232
6233 There are some cases where the obvious value we could return
6234 would regenerate to OP if converted to OP's type,
6235 but would not extend like OP to wider types.
6236 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
6237 For example, if OP is (unsigned short)(signed char)-1,
6238 we avoid returning (signed char)-1 if FOR_TYPE is int,
6239 even though extending that to an unsigned short would regenerate OP,
6240 since the result of extending (signed char)-1 to (int)
6241 is different from (int) OP. */
6242
6243 tree
6244 get_unwidened (tree op, tree for_type)
6245 {
6246 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
6247 tree type = TREE_TYPE (op);
6248 unsigned final_prec
6249 = TYPE_PRECISION (for_type != 0 ? for_type : type);
6250 int uns
6251 = (for_type != 0 && for_type != type
6252 && final_prec > TYPE_PRECISION (type)
6253 && TYPE_UNSIGNED (type));
6254 tree win = op;
6255
6256 while (CONVERT_EXPR_P (op))
6257 {
6258 int bitschange;
6259
6260 /* TYPE_PRECISION on vector types has different meaning
6261 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
6262 so avoid them here. */
6263 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
6264 break;
6265
6266 bitschange = TYPE_PRECISION (TREE_TYPE (op))
6267 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
6268
6269 /* Truncations are many-one so cannot be removed.
6270 Unless we are later going to truncate down even farther. */
6271 if (bitschange < 0
6272 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
6273 break;
6274
6275 /* See what's inside this conversion. If we decide to strip it,
6276 we will set WIN. */
6277 op = TREE_OPERAND (op, 0);
6278
6279 /* If we have not stripped any zero-extensions (uns is 0),
6280 we can strip any kind of extension.
6281 If we have previously stripped a zero-extension,
6282 only zero-extensions can safely be stripped.
6283 Any extension can be stripped if the bits it would produce
6284 are all going to be discarded later by truncating to FOR_TYPE. */
6285
6286 if (bitschange > 0)
6287 {
6288 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
6289 win = op;
6290 /* TYPE_UNSIGNED says whether this is a zero-extension.
6291 Let's avoid computing it if it does not affect WIN
6292 and if UNS will not be needed again. */
6293 if ((uns
6294 || CONVERT_EXPR_P (op))
6295 && TYPE_UNSIGNED (TREE_TYPE (op)))
6296 {
6297 uns = 1;
6298 win = op;
6299 }
6300 }
6301 }
6302
6303 return win;
6304 }
6305 \f
6306 /* Return OP or a simpler expression for a narrower value
6307 which can be sign-extended or zero-extended to give back OP.
6308 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
6309 or 0 if the value should be sign-extended. */
6310
6311 tree
6312 get_narrower (tree op, int *unsignedp_ptr)
6313 {
6314 int uns = 0;
6315 int first = 1;
6316 tree win = op;
6317 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
6318
6319 while (TREE_CODE (op) == NOP_EXPR)
6320 {
6321 int bitschange
6322 = (TYPE_PRECISION (TREE_TYPE (op))
6323 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
6324
6325 /* Truncations are many-one so cannot be removed. */
6326 if (bitschange < 0)
6327 break;
6328
6329 /* See what's inside this conversion. If we decide to strip it,
6330 we will set WIN. */
6331
6332 if (bitschange > 0)
6333 {
6334 op = TREE_OPERAND (op, 0);
6335 /* An extension: the outermost one can be stripped,
6336 but remember whether it is zero or sign extension. */
6337 if (first)
6338 uns = TYPE_UNSIGNED (TREE_TYPE (op));
6339 /* Otherwise, if a sign extension has been stripped,
6340 only sign extensions can now be stripped;
6341 if a zero extension has been stripped, only zero-extensions. */
6342 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
6343 break;
6344 first = 0;
6345 }
6346 else /* bitschange == 0 */
6347 {
6348 /* A change in nominal type can always be stripped, but we must
6349 preserve the unsignedness. */
6350 if (first)
6351 uns = TYPE_UNSIGNED (TREE_TYPE (op));
6352 first = 0;
6353 op = TREE_OPERAND (op, 0);
6354 /* Keep trying to narrow, but don't assign op to win if it
6355 would turn an integral type into something else. */
6356 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
6357 continue;
6358 }
6359
6360 win = op;
6361 }
6362
6363 if (TREE_CODE (op) == COMPONENT_REF
6364 /* Since type_for_size always gives an integer type. */
6365 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
6366 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
6367 /* Ensure field is laid out already. */
6368 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
6369 && host_integerp (DECL_SIZE (TREE_OPERAND (op, 1)), 1))
6370 {
6371 unsigned HOST_WIDE_INT innerprec
6372 = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
6373 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
6374 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
6375 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
6376
6377 /* We can get this structure field in a narrower type that fits it,
6378 but the resulting extension to its nominal type (a fullword type)
6379 must satisfy the same conditions as for other extensions.
6380
6381 Do this only for fields that are aligned (not bit-fields),
6382 because when bit-field insns will be used there is no
6383 advantage in doing this. */
6384
6385 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
6386 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
6387 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
6388 && type != 0)
6389 {
6390 if (first)
6391 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
6392 win = fold_convert (type, op);
6393 }
6394 }
6395
6396 *unsignedp_ptr = uns;
6397 return win;
6398 }
6399 \f
6400 /* Nonzero if integer constant C has a value that is permissible
6401 for type TYPE (an INTEGER_TYPE). */
6402
6403 int
6404 int_fits_type_p (const_tree c, const_tree type)
6405 {
6406 tree type_low_bound = TYPE_MIN_VALUE (type);
6407 tree type_high_bound = TYPE_MAX_VALUE (type);
6408 bool ok_for_low_bound, ok_for_high_bound;
6409 unsigned HOST_WIDE_INT low;
6410 HOST_WIDE_INT high;
6411
6412 /* If at least one bound of the type is a constant integer, we can check
6413 ourselves and maybe make a decision. If no such decision is possible, but
6414 this type is a subtype, try checking against that. Otherwise, use
6415 fit_double_type, which checks against the precision.
6416
6417 Compute the status for each possibly constant bound, and return if we see
6418 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
6419 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
6420 for "constant known to fit". */
6421
6422 if (TREE_TYPE (c) == sizetype
6423 && TYPE_UNSIGNED (TREE_TYPE (c))
6424 && TREE_INT_CST_HIGH (c) == -1
6425 && !TREE_OVERFLOW (c))
6426 /* So c is an unsigned integer which type is sizetype.
6427 sizetype'd integers are sign extended even though they are
6428 unsigned. If the integer value fits in the lower end word of c,
6429 and if the higher end word has all its bits set to 1, that
6430 means the higher end bits are set to 1 only for sign extension.
6431 So let's convert c into an equivalent zero extended unsigned
6432 integer. */
6433 c = force_fit_type_double (size_type_node,
6434 TREE_INT_CST_LOW (c),
6435 TREE_INT_CST_HIGH (c),
6436 false, false);
6437 /* Check if C >= type_low_bound. */
6438 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
6439 {
6440 if (tree_int_cst_lt (c, type_low_bound))
6441 return 0;
6442 ok_for_low_bound = true;
6443 }
6444 else
6445 ok_for_low_bound = false;
6446
6447 /* Check if c <= type_high_bound. */
6448 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
6449 {
6450 if (tree_int_cst_lt (type_high_bound, c))
6451 return 0;
6452 ok_for_high_bound = true;
6453 }
6454 else
6455 ok_for_high_bound = false;
6456
6457 /* If the constant fits both bounds, the result is known. */
6458 if (ok_for_low_bound && ok_for_high_bound)
6459 return 1;
6460
6461 /* Perform some generic filtering which may allow making a decision
6462 even if the bounds are not constant. First, negative integers
6463 never fit in unsigned types, */
6464 if (TYPE_UNSIGNED (type) && tree_int_cst_sgn (c) < 0)
6465 return 0;
6466
6467 /* Second, narrower types always fit in wider ones. */
6468 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
6469 return 1;
6470
6471 /* Third, unsigned integers with top bit set never fit signed types. */
6472 if (! TYPE_UNSIGNED (type)
6473 && TYPE_UNSIGNED (TREE_TYPE (c))
6474 && tree_int_cst_msb (c))
6475 return 0;
6476
6477 /* If we haven't been able to decide at this point, there nothing more we
6478 can check ourselves here. Look at the base type if we have one and it
6479 has the same precision. */
6480 if (TREE_CODE (type) == INTEGER_TYPE
6481 && TREE_TYPE (type) != 0
6482 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
6483 return int_fits_type_p (c, TREE_TYPE (type));
6484
6485 /* Or to fit_double_type, if nothing else. */
6486 low = TREE_INT_CST_LOW (c);
6487 high = TREE_INT_CST_HIGH (c);
6488 return !fit_double_type (low, high, &low, &high, type);
6489 }
6490
6491 /* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
6492 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
6493 represented (assuming two's-complement arithmetic) within the bit
6494 precision of the type are returned instead. */
6495
6496 void
6497 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
6498 {
6499 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
6500 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
6501 mpz_set_double_int (min, tree_to_double_int (TYPE_MIN_VALUE (type)),
6502 TYPE_UNSIGNED (type));
6503 else
6504 {
6505 if (TYPE_UNSIGNED (type))
6506 mpz_set_ui (min, 0);
6507 else
6508 {
6509 double_int mn;
6510 mn = double_int_mask (TYPE_PRECISION (type) - 1);
6511 mn = double_int_sext (double_int_add (mn, double_int_one),
6512 TYPE_PRECISION (type));
6513 mpz_set_double_int (min, mn, false);
6514 }
6515 }
6516
6517 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
6518 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
6519 mpz_set_double_int (max, tree_to_double_int (TYPE_MAX_VALUE (type)),
6520 TYPE_UNSIGNED (type));
6521 else
6522 {
6523 if (TYPE_UNSIGNED (type))
6524 mpz_set_double_int (max, double_int_mask (TYPE_PRECISION (type)),
6525 true);
6526 else
6527 mpz_set_double_int (max, double_int_mask (TYPE_PRECISION (type) - 1),
6528 true);
6529 }
6530 }
6531
6532 /* Return true if VAR is an automatic variable defined in function FN. */
6533
6534 bool
6535 auto_var_in_fn_p (const_tree var, const_tree fn)
6536 {
6537 return (DECL_P (var) && DECL_CONTEXT (var) == fn
6538 && (((TREE_CODE (var) == VAR_DECL || TREE_CODE (var) == PARM_DECL)
6539 && ! TREE_STATIC (var))
6540 || TREE_CODE (var) == LABEL_DECL
6541 || TREE_CODE (var) == RESULT_DECL));
6542 }
6543
6544 /* Subprogram of following function. Called by walk_tree.
6545
6546 Return *TP if it is an automatic variable or parameter of the
6547 function passed in as DATA. */
6548
6549 static tree
6550 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
6551 {
6552 tree fn = (tree) data;
6553
6554 if (TYPE_P (*tp))
6555 *walk_subtrees = 0;
6556
6557 else if (DECL_P (*tp)
6558 && auto_var_in_fn_p (*tp, fn))
6559 return *tp;
6560
6561 return NULL_TREE;
6562 }
6563
6564 /* Returns true if T is, contains, or refers to a type with variable
6565 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
6566 arguments, but not the return type. If FN is nonzero, only return
6567 true if a modifier of the type or position of FN is a variable or
6568 parameter inside FN.
6569
6570 This concept is more general than that of C99 'variably modified types':
6571 in C99, a struct type is never variably modified because a VLA may not
6572 appear as a structure member. However, in GNU C code like:
6573
6574 struct S { int i[f()]; };
6575
6576 is valid, and other languages may define similar constructs. */
6577
6578 bool
6579 variably_modified_type_p (tree type, tree fn)
6580 {
6581 tree t;
6582
6583 /* Test if T is either variable (if FN is zero) or an expression containing
6584 a variable in FN. */
6585 #define RETURN_TRUE_IF_VAR(T) \
6586 do { tree _t = (T); \
6587 if (_t && _t != error_mark_node && TREE_CODE (_t) != INTEGER_CST \
6588 && (!fn || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
6589 return true; } while (0)
6590
6591 if (type == error_mark_node)
6592 return false;
6593
6594 /* If TYPE itself has variable size, it is variably modified. */
6595 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
6596 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
6597
6598 switch (TREE_CODE (type))
6599 {
6600 case POINTER_TYPE:
6601 case REFERENCE_TYPE:
6602 case VECTOR_TYPE:
6603 if (variably_modified_type_p (TREE_TYPE (type), fn))
6604 return true;
6605 break;
6606
6607 case FUNCTION_TYPE:
6608 case METHOD_TYPE:
6609 /* If TYPE is a function type, it is variably modified if the
6610 return type is variably modified. */
6611 if (variably_modified_type_p (TREE_TYPE (type), fn))
6612 return true;
6613 break;
6614
6615 case INTEGER_TYPE:
6616 case REAL_TYPE:
6617 case FIXED_POINT_TYPE:
6618 case ENUMERAL_TYPE:
6619 case BOOLEAN_TYPE:
6620 /* Scalar types are variably modified if their end points
6621 aren't constant. */
6622 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
6623 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
6624 break;
6625
6626 case RECORD_TYPE:
6627 case UNION_TYPE:
6628 case QUAL_UNION_TYPE:
6629 /* We can't see if any of the fields are variably-modified by the
6630 definition we normally use, since that would produce infinite
6631 recursion via pointers. */
6632 /* This is variably modified if some field's type is. */
6633 for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t))
6634 if (TREE_CODE (t) == FIELD_DECL)
6635 {
6636 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
6637 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
6638 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
6639
6640 if (TREE_CODE (type) == QUAL_UNION_TYPE)
6641 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
6642 }
6643 break;
6644
6645 case ARRAY_TYPE:
6646 /* Do not call ourselves to avoid infinite recursion. This is
6647 variably modified if the element type is. */
6648 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
6649 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
6650 break;
6651
6652 default:
6653 break;
6654 }
6655
6656 /* The current language may have other cases to check, but in general,
6657 all other types are not variably modified. */
6658 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
6659
6660 #undef RETURN_TRUE_IF_VAR
6661 }
6662
6663 /* Given a DECL or TYPE, return the scope in which it was declared, or
6664 NULL_TREE if there is no containing scope. */
6665
6666 tree
6667 get_containing_scope (const_tree t)
6668 {
6669 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
6670 }
6671
6672 /* Return the innermost context enclosing DECL that is
6673 a FUNCTION_DECL, or zero if none. */
6674
6675 tree
6676 decl_function_context (const_tree decl)
6677 {
6678 tree context;
6679
6680 if (TREE_CODE (decl) == ERROR_MARK)
6681 return 0;
6682
6683 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
6684 where we look up the function at runtime. Such functions always take
6685 a first argument of type 'pointer to real context'.
6686
6687 C++ should really be fixed to use DECL_CONTEXT for the real context,
6688 and use something else for the "virtual context". */
6689 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
6690 context
6691 = TYPE_MAIN_VARIANT
6692 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
6693 else
6694 context = DECL_CONTEXT (decl);
6695
6696 while (context && TREE_CODE (context) != FUNCTION_DECL)
6697 {
6698 if (TREE_CODE (context) == BLOCK)
6699 context = BLOCK_SUPERCONTEXT (context);
6700 else
6701 context = get_containing_scope (context);
6702 }
6703
6704 return context;
6705 }
6706
6707 /* Return the innermost context enclosing DECL that is
6708 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
6709 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
6710
6711 tree
6712 decl_type_context (const_tree decl)
6713 {
6714 tree context = DECL_CONTEXT (decl);
6715
6716 while (context)
6717 switch (TREE_CODE (context))
6718 {
6719 case NAMESPACE_DECL:
6720 case TRANSLATION_UNIT_DECL:
6721 return NULL_TREE;
6722
6723 case RECORD_TYPE:
6724 case UNION_TYPE:
6725 case QUAL_UNION_TYPE:
6726 return context;
6727
6728 case TYPE_DECL:
6729 case FUNCTION_DECL:
6730 context = DECL_CONTEXT (context);
6731 break;
6732
6733 case BLOCK:
6734 context = BLOCK_SUPERCONTEXT (context);
6735 break;
6736
6737 default:
6738 gcc_unreachable ();
6739 }
6740
6741 return NULL_TREE;
6742 }
6743
6744 /* CALL is a CALL_EXPR. Return the declaration for the function
6745 called, or NULL_TREE if the called function cannot be
6746 determined. */
6747
6748 tree
6749 get_callee_fndecl (const_tree call)
6750 {
6751 tree addr;
6752
6753 if (call == error_mark_node)
6754 return error_mark_node;
6755
6756 /* It's invalid to call this function with anything but a
6757 CALL_EXPR. */
6758 gcc_assert (TREE_CODE (call) == CALL_EXPR);
6759
6760 /* The first operand to the CALL is the address of the function
6761 called. */
6762 addr = CALL_EXPR_FN (call);
6763
6764 STRIP_NOPS (addr);
6765
6766 /* If this is a readonly function pointer, extract its initial value. */
6767 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
6768 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
6769 && DECL_INITIAL (addr))
6770 addr = DECL_INITIAL (addr);
6771
6772 /* If the address is just `&f' for some function `f', then we know
6773 that `f' is being called. */
6774 if (TREE_CODE (addr) == ADDR_EXPR
6775 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
6776 return TREE_OPERAND (addr, 0);
6777
6778 /* We couldn't figure out what was being called. */
6779 return NULL_TREE;
6780 }
6781
6782 /* Print debugging information about tree nodes generated during the compile,
6783 and any language-specific information. */
6784
6785 void
6786 dump_tree_statistics (void)
6787 {
6788 #ifdef GATHER_STATISTICS
6789 int i;
6790 int total_nodes, total_bytes;
6791 #endif
6792
6793 fprintf (stderr, "\n??? tree nodes created\n\n");
6794 #ifdef GATHER_STATISTICS
6795 fprintf (stderr, "Kind Nodes Bytes\n");
6796 fprintf (stderr, "---------------------------------------\n");
6797 total_nodes = total_bytes = 0;
6798 for (i = 0; i < (int) all_kinds; i++)
6799 {
6800 fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
6801 tree_node_counts[i], tree_node_sizes[i]);
6802 total_nodes += tree_node_counts[i];
6803 total_bytes += tree_node_sizes[i];
6804 }
6805 fprintf (stderr, "---------------------------------------\n");
6806 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
6807 fprintf (stderr, "---------------------------------------\n");
6808 ssanames_print_statistics ();
6809 phinodes_print_statistics ();
6810 #else
6811 fprintf (stderr, "(No per-node statistics)\n");
6812 #endif
6813 print_type_hash_statistics ();
6814 print_debug_expr_statistics ();
6815 print_value_expr_statistics ();
6816 print_restrict_base_statistics ();
6817 lang_hooks.print_statistics ();
6818 }
6819 \f
6820 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
6821
6822 /* Generate a crc32 of a string. */
6823
6824 unsigned
6825 crc32_string (unsigned chksum, const char *string)
6826 {
6827 do
6828 {
6829 unsigned value = *string << 24;
6830 unsigned ix;
6831
6832 for (ix = 8; ix--; value <<= 1)
6833 {
6834 unsigned feedback;
6835
6836 feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
6837 chksum <<= 1;
6838 chksum ^= feedback;
6839 }
6840 }
6841 while (*string++);
6842 return chksum;
6843 }
6844
6845 /* P is a string that will be used in a symbol. Mask out any characters
6846 that are not valid in that context. */
6847
6848 void
6849 clean_symbol_name (char *p)
6850 {
6851 for (; *p; p++)
6852 if (! (ISALNUM (*p)
6853 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
6854 || *p == '$'
6855 #endif
6856 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
6857 || *p == '.'
6858 #endif
6859 ))
6860 *p = '_';
6861 }
6862
6863 /* Generate a name for a special-purpose function function.
6864 The generated name may need to be unique across the whole link.
6865 TYPE is some string to identify the purpose of this function to the
6866 linker or collect2; it must start with an uppercase letter,
6867 one of:
6868 I - for constructors
6869 D - for destructors
6870 N - for C++ anonymous namespaces
6871 F - for DWARF unwind frame information. */
6872
6873 tree
6874 get_file_function_name (const char *type)
6875 {
6876 char *buf;
6877 const char *p;
6878 char *q;
6879
6880 /* If we already have a name we know to be unique, just use that. */
6881 if (first_global_object_name)
6882 p = first_global_object_name;
6883 /* If the target is handling the constructors/destructors, they
6884 will be local to this file and the name is only necessary for
6885 debugging purposes. */
6886 else if ((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
6887 {
6888 const char *file = main_input_filename;
6889 if (! file)
6890 file = input_filename;
6891 /* Just use the file's basename, because the full pathname
6892 might be quite long. */
6893 p = strrchr (file, '/');
6894 if (p)
6895 p++;
6896 else
6897 p = file;
6898 p = q = ASTRDUP (p);
6899 clean_symbol_name (q);
6900 }
6901 else
6902 {
6903 /* Otherwise, the name must be unique across the entire link.
6904 We don't have anything that we know to be unique to this translation
6905 unit, so use what we do have and throw in some randomness. */
6906 unsigned len;
6907 const char *name = weak_global_object_name;
6908 const char *file = main_input_filename;
6909
6910 if (! name)
6911 name = "";
6912 if (! file)
6913 file = input_filename;
6914
6915 len = strlen (file);
6916 q = (char *) alloca (9 * 2 + len + 1);
6917 memcpy (q, file, len + 1);
6918 clean_symbol_name (q);
6919
6920 sprintf (q + len, "_%08X_%08X", crc32_string (0, name),
6921 crc32_string (0, get_random_seed (false)));
6922
6923 p = q;
6924 }
6925
6926 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
6927 + strlen (type));
6928
6929 /* Set up the name of the file-level functions we may need.
6930 Use a global object (which is already required to be unique over
6931 the program) rather than the file name (which imposes extra
6932 constraints). */
6933 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
6934
6935 return get_identifier (buf);
6936 }
6937 \f
6938 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
6939
6940 /* Complain that the tree code of NODE does not match the expected 0
6941 terminated list of trailing codes. The trailing code list can be
6942 empty, for a more vague error message. FILE, LINE, and FUNCTION
6943 are of the caller. */
6944
6945 void
6946 tree_check_failed (const_tree node, const char *file,
6947 int line, const char *function, ...)
6948 {
6949 va_list args;
6950 const char *buffer;
6951 unsigned length = 0;
6952 int code;
6953
6954 va_start (args, function);
6955 while ((code = va_arg (args, int)))
6956 length += 4 + strlen (tree_code_name[code]);
6957 va_end (args);
6958 if (length)
6959 {
6960 char *tmp;
6961 va_start (args, function);
6962 length += strlen ("expected ");
6963 buffer = tmp = (char *) alloca (length);
6964 length = 0;
6965 while ((code = va_arg (args, int)))
6966 {
6967 const char *prefix = length ? " or " : "expected ";
6968
6969 strcpy (tmp + length, prefix);
6970 length += strlen (prefix);
6971 strcpy (tmp + length, tree_code_name[code]);
6972 length += strlen (tree_code_name[code]);
6973 }
6974 va_end (args);
6975 }
6976 else
6977 buffer = "unexpected node";
6978
6979 internal_error ("tree check: %s, have %s in %s, at %s:%d",
6980 buffer, tree_code_name[TREE_CODE (node)],
6981 function, trim_filename (file), line);
6982 }
6983
6984 /* Complain that the tree code of NODE does match the expected 0
6985 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
6986 the caller. */
6987
6988 void
6989 tree_not_check_failed (const_tree node, const char *file,
6990 int line, const char *function, ...)
6991 {
6992 va_list args;
6993 char *buffer;
6994 unsigned length = 0;
6995 int code;
6996
6997 va_start (args, function);
6998 while ((code = va_arg (args, int)))
6999 length += 4 + strlen (tree_code_name[code]);
7000 va_end (args);
7001 va_start (args, function);
7002 buffer = (char *) alloca (length);
7003 length = 0;
7004 while ((code = va_arg (args, int)))
7005 {
7006 if (length)
7007 {
7008 strcpy (buffer + length, " or ");
7009 length += 4;
7010 }
7011 strcpy (buffer + length, tree_code_name[code]);
7012 length += strlen (tree_code_name[code]);
7013 }
7014 va_end (args);
7015
7016 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
7017 buffer, tree_code_name[TREE_CODE (node)],
7018 function, trim_filename (file), line);
7019 }
7020
7021 /* Similar to tree_check_failed, except that we check for a class of tree
7022 code, given in CL. */
7023
7024 void
7025 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
7026 const char *file, int line, const char *function)
7027 {
7028 internal_error
7029 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
7030 TREE_CODE_CLASS_STRING (cl),
7031 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
7032 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
7033 }
7034
7035 /* Similar to tree_check_failed, except that instead of specifying a
7036 dozen codes, use the knowledge that they're all sequential. */
7037
7038 void
7039 tree_range_check_failed (const_tree node, const char *file, int line,
7040 const char *function, enum tree_code c1,
7041 enum tree_code c2)
7042 {
7043 char *buffer;
7044 unsigned length = 0;
7045 enum tree_code c;
7046
7047 for (c = c1; c <= c2; ++c)
7048 length += 4 + strlen (tree_code_name[c]);
7049
7050 length += strlen ("expected ");
7051 buffer = (char *) alloca (length);
7052 length = 0;
7053
7054 for (c = c1; c <= c2; ++c)
7055 {
7056 const char *prefix = length ? " or " : "expected ";
7057
7058 strcpy (buffer + length, prefix);
7059 length += strlen (prefix);
7060 strcpy (buffer + length, tree_code_name[c]);
7061 length += strlen (tree_code_name[c]);
7062 }
7063
7064 internal_error ("tree check: %s, have %s in %s, at %s:%d",
7065 buffer, tree_code_name[TREE_CODE (node)],
7066 function, trim_filename (file), line);
7067 }
7068
7069
7070 /* Similar to tree_check_failed, except that we check that a tree does
7071 not have the specified code, given in CL. */
7072
7073 void
7074 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
7075 const char *file, int line, const char *function)
7076 {
7077 internal_error
7078 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
7079 TREE_CODE_CLASS_STRING (cl),
7080 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
7081 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
7082 }
7083
7084
7085 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
7086
7087 void
7088 omp_clause_check_failed (const_tree node, const char *file, int line,
7089 const char *function, enum omp_clause_code code)
7090 {
7091 internal_error ("tree check: expected omp_clause %s, have %s in %s, at %s:%d",
7092 omp_clause_code_name[code], tree_code_name[TREE_CODE (node)],
7093 function, trim_filename (file), line);
7094 }
7095
7096
7097 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
7098
7099 void
7100 omp_clause_range_check_failed (const_tree node, const char *file, int line,
7101 const char *function, enum omp_clause_code c1,
7102 enum omp_clause_code c2)
7103 {
7104 char *buffer;
7105 unsigned length = 0;
7106 enum omp_clause_code c;
7107
7108 for (c = c1; c <= c2; ++c)
7109 length += 4 + strlen (omp_clause_code_name[c]);
7110
7111 length += strlen ("expected ");
7112 buffer = (char *) alloca (length);
7113 length = 0;
7114
7115 for (c = c1; c <= c2; ++c)
7116 {
7117 const char *prefix = length ? " or " : "expected ";
7118
7119 strcpy (buffer + length, prefix);
7120 length += strlen (prefix);
7121 strcpy (buffer + length, omp_clause_code_name[c]);
7122 length += strlen (omp_clause_code_name[c]);
7123 }
7124
7125 internal_error ("tree check: %s, have %s in %s, at %s:%d",
7126 buffer, omp_clause_code_name[TREE_CODE (node)],
7127 function, trim_filename (file), line);
7128 }
7129
7130
7131 #undef DEFTREESTRUCT
7132 #define DEFTREESTRUCT(VAL, NAME) NAME,
7133
7134 static const char *ts_enum_names[] = {
7135 #include "treestruct.def"
7136 };
7137 #undef DEFTREESTRUCT
7138
7139 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
7140
7141 /* Similar to tree_class_check_failed, except that we check for
7142 whether CODE contains the tree structure identified by EN. */
7143
7144 void
7145 tree_contains_struct_check_failed (const_tree node,
7146 const enum tree_node_structure_enum en,
7147 const char *file, int line,
7148 const char *function)
7149 {
7150 internal_error
7151 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
7152 TS_ENUM_NAME(en),
7153 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
7154 }
7155
7156
7157 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
7158 (dynamically sized) vector. */
7159
7160 void
7161 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
7162 const char *function)
7163 {
7164 internal_error
7165 ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
7166 idx + 1, len, function, trim_filename (file), line);
7167 }
7168
7169 /* Similar to above, except that the check is for the bounds of the operand
7170 vector of an expression node EXP. */
7171
7172 void
7173 tree_operand_check_failed (int idx, const_tree exp, const char *file,
7174 int line, const char *function)
7175 {
7176 int code = TREE_CODE (exp);
7177 internal_error
7178 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
7179 idx + 1, tree_code_name[code], TREE_OPERAND_LENGTH (exp),
7180 function, trim_filename (file), line);
7181 }
7182
7183 /* Similar to above, except that the check is for the number of
7184 operands of an OMP_CLAUSE node. */
7185
7186 void
7187 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
7188 int line, const char *function)
7189 {
7190 internal_error
7191 ("tree check: accessed operand %d of omp_clause %s with %d operands "
7192 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
7193 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
7194 trim_filename (file), line);
7195 }
7196 #endif /* ENABLE_TREE_CHECKING */
7197 \f
7198 /* Create a new vector type node holding SUBPARTS units of type INNERTYPE,
7199 and mapped to the machine mode MODE. Initialize its fields and build
7200 the information necessary for debugging output. */
7201
7202 static tree
7203 make_vector_type (tree innertype, int nunits, enum machine_mode mode)
7204 {
7205 tree t;
7206 hashval_t hashcode = 0;
7207
7208 /* Build a main variant, based on the main variant of the inner type, then
7209 use it to build the variant we return. */
7210 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
7211 && TYPE_MAIN_VARIANT (innertype) != innertype)
7212 return build_type_attribute_qual_variant (
7213 make_vector_type (TYPE_MAIN_VARIANT (innertype), nunits, mode),
7214 TYPE_ATTRIBUTES (innertype),
7215 TYPE_QUALS (innertype));
7216
7217 t = make_node (VECTOR_TYPE);
7218 TREE_TYPE (t) = TYPE_MAIN_VARIANT (innertype);
7219 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
7220 TYPE_MODE (t) = mode;
7221 TYPE_READONLY (t) = TYPE_READONLY (innertype);
7222 TYPE_VOLATILE (t) = TYPE_VOLATILE (innertype);
7223
7224 if (TYPE_STRUCTURAL_EQUALITY_P (innertype))
7225 SET_TYPE_STRUCTURAL_EQUALITY (t);
7226 else if (TYPE_CANONICAL (innertype) != innertype
7227 || mode != VOIDmode)
7228 TYPE_CANONICAL (t)
7229 = make_vector_type (TYPE_CANONICAL (innertype), nunits, VOIDmode);
7230
7231 layout_type (t);
7232
7233 {
7234 tree index = build_int_cst (NULL_TREE, nunits - 1);
7235 tree array = build_array_type (innertype, build_index_type (index));
7236 tree rt = make_node (RECORD_TYPE);
7237
7238 TYPE_FIELDS (rt) = build_decl (FIELD_DECL, get_identifier ("f"), array);
7239 DECL_CONTEXT (TYPE_FIELDS (rt)) = rt;
7240 layout_type (rt);
7241 TYPE_DEBUG_REPRESENTATION_TYPE (t) = rt;
7242 /* In dwarfout.c, type lookup uses TYPE_UID numbers. We want to output
7243 the representation type, and we want to find that die when looking up
7244 the vector type. This is most easily achieved by making the TYPE_UID
7245 numbers equal. */
7246 TYPE_UID (rt) = TYPE_UID (t);
7247 }
7248
7249 hashcode = iterative_hash_host_wide_int (VECTOR_TYPE, hashcode);
7250 hashcode = iterative_hash_host_wide_int (mode, hashcode);
7251 hashcode = iterative_hash_object (TYPE_HASH (innertype), hashcode);
7252 return type_hash_canon (hashcode, t);
7253 }
7254
7255 static tree
7256 make_or_reuse_type (unsigned size, int unsignedp)
7257 {
7258 if (size == INT_TYPE_SIZE)
7259 return unsignedp ? unsigned_type_node : integer_type_node;
7260 if (size == CHAR_TYPE_SIZE)
7261 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
7262 if (size == SHORT_TYPE_SIZE)
7263 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
7264 if (size == LONG_TYPE_SIZE)
7265 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
7266 if (size == LONG_LONG_TYPE_SIZE)
7267 return (unsignedp ? long_long_unsigned_type_node
7268 : long_long_integer_type_node);
7269
7270 if (unsignedp)
7271 return make_unsigned_type (size);
7272 else
7273 return make_signed_type (size);
7274 }
7275
7276 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
7277
7278 static tree
7279 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
7280 {
7281 if (satp)
7282 {
7283 if (size == SHORT_FRACT_TYPE_SIZE)
7284 return unsignedp ? sat_unsigned_short_fract_type_node
7285 : sat_short_fract_type_node;
7286 if (size == FRACT_TYPE_SIZE)
7287 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
7288 if (size == LONG_FRACT_TYPE_SIZE)
7289 return unsignedp ? sat_unsigned_long_fract_type_node
7290 : sat_long_fract_type_node;
7291 if (size == LONG_LONG_FRACT_TYPE_SIZE)
7292 return unsignedp ? sat_unsigned_long_long_fract_type_node
7293 : sat_long_long_fract_type_node;
7294 }
7295 else
7296 {
7297 if (size == SHORT_FRACT_TYPE_SIZE)
7298 return unsignedp ? unsigned_short_fract_type_node
7299 : short_fract_type_node;
7300 if (size == FRACT_TYPE_SIZE)
7301 return unsignedp ? unsigned_fract_type_node : fract_type_node;
7302 if (size == LONG_FRACT_TYPE_SIZE)
7303 return unsignedp ? unsigned_long_fract_type_node
7304 : long_fract_type_node;
7305 if (size == LONG_LONG_FRACT_TYPE_SIZE)
7306 return unsignedp ? unsigned_long_long_fract_type_node
7307 : long_long_fract_type_node;
7308 }
7309
7310 return make_fract_type (size, unsignedp, satp);
7311 }
7312
7313 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
7314
7315 static tree
7316 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
7317 {
7318 if (satp)
7319 {
7320 if (size == SHORT_ACCUM_TYPE_SIZE)
7321 return unsignedp ? sat_unsigned_short_accum_type_node
7322 : sat_short_accum_type_node;
7323 if (size == ACCUM_TYPE_SIZE)
7324 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
7325 if (size == LONG_ACCUM_TYPE_SIZE)
7326 return unsignedp ? sat_unsigned_long_accum_type_node
7327 : sat_long_accum_type_node;
7328 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
7329 return unsignedp ? sat_unsigned_long_long_accum_type_node
7330 : sat_long_long_accum_type_node;
7331 }
7332 else
7333 {
7334 if (size == SHORT_ACCUM_TYPE_SIZE)
7335 return unsignedp ? unsigned_short_accum_type_node
7336 : short_accum_type_node;
7337 if (size == ACCUM_TYPE_SIZE)
7338 return unsignedp ? unsigned_accum_type_node : accum_type_node;
7339 if (size == LONG_ACCUM_TYPE_SIZE)
7340 return unsignedp ? unsigned_long_accum_type_node
7341 : long_accum_type_node;
7342 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
7343 return unsignedp ? unsigned_long_long_accum_type_node
7344 : long_long_accum_type_node;
7345 }
7346
7347 return make_accum_type (size, unsignedp, satp);
7348 }
7349
7350 /* Create nodes for all integer types (and error_mark_node) using the sizes
7351 of C datatypes. The caller should call set_sizetype soon after calling
7352 this function to select one of the types as sizetype. */
7353
7354 void
7355 build_common_tree_nodes (bool signed_char, bool signed_sizetype)
7356 {
7357 error_mark_node = make_node (ERROR_MARK);
7358 TREE_TYPE (error_mark_node) = error_mark_node;
7359
7360 initialize_sizetypes (signed_sizetype);
7361
7362 /* Define both `signed char' and `unsigned char'. */
7363 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
7364 TYPE_STRING_FLAG (signed_char_type_node) = 1;
7365 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
7366 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
7367
7368 /* Define `char', which is like either `signed char' or `unsigned char'
7369 but not the same as either. */
7370 char_type_node
7371 = (signed_char
7372 ? make_signed_type (CHAR_TYPE_SIZE)
7373 : make_unsigned_type (CHAR_TYPE_SIZE));
7374 TYPE_STRING_FLAG (char_type_node) = 1;
7375
7376 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
7377 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
7378 integer_type_node = make_signed_type (INT_TYPE_SIZE);
7379 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
7380 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
7381 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
7382 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
7383 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
7384
7385 /* Define a boolean type. This type only represents boolean values but
7386 may be larger than char depending on the value of BOOL_TYPE_SIZE.
7387 Front ends which want to override this size (i.e. Java) can redefine
7388 boolean_type_node before calling build_common_tree_nodes_2. */
7389 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
7390 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
7391 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
7392 TYPE_PRECISION (boolean_type_node) = 1;
7393
7394 /* Fill in the rest of the sized types. Reuse existing type nodes
7395 when possible. */
7396 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
7397 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
7398 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
7399 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
7400 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
7401
7402 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
7403 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
7404 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
7405 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
7406 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
7407
7408 access_public_node = get_identifier ("public");
7409 access_protected_node = get_identifier ("protected");
7410 access_private_node = get_identifier ("private");
7411 }
7412
7413 /* Call this function after calling build_common_tree_nodes and set_sizetype.
7414 It will create several other common tree nodes. */
7415
7416 void
7417 build_common_tree_nodes_2 (int short_double)
7418 {
7419 /* Define these next since types below may used them. */
7420 integer_zero_node = build_int_cst (NULL_TREE, 0);
7421 integer_one_node = build_int_cst (NULL_TREE, 1);
7422 integer_minus_one_node = build_int_cst (NULL_TREE, -1);
7423
7424 size_zero_node = size_int (0);
7425 size_one_node = size_int (1);
7426 bitsize_zero_node = bitsize_int (0);
7427 bitsize_one_node = bitsize_int (1);
7428 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
7429
7430 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
7431 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
7432
7433 void_type_node = make_node (VOID_TYPE);
7434 layout_type (void_type_node);
7435
7436 /* We are not going to have real types in C with less than byte alignment,
7437 so we might as well not have any types that claim to have it. */
7438 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
7439 TYPE_USER_ALIGN (void_type_node) = 0;
7440
7441 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
7442 layout_type (TREE_TYPE (null_pointer_node));
7443
7444 ptr_type_node = build_pointer_type (void_type_node);
7445 const_ptr_type_node
7446 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
7447 fileptr_type_node = ptr_type_node;
7448
7449 float_type_node = make_node (REAL_TYPE);
7450 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
7451 layout_type (float_type_node);
7452
7453 double_type_node = make_node (REAL_TYPE);
7454 if (short_double)
7455 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
7456 else
7457 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
7458 layout_type (double_type_node);
7459
7460 long_double_type_node = make_node (REAL_TYPE);
7461 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
7462 layout_type (long_double_type_node);
7463
7464 float_ptr_type_node = build_pointer_type (float_type_node);
7465 double_ptr_type_node = build_pointer_type (double_type_node);
7466 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
7467 integer_ptr_type_node = build_pointer_type (integer_type_node);
7468
7469 /* Fixed size integer types. */
7470 uint32_type_node = build_nonstandard_integer_type (32, true);
7471 uint64_type_node = build_nonstandard_integer_type (64, true);
7472
7473 /* Decimal float types. */
7474 dfloat32_type_node = make_node (REAL_TYPE);
7475 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
7476 layout_type (dfloat32_type_node);
7477 TYPE_MODE (dfloat32_type_node) = SDmode;
7478 dfloat32_ptr_type_node = build_pointer_type (dfloat32_type_node);
7479
7480 dfloat64_type_node = make_node (REAL_TYPE);
7481 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
7482 layout_type (dfloat64_type_node);
7483 TYPE_MODE (dfloat64_type_node) = DDmode;
7484 dfloat64_ptr_type_node = build_pointer_type (dfloat64_type_node);
7485
7486 dfloat128_type_node = make_node (REAL_TYPE);
7487 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
7488 layout_type (dfloat128_type_node);
7489 TYPE_MODE (dfloat128_type_node) = TDmode;
7490 dfloat128_ptr_type_node = build_pointer_type (dfloat128_type_node);
7491
7492 complex_integer_type_node = build_complex_type (integer_type_node);
7493 complex_float_type_node = build_complex_type (float_type_node);
7494 complex_double_type_node = build_complex_type (double_type_node);
7495 complex_long_double_type_node = build_complex_type (long_double_type_node);
7496
7497 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
7498 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
7499 sat_ ## KIND ## _type_node = \
7500 make_sat_signed_ ## KIND ## _type (SIZE); \
7501 sat_unsigned_ ## KIND ## _type_node = \
7502 make_sat_unsigned_ ## KIND ## _type (SIZE); \
7503 KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
7504 unsigned_ ## KIND ## _type_node = \
7505 make_unsigned_ ## KIND ## _type (SIZE);
7506
7507 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
7508 sat_ ## WIDTH ## KIND ## _type_node = \
7509 make_sat_signed_ ## KIND ## _type (SIZE); \
7510 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
7511 make_sat_unsigned_ ## KIND ## _type (SIZE); \
7512 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
7513 unsigned_ ## WIDTH ## KIND ## _type_node = \
7514 make_unsigned_ ## KIND ## _type (SIZE);
7515
7516 /* Make fixed-point type nodes based on four different widths. */
7517 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
7518 MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
7519 MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
7520 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
7521 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
7522
7523 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
7524 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
7525 NAME ## _type_node = \
7526 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
7527 u ## NAME ## _type_node = \
7528 make_or_reuse_unsigned_ ## KIND ## _type \
7529 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
7530 sat_ ## NAME ## _type_node = \
7531 make_or_reuse_sat_signed_ ## KIND ## _type \
7532 (GET_MODE_BITSIZE (MODE ## mode)); \
7533 sat_u ## NAME ## _type_node = \
7534 make_or_reuse_sat_unsigned_ ## KIND ## _type \
7535 (GET_MODE_BITSIZE (U ## MODE ## mode));
7536
7537 /* Fixed-point type and mode nodes. */
7538 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
7539 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
7540 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
7541 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
7542 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
7543 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
7544 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
7545 MAKE_FIXED_MODE_NODE (accum, ha, HA)
7546 MAKE_FIXED_MODE_NODE (accum, sa, SA)
7547 MAKE_FIXED_MODE_NODE (accum, da, DA)
7548 MAKE_FIXED_MODE_NODE (accum, ta, TA)
7549
7550 {
7551 tree t = targetm.build_builtin_va_list ();
7552
7553 /* Many back-ends define record types without setting TYPE_NAME.
7554 If we copied the record type here, we'd keep the original
7555 record type without a name. This breaks name mangling. So,
7556 don't copy record types and let c_common_nodes_and_builtins()
7557 declare the type to be __builtin_va_list. */
7558 if (TREE_CODE (t) != RECORD_TYPE)
7559 t = build_variant_type_copy (t);
7560
7561 va_list_type_node = t;
7562 }
7563 }
7564
7565 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
7566
7567 static void
7568 local_define_builtin (const char *name, tree type, enum built_in_function code,
7569 const char *library_name, int ecf_flags)
7570 {
7571 tree decl;
7572
7573 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
7574 library_name, NULL_TREE);
7575 if (ecf_flags & ECF_CONST)
7576 TREE_READONLY (decl) = 1;
7577 if (ecf_flags & ECF_PURE)
7578 DECL_PURE_P (decl) = 1;
7579 if (ecf_flags & ECF_LOOPING_CONST_OR_PURE)
7580 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
7581 if (ecf_flags & ECF_NORETURN)
7582 TREE_THIS_VOLATILE (decl) = 1;
7583 if (ecf_flags & ECF_NOTHROW)
7584 TREE_NOTHROW (decl) = 1;
7585 if (ecf_flags & ECF_MALLOC)
7586 DECL_IS_MALLOC (decl) = 1;
7587
7588 built_in_decls[code] = decl;
7589 implicit_built_in_decls[code] = decl;
7590 }
7591
7592 /* Call this function after instantiating all builtins that the language
7593 front end cares about. This will build the rest of the builtins that
7594 are relied upon by the tree optimizers and the middle-end. */
7595
7596 void
7597 build_common_builtin_nodes (void)
7598 {
7599 tree tmp, ftype;
7600
7601 if (built_in_decls[BUILT_IN_MEMCPY] == NULL
7602 || built_in_decls[BUILT_IN_MEMMOVE] == NULL)
7603 {
7604 tmp = tree_cons (NULL_TREE, size_type_node, void_list_node);
7605 tmp = tree_cons (NULL_TREE, const_ptr_type_node, tmp);
7606 tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
7607 ftype = build_function_type (ptr_type_node, tmp);
7608
7609 if (built_in_decls[BUILT_IN_MEMCPY] == NULL)
7610 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
7611 "memcpy", ECF_NOTHROW);
7612 if (built_in_decls[BUILT_IN_MEMMOVE] == NULL)
7613 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
7614 "memmove", ECF_NOTHROW);
7615 }
7616
7617 if (built_in_decls[BUILT_IN_MEMCMP] == NULL)
7618 {
7619 tmp = tree_cons (NULL_TREE, size_type_node, void_list_node);
7620 tmp = tree_cons (NULL_TREE, const_ptr_type_node, tmp);
7621 tmp = tree_cons (NULL_TREE, const_ptr_type_node, tmp);
7622 ftype = build_function_type (integer_type_node, tmp);
7623 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
7624 "memcmp", ECF_PURE | ECF_NOTHROW);
7625 }
7626
7627 if (built_in_decls[BUILT_IN_MEMSET] == NULL)
7628 {
7629 tmp = tree_cons (NULL_TREE, size_type_node, void_list_node);
7630 tmp = tree_cons (NULL_TREE, integer_type_node, tmp);
7631 tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
7632 ftype = build_function_type (ptr_type_node, tmp);
7633 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
7634 "memset", ECF_NOTHROW);
7635 }
7636
7637 if (built_in_decls[BUILT_IN_ALLOCA] == NULL)
7638 {
7639 tmp = tree_cons (NULL_TREE, size_type_node, void_list_node);
7640 ftype = build_function_type (ptr_type_node, tmp);
7641 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
7642 "alloca", ECF_NOTHROW | ECF_MALLOC);
7643 }
7644
7645 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7646 tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
7647 tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
7648 ftype = build_function_type (void_type_node, tmp);
7649 local_define_builtin ("__builtin_init_trampoline", ftype,
7650 BUILT_IN_INIT_TRAMPOLINE,
7651 "__builtin_init_trampoline", ECF_NOTHROW);
7652
7653 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7654 ftype = build_function_type (ptr_type_node, tmp);
7655 local_define_builtin ("__builtin_adjust_trampoline", ftype,
7656 BUILT_IN_ADJUST_TRAMPOLINE,
7657 "__builtin_adjust_trampoline",
7658 ECF_CONST | ECF_NOTHROW);
7659
7660 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7661 tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
7662 ftype = build_function_type (void_type_node, tmp);
7663 local_define_builtin ("__builtin_nonlocal_goto", ftype,
7664 BUILT_IN_NONLOCAL_GOTO,
7665 "__builtin_nonlocal_goto",
7666 ECF_NORETURN | ECF_NOTHROW);
7667
7668 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7669 tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
7670 ftype = build_function_type (void_type_node, tmp);
7671 local_define_builtin ("__builtin_setjmp_setup", ftype,
7672 BUILT_IN_SETJMP_SETUP,
7673 "__builtin_setjmp_setup", ECF_NOTHROW);
7674
7675 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7676 ftype = build_function_type (ptr_type_node, tmp);
7677 local_define_builtin ("__builtin_setjmp_dispatcher", ftype,
7678 BUILT_IN_SETJMP_DISPATCHER,
7679 "__builtin_setjmp_dispatcher",
7680 ECF_PURE | ECF_NOTHROW);
7681
7682 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7683 ftype = build_function_type (void_type_node, tmp);
7684 local_define_builtin ("__builtin_setjmp_receiver", ftype,
7685 BUILT_IN_SETJMP_RECEIVER,
7686 "__builtin_setjmp_receiver", ECF_NOTHROW);
7687
7688 ftype = build_function_type (ptr_type_node, void_list_node);
7689 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
7690 "__builtin_stack_save", ECF_NOTHROW);
7691
7692 tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
7693 ftype = build_function_type (void_type_node, tmp);
7694 local_define_builtin ("__builtin_stack_restore", ftype,
7695 BUILT_IN_STACK_RESTORE,
7696 "__builtin_stack_restore", ECF_NOTHROW);
7697
7698 ftype = build_function_type (void_type_node, void_list_node);
7699 local_define_builtin ("__builtin_profile_func_enter", ftype,
7700 BUILT_IN_PROFILE_FUNC_ENTER, "profile_func_enter", 0);
7701 local_define_builtin ("__builtin_profile_func_exit", ftype,
7702 BUILT_IN_PROFILE_FUNC_EXIT, "profile_func_exit", 0);
7703
7704 /* Complex multiplication and division. These are handled as builtins
7705 rather than optabs because emit_library_call_value doesn't support
7706 complex. Further, we can do slightly better with folding these
7707 beasties if the real and complex parts of the arguments are separate. */
7708 {
7709 enum machine_mode mode;
7710
7711 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
7712 {
7713 char mode_name_buf[4], *q;
7714 const char *p;
7715 enum built_in_function mcode, dcode;
7716 tree type, inner_type;
7717
7718 type = lang_hooks.types.type_for_mode (mode, 0);
7719 if (type == NULL)
7720 continue;
7721 inner_type = TREE_TYPE (type);
7722
7723 tmp = tree_cons (NULL_TREE, inner_type, void_list_node);
7724 tmp = tree_cons (NULL_TREE, inner_type, tmp);
7725 tmp = tree_cons (NULL_TREE, inner_type, tmp);
7726 tmp = tree_cons (NULL_TREE, inner_type, tmp);
7727 ftype = build_function_type (type, tmp);
7728
7729 mcode = BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT;
7730 dcode = BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT;
7731
7732 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
7733 *q = TOLOWER (*p);
7734 *q = '\0';
7735
7736 built_in_names[mcode] = concat ("__mul", mode_name_buf, "3", NULL);
7737 local_define_builtin (built_in_names[mcode], ftype, mcode,
7738 built_in_names[mcode], ECF_CONST | ECF_NOTHROW);
7739
7740 built_in_names[dcode] = concat ("__div", mode_name_buf, "3", NULL);
7741 local_define_builtin (built_in_names[dcode], ftype, dcode,
7742 built_in_names[dcode], ECF_CONST | ECF_NOTHROW);
7743 }
7744 }
7745 }
7746
7747 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
7748 better way.
7749
7750 If we requested a pointer to a vector, build up the pointers that
7751 we stripped off while looking for the inner type. Similarly for
7752 return values from functions.
7753
7754 The argument TYPE is the top of the chain, and BOTTOM is the
7755 new type which we will point to. */
7756
7757 tree
7758 reconstruct_complex_type (tree type, tree bottom)
7759 {
7760 tree inner, outer;
7761
7762 if (TREE_CODE (type) == POINTER_TYPE)
7763 {
7764 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
7765 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
7766 TYPE_REF_CAN_ALIAS_ALL (type));
7767 }
7768 else if (TREE_CODE (type) == REFERENCE_TYPE)
7769 {
7770 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
7771 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
7772 TYPE_REF_CAN_ALIAS_ALL (type));
7773 }
7774 else if (TREE_CODE (type) == ARRAY_TYPE)
7775 {
7776 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
7777 outer = build_array_type (inner, TYPE_DOMAIN (type));
7778 }
7779 else if (TREE_CODE (type) == FUNCTION_TYPE)
7780 {
7781 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
7782 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
7783 }
7784 else if (TREE_CODE (type) == METHOD_TYPE)
7785 {
7786 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
7787 /* The build_method_type_directly() routine prepends 'this' to argument list,
7788 so we must compensate by getting rid of it. */
7789 outer
7790 = build_method_type_directly
7791 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
7792 inner,
7793 TREE_CHAIN (TYPE_ARG_TYPES (type)));
7794 }
7795 else if (TREE_CODE (type) == OFFSET_TYPE)
7796 {
7797 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
7798 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
7799 }
7800 else
7801 return bottom;
7802
7803 return build_qualified_type (outer, TYPE_QUALS (type));
7804 }
7805
7806 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
7807 the inner type. */
7808 tree
7809 build_vector_type_for_mode (tree innertype, enum machine_mode mode)
7810 {
7811 int nunits;
7812
7813 switch (GET_MODE_CLASS (mode))
7814 {
7815 case MODE_VECTOR_INT:
7816 case MODE_VECTOR_FLOAT:
7817 case MODE_VECTOR_FRACT:
7818 case MODE_VECTOR_UFRACT:
7819 case MODE_VECTOR_ACCUM:
7820 case MODE_VECTOR_UACCUM:
7821 nunits = GET_MODE_NUNITS (mode);
7822 break;
7823
7824 case MODE_INT:
7825 /* Check that there are no leftover bits. */
7826 gcc_assert (GET_MODE_BITSIZE (mode)
7827 % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
7828
7829 nunits = GET_MODE_BITSIZE (mode)
7830 / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
7831 break;
7832
7833 default:
7834 gcc_unreachable ();
7835 }
7836
7837 return make_vector_type (innertype, nunits, mode);
7838 }
7839
7840 /* Similarly, but takes the inner type and number of units, which must be
7841 a power of two. */
7842
7843 tree
7844 build_vector_type (tree innertype, int nunits)
7845 {
7846 return make_vector_type (innertype, nunits, VOIDmode);
7847 }
7848
7849
7850 /* Build RESX_EXPR with given REGION_NUMBER. */
7851 tree
7852 build_resx (int region_number)
7853 {
7854 tree t;
7855 t = build1 (RESX_EXPR, void_type_node,
7856 build_int_cst (NULL_TREE, region_number));
7857 return t;
7858 }
7859
7860 /* Given an initializer INIT, return TRUE if INIT is zero or some
7861 aggregate of zeros. Otherwise return FALSE. */
7862 bool
7863 initializer_zerop (const_tree init)
7864 {
7865 tree elt;
7866
7867 STRIP_NOPS (init);
7868
7869 switch (TREE_CODE (init))
7870 {
7871 case INTEGER_CST:
7872 return integer_zerop (init);
7873
7874 case REAL_CST:
7875 /* ??? Note that this is not correct for C4X float formats. There,
7876 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
7877 negative exponent. */
7878 return real_zerop (init)
7879 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
7880
7881 case FIXED_CST:
7882 return fixed_zerop (init);
7883
7884 case COMPLEX_CST:
7885 return integer_zerop (init)
7886 || (real_zerop (init)
7887 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
7888 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
7889
7890 case VECTOR_CST:
7891 for (elt = TREE_VECTOR_CST_ELTS (init); elt; elt = TREE_CHAIN (elt))
7892 if (!initializer_zerop (TREE_VALUE (elt)))
7893 return false;
7894 return true;
7895
7896 case CONSTRUCTOR:
7897 {
7898 unsigned HOST_WIDE_INT idx;
7899
7900 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
7901 if (!initializer_zerop (elt))
7902 return false;
7903 return true;
7904 }
7905
7906 default:
7907 return false;
7908 }
7909 }
7910
7911 /* Build an empty statement. */
7912
7913 tree
7914 build_empty_stmt (void)
7915 {
7916 return build1 (NOP_EXPR, void_type_node, size_zero_node);
7917 }
7918
7919
7920 /* Build an OpenMP clause with code CODE. */
7921
7922 tree
7923 build_omp_clause (enum omp_clause_code code)
7924 {
7925 tree t;
7926 int size, length;
7927
7928 length = omp_clause_num_ops[code];
7929 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
7930
7931 t = GGC_NEWVAR (union tree_node, size);
7932 memset (t, 0, size);
7933 TREE_SET_CODE (t, OMP_CLAUSE);
7934 OMP_CLAUSE_SET_CODE (t, code);
7935
7936 #ifdef GATHER_STATISTICS
7937 tree_node_counts[(int) omp_clause_kind]++;
7938 tree_node_sizes[(int) omp_clause_kind] += size;
7939 #endif
7940
7941 return t;
7942 }
7943
7944 /* Set various status flags when building a CALL_EXPR object T. */
7945
7946 static void
7947 process_call_operands (tree t)
7948 {
7949 bool side_effects;
7950
7951 side_effects = TREE_SIDE_EFFECTS (t);
7952 if (!side_effects)
7953 {
7954 int i, n;
7955 n = TREE_OPERAND_LENGTH (t);
7956 for (i = 1; i < n; i++)
7957 {
7958 tree op = TREE_OPERAND (t, i);
7959 if (op && TREE_SIDE_EFFECTS (op))
7960 {
7961 side_effects = 1;
7962 break;
7963 }
7964 }
7965 }
7966 if (!side_effects)
7967 {
7968 int i;
7969
7970 /* Calls have side-effects, except those to const or
7971 pure functions. */
7972 i = call_expr_flags (t);
7973 if ((i & ECF_LOOPING_CONST_OR_PURE) || !(i & (ECF_CONST | ECF_PURE)))
7974 side_effects = 1;
7975 }
7976 TREE_SIDE_EFFECTS (t) = side_effects;
7977 }
7978
7979 /* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
7980 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
7981 Except for the CODE and operand count field, other storage for the
7982 object is initialized to zeros. */
7983
7984 tree
7985 build_vl_exp_stat (enum tree_code code, int len MEM_STAT_DECL)
7986 {
7987 tree t;
7988 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
7989
7990 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
7991 gcc_assert (len >= 1);
7992
7993 #ifdef GATHER_STATISTICS
7994 tree_node_counts[(int) e_kind]++;
7995 tree_node_sizes[(int) e_kind] += length;
7996 #endif
7997
7998 t = (tree) ggc_alloc_zone_pass_stat (length, &tree_zone);
7999
8000 memset (t, 0, length);
8001
8002 TREE_SET_CODE (t, code);
8003
8004 /* Can't use TREE_OPERAND to store the length because if checking is
8005 enabled, it will try to check the length before we store it. :-P */
8006 t->exp.operands[0] = build_int_cst (sizetype, len);
8007
8008 return t;
8009 }
8010
8011
8012 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE
8013 and FN and a null static chain slot. ARGLIST is a TREE_LIST of the
8014 arguments. */
8015
8016 tree
8017 build_call_list (tree return_type, tree fn, tree arglist)
8018 {
8019 tree t;
8020 int i;
8021
8022 t = build_vl_exp (CALL_EXPR, list_length (arglist) + 3);
8023 TREE_TYPE (t) = return_type;
8024 CALL_EXPR_FN (t) = fn;
8025 CALL_EXPR_STATIC_CHAIN (t) = NULL_TREE;
8026 for (i = 0; arglist; arglist = TREE_CHAIN (arglist), i++)
8027 CALL_EXPR_ARG (t, i) = TREE_VALUE (arglist);
8028 process_call_operands (t);
8029 return t;
8030 }
8031
8032 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
8033 FN and a null static chain slot. NARGS is the number of call arguments
8034 which are specified as "..." arguments. */
8035
8036 tree
8037 build_call_nary (tree return_type, tree fn, int nargs, ...)
8038 {
8039 tree ret;
8040 va_list args;
8041 va_start (args, nargs);
8042 ret = build_call_valist (return_type, fn, nargs, args);
8043 va_end (args);
8044 return ret;
8045 }
8046
8047 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
8048 FN and a null static chain slot. NARGS is the number of call arguments
8049 which are specified as a va_list ARGS. */
8050
8051 tree
8052 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
8053 {
8054 tree t;
8055 int i;
8056
8057 t = build_vl_exp (CALL_EXPR, nargs + 3);
8058 TREE_TYPE (t) = return_type;
8059 CALL_EXPR_FN (t) = fn;
8060 CALL_EXPR_STATIC_CHAIN (t) = NULL_TREE;
8061 for (i = 0; i < nargs; i++)
8062 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
8063 process_call_operands (t);
8064 return t;
8065 }
8066
8067 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
8068 FN and a null static chain slot. NARGS is the number of call arguments
8069 which are specified as a tree array ARGS. */
8070
8071 tree
8072 build_call_array (tree return_type, tree fn, int nargs, tree *args)
8073 {
8074 tree t;
8075 int i;
8076
8077 t = build_vl_exp (CALL_EXPR, nargs + 3);
8078 TREE_TYPE (t) = return_type;
8079 CALL_EXPR_FN (t) = fn;
8080 CALL_EXPR_STATIC_CHAIN (t) = NULL_TREE;
8081 for (i = 0; i < nargs; i++)
8082 CALL_EXPR_ARG (t, i) = args[i];
8083 process_call_operands (t);
8084 return t;
8085 }
8086
8087
8088 /* Returns true if it is possible to prove that the index of
8089 an array access REF (an ARRAY_REF expression) falls into the
8090 array bounds. */
8091
8092 bool
8093 in_array_bounds_p (tree ref)
8094 {
8095 tree idx = TREE_OPERAND (ref, 1);
8096 tree min, max;
8097
8098 if (TREE_CODE (idx) != INTEGER_CST)
8099 return false;
8100
8101 min = array_ref_low_bound (ref);
8102 max = array_ref_up_bound (ref);
8103 if (!min
8104 || !max
8105 || TREE_CODE (min) != INTEGER_CST
8106 || TREE_CODE (max) != INTEGER_CST)
8107 return false;
8108
8109 if (tree_int_cst_lt (idx, min)
8110 || tree_int_cst_lt (max, idx))
8111 return false;
8112
8113 return true;
8114 }
8115
8116 /* Returns true if it is possible to prove that the range of
8117 an array access REF (an ARRAY_RANGE_REF expression) falls
8118 into the array bounds. */
8119
8120 bool
8121 range_in_array_bounds_p (tree ref)
8122 {
8123 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ref));
8124 tree range_min, range_max, min, max;
8125
8126 range_min = TYPE_MIN_VALUE (domain_type);
8127 range_max = TYPE_MAX_VALUE (domain_type);
8128 if (!range_min
8129 || !range_max
8130 || TREE_CODE (range_min) != INTEGER_CST
8131 || TREE_CODE (range_max) != INTEGER_CST)
8132 return false;
8133
8134 min = array_ref_low_bound (ref);
8135 max = array_ref_up_bound (ref);
8136 if (!min
8137 || !max
8138 || TREE_CODE (min) != INTEGER_CST
8139 || TREE_CODE (max) != INTEGER_CST)
8140 return false;
8141
8142 if (tree_int_cst_lt (range_min, min)
8143 || tree_int_cst_lt (max, range_max))
8144 return false;
8145
8146 return true;
8147 }
8148
8149 /* Return true if T (assumed to be a DECL) must be assigned a memory
8150 location. */
8151
8152 bool
8153 needs_to_live_in_memory (const_tree t)
8154 {
8155 if (TREE_CODE (t) == SSA_NAME)
8156 t = SSA_NAME_VAR (t);
8157
8158 return (TREE_ADDRESSABLE (t)
8159 || is_global_var (t)
8160 || (TREE_CODE (t) == RESULT_DECL
8161 && aggregate_value_p (t, current_function_decl)));
8162 }
8163
8164 /* There are situations in which a language considers record types
8165 compatible which have different field lists. Decide if two fields
8166 are compatible. It is assumed that the parent records are compatible. */
8167
8168 bool
8169 fields_compatible_p (const_tree f1, const_tree f2)
8170 {
8171 if (!operand_equal_p (DECL_FIELD_BIT_OFFSET (f1),
8172 DECL_FIELD_BIT_OFFSET (f2), OEP_ONLY_CONST))
8173 return false;
8174
8175 if (!operand_equal_p (DECL_FIELD_OFFSET (f1),
8176 DECL_FIELD_OFFSET (f2), OEP_ONLY_CONST))
8177 return false;
8178
8179 if (!types_compatible_p (TREE_TYPE (f1), TREE_TYPE (f2)))
8180 return false;
8181
8182 return true;
8183 }
8184
8185 /* Locate within RECORD a field that is compatible with ORIG_FIELD. */
8186
8187 tree
8188 find_compatible_field (tree record, tree orig_field)
8189 {
8190 tree f;
8191
8192 for (f = TYPE_FIELDS (record); f ; f = TREE_CHAIN (f))
8193 if (TREE_CODE (f) == FIELD_DECL
8194 && fields_compatible_p (f, orig_field))
8195 return f;
8196
8197 /* ??? Why isn't this on the main fields list? */
8198 f = TYPE_VFIELD (record);
8199 if (f && TREE_CODE (f) == FIELD_DECL
8200 && fields_compatible_p (f, orig_field))
8201 return f;
8202
8203 /* ??? We should abort here, but Java appears to do Bad Things
8204 with inherited fields. */
8205 return orig_field;
8206 }
8207
8208 /* Return value of a constant X and sign-extend it. */
8209
8210 HOST_WIDE_INT
8211 int_cst_value (const_tree x)
8212 {
8213 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
8214 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
8215
8216 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
8217 gcc_assert (TREE_INT_CST_HIGH (x) == 0
8218 || TREE_INT_CST_HIGH (x) == -1);
8219
8220 if (bits < HOST_BITS_PER_WIDE_INT)
8221 {
8222 bool negative = ((val >> (bits - 1)) & 1) != 0;
8223 if (negative)
8224 val |= (~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1;
8225 else
8226 val &= ~((~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1);
8227 }
8228
8229 return val;
8230 }
8231
8232 /* If TYPE is an integral type, return an equivalent type which is
8233 unsigned iff UNSIGNEDP is true. If TYPE is not an integral type,
8234 return TYPE itself. */
8235
8236 tree
8237 signed_or_unsigned_type_for (int unsignedp, tree type)
8238 {
8239 tree t = type;
8240 if (POINTER_TYPE_P (type))
8241 t = size_type_node;
8242
8243 if (!INTEGRAL_TYPE_P (t) || TYPE_UNSIGNED (t) == unsignedp)
8244 return t;
8245
8246 return lang_hooks.types.type_for_size (TYPE_PRECISION (t), unsignedp);
8247 }
8248
8249 /* Returns unsigned variant of TYPE. */
8250
8251 tree
8252 unsigned_type_for (tree type)
8253 {
8254 return signed_or_unsigned_type_for (1, type);
8255 }
8256
8257 /* Returns signed variant of TYPE. */
8258
8259 tree
8260 signed_type_for (tree type)
8261 {
8262 return signed_or_unsigned_type_for (0, type);
8263 }
8264
8265 /* Returns the largest value obtainable by casting something in INNER type to
8266 OUTER type. */
8267
8268 tree
8269 upper_bound_in_type (tree outer, tree inner)
8270 {
8271 unsigned HOST_WIDE_INT lo, hi;
8272 unsigned int det = 0;
8273 unsigned oprec = TYPE_PRECISION (outer);
8274 unsigned iprec = TYPE_PRECISION (inner);
8275 unsigned prec;
8276
8277 /* Compute a unique number for every combination. */
8278 det |= (oprec > iprec) ? 4 : 0;
8279 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
8280 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
8281
8282 /* Determine the exponent to use. */
8283 switch (det)
8284 {
8285 case 0:
8286 case 1:
8287 /* oprec <= iprec, outer: signed, inner: don't care. */
8288 prec = oprec - 1;
8289 break;
8290 case 2:
8291 case 3:
8292 /* oprec <= iprec, outer: unsigned, inner: don't care. */
8293 prec = oprec;
8294 break;
8295 case 4:
8296 /* oprec > iprec, outer: signed, inner: signed. */
8297 prec = iprec - 1;
8298 break;
8299 case 5:
8300 /* oprec > iprec, outer: signed, inner: unsigned. */
8301 prec = iprec;
8302 break;
8303 case 6:
8304 /* oprec > iprec, outer: unsigned, inner: signed. */
8305 prec = oprec;
8306 break;
8307 case 7:
8308 /* oprec > iprec, outer: unsigned, inner: unsigned. */
8309 prec = iprec;
8310 break;
8311 default:
8312 gcc_unreachable ();
8313 }
8314
8315 /* Compute 2^^prec - 1. */
8316 if (prec <= HOST_BITS_PER_WIDE_INT)
8317 {
8318 hi = 0;
8319 lo = ((~(unsigned HOST_WIDE_INT) 0)
8320 >> (HOST_BITS_PER_WIDE_INT - prec));
8321 }
8322 else
8323 {
8324 hi = ((~(unsigned HOST_WIDE_INT) 0)
8325 >> (2 * HOST_BITS_PER_WIDE_INT - prec));
8326 lo = ~(unsigned HOST_WIDE_INT) 0;
8327 }
8328
8329 return build_int_cst_wide (outer, lo, hi);
8330 }
8331
8332 /* Returns the smallest value obtainable by casting something in INNER type to
8333 OUTER type. */
8334
8335 tree
8336 lower_bound_in_type (tree outer, tree inner)
8337 {
8338 unsigned HOST_WIDE_INT lo, hi;
8339 unsigned oprec = TYPE_PRECISION (outer);
8340 unsigned iprec = TYPE_PRECISION (inner);
8341
8342 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
8343 and obtain 0. */
8344 if (TYPE_UNSIGNED (outer)
8345 /* If we are widening something of an unsigned type, OUTER type
8346 contains all values of INNER type. In particular, both INNER
8347 and OUTER types have zero in common. */
8348 || (oprec > iprec && TYPE_UNSIGNED (inner)))
8349 lo = hi = 0;
8350 else
8351 {
8352 /* If we are widening a signed type to another signed type, we
8353 want to obtain -2^^(iprec-1). If we are keeping the
8354 precision or narrowing to a signed type, we want to obtain
8355 -2^(oprec-1). */
8356 unsigned prec = oprec > iprec ? iprec : oprec;
8357
8358 if (prec <= HOST_BITS_PER_WIDE_INT)
8359 {
8360 hi = ~(unsigned HOST_WIDE_INT) 0;
8361 lo = (~(unsigned HOST_WIDE_INT) 0) << (prec - 1);
8362 }
8363 else
8364 {
8365 hi = ((~(unsigned HOST_WIDE_INT) 0)
8366 << (prec - HOST_BITS_PER_WIDE_INT - 1));
8367 lo = 0;
8368 }
8369 }
8370
8371 return build_int_cst_wide (outer, lo, hi);
8372 }
8373
8374 /* Return nonzero if two operands that are suitable for PHI nodes are
8375 necessarily equal. Specifically, both ARG0 and ARG1 must be either
8376 SSA_NAME or invariant. Note that this is strictly an optimization.
8377 That is, callers of this function can directly call operand_equal_p
8378 and get the same result, only slower. */
8379
8380 int
8381 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
8382 {
8383 if (arg0 == arg1)
8384 return 1;
8385 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
8386 return 0;
8387 return operand_equal_p (arg0, arg1, 0);
8388 }
8389
8390 /* Returns number of zeros at the end of binary representation of X.
8391
8392 ??? Use ffs if available? */
8393
8394 tree
8395 num_ending_zeros (const_tree x)
8396 {
8397 unsigned HOST_WIDE_INT fr, nfr;
8398 unsigned num, abits;
8399 tree type = TREE_TYPE (x);
8400
8401 if (TREE_INT_CST_LOW (x) == 0)
8402 {
8403 num = HOST_BITS_PER_WIDE_INT;
8404 fr = TREE_INT_CST_HIGH (x);
8405 }
8406 else
8407 {
8408 num = 0;
8409 fr = TREE_INT_CST_LOW (x);
8410 }
8411
8412 for (abits = HOST_BITS_PER_WIDE_INT / 2; abits; abits /= 2)
8413 {
8414 nfr = fr >> abits;
8415 if (nfr << abits == fr)
8416 {
8417 num += abits;
8418 fr = nfr;
8419 }
8420 }
8421
8422 if (num > TYPE_PRECISION (type))
8423 num = TYPE_PRECISION (type);
8424
8425 return build_int_cst_type (type, num);
8426 }
8427
8428
8429 #define WALK_SUBTREE(NODE) \
8430 do \
8431 { \
8432 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
8433 if (result) \
8434 return result; \
8435 } \
8436 while (0)
8437
8438 /* This is a subroutine of walk_tree that walks field of TYPE that are to
8439 be walked whenever a type is seen in the tree. Rest of operands and return
8440 value are as for walk_tree. */
8441
8442 static tree
8443 walk_type_fields (tree type, walk_tree_fn func, void *data,
8444 struct pointer_set_t *pset, walk_tree_lh lh)
8445 {
8446 tree result = NULL_TREE;
8447
8448 switch (TREE_CODE (type))
8449 {
8450 case POINTER_TYPE:
8451 case REFERENCE_TYPE:
8452 /* We have to worry about mutually recursive pointers. These can't
8453 be written in C. They can in Ada. It's pathological, but
8454 there's an ACATS test (c38102a) that checks it. Deal with this
8455 by checking if we're pointing to another pointer, that one
8456 points to another pointer, that one does too, and we have no htab.
8457 If so, get a hash table. We check three levels deep to avoid
8458 the cost of the hash table if we don't need one. */
8459 if (POINTER_TYPE_P (TREE_TYPE (type))
8460 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
8461 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
8462 && !pset)
8463 {
8464 result = walk_tree_without_duplicates (&TREE_TYPE (type),
8465 func, data);
8466 if (result)
8467 return result;
8468
8469 break;
8470 }
8471
8472 /* ... fall through ... */
8473
8474 case COMPLEX_TYPE:
8475 WALK_SUBTREE (TREE_TYPE (type));
8476 break;
8477
8478 case METHOD_TYPE:
8479 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
8480
8481 /* Fall through. */
8482
8483 case FUNCTION_TYPE:
8484 WALK_SUBTREE (TREE_TYPE (type));
8485 {
8486 tree arg;
8487
8488 /* We never want to walk into default arguments. */
8489 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
8490 WALK_SUBTREE (TREE_VALUE (arg));
8491 }
8492 break;
8493
8494 case ARRAY_TYPE:
8495 /* Don't follow this nodes's type if a pointer for fear that
8496 we'll have infinite recursion. If we have a PSET, then we
8497 need not fear. */
8498 if (pset
8499 || (!POINTER_TYPE_P (TREE_TYPE (type))
8500 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
8501 WALK_SUBTREE (TREE_TYPE (type));
8502 WALK_SUBTREE (TYPE_DOMAIN (type));
8503 break;
8504
8505 case OFFSET_TYPE:
8506 WALK_SUBTREE (TREE_TYPE (type));
8507 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
8508 break;
8509
8510 default:
8511 break;
8512 }
8513
8514 return NULL_TREE;
8515 }
8516
8517 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
8518 called with the DATA and the address of each sub-tree. If FUNC returns a
8519 non-NULL value, the traversal is stopped, and the value returned by FUNC
8520 is returned. If PSET is non-NULL it is used to record the nodes visited,
8521 and to avoid visiting a node more than once. */
8522
8523 tree
8524 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
8525 struct pointer_set_t *pset, walk_tree_lh lh)
8526 {
8527 enum tree_code code;
8528 int walk_subtrees;
8529 tree result;
8530
8531 #define WALK_SUBTREE_TAIL(NODE) \
8532 do \
8533 { \
8534 tp = & (NODE); \
8535 goto tail_recurse; \
8536 } \
8537 while (0)
8538
8539 tail_recurse:
8540 /* Skip empty subtrees. */
8541 if (!*tp)
8542 return NULL_TREE;
8543
8544 /* Don't walk the same tree twice, if the user has requested
8545 that we avoid doing so. */
8546 if (pset && pointer_set_insert (pset, *tp))
8547 return NULL_TREE;
8548
8549 /* Call the function. */
8550 walk_subtrees = 1;
8551 result = (*func) (tp, &walk_subtrees, data);
8552
8553 /* If we found something, return it. */
8554 if (result)
8555 return result;
8556
8557 code = TREE_CODE (*tp);
8558
8559 /* Even if we didn't, FUNC may have decided that there was nothing
8560 interesting below this point in the tree. */
8561 if (!walk_subtrees)
8562 {
8563 /* But we still need to check our siblings. */
8564 if (code == TREE_LIST)
8565 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
8566 else if (code == OMP_CLAUSE)
8567 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
8568 else
8569 return NULL_TREE;
8570 }
8571
8572 if (lh)
8573 {
8574 result = (*lh) (tp, &walk_subtrees, func, data, pset);
8575 if (result || !walk_subtrees)
8576 return result;
8577 }
8578
8579 switch (code)
8580 {
8581 case ERROR_MARK:
8582 case IDENTIFIER_NODE:
8583 case INTEGER_CST:
8584 case REAL_CST:
8585 case FIXED_CST:
8586 case VECTOR_CST:
8587 case STRING_CST:
8588 case BLOCK:
8589 case PLACEHOLDER_EXPR:
8590 case SSA_NAME:
8591 case FIELD_DECL:
8592 case RESULT_DECL:
8593 /* None of these have subtrees other than those already walked
8594 above. */
8595 break;
8596
8597 case TREE_LIST:
8598 WALK_SUBTREE (TREE_VALUE (*tp));
8599 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
8600 break;
8601
8602 case TREE_VEC:
8603 {
8604 int len = TREE_VEC_LENGTH (*tp);
8605
8606 if (len == 0)
8607 break;
8608
8609 /* Walk all elements but the first. */
8610 while (--len)
8611 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
8612
8613 /* Now walk the first one as a tail call. */
8614 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
8615 }
8616
8617 case COMPLEX_CST:
8618 WALK_SUBTREE (TREE_REALPART (*tp));
8619 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
8620
8621 case CONSTRUCTOR:
8622 {
8623 unsigned HOST_WIDE_INT idx;
8624 constructor_elt *ce;
8625
8626 for (idx = 0;
8627 VEC_iterate(constructor_elt, CONSTRUCTOR_ELTS (*tp), idx, ce);
8628 idx++)
8629 WALK_SUBTREE (ce->value);
8630 }
8631 break;
8632
8633 case SAVE_EXPR:
8634 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
8635
8636 case BIND_EXPR:
8637 {
8638 tree decl;
8639 for (decl = BIND_EXPR_VARS (*tp); decl; decl = TREE_CHAIN (decl))
8640 {
8641 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
8642 into declarations that are just mentioned, rather than
8643 declared; they don't really belong to this part of the tree.
8644 And, we can see cycles: the initializer for a declaration
8645 can refer to the declaration itself. */
8646 WALK_SUBTREE (DECL_INITIAL (decl));
8647 WALK_SUBTREE (DECL_SIZE (decl));
8648 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
8649 }
8650 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
8651 }
8652
8653 case STATEMENT_LIST:
8654 {
8655 tree_stmt_iterator i;
8656 for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
8657 WALK_SUBTREE (*tsi_stmt_ptr (i));
8658 }
8659 break;
8660
8661 case OMP_CLAUSE:
8662 switch (OMP_CLAUSE_CODE (*tp))
8663 {
8664 case OMP_CLAUSE_PRIVATE:
8665 case OMP_CLAUSE_SHARED:
8666 case OMP_CLAUSE_FIRSTPRIVATE:
8667 case OMP_CLAUSE_COPYIN:
8668 case OMP_CLAUSE_COPYPRIVATE:
8669 case OMP_CLAUSE_IF:
8670 case OMP_CLAUSE_NUM_THREADS:
8671 case OMP_CLAUSE_SCHEDULE:
8672 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0));
8673 /* FALLTHRU */
8674
8675 case OMP_CLAUSE_NOWAIT:
8676 case OMP_CLAUSE_ORDERED:
8677 case OMP_CLAUSE_DEFAULT:
8678 case OMP_CLAUSE_UNTIED:
8679 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
8680
8681 case OMP_CLAUSE_LASTPRIVATE:
8682 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
8683 WALK_SUBTREE (OMP_CLAUSE_LASTPRIVATE_STMT (*tp));
8684 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
8685
8686 case OMP_CLAUSE_COLLAPSE:
8687 {
8688 int i;
8689 for (i = 0; i < 3; i++)
8690 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
8691 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
8692 }
8693
8694 case OMP_CLAUSE_REDUCTION:
8695 {
8696 int i;
8697 for (i = 0; i < 4; i++)
8698 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
8699 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
8700 }
8701
8702 default:
8703 gcc_unreachable ();
8704 }
8705 break;
8706
8707 case TARGET_EXPR:
8708 {
8709 int i, len;
8710
8711 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
8712 But, we only want to walk once. */
8713 len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
8714 for (i = 0; i < len; ++i)
8715 WALK_SUBTREE (TREE_OPERAND (*tp, i));
8716 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
8717 }
8718
8719 case CHANGE_DYNAMIC_TYPE_EXPR:
8720 WALK_SUBTREE (CHANGE_DYNAMIC_TYPE_NEW_TYPE (*tp));
8721 WALK_SUBTREE_TAIL (CHANGE_DYNAMIC_TYPE_LOCATION (*tp));
8722
8723 case DECL_EXPR:
8724 /* If this is a TYPE_DECL, walk into the fields of the type that it's
8725 defining. We only want to walk into these fields of a type in this
8726 case and not in the general case of a mere reference to the type.
8727
8728 The criterion is as follows: if the field can be an expression, it
8729 must be walked only here. This should be in keeping with the fields
8730 that are directly gimplified in gimplify_type_sizes in order for the
8731 mark/copy-if-shared/unmark machinery of the gimplifier to work with
8732 variable-sized types.
8733
8734 Note that DECLs get walked as part of processing the BIND_EXPR. */
8735 if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
8736 {
8737 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
8738 if (TREE_CODE (*type_p) == ERROR_MARK)
8739 return NULL_TREE;
8740
8741 /* Call the function for the type. See if it returns anything or
8742 doesn't want us to continue. If we are to continue, walk both
8743 the normal fields and those for the declaration case. */
8744 result = (*func) (type_p, &walk_subtrees, data);
8745 if (result || !walk_subtrees)
8746 return result;
8747
8748 result = walk_type_fields (*type_p, func, data, pset, lh);
8749 if (result)
8750 return result;
8751
8752 /* If this is a record type, also walk the fields. */
8753 if (TREE_CODE (*type_p) == RECORD_TYPE
8754 || TREE_CODE (*type_p) == UNION_TYPE
8755 || TREE_CODE (*type_p) == QUAL_UNION_TYPE)
8756 {
8757 tree field;
8758
8759 for (field = TYPE_FIELDS (*type_p); field;
8760 field = TREE_CHAIN (field))
8761 {
8762 /* We'd like to look at the type of the field, but we can
8763 easily get infinite recursion. So assume it's pointed
8764 to elsewhere in the tree. Also, ignore things that
8765 aren't fields. */
8766 if (TREE_CODE (field) != FIELD_DECL)
8767 continue;
8768
8769 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
8770 WALK_SUBTREE (DECL_SIZE (field));
8771 WALK_SUBTREE (DECL_SIZE_UNIT (field));
8772 if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
8773 WALK_SUBTREE (DECL_QUALIFIER (field));
8774 }
8775 }
8776
8777 /* Same for scalar types. */
8778 else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
8779 || TREE_CODE (*type_p) == ENUMERAL_TYPE
8780 || TREE_CODE (*type_p) == INTEGER_TYPE
8781 || TREE_CODE (*type_p) == FIXED_POINT_TYPE
8782 || TREE_CODE (*type_p) == REAL_TYPE)
8783 {
8784 WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
8785 WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
8786 }
8787
8788 WALK_SUBTREE (TYPE_SIZE (*type_p));
8789 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
8790 }
8791 /* FALLTHRU */
8792
8793 default:
8794 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
8795 {
8796 int i, len;
8797
8798 /* Walk over all the sub-trees of this operand. */
8799 len = TREE_OPERAND_LENGTH (*tp);
8800
8801 /* Go through the subtrees. We need to do this in forward order so
8802 that the scope of a FOR_EXPR is handled properly. */
8803 if (len)
8804 {
8805 for (i = 0; i < len - 1; ++i)
8806 WALK_SUBTREE (TREE_OPERAND (*tp, i));
8807 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
8808 }
8809 }
8810 /* If this is a type, walk the needed fields in the type. */
8811 else if (TYPE_P (*tp))
8812 return walk_type_fields (*tp, func, data, pset, lh);
8813 break;
8814 }
8815
8816 /* We didn't find what we were looking for. */
8817 return NULL_TREE;
8818
8819 #undef WALK_SUBTREE_TAIL
8820 }
8821 #undef WALK_SUBTREE
8822
8823 /* Like walk_tree, but does not walk duplicate nodes more than once. */
8824
8825 tree
8826 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
8827 walk_tree_lh lh)
8828 {
8829 tree result;
8830 struct pointer_set_t *pset;
8831
8832 pset = pointer_set_create ();
8833 result = walk_tree_1 (tp, func, data, pset, lh);
8834 pointer_set_destroy (pset);
8835 return result;
8836 }
8837
8838
8839 tree *
8840 tree_block (tree t)
8841 {
8842 char const c = TREE_CODE_CLASS (TREE_CODE (t));
8843
8844 if (IS_EXPR_CODE_CLASS (c))
8845 return &t->exp.block;
8846 gcc_unreachable ();
8847 return NULL;
8848 }
8849
8850 /* Build and return a TREE_LIST of arguments in the CALL_EXPR exp.
8851 FIXME: don't use this function. It exists for compatibility with
8852 the old representation of CALL_EXPRs where a list was used to hold the
8853 arguments. Places that currently extract the arglist from a CALL_EXPR
8854 ought to be rewritten to use the CALL_EXPR itself. */
8855 tree
8856 call_expr_arglist (tree exp)
8857 {
8858 tree arglist = NULL_TREE;
8859 int i;
8860 for (i = call_expr_nargs (exp) - 1; i >= 0; i--)
8861 arglist = tree_cons (NULL_TREE, CALL_EXPR_ARG (exp, i), arglist);
8862 return arglist;
8863 }
8864
8865
8866 /* Create a nameless artificial label and put it in the current function
8867 context. Returns the newly created label. */
8868
8869 tree
8870 create_artificial_label (void)
8871 {
8872 tree lab = build_decl (LABEL_DECL, NULL_TREE, void_type_node);
8873
8874 DECL_ARTIFICIAL (lab) = 1;
8875 DECL_IGNORED_P (lab) = 1;
8876 DECL_CONTEXT (lab) = current_function_decl;
8877 return lab;
8878 }
8879
8880 /* Given a tree, try to return a useful variable name that we can use
8881 to prefix a temporary that is being assigned the value of the tree.
8882 I.E. given <temp> = &A, return A. */
8883
8884 const char *
8885 get_name (tree t)
8886 {
8887 tree stripped_decl;
8888
8889 stripped_decl = t;
8890 STRIP_NOPS (stripped_decl);
8891 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
8892 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
8893 else
8894 {
8895 switch (TREE_CODE (stripped_decl))
8896 {
8897 case ADDR_EXPR:
8898 return get_name (TREE_OPERAND (stripped_decl, 0));
8899 default:
8900 return NULL;
8901 }
8902 }
8903 }
8904
8905 /* Return true if TYPE has a variable argument list. */
8906
8907 bool
8908 stdarg_p (tree fntype)
8909 {
8910 function_args_iterator args_iter;
8911 tree n = NULL_TREE, t;
8912
8913 if (!fntype)
8914 return false;
8915
8916 FOREACH_FUNCTION_ARGS(fntype, t, args_iter)
8917 {
8918 n = t;
8919 }
8920
8921 return n != NULL_TREE && n != void_type_node;
8922 }
8923
8924 /* Return true if TYPE has a prototype. */
8925
8926 bool
8927 prototype_p (tree fntype)
8928 {
8929 tree t;
8930
8931 gcc_assert (fntype != NULL_TREE);
8932
8933 t = TYPE_ARG_TYPES (fntype);
8934 return (t != NULL_TREE);
8935 }
8936
8937 /* Return the number of arguments that a function has. */
8938
8939 int
8940 function_args_count (tree fntype)
8941 {
8942 function_args_iterator args_iter;
8943 tree t;
8944 int num = 0;
8945
8946 if (fntype)
8947 {
8948 FOREACH_FUNCTION_ARGS(fntype, t, args_iter)
8949 {
8950 num++;
8951 }
8952 }
8953
8954 return num;
8955 }
8956
8957 /* If BLOCK is inlined from an __attribute__((__artificial__))
8958 routine, return pointer to location from where it has been
8959 called. */
8960 location_t *
8961 block_nonartificial_location (tree block)
8962 {
8963 location_t *ret = NULL;
8964
8965 while (block && TREE_CODE (block) == BLOCK
8966 && BLOCK_ABSTRACT_ORIGIN (block))
8967 {
8968 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
8969
8970 while (TREE_CODE (ao) == BLOCK
8971 && BLOCK_ABSTRACT_ORIGIN (ao)
8972 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
8973 ao = BLOCK_ABSTRACT_ORIGIN (ao);
8974
8975 if (TREE_CODE (ao) == FUNCTION_DECL)
8976 {
8977 /* If AO is an artificial inline, point RET to the
8978 call site locus at which it has been inlined and continue
8979 the loop, in case AO's caller is also an artificial
8980 inline. */
8981 if (DECL_DECLARED_INLINE_P (ao)
8982 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
8983 ret = &BLOCK_SOURCE_LOCATION (block);
8984 else
8985 break;
8986 }
8987 else if (TREE_CODE (ao) != BLOCK)
8988 break;
8989
8990 block = BLOCK_SUPERCONTEXT (block);
8991 }
8992 return ret;
8993 }
8994
8995 /* These are the hash table functions for the hash table of OPTIMIZATION_NODEq
8996 nodes. */
8997
8998 /* Return the hash code code X, an OPTIMIZATION_NODE or TARGET_OPTION code. */
8999
9000 static hashval_t
9001 cl_option_hash_hash (const void *x)
9002 {
9003 const_tree const t = (const_tree) x;
9004 const char *p;
9005 size_t i;
9006 size_t len = 0;
9007 hashval_t hash = 0;
9008
9009 if (TREE_CODE (t) == OPTIMIZATION_NODE)
9010 {
9011 p = (const char *)TREE_OPTIMIZATION (t);
9012 len = sizeof (struct cl_optimization);
9013 }
9014
9015 else if (TREE_CODE (t) == TARGET_OPTION_NODE)
9016 {
9017 p = (const char *)TREE_TARGET_OPTION (t);
9018 len = sizeof (struct cl_target_option);
9019 }
9020
9021 else
9022 gcc_unreachable ();
9023
9024 /* assume most opt flags are just 0/1, some are 2-3, and a few might be
9025 something else. */
9026 for (i = 0; i < len; i++)
9027 if (p[i])
9028 hash = (hash << 4) ^ ((i << 2) | p[i]);
9029
9030 return hash;
9031 }
9032
9033 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
9034 TARGET_OPTION tree node) is the same as that given by *Y, which is the
9035 same. */
9036
9037 static int
9038 cl_option_hash_eq (const void *x, const void *y)
9039 {
9040 const_tree const xt = (const_tree) x;
9041 const_tree const yt = (const_tree) y;
9042 const char *xp;
9043 const char *yp;
9044 size_t len;
9045
9046 if (TREE_CODE (xt) != TREE_CODE (yt))
9047 return 0;
9048
9049 if (TREE_CODE (xt) == OPTIMIZATION_NODE)
9050 {
9051 xp = (const char *)TREE_OPTIMIZATION (xt);
9052 yp = (const char *)TREE_OPTIMIZATION (yt);
9053 len = sizeof (struct cl_optimization);
9054 }
9055
9056 else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
9057 {
9058 xp = (const char *)TREE_TARGET_OPTION (xt);
9059 yp = (const char *)TREE_TARGET_OPTION (yt);
9060 len = sizeof (struct cl_target_option);
9061 }
9062
9063 else
9064 gcc_unreachable ();
9065
9066 return (memcmp (xp, yp, len) == 0);
9067 }
9068
9069 /* Build an OPTIMIZATION_NODE based on the current options. */
9070
9071 tree
9072 build_optimization_node (void)
9073 {
9074 tree t;
9075 void **slot;
9076
9077 /* Use the cache of optimization nodes. */
9078
9079 cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node));
9080
9081 slot = htab_find_slot (cl_option_hash_table, cl_optimization_node, INSERT);
9082 t = (tree) *slot;
9083 if (!t)
9084 {
9085 /* Insert this one into the hash table. */
9086 t = cl_optimization_node;
9087 *slot = t;
9088
9089 /* Make a new node for next time round. */
9090 cl_optimization_node = make_node (OPTIMIZATION_NODE);
9091 }
9092
9093 return t;
9094 }
9095
9096 /* Build a TARGET_OPTION_NODE based on the current options. */
9097
9098 tree
9099 build_target_option_node (void)
9100 {
9101 tree t;
9102 void **slot;
9103
9104 /* Use the cache of optimization nodes. */
9105
9106 cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node));
9107
9108 slot = htab_find_slot (cl_option_hash_table, cl_target_option_node, INSERT);
9109 t = (tree) *slot;
9110 if (!t)
9111 {
9112 /* Insert this one into the hash table. */
9113 t = cl_target_option_node;
9114 *slot = t;
9115
9116 /* Make a new node for next time round. */
9117 cl_target_option_node = make_node (TARGET_OPTION_NODE);
9118 }
9119
9120 return t;
9121 }
9122
9123 #include "gt-tree.h"