OSDN Git Service

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