OSDN Git Service

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