OSDN Git Service

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