OSDN Git Service

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