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