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