OSDN Git Service

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