OSDN Git Service

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