OSDN Git Service

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