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