OSDN Git Service

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