OSDN Git Service

2009-07-07 Manuel López-Ibáñez <manu@gcc.gnu.org>
[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 \f
2198 /* Return the bit position of FIELD, in bits from the start of the record.
2199    This is a tree of type bitsizetype.  */
2200
2201 tree
2202 bit_position (const_tree field)
2203 {
2204   return bit_from_pos (DECL_FIELD_OFFSET (field),
2205                        DECL_FIELD_BIT_OFFSET (field));
2206 }
2207
2208 /* Likewise, but return as an integer.  It must be representable in
2209    that way (since it could be a signed value, we don't have the
2210    option of returning -1 like int_size_in_byte can.  */
2211
2212 HOST_WIDE_INT
2213 int_bit_position (const_tree field)
2214 {
2215   return tree_low_cst (bit_position (field), 0);
2216 }
2217 \f
2218 /* Return the byte position of FIELD, in bytes from the start of the record.
2219    This is a tree of type sizetype.  */
2220
2221 tree
2222 byte_position (const_tree field)
2223 {
2224   return byte_from_pos (DECL_FIELD_OFFSET (field),
2225                         DECL_FIELD_BIT_OFFSET (field));
2226 }
2227
2228 /* Likewise, but return as an integer.  It must be representable in
2229    that way (since it could be a signed value, we don't have the
2230    option of returning -1 like int_size_in_byte can.  */
2231
2232 HOST_WIDE_INT
2233 int_byte_position (const_tree field)
2234 {
2235   return tree_low_cst (byte_position (field), 0);
2236 }
2237 \f
2238 /* Return the strictest alignment, in bits, that T is known to have.  */
2239
2240 unsigned int
2241 expr_align (const_tree t)
2242 {
2243   unsigned int align0, align1;
2244
2245   switch (TREE_CODE (t))
2246     {
2247     CASE_CONVERT:  case NON_LVALUE_EXPR:
2248       /* If we have conversions, we know that the alignment of the
2249          object must meet each of the alignments of the types.  */
2250       align0 = expr_align (TREE_OPERAND (t, 0));
2251       align1 = TYPE_ALIGN (TREE_TYPE (t));
2252       return MAX (align0, align1);
2253
2254     case SAVE_EXPR:         case COMPOUND_EXPR:       case MODIFY_EXPR:
2255     case INIT_EXPR:         case TARGET_EXPR:         case WITH_CLEANUP_EXPR:
2256     case CLEANUP_POINT_EXPR:
2257       /* These don't change the alignment of an object.  */
2258       return expr_align (TREE_OPERAND (t, 0));
2259
2260     case COND_EXPR:
2261       /* The best we can do is say that the alignment is the least aligned
2262          of the two arms.  */
2263       align0 = expr_align (TREE_OPERAND (t, 1));
2264       align1 = expr_align (TREE_OPERAND (t, 2));
2265       return MIN (align0, align1);
2266
2267       /* FIXME: LABEL_DECL and CONST_DECL never have DECL_ALIGN set
2268          meaningfully, it's always 1.  */
2269     case LABEL_DECL:     case CONST_DECL:
2270     case VAR_DECL:       case PARM_DECL:   case RESULT_DECL:
2271     case FUNCTION_DECL:
2272       gcc_assert (DECL_ALIGN (t) != 0);
2273       return DECL_ALIGN (t);
2274
2275     default:
2276       break;
2277     }
2278
2279   /* Otherwise take the alignment from that of the type.  */
2280   return TYPE_ALIGN (TREE_TYPE (t));
2281 }
2282 \f
2283 /* Return, as a tree node, the number of elements for TYPE (which is an
2284    ARRAY_TYPE) minus one. This counts only elements of the top array.  */
2285
2286 tree
2287 array_type_nelts (const_tree type)
2288 {
2289   tree index_type, min, max;
2290
2291   /* If they did it with unspecified bounds, then we should have already
2292      given an error about it before we got here.  */
2293   if (! TYPE_DOMAIN (type))
2294     return error_mark_node;
2295
2296   index_type = TYPE_DOMAIN (type);
2297   min = TYPE_MIN_VALUE (index_type);
2298   max = TYPE_MAX_VALUE (index_type);
2299
2300   return (integer_zerop (min)
2301           ? max
2302           : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
2303 }
2304 \f
2305 /* If arg is static -- a reference to an object in static storage -- then
2306    return the object.  This is not the same as the C meaning of `static'.
2307    If arg isn't static, return NULL.  */
2308
2309 tree
2310 staticp (tree arg)
2311 {
2312   switch (TREE_CODE (arg))
2313     {
2314     case FUNCTION_DECL:
2315       /* Nested functions are static, even though taking their address will
2316          involve a trampoline as we unnest the nested function and create
2317          the trampoline on the tree level.  */
2318       return arg;
2319
2320     case VAR_DECL:
2321       return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2322               && ! DECL_THREAD_LOCAL_P (arg)
2323               && ! DECL_DLLIMPORT_P (arg)
2324               ? arg : NULL);
2325
2326     case CONST_DECL:
2327       return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
2328               ? arg : NULL);
2329
2330     case CONSTRUCTOR:
2331       return TREE_STATIC (arg) ? arg : NULL;
2332
2333     case LABEL_DECL:
2334     case STRING_CST:
2335       return arg;
2336
2337     case COMPONENT_REF:
2338       /* If the thing being referenced is not a field, then it is
2339          something language specific.  */
2340       gcc_assert (TREE_CODE (TREE_OPERAND (arg, 1)) == FIELD_DECL);
2341
2342       /* If we are referencing a bitfield, we can't evaluate an
2343          ADDR_EXPR at compile time and so it isn't a constant.  */
2344       if (DECL_BIT_FIELD (TREE_OPERAND (arg, 1)))
2345         return NULL;
2346
2347       return staticp (TREE_OPERAND (arg, 0));
2348
2349     case BIT_FIELD_REF:
2350       return NULL;
2351
2352     case MISALIGNED_INDIRECT_REF:
2353     case ALIGN_INDIRECT_REF:
2354     case INDIRECT_REF:
2355       return TREE_CONSTANT (TREE_OPERAND (arg, 0)) ? arg : NULL;
2356
2357     case ARRAY_REF:
2358     case ARRAY_RANGE_REF:
2359       if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
2360           && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
2361         return staticp (TREE_OPERAND (arg, 0));
2362       else
2363         return NULL;
2364
2365     case COMPOUND_LITERAL_EXPR:
2366       return TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (arg)) ? arg : NULL;
2367
2368     default:
2369       return NULL;
2370     }
2371 }
2372
2373 \f
2374
2375
2376 /* Return whether OP is a DECL whose address is function-invariant.  */
2377
2378 bool
2379 decl_address_invariant_p (const_tree op)
2380 {
2381   /* The conditions below are slightly less strict than the one in
2382      staticp.  */
2383
2384   switch (TREE_CODE (op))
2385     {
2386     case PARM_DECL:
2387     case RESULT_DECL:
2388     case LABEL_DECL:
2389     case FUNCTION_DECL:
2390       return true;
2391
2392     case VAR_DECL:
2393       if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
2394            && !DECL_DLLIMPORT_P (op))
2395           || DECL_THREAD_LOCAL_P (op)
2396           || DECL_CONTEXT (op) == current_function_decl
2397           || decl_function_context (op) == current_function_decl)
2398         return true;
2399       break;
2400
2401     case CONST_DECL:
2402       if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
2403           || decl_function_context (op) == current_function_decl)
2404         return true;
2405       break;
2406
2407     default:
2408       break;
2409     }
2410
2411   return false;
2412 }
2413
2414 /* Return whether OP is a DECL whose address is interprocedural-invariant.  */
2415
2416 bool
2417 decl_address_ip_invariant_p (const_tree op)
2418 {
2419   /* The conditions below are slightly less strict than the one in
2420      staticp.  */
2421
2422   switch (TREE_CODE (op))
2423     {
2424     case LABEL_DECL:
2425     case FUNCTION_DECL:
2426     case STRING_CST:
2427       return true;
2428
2429     case VAR_DECL:
2430       if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
2431            && !DECL_DLLIMPORT_P (op))
2432           || DECL_THREAD_LOCAL_P (op))
2433         return true;
2434       break;
2435
2436     case CONST_DECL:
2437       if ((TREE_STATIC (op) || DECL_EXTERNAL (op)))
2438         return true;
2439       break;
2440
2441     default:
2442       break;
2443     }
2444
2445   return false;
2446 }
2447
2448
2449 /* Return true if T is function-invariant (internal function, does
2450    not handle arithmetic; that's handled in skip_simple_arithmetic and
2451    tree_invariant_p).  */
2452
2453 static bool tree_invariant_p (tree t);
2454
2455 static bool
2456 tree_invariant_p_1 (tree t)
2457 {
2458   tree op;
2459
2460   if (TREE_CONSTANT (t)
2461       || (TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t)))
2462     return true;
2463
2464   switch (TREE_CODE (t))
2465     {
2466     case SAVE_EXPR:
2467       return true;
2468
2469     case ADDR_EXPR:
2470       op = TREE_OPERAND (t, 0);
2471       while (handled_component_p (op))
2472         {
2473           switch (TREE_CODE (op))
2474             {
2475             case ARRAY_REF:
2476             case ARRAY_RANGE_REF:
2477               if (!tree_invariant_p (TREE_OPERAND (op, 1))
2478                   || TREE_OPERAND (op, 2) != NULL_TREE
2479                   || TREE_OPERAND (op, 3) != NULL_TREE)
2480                 return false;
2481               break;
2482
2483             case COMPONENT_REF:
2484               if (TREE_OPERAND (op, 2) != NULL_TREE)
2485                 return false;
2486               break;
2487
2488             default:;
2489             }
2490           op = TREE_OPERAND (op, 0);
2491         }
2492
2493       return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
2494
2495     default:
2496       break;
2497     }
2498
2499   return false;
2500 }
2501
2502 /* Return true if T is function-invariant.  */
2503
2504 static bool
2505 tree_invariant_p (tree t)
2506 {
2507   tree inner = skip_simple_arithmetic (t);
2508   return tree_invariant_p_1 (inner);
2509 }
2510
2511 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
2512    Do this to any expression which may be used in more than one place,
2513    but must be evaluated only once.
2514
2515    Normally, expand_expr would reevaluate the expression each time.
2516    Calling save_expr produces something that is evaluated and recorded
2517    the first time expand_expr is called on it.  Subsequent calls to
2518    expand_expr just reuse the recorded value.
2519
2520    The call to expand_expr that generates code that actually computes
2521    the value is the first call *at compile time*.  Subsequent calls
2522    *at compile time* generate code to use the saved value.
2523    This produces correct result provided that *at run time* control
2524    always flows through the insns made by the first expand_expr
2525    before reaching the other places where the save_expr was evaluated.
2526    You, the caller of save_expr, must make sure this is so.
2527
2528    Constants, and certain read-only nodes, are returned with no
2529    SAVE_EXPR because that is safe.  Expressions containing placeholders
2530    are not touched; see tree.def for an explanation of what these
2531    are used for.  */
2532
2533 tree
2534 save_expr (tree expr)
2535 {
2536   tree t = fold (expr);
2537   tree inner;
2538
2539   /* If the tree evaluates to a constant, then we don't want to hide that
2540      fact (i.e. this allows further folding, and direct checks for constants).
2541      However, a read-only object that has side effects cannot be bypassed.
2542      Since it is no problem to reevaluate literals, we just return the
2543      literal node.  */
2544   inner = skip_simple_arithmetic (t);
2545   if (TREE_CODE (inner) == ERROR_MARK)
2546     return inner;
2547
2548   if (tree_invariant_p_1 (inner))
2549     return t;
2550
2551   /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
2552      it means that the size or offset of some field of an object depends on
2553      the value within another field.
2554
2555      Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
2556      and some variable since it would then need to be both evaluated once and
2557      evaluated more than once.  Front-ends must assure this case cannot
2558      happen by surrounding any such subexpressions in their own SAVE_EXPR
2559      and forcing evaluation at the proper time.  */
2560   if (contains_placeholder_p (inner))
2561     return t;
2562
2563   t = build1 (SAVE_EXPR, TREE_TYPE (expr), t);
2564   SET_EXPR_LOCATION (t, EXPR_LOCATION (expr));
2565
2566   /* This expression might be placed ahead of a jump to ensure that the
2567      value was computed on both sides of the jump.  So make sure it isn't
2568      eliminated as dead.  */
2569   TREE_SIDE_EFFECTS (t) = 1;
2570   return t;
2571 }
2572
2573 /* Look inside EXPR and into any simple arithmetic operations.  Return
2574    the innermost non-arithmetic node.  */
2575
2576 tree
2577 skip_simple_arithmetic (tree expr)
2578 {
2579   tree inner;
2580
2581   /* We don't care about whether this can be used as an lvalue in this
2582      context.  */
2583   while (TREE_CODE (expr) == NON_LVALUE_EXPR)
2584     expr = TREE_OPERAND (expr, 0);
2585
2586   /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
2587      a constant, it will be more efficient to not make another SAVE_EXPR since
2588      it will allow better simplification and GCSE will be able to merge the
2589      computations if they actually occur.  */
2590   inner = expr;
2591   while (1)
2592     {
2593       if (UNARY_CLASS_P (inner))
2594         inner = TREE_OPERAND (inner, 0);
2595       else if (BINARY_CLASS_P (inner))
2596         {
2597           if (tree_invariant_p (TREE_OPERAND (inner, 1)))
2598             inner = TREE_OPERAND (inner, 0);
2599           else if (tree_invariant_p (TREE_OPERAND (inner, 0)))
2600             inner = TREE_OPERAND (inner, 1);
2601           else
2602             break;
2603         }
2604       else
2605         break;
2606     }
2607
2608   return inner;
2609 }
2610
2611
2612 /* Return which tree structure is used by T.  */
2613
2614 enum tree_node_structure_enum
2615 tree_node_structure (const_tree t)
2616 {
2617   const enum tree_code code = TREE_CODE (t);
2618   return tree_node_structure_for_code (code);
2619 }
2620
2621 /* Set various status flags when building a CALL_EXPR object T.  */
2622
2623 static void
2624 process_call_operands (tree t)
2625 {
2626   bool side_effects = TREE_SIDE_EFFECTS (t);
2627   bool read_only = false;
2628   int i = call_expr_flags (t);
2629
2630   /* Calls have side-effects, except those to const or pure functions.  */
2631   if ((i & ECF_LOOPING_CONST_OR_PURE) || !(i & (ECF_CONST | ECF_PURE)))
2632     side_effects = true;
2633   /* Propagate TREE_READONLY of arguments for const functions.  */
2634   if (i & ECF_CONST)
2635     read_only = true;
2636
2637   if (!side_effects || read_only)
2638     for (i = 1; i < TREE_OPERAND_LENGTH (t); i++)
2639       {
2640         tree op = TREE_OPERAND (t, i);
2641         if (op && TREE_SIDE_EFFECTS (op))
2642           side_effects = true;
2643         if (op && !TREE_READONLY (op) && !CONSTANT_CLASS_P (op))
2644           read_only = false;
2645       }
2646
2647   TREE_SIDE_EFFECTS (t) = side_effects;
2648   TREE_READONLY (t) = read_only;
2649 }
2650 \f
2651 /* Return 1 if EXP contains a PLACEHOLDER_EXPR; i.e., if it represents a size
2652    or offset that depends on a field within a record.  */
2653
2654 bool
2655 contains_placeholder_p (const_tree exp)
2656 {
2657   enum tree_code code;
2658
2659   if (!exp)
2660     return 0;
2661
2662   code = TREE_CODE (exp);
2663   if (code == PLACEHOLDER_EXPR)
2664     return 1;
2665
2666   switch (TREE_CODE_CLASS (code))
2667     {
2668     case tcc_reference:
2669       /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
2670          position computations since they will be converted into a
2671          WITH_RECORD_EXPR involving the reference, which will assume
2672          here will be valid.  */
2673       return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
2674
2675     case tcc_exceptional:
2676       if (code == TREE_LIST)
2677         return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
2678                 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
2679       break;
2680
2681     case tcc_unary:
2682     case tcc_binary:
2683     case tcc_comparison:
2684     case tcc_expression:
2685       switch (code)
2686         {
2687         case COMPOUND_EXPR:
2688           /* Ignoring the first operand isn't quite right, but works best.  */
2689           return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
2690
2691         case COND_EXPR:
2692           return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
2693                   || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
2694                   || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
2695
2696         case SAVE_EXPR:
2697           /* The save_expr function never wraps anything containing
2698              a PLACEHOLDER_EXPR. */
2699           return 0;
2700
2701         default:
2702           break;
2703         }
2704
2705       switch (TREE_CODE_LENGTH (code))
2706         {
2707         case 1:
2708           return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
2709         case 2:
2710           return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
2711                   || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
2712         default:
2713           return 0;
2714         }
2715
2716     case tcc_vl_exp:
2717       switch (code)
2718         {
2719         case CALL_EXPR:
2720           {
2721             const_tree arg;
2722             const_call_expr_arg_iterator iter;
2723             FOR_EACH_CONST_CALL_EXPR_ARG (arg, iter, exp)
2724               if (CONTAINS_PLACEHOLDER_P (arg))
2725                 return 1;
2726             return 0;
2727           }
2728         default:
2729           return 0;
2730         }
2731
2732     default:
2733       return 0;
2734     }
2735   return 0;
2736 }
2737
2738 /* Return true if any part of the computation of TYPE involves a
2739    PLACEHOLDER_EXPR.  This includes size, bounds, qualifiers
2740    (for QUAL_UNION_TYPE) and field positions.  */
2741
2742 static bool
2743 type_contains_placeholder_1 (const_tree type)
2744 {
2745   /* If the size contains a placeholder or the parent type (component type in
2746      the case of arrays) type involves a placeholder, this type does.  */
2747   if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
2748       || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
2749       || (TREE_TYPE (type) != 0
2750           && type_contains_placeholder_p (TREE_TYPE (type))))
2751     return true;
2752
2753   /* Now do type-specific checks.  Note that the last part of the check above
2754      greatly limits what we have to do below.  */
2755   switch (TREE_CODE (type))
2756     {
2757     case VOID_TYPE:
2758     case COMPLEX_TYPE:
2759     case ENUMERAL_TYPE:
2760     case BOOLEAN_TYPE:
2761     case POINTER_TYPE:
2762     case OFFSET_TYPE:
2763     case REFERENCE_TYPE:
2764     case METHOD_TYPE:
2765     case FUNCTION_TYPE:
2766     case VECTOR_TYPE:
2767       return false;
2768
2769     case INTEGER_TYPE:
2770     case REAL_TYPE:
2771     case FIXED_POINT_TYPE:
2772       /* Here we just check the bounds.  */
2773       return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
2774               || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
2775
2776     case ARRAY_TYPE:
2777       /* We're already checked the component type (TREE_TYPE), so just check
2778          the index type.  */
2779       return type_contains_placeholder_p (TYPE_DOMAIN (type));
2780
2781     case RECORD_TYPE:
2782     case UNION_TYPE:
2783     case QUAL_UNION_TYPE:
2784       {
2785         tree field;
2786
2787         for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2788           if (TREE_CODE (field) == FIELD_DECL
2789               && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
2790                   || (TREE_CODE (type) == QUAL_UNION_TYPE
2791                       && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
2792                   || type_contains_placeholder_p (TREE_TYPE (field))))
2793             return true;
2794
2795         return false;
2796       }
2797
2798     default:
2799       gcc_unreachable ();
2800     }
2801 }
2802
2803 bool
2804 type_contains_placeholder_p (tree type)
2805 {
2806   bool result;
2807
2808   /* If the contains_placeholder_bits field has been initialized,
2809      then we know the answer.  */
2810   if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
2811     return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
2812
2813   /* Indicate that we've seen this type node, and the answer is false.
2814      This is what we want to return if we run into recursion via fields.  */
2815   TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
2816
2817   /* Compute the real value.  */
2818   result = type_contains_placeholder_1 (type);
2819
2820   /* Store the real value.  */
2821   TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
2822
2823   return result;
2824 }
2825 \f
2826 /* Push tree EXP onto vector QUEUE if it is not already present.  */
2827
2828 static void
2829 push_without_duplicates (tree exp, VEC (tree, heap) **queue)
2830 {
2831   unsigned int i;
2832   tree iter;
2833
2834   for (i = 0; VEC_iterate (tree, *queue, i, iter); i++)
2835     if (simple_cst_equal (iter, exp) == 1)
2836       break;
2837
2838   if (!iter)
2839     VEC_safe_push (tree, heap, *queue, exp);
2840 }
2841
2842 /* Given a tree EXP, find all occurences of references to fields
2843    in a PLACEHOLDER_EXPR and place them in vector REFS without
2844    duplicates.  Also record VAR_DECLs and CONST_DECLs.  Note that
2845    we assume here that EXP contains only arithmetic expressions
2846    or CALL_EXPRs with PLACEHOLDER_EXPRs occurring only in their
2847    argument list.  */
2848
2849 void
2850 find_placeholder_in_expr (tree exp, VEC (tree, heap) **refs)
2851 {
2852   enum tree_code code = TREE_CODE (exp);
2853   tree inner;
2854   int i;
2855
2856   /* We handle TREE_LIST and COMPONENT_REF separately.  */
2857   if (code == TREE_LIST)
2858     {
2859       FIND_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), refs);
2860       FIND_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), refs);
2861     }
2862   else if (code == COMPONENT_REF)
2863     {
2864       for (inner = TREE_OPERAND (exp, 0);
2865            REFERENCE_CLASS_P (inner);
2866            inner = TREE_OPERAND (inner, 0))
2867         ;
2868
2869       if (TREE_CODE (inner) == PLACEHOLDER_EXPR)
2870         push_without_duplicates (exp, refs);
2871       else
2872         FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), refs);
2873    }
2874   else
2875     switch (TREE_CODE_CLASS (code))
2876       {
2877       case tcc_constant:
2878         break;
2879
2880       case tcc_declaration:
2881         /* Variables allocated to static storage can stay.  */
2882         if (!TREE_STATIC (exp))
2883           push_without_duplicates (exp, refs);
2884         break;
2885
2886       case tcc_expression:
2887         /* This is the pattern built in ada/make_aligning_type.  */
2888         if (code == ADDR_EXPR
2889             && TREE_CODE (TREE_OPERAND (exp, 0)) == PLACEHOLDER_EXPR)
2890           {
2891             push_without_duplicates (exp, refs);
2892             break;
2893           }
2894
2895         /* Fall through...  */
2896
2897       case tcc_exceptional:
2898       case tcc_unary:
2899       case tcc_binary:
2900       case tcc_comparison:
2901       case tcc_reference:
2902         for (i = 0; i < TREE_CODE_LENGTH (code); i++)
2903           FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
2904         break;
2905
2906       case tcc_vl_exp:
2907         for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
2908           FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
2909         break;
2910
2911       default:
2912         gcc_unreachable ();
2913       }
2914 }
2915
2916 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
2917    return a tree with all occurrences of references to F in a
2918    PLACEHOLDER_EXPR replaced by R.  Also handle VAR_DECLs and
2919    CONST_DECLs.  Note that we assume here that EXP contains only
2920    arithmetic expressions or CALL_EXPRs with PLACEHOLDER_EXPRs
2921    occurring only in their argument list.  */
2922
2923 tree
2924 substitute_in_expr (tree exp, tree f, tree r)
2925 {
2926   enum tree_code code = TREE_CODE (exp);
2927   tree op0, op1, op2, op3;
2928   tree new_tree;
2929
2930   /* We handle TREE_LIST and COMPONENT_REF separately.  */
2931   if (code == TREE_LIST)
2932     {
2933       op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
2934       op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
2935       if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
2936         return exp;
2937
2938       return tree_cons (TREE_PURPOSE (exp), op1, op0);
2939     }
2940   else if (code == COMPONENT_REF)
2941     {
2942       tree inner;
2943
2944       /* If this expression is getting a value from a PLACEHOLDER_EXPR
2945          and it is the right field, replace it with R.  */
2946       for (inner = TREE_OPERAND (exp, 0);
2947            REFERENCE_CLASS_P (inner);
2948            inner = TREE_OPERAND (inner, 0))
2949         ;
2950
2951       /* The field.  */
2952       op1 = TREE_OPERAND (exp, 1);
2953
2954       if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f)
2955         return r;
2956
2957       /* If this expression hasn't been completed let, leave it alone.  */
2958       if (TREE_CODE (inner) == PLACEHOLDER_EXPR && !TREE_TYPE (inner))
2959         return exp;
2960
2961       op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2962       if (op0 == TREE_OPERAND (exp, 0))
2963         return exp;
2964
2965       new_tree
2966         = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE);
2967    }
2968   else
2969     switch (TREE_CODE_CLASS (code))
2970       {
2971       case tcc_constant:
2972         return exp;
2973
2974       case tcc_declaration:
2975         if (exp == f)
2976           return r;
2977         else
2978           return exp;
2979
2980       case tcc_expression:
2981         if (exp == f)
2982           return r;
2983
2984         /* Fall through...  */
2985
2986       case tcc_exceptional:
2987       case tcc_unary:
2988       case tcc_binary:
2989       case tcc_comparison:
2990       case tcc_reference:
2991         switch (TREE_CODE_LENGTH (code))
2992           {
2993           case 0:
2994             return exp;
2995
2996           case 1:
2997             op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2998             if (op0 == TREE_OPERAND (exp, 0))
2999               return exp;
3000
3001             new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3002             break;
3003
3004           case 2:
3005             op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3006             op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3007
3008             if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3009               return exp;
3010
3011             new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3012             break;
3013
3014           case 3:
3015             op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3016             op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3017             op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3018
3019             if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3020                 && op2 == TREE_OPERAND (exp, 2))
3021               return exp;
3022
3023             new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3024             break;
3025
3026           case 4:
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             op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
3031
3032             if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3033                 && op2 == TREE_OPERAND (exp, 2)
3034                 && op3 == TREE_OPERAND (exp, 3))
3035               return exp;
3036
3037             new_tree
3038               = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3039             break;
3040
3041           default:
3042             gcc_unreachable ();
3043           }
3044         break;
3045
3046       case tcc_vl_exp:
3047         {
3048           int i;
3049
3050           new_tree = NULL_TREE;
3051
3052           /* If we are trying to replace F with a constant, inline back
3053              functions which do nothing else than computing a value from
3054              the arguments they are passed.  This makes it possible to
3055              fold partially or entirely the replacement expression.  */
3056           if (CONSTANT_CLASS_P (r) && code == CALL_EXPR)
3057             {
3058               tree t = maybe_inline_call_in_expr (exp);
3059               if (t)
3060                 return SUBSTITUTE_IN_EXPR (t, f, r);
3061             }
3062
3063           for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3064             {
3065               tree op = TREE_OPERAND (exp, i);
3066               tree new_op = SUBSTITUTE_IN_EXPR (op, f, r);
3067               if (new_op != op)
3068                 {
3069                   if (!new_tree)
3070                     new_tree = copy_node (exp);
3071                   TREE_OPERAND (new_tree, i) = new_op;
3072                 }
3073             }
3074
3075           if (new_tree)
3076             {
3077               new_tree = fold (new_tree);
3078               if (TREE_CODE (new_tree) == CALL_EXPR)
3079                 process_call_operands (new_tree);
3080             }
3081           else
3082             return exp;
3083         }
3084         break;
3085
3086       default:
3087         gcc_unreachable ();
3088       }
3089
3090   TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3091   return new_tree;
3092 }
3093
3094 /* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
3095    for it within OBJ, a tree that is an object or a chain of references.  */
3096
3097 tree
3098 substitute_placeholder_in_expr (tree exp, tree obj)
3099 {
3100   enum tree_code code = TREE_CODE (exp);
3101   tree op0, op1, op2, op3;
3102   tree new_tree;
3103
3104   /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
3105      in the chain of OBJ.  */
3106   if (code == PLACEHOLDER_EXPR)
3107     {
3108       tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
3109       tree elt;
3110
3111       for (elt = obj; elt != 0;
3112            elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3113                    || TREE_CODE (elt) == COND_EXPR)
3114                   ? TREE_OPERAND (elt, 1)
3115                   : (REFERENCE_CLASS_P (elt)
3116                      || UNARY_CLASS_P (elt)
3117                      || BINARY_CLASS_P (elt)
3118                      || VL_EXP_CLASS_P (elt)
3119                      || EXPRESSION_CLASS_P (elt))
3120                   ? TREE_OPERAND (elt, 0) : 0))
3121         if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
3122           return elt;
3123
3124       for (elt = obj; elt != 0;
3125            elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3126                    || TREE_CODE (elt) == COND_EXPR)
3127                   ? TREE_OPERAND (elt, 1)
3128                   : (REFERENCE_CLASS_P (elt)
3129                      || UNARY_CLASS_P (elt)
3130                      || BINARY_CLASS_P (elt)
3131                      || VL_EXP_CLASS_P (elt)
3132                      || EXPRESSION_CLASS_P (elt))
3133                   ? TREE_OPERAND (elt, 0) : 0))
3134         if (POINTER_TYPE_P (TREE_TYPE (elt))
3135             && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
3136                 == need_type))
3137           return fold_build1 (INDIRECT_REF, need_type, elt);
3138
3139       /* If we didn't find it, return the original PLACEHOLDER_EXPR.  If it
3140          survives until RTL generation, there will be an error.  */
3141       return exp;
3142     }
3143
3144   /* TREE_LIST is special because we need to look at TREE_VALUE
3145      and TREE_CHAIN, not TREE_OPERANDS.  */
3146   else if (code == TREE_LIST)
3147     {
3148       op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
3149       op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
3150       if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3151         return exp;
3152
3153       return tree_cons (TREE_PURPOSE (exp), op1, op0);
3154     }
3155   else
3156     switch (TREE_CODE_CLASS (code))
3157       {
3158       case tcc_constant:
3159       case tcc_declaration:
3160         return exp;
3161
3162       case tcc_exceptional:
3163       case tcc_unary:
3164       case tcc_binary:
3165       case tcc_comparison:
3166       case tcc_expression:
3167       case tcc_reference:
3168       case tcc_statement:
3169         switch (TREE_CODE_LENGTH (code))
3170           {
3171           case 0:
3172             return exp;
3173
3174           case 1:
3175             op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3176             if (op0 == TREE_OPERAND (exp, 0))
3177               return exp;
3178
3179             new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3180             break;
3181
3182           case 2:
3183             op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3184             op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3185
3186             if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3187               return exp;
3188
3189             new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3190             break;
3191
3192           case 3:
3193             op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3194             op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
3195             op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
3196
3197             if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3198                 && op2 == TREE_OPERAND (exp, 2))
3199               return exp;
3200
3201             new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3202             break;
3203
3204           case 4:
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             op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
3209
3210             if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3211                 && op2 == TREE_OPERAND (exp, 2)
3212                 && op3 == TREE_OPERAND (exp, 3))
3213               return exp;
3214
3215             new_tree
3216               = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3217             break;
3218
3219           default:
3220             gcc_unreachable ();
3221           }
3222         break;
3223
3224       case tcc_vl_exp:
3225         {
3226           int i;
3227
3228           new_tree = NULL_TREE;
3229
3230           for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3231             {
3232               tree op = TREE_OPERAND (exp, i);
3233               tree new_op = SUBSTITUTE_PLACEHOLDER_IN_EXPR (op, obj);
3234               if (new_op != op)
3235                 {
3236                   if (!new_tree)
3237                     new_tree = copy_node (exp);
3238                   TREE_OPERAND (new_tree, i) = new_op;
3239                 }
3240             }
3241
3242           if (new_tree)
3243             {
3244               new_tree = fold (new_tree);
3245               if (TREE_CODE (new_tree) == CALL_EXPR)
3246                 process_call_operands (new_tree);
3247             }
3248           else
3249             return exp;
3250         }
3251         break;
3252
3253       default:
3254         gcc_unreachable ();
3255       }
3256
3257   TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3258   return new_tree;
3259 }
3260 \f
3261 /* Stabilize a reference so that we can use it any number of times
3262    without causing its operands to be evaluated more than once.
3263    Returns the stabilized reference.  This works by means of save_expr,
3264    so see the caveats in the comments about save_expr.
3265
3266    Also allows conversion expressions whose operands are references.
3267    Any other kind of expression is returned unchanged.  */
3268
3269 tree
3270 stabilize_reference (tree ref)
3271 {
3272   tree result;
3273   enum tree_code code = TREE_CODE (ref);
3274
3275   switch (code)
3276     {
3277     case VAR_DECL:
3278     case PARM_DECL:
3279     case RESULT_DECL:
3280       /* No action is needed in this case.  */
3281       return ref;
3282
3283     CASE_CONVERT:
3284     case FLOAT_EXPR:
3285     case FIX_TRUNC_EXPR:
3286       result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
3287       break;
3288
3289     case INDIRECT_REF:
3290       result = build_nt (INDIRECT_REF,
3291                          stabilize_reference_1 (TREE_OPERAND (ref, 0)));
3292       break;
3293
3294     case COMPONENT_REF:
3295       result = build_nt (COMPONENT_REF,
3296                          stabilize_reference (TREE_OPERAND (ref, 0)),
3297                          TREE_OPERAND (ref, 1), NULL_TREE);
3298       break;
3299
3300     case BIT_FIELD_REF:
3301       result = build_nt (BIT_FIELD_REF,
3302                          stabilize_reference (TREE_OPERAND (ref, 0)),
3303                          stabilize_reference_1 (TREE_OPERAND (ref, 1)),
3304                          stabilize_reference_1 (TREE_OPERAND (ref, 2)));
3305       break;
3306
3307     case ARRAY_REF:
3308       result = build_nt (ARRAY_REF,
3309                          stabilize_reference (TREE_OPERAND (ref, 0)),
3310                          stabilize_reference_1 (TREE_OPERAND (ref, 1)),
3311                          TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
3312       break;
3313
3314     case ARRAY_RANGE_REF:
3315       result = build_nt (ARRAY_RANGE_REF,
3316                          stabilize_reference (TREE_OPERAND (ref, 0)),
3317                          stabilize_reference_1 (TREE_OPERAND (ref, 1)),
3318                          TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
3319       break;
3320
3321     case COMPOUND_EXPR:
3322       /* We cannot wrap the first expression in a SAVE_EXPR, as then
3323          it wouldn't be ignored.  This matters when dealing with
3324          volatiles.  */
3325       return stabilize_reference_1 (ref);
3326
3327       /* If arg isn't a kind of lvalue we recognize, make no change.
3328          Caller should recognize the error for an invalid lvalue.  */
3329     default:
3330       return ref;
3331
3332     case ERROR_MARK:
3333       return error_mark_node;
3334     }
3335
3336   TREE_TYPE (result) = TREE_TYPE (ref);
3337   TREE_READONLY (result) = TREE_READONLY (ref);
3338   TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
3339   TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
3340
3341   return result;
3342 }
3343
3344 /* Subroutine of stabilize_reference; this is called for subtrees of
3345    references.  Any expression with side-effects must be put in a SAVE_EXPR
3346    to ensure that it is only evaluated once.
3347
3348    We don't put SAVE_EXPR nodes around everything, because assigning very
3349    simple expressions to temporaries causes us to miss good opportunities
3350    for optimizations.  Among other things, the opportunity to fold in the
3351    addition of a constant into an addressing mode often gets lost, e.g.
3352    "y[i+1] += x;".  In general, we take the approach that we should not make
3353    an assignment unless we are forced into it - i.e., that any non-side effect
3354    operator should be allowed, and that cse should take care of coalescing
3355    multiple utterances of the same expression should that prove fruitful.  */
3356
3357 tree
3358 stabilize_reference_1 (tree e)
3359 {
3360   tree result;
3361   enum tree_code code = TREE_CODE (e);
3362
3363   /* We cannot ignore const expressions because it might be a reference
3364      to a const array but whose index contains side-effects.  But we can
3365      ignore things that are actual constant or that already have been
3366      handled by this function.  */
3367
3368   if (tree_invariant_p (e))
3369     return e;
3370
3371   switch (TREE_CODE_CLASS (code))
3372     {
3373     case tcc_exceptional:
3374     case tcc_type:
3375     case tcc_declaration:
3376     case tcc_comparison:
3377     case tcc_statement:
3378     case tcc_expression:
3379     case tcc_reference:
3380     case tcc_vl_exp:
3381       /* If the expression has side-effects, then encase it in a SAVE_EXPR
3382          so that it will only be evaluated once.  */
3383       /* The reference (r) and comparison (<) classes could be handled as
3384          below, but it is generally faster to only evaluate them once.  */
3385       if (TREE_SIDE_EFFECTS (e))
3386         return save_expr (e);
3387       return e;
3388
3389     case tcc_constant:
3390       /* Constants need no processing.  In fact, we should never reach
3391          here.  */
3392       return e;
3393
3394     case tcc_binary:
3395       /* Division is slow and tends to be compiled with jumps,
3396          especially the division by powers of 2 that is often
3397          found inside of an array reference.  So do it just once.  */
3398       if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
3399           || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
3400           || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
3401           || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
3402         return save_expr (e);
3403       /* Recursively stabilize each operand.  */
3404       result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
3405                          stabilize_reference_1 (TREE_OPERAND (e, 1)));
3406       break;
3407
3408     case tcc_unary:
3409       /* Recursively stabilize each operand.  */
3410       result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
3411       break;
3412
3413     default:
3414       gcc_unreachable ();
3415     }
3416
3417   TREE_TYPE (result) = TREE_TYPE (e);
3418   TREE_READONLY (result) = TREE_READONLY (e);
3419   TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
3420   TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
3421
3422   return result;
3423 }
3424 \f
3425 /* Low-level constructors for expressions.  */
3426
3427 /* A helper function for build1 and constant folders.  Set TREE_CONSTANT,
3428    and TREE_SIDE_EFFECTS for an ADDR_EXPR.  */
3429
3430 void
3431 recompute_tree_invariant_for_addr_expr (tree t)
3432 {
3433   tree node;
3434   bool tc = true, se = false;
3435
3436   /* We started out assuming this address is both invariant and constant, but
3437      does not have side effects.  Now go down any handled components and see if
3438      any of them involve offsets that are either non-constant or non-invariant.
3439      Also check for side-effects.
3440
3441      ??? Note that this code makes no attempt to deal with the case where
3442      taking the address of something causes a copy due to misalignment.  */
3443
3444 #define UPDATE_FLAGS(NODE)  \
3445 do { tree _node = (NODE); \
3446      if (_node && !TREE_CONSTANT (_node)) tc = false; \
3447      if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
3448
3449   for (node = TREE_OPERAND (t, 0); handled_component_p (node);
3450        node = TREE_OPERAND (node, 0))
3451     {
3452       /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
3453          array reference (probably made temporarily by the G++ front end),
3454          so ignore all the operands.  */
3455       if ((TREE_CODE (node) == ARRAY_REF
3456            || TREE_CODE (node) == ARRAY_RANGE_REF)
3457           && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
3458         {
3459           UPDATE_FLAGS (TREE_OPERAND (node, 1));
3460           if (TREE_OPERAND (node, 2))
3461             UPDATE_FLAGS (TREE_OPERAND (node, 2));
3462           if (TREE_OPERAND (node, 3))
3463             UPDATE_FLAGS (TREE_OPERAND (node, 3));
3464         }
3465       /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
3466          FIELD_DECL, apparently.  The G++ front end can put something else
3467          there, at least temporarily.  */
3468       else if (TREE_CODE (node) == COMPONENT_REF
3469                && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
3470         {
3471           if (TREE_OPERAND (node, 2))
3472             UPDATE_FLAGS (TREE_OPERAND (node, 2));
3473         }
3474       else if (TREE_CODE (node) == BIT_FIELD_REF)
3475         UPDATE_FLAGS (TREE_OPERAND (node, 2));
3476     }
3477
3478   node = lang_hooks.expr_to_decl (node, &tc, &se);
3479
3480   /* Now see what's inside.  If it's an INDIRECT_REF, copy our properties from
3481      the address, since &(*a)->b is a form of addition.  If it's a constant, the
3482      address is constant too.  If it's a decl, its address is constant if the
3483      decl is static.  Everything else is not constant and, furthermore,
3484      taking the address of a volatile variable is not volatile.  */
3485   if (TREE_CODE (node) == INDIRECT_REF)
3486     UPDATE_FLAGS (TREE_OPERAND (node, 0));
3487   else if (CONSTANT_CLASS_P (node))
3488     ;
3489   else if (DECL_P (node))
3490     tc &= (staticp (node) != NULL_TREE);
3491   else
3492     {
3493       tc = false;
3494       se |= TREE_SIDE_EFFECTS (node);
3495     }
3496
3497
3498   TREE_CONSTANT (t) = tc;
3499   TREE_SIDE_EFFECTS (t) = se;
3500 #undef UPDATE_FLAGS
3501 }
3502
3503 /* Build an expression of code CODE, data type TYPE, and operands as
3504    specified.  Expressions and reference nodes can be created this way.
3505    Constants, decls, types and misc nodes cannot be.
3506
3507    We define 5 non-variadic functions, from 0 to 4 arguments.  This is
3508    enough for all extant tree codes.  */
3509
3510 tree
3511 build0_stat (enum tree_code code, tree tt MEM_STAT_DECL)
3512 {
3513   tree t;
3514
3515   gcc_assert (TREE_CODE_LENGTH (code) == 0);
3516
3517   t = make_node_stat (code PASS_MEM_STAT);
3518   TREE_TYPE (t) = tt;
3519
3520   return t;
3521 }
3522
3523 tree
3524 build1_stat (enum tree_code code, tree type, tree node MEM_STAT_DECL)
3525 {
3526   int length = sizeof (struct tree_exp);
3527 #ifdef GATHER_STATISTICS
3528   tree_node_kind kind;
3529 #endif
3530   tree t;
3531
3532 #ifdef GATHER_STATISTICS
3533   switch (TREE_CODE_CLASS (code))
3534     {
3535     case tcc_statement:  /* an expression with side effects */
3536       kind = s_kind;
3537       break;
3538     case tcc_reference:  /* a reference */
3539       kind = r_kind;
3540       break;
3541     default:
3542       kind = e_kind;
3543       break;
3544     }
3545
3546   tree_node_counts[(int) kind]++;
3547   tree_node_sizes[(int) kind] += length;
3548 #endif
3549
3550   gcc_assert (TREE_CODE_LENGTH (code) == 1);
3551
3552   t = (tree) ggc_alloc_zone_pass_stat (length, &tree_zone);
3553
3554   memset (t, 0, sizeof (struct tree_common));
3555
3556   TREE_SET_CODE (t, code);
3557
3558   TREE_TYPE (t) = type;
3559   SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
3560   TREE_OPERAND (t, 0) = node;
3561   TREE_BLOCK (t) = NULL_TREE;
3562   if (node && !TYPE_P (node))
3563     {
3564       TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
3565       TREE_READONLY (t) = TREE_READONLY (node);
3566     }
3567
3568   if (TREE_CODE_CLASS (code) == tcc_statement)
3569     TREE_SIDE_EFFECTS (t) = 1;
3570   else switch (code)
3571     {
3572     case VA_ARG_EXPR:
3573       /* All of these have side-effects, no matter what their
3574          operands are.  */
3575       TREE_SIDE_EFFECTS (t) = 1;
3576       TREE_READONLY (t) = 0;
3577       break;
3578
3579     case MISALIGNED_INDIRECT_REF:
3580     case ALIGN_INDIRECT_REF:
3581     case INDIRECT_REF:
3582       /* Whether a dereference is readonly has nothing to do with whether
3583          its operand is readonly.  */
3584       TREE_READONLY (t) = 0;
3585       break;
3586
3587     case ADDR_EXPR:
3588       if (node)
3589         recompute_tree_invariant_for_addr_expr (t);
3590       break;
3591
3592     default:
3593       if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
3594           && node && !TYPE_P (node)
3595           && TREE_CONSTANT (node))
3596         TREE_CONSTANT (t) = 1;
3597       if (TREE_CODE_CLASS (code) == tcc_reference
3598           && node && TREE_THIS_VOLATILE (node))
3599         TREE_THIS_VOLATILE (t) = 1;
3600       break;
3601     }
3602
3603   return t;
3604 }
3605
3606 #define PROCESS_ARG(N)                          \
3607   do {                                          \
3608     TREE_OPERAND (t, N) = arg##N;               \
3609     if (arg##N &&!TYPE_P (arg##N))              \
3610       {                                         \
3611         if (TREE_SIDE_EFFECTS (arg##N))         \
3612           side_effects = 1;                     \
3613         if (!TREE_READONLY (arg##N)             \
3614             && !CONSTANT_CLASS_P (arg##N))      \
3615           read_only = 0;                        \
3616         if (!TREE_CONSTANT (arg##N))            \
3617           constant = 0;                         \
3618       }                                         \
3619   } while (0)
3620
3621 tree
3622 build2_stat (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
3623 {
3624   bool constant, read_only, side_effects;
3625   tree t;
3626
3627   gcc_assert (TREE_CODE_LENGTH (code) == 2);
3628
3629   if ((code == MINUS_EXPR || code == PLUS_EXPR || code == MULT_EXPR)
3630       && arg0 && arg1 && tt && POINTER_TYPE_P (tt)
3631       /* When sizetype precision doesn't match that of pointers
3632          we need to be able to build explicit extensions or truncations
3633          of the offset argument.  */
3634       && TYPE_PRECISION (sizetype) == TYPE_PRECISION (tt))
3635     gcc_assert (TREE_CODE (arg0) == INTEGER_CST
3636                 && TREE_CODE (arg1) == INTEGER_CST);
3637
3638   if (code == POINTER_PLUS_EXPR && arg0 && arg1 && tt)
3639     gcc_assert (POINTER_TYPE_P (tt) && POINTER_TYPE_P (TREE_TYPE (arg0))
3640                 && INTEGRAL_TYPE_P (TREE_TYPE (arg1))
3641                 && useless_type_conversion_p (sizetype, TREE_TYPE (arg1)));
3642
3643   t = make_node_stat (code PASS_MEM_STAT);
3644   TREE_TYPE (t) = tt;
3645
3646   /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
3647      result based on those same flags for the arguments.  But if the
3648      arguments aren't really even `tree' expressions, we shouldn't be trying
3649      to do this.  */
3650
3651   /* Expressions without side effects may be constant if their
3652      arguments are as well.  */
3653   constant = (TREE_CODE_CLASS (code) == tcc_comparison
3654               || TREE_CODE_CLASS (code) == tcc_binary);
3655   read_only = 1;
3656   side_effects = TREE_SIDE_EFFECTS (t);
3657
3658   PROCESS_ARG(0);
3659   PROCESS_ARG(1);
3660
3661   TREE_READONLY (t) = read_only;
3662   TREE_CONSTANT (t) = constant;
3663   TREE_SIDE_EFFECTS (t) = side_effects;
3664   TREE_THIS_VOLATILE (t)
3665     = (TREE_CODE_CLASS (code) == tcc_reference
3666        && arg0 && TREE_THIS_VOLATILE (arg0));
3667
3668   return t;
3669 }
3670
3671
3672 tree
3673 build3_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3674              tree arg2 MEM_STAT_DECL)
3675 {
3676   bool constant, read_only, side_effects;
3677   tree t;
3678
3679   gcc_assert (TREE_CODE_LENGTH (code) == 3);
3680   gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3681
3682   t = make_node_stat (code PASS_MEM_STAT);
3683   TREE_TYPE (t) = tt;
3684
3685   read_only = 1;
3686
3687   /* As a special exception, if COND_EXPR has NULL branches, we
3688      assume that it is a gimple statement and always consider
3689      it to have side effects.  */
3690   if (code == COND_EXPR
3691       && tt == void_type_node
3692       && arg1 == NULL_TREE
3693       && arg2 == NULL_TREE)
3694     side_effects = true;
3695   else
3696     side_effects = TREE_SIDE_EFFECTS (t);
3697
3698   PROCESS_ARG(0);
3699   PROCESS_ARG(1);
3700   PROCESS_ARG(2);
3701
3702   if (code == COND_EXPR)
3703     TREE_READONLY (t) = read_only;
3704
3705   TREE_SIDE_EFFECTS (t) = side_effects;
3706   TREE_THIS_VOLATILE (t)
3707     = (TREE_CODE_CLASS (code) == tcc_reference
3708        && arg0 && TREE_THIS_VOLATILE (arg0));
3709
3710   return t;
3711 }
3712
3713 tree
3714 build4_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3715              tree arg2, tree arg3 MEM_STAT_DECL)
3716 {
3717   bool constant, read_only, side_effects;
3718   tree t;
3719
3720   gcc_assert (TREE_CODE_LENGTH (code) == 4);
3721
3722   t = make_node_stat (code PASS_MEM_STAT);
3723   TREE_TYPE (t) = tt;
3724
3725   side_effects = TREE_SIDE_EFFECTS (t);
3726
3727   PROCESS_ARG(0);
3728   PROCESS_ARG(1);
3729   PROCESS_ARG(2);
3730   PROCESS_ARG(3);
3731
3732   TREE_SIDE_EFFECTS (t) = side_effects;
3733   TREE_THIS_VOLATILE (t)
3734     = (TREE_CODE_CLASS (code) == tcc_reference
3735        && arg0 && TREE_THIS_VOLATILE (arg0));
3736
3737   return t;
3738 }
3739
3740 tree
3741 build5_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3742              tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
3743 {
3744   bool constant, read_only, side_effects;
3745   tree t;
3746
3747   gcc_assert (TREE_CODE_LENGTH (code) == 5);
3748
3749   t = make_node_stat (code PASS_MEM_STAT);
3750   TREE_TYPE (t) = tt;
3751
3752   side_effects = TREE_SIDE_EFFECTS (t);
3753
3754   PROCESS_ARG(0);
3755   PROCESS_ARG(1);
3756   PROCESS_ARG(2);
3757   PROCESS_ARG(3);
3758   PROCESS_ARG(4);
3759
3760   TREE_SIDE_EFFECTS (t) = side_effects;
3761   TREE_THIS_VOLATILE (t)
3762     = (TREE_CODE_CLASS (code) == tcc_reference
3763        && arg0 && TREE_THIS_VOLATILE (arg0));
3764
3765   return t;
3766 }
3767
3768 tree
3769 build6_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
3770              tree arg2, tree arg3, tree arg4, tree arg5 MEM_STAT_DECL)
3771 {
3772   bool constant, read_only, side_effects;
3773   tree t;
3774
3775   gcc_assert (code == TARGET_MEM_REF);
3776
3777   t = make_node_stat (code PASS_MEM_STAT);
3778   TREE_TYPE (t) = tt;
3779
3780   side_effects = TREE_SIDE_EFFECTS (t);
3781
3782   PROCESS_ARG(0);
3783   PROCESS_ARG(1);
3784   PROCESS_ARG(2);
3785   PROCESS_ARG(3);
3786   PROCESS_ARG(4);
3787   PROCESS_ARG(5);
3788
3789   TREE_SIDE_EFFECTS (t) = side_effects;
3790   TREE_THIS_VOLATILE (t) = 0;
3791
3792   return t;
3793 }
3794
3795 /* Similar except don't specify the TREE_TYPE
3796    and leave the TREE_SIDE_EFFECTS as 0.
3797    It is permissible for arguments to be null,
3798    or even garbage if their values do not matter.  */
3799
3800 tree
3801 build_nt (enum tree_code code, ...)
3802 {
3803   tree t;
3804   int length;
3805   int i;
3806   va_list p;
3807
3808   gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3809
3810   va_start (p, code);
3811
3812   t = make_node (code);
3813   length = TREE_CODE_LENGTH (code);
3814
3815   for (i = 0; i < length; i++)
3816     TREE_OPERAND (t, i) = va_arg (p, tree);
3817
3818   va_end (p);
3819   return t;
3820 }
3821
3822 /* Similar to build_nt, but for creating a CALL_EXPR object with
3823    ARGLIST passed as a list.  */
3824
3825 tree
3826 build_nt_call_list (tree fn, tree arglist)
3827 {
3828   tree t;
3829   int i;
3830
3831   t = build_vl_exp (CALL_EXPR, list_length (arglist) + 3);
3832   CALL_EXPR_FN (t) = fn;
3833   CALL_EXPR_STATIC_CHAIN (t) = NULL_TREE;
3834   for (i = 0; arglist; arglist = TREE_CHAIN (arglist), i++)
3835     CALL_EXPR_ARG (t, i) = TREE_VALUE (arglist);
3836   return t;
3837 }
3838
3839 /* Similar to build_nt, but for creating a CALL_EXPR object with a
3840    tree VEC.  */
3841
3842 tree
3843 build_nt_call_vec (tree fn, VEC(tree,gc) *args)
3844 {
3845   tree ret, t;
3846   unsigned int ix;
3847
3848   ret = build_vl_exp (CALL_EXPR, VEC_length (tree, args) + 3);
3849   CALL_EXPR_FN (ret) = fn;
3850   CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
3851   for (ix = 0; VEC_iterate (tree, args, ix, t); ++ix)
3852     CALL_EXPR_ARG (ret, ix) = t;
3853   return ret;
3854 }
3855 \f
3856 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
3857    We do NOT enter this node in any sort of symbol table.
3858
3859    LOC is the location of the decl.
3860
3861    layout_decl is used to set up the decl's storage layout.
3862    Other slots are initialized to 0 or null pointers.  */
3863
3864 tree
3865 build_decl_stat (location_t loc, enum tree_code code, tree name,
3866                  tree type MEM_STAT_DECL)
3867 {
3868   tree t;
3869
3870   t = make_node_stat (code PASS_MEM_STAT);
3871   DECL_SOURCE_LOCATION (t) = loc;
3872
3873 /*  if (type == error_mark_node)
3874     type = integer_type_node; */
3875 /* That is not done, deliberately, so that having error_mark_node
3876    as the type can suppress useless errors in the use of this variable.  */
3877
3878   DECL_NAME (t) = name;
3879   TREE_TYPE (t) = type;
3880
3881   if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
3882     layout_decl (t, 0);
3883
3884   return t;
3885 }
3886
3887 /* Builds and returns function declaration with NAME and TYPE.  */
3888
3889 tree
3890 build_fn_decl (const char *name, tree type)
3891 {
3892   tree id = get_identifier (name);
3893   tree decl = build_decl (input_location, FUNCTION_DECL, id, type);
3894
3895   DECL_EXTERNAL (decl) = 1;
3896   TREE_PUBLIC (decl) = 1;
3897   DECL_ARTIFICIAL (decl) = 1;
3898   TREE_NOTHROW (decl) = 1;
3899
3900   return decl;
3901 }
3902
3903 \f
3904 /* BLOCK nodes are used to represent the structure of binding contours
3905    and declarations, once those contours have been exited and their contents
3906    compiled.  This information is used for outputting debugging info.  */
3907
3908 tree
3909 build_block (tree vars, tree subblocks, tree supercontext, tree chain)
3910 {
3911   tree block = make_node (BLOCK);
3912
3913   BLOCK_VARS (block) = vars;
3914   BLOCK_SUBBLOCKS (block) = subblocks;
3915   BLOCK_SUPERCONTEXT (block) = supercontext;
3916   BLOCK_CHAIN (block) = chain;
3917   return block;
3918 }
3919
3920 expanded_location
3921 expand_location (source_location loc)
3922 {
3923   expanded_location xloc;
3924   if (loc == 0)
3925     {
3926       xloc.file = NULL;
3927       xloc.line = 0;
3928       xloc.column = 0;
3929       xloc.sysp = 0;
3930     }
3931   else
3932     {
3933       const struct line_map *map = linemap_lookup (line_table, loc);
3934       xloc.file = map->to_file;
3935       xloc.line = SOURCE_LINE (map, loc);
3936       xloc.column = SOURCE_COLUMN (map, loc);
3937       xloc.sysp = map->sysp != 0;
3938     };
3939   return xloc;
3940 }
3941
3942 \f
3943 /* Like SET_EXPR_LOCATION, but make sure the tree can have a location.
3944
3945    LOC is the location to use in tree T.  */
3946
3947 void
3948 protected_set_expr_location (tree t, location_t loc)
3949 {
3950   if (t && CAN_HAVE_LOCATION_P (t))
3951     SET_EXPR_LOCATION (t, loc);
3952 }
3953 \f
3954 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
3955    is ATTRIBUTE.  */
3956
3957 tree
3958 build_decl_attribute_variant (tree ddecl, tree attribute)
3959 {
3960   DECL_ATTRIBUTES (ddecl) = attribute;
3961   return ddecl;
3962 }
3963
3964 /* Borrowed from hashtab.c iterative_hash implementation.  */
3965 #define mix(a,b,c) \
3966 { \
3967   a -= b; a -= c; a ^= (c>>13); \
3968   b -= c; b -= a; b ^= (a<< 8); \
3969   c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \
3970   a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \
3971   b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \
3972   c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \
3973   a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \
3974   b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \
3975   c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \
3976 }
3977
3978
3979 /* Produce good hash value combining VAL and VAL2.  */
3980 hashval_t
3981 iterative_hash_hashval_t (hashval_t val, hashval_t val2)
3982 {
3983   /* the golden ratio; an arbitrary value.  */
3984   hashval_t a = 0x9e3779b9;
3985
3986   mix (a, val, val2);
3987   return val2;
3988 }
3989
3990 /* Produce good hash value combining VAL and VAL2.  */
3991 static inline hashval_t
3992 iterative_hash_host_wide_int (HOST_WIDE_INT val, hashval_t val2)
3993 {
3994   if (sizeof (HOST_WIDE_INT) == sizeof (hashval_t))
3995     return iterative_hash_hashval_t (val, val2);
3996   else
3997     {
3998       hashval_t a = (hashval_t) val;
3999       /* Avoid warnings about shifting of more than the width of the type on
4000          hosts that won't execute this path.  */
4001       int zero = 0;
4002       hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 8 + zero));
4003       mix (a, b, val2);
4004       if (sizeof (HOST_WIDE_INT) > 2 * sizeof (hashval_t))
4005         {
4006           hashval_t a = (hashval_t) (val >> (sizeof (hashval_t) * 16 + zero));
4007           hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 24 + zero));
4008           mix (a, b, val2);
4009         }
4010       return val2;
4011     }
4012 }
4013
4014 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4015    is ATTRIBUTE and its qualifiers are QUALS.
4016
4017    Record such modified types already made so we don't make duplicates.  */
4018
4019 static tree
4020 build_type_attribute_qual_variant (tree ttype, tree attribute, int quals)
4021 {
4022   if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
4023     {
4024       hashval_t hashcode = 0;
4025       tree ntype;
4026       enum tree_code code = TREE_CODE (ttype);
4027
4028       /* Building a distinct copy of a tagged type is inappropriate; it
4029          causes breakage in code that expects there to be a one-to-one
4030          relationship between a struct and its fields.
4031          build_duplicate_type is another solution (as used in
4032          handle_transparent_union_attribute), but that doesn't play well
4033          with the stronger C++ type identity model.  */
4034       if (TREE_CODE (ttype) == RECORD_TYPE
4035           || TREE_CODE (ttype) == UNION_TYPE
4036           || TREE_CODE (ttype) == QUAL_UNION_TYPE
4037           || TREE_CODE (ttype) == ENUMERAL_TYPE)
4038         {
4039           warning (OPT_Wattributes,
4040                    "ignoring attributes applied to %qT after definition",
4041                    TYPE_MAIN_VARIANT (ttype));
4042           return build_qualified_type (ttype, quals);
4043         }
4044
4045       ttype = build_qualified_type (ttype, TYPE_UNQUALIFIED);
4046       ntype = build_distinct_type_copy (ttype);
4047
4048       TYPE_ATTRIBUTES (ntype) = attribute;
4049
4050       hashcode = iterative_hash_object (code, hashcode);
4051       if (TREE_TYPE (ntype))
4052         hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (ntype)),
4053                                           hashcode);
4054       hashcode = attribute_hash_list (attribute, hashcode);
4055
4056       switch (TREE_CODE (ntype))
4057         {
4058         case FUNCTION_TYPE:
4059           hashcode = type_hash_list (TYPE_ARG_TYPES (ntype), hashcode);
4060           break;
4061         case ARRAY_TYPE:
4062           if (TYPE_DOMAIN (ntype))
4063             hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (ntype)),
4064                                               hashcode);
4065           break;
4066         case INTEGER_TYPE:
4067           hashcode = iterative_hash_object
4068             (TREE_INT_CST_LOW (TYPE_MAX_VALUE (ntype)), hashcode);
4069           hashcode = iterative_hash_object
4070             (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (ntype)), hashcode);
4071           break;
4072         case REAL_TYPE:
4073         case FIXED_POINT_TYPE:
4074           {
4075             unsigned int precision = TYPE_PRECISION (ntype);
4076             hashcode = iterative_hash_object (precision, hashcode);
4077           }
4078           break;
4079         default:
4080           break;
4081         }
4082
4083       ntype = type_hash_canon (hashcode, ntype);
4084
4085       /* If the target-dependent attributes make NTYPE different from
4086          its canonical type, we will need to use structural equality
4087          checks for this type. */
4088       if (TYPE_STRUCTURAL_EQUALITY_P (ttype)
4089           || !targetm.comp_type_attributes (ntype, ttype))
4090         SET_TYPE_STRUCTURAL_EQUALITY (ntype);
4091       else if (TYPE_CANONICAL (ntype) == ntype)
4092         TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype);
4093
4094       ttype = build_qualified_type (ntype, quals);
4095     }
4096   else if (TYPE_QUALS (ttype) != quals)
4097     ttype = build_qualified_type (ttype, quals);
4098
4099   return ttype;
4100 }
4101
4102
4103 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4104    is ATTRIBUTE.
4105
4106    Record such modified types already made so we don't make duplicates.  */
4107
4108 tree
4109 build_type_attribute_variant (tree ttype, tree attribute)
4110 {
4111   return build_type_attribute_qual_variant (ttype, attribute,
4112                                             TYPE_QUALS (ttype));
4113 }
4114
4115 /* Return nonzero if IDENT is a valid name for attribute ATTR,
4116    or zero if not.
4117
4118    We try both `text' and `__text__', ATTR may be either one.  */
4119 /* ??? It might be a reasonable simplification to require ATTR to be only
4120    `text'.  One might then also require attribute lists to be stored in
4121    their canonicalized form.  */
4122
4123 static int
4124 is_attribute_with_length_p (const char *attr, int attr_len, const_tree ident)
4125 {
4126   int ident_len;
4127   const char *p;
4128
4129   if (TREE_CODE (ident) != IDENTIFIER_NODE)
4130     return 0;
4131   
4132   p = IDENTIFIER_POINTER (ident);
4133   ident_len = IDENTIFIER_LENGTH (ident);
4134   
4135   if (ident_len == attr_len
4136       && strcmp (attr, p) == 0)
4137     return 1;
4138
4139   /* If ATTR is `__text__', IDENT must be `text'; and vice versa.  */
4140   if (attr[0] == '_')
4141     {
4142       gcc_assert (attr[1] == '_');
4143       gcc_assert (attr[attr_len - 2] == '_');
4144       gcc_assert (attr[attr_len - 1] == '_');
4145       if (ident_len == attr_len - 4
4146           && strncmp (attr + 2, p, attr_len - 4) == 0)
4147         return 1;
4148     }
4149   else
4150     {
4151       if (ident_len == attr_len + 4
4152           && p[0] == '_' && p[1] == '_'
4153           && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
4154           && strncmp (attr, p + 2, attr_len) == 0)
4155         return 1;
4156     }
4157
4158   return 0;
4159 }
4160
4161 /* Return nonzero if IDENT is a valid name for attribute ATTR,
4162    or zero if not.
4163
4164    We try both `text' and `__text__', ATTR may be either one.  */
4165
4166 int
4167 is_attribute_p (const char *attr, const_tree ident)
4168 {
4169   return is_attribute_with_length_p (attr, strlen (attr), ident);
4170 }
4171
4172 /* Given an attribute name and a list of attributes, return a pointer to the
4173    attribute's list element if the attribute is part of the list, or NULL_TREE
4174    if not found.  If the attribute appears more than once, this only
4175    returns the first occurrence; the TREE_CHAIN of the return value should
4176    be passed back in if further occurrences are wanted.  */
4177
4178 tree
4179 lookup_attribute (const char *attr_name, tree list)
4180 {
4181   tree l;
4182   size_t attr_len = strlen (attr_name);
4183
4184   for (l = list; l; l = TREE_CHAIN (l))
4185     {
4186       gcc_assert (TREE_CODE (TREE_PURPOSE (l)) == IDENTIFIER_NODE);
4187       if (is_attribute_with_length_p (attr_name, attr_len, TREE_PURPOSE (l)))
4188         return l;
4189     }
4190   return NULL_TREE;
4191 }
4192
4193 /* Remove any instances of attribute ATTR_NAME in LIST and return the
4194    modified list.  */
4195
4196 tree
4197 remove_attribute (const char *attr_name, tree list)
4198 {
4199   tree *p;
4200   size_t attr_len = strlen (attr_name);
4201
4202   for (p = &list; *p; )
4203     {
4204       tree l = *p;
4205       gcc_assert (TREE_CODE (TREE_PURPOSE (l)) == IDENTIFIER_NODE);
4206       if (is_attribute_with_length_p (attr_name, attr_len, TREE_PURPOSE (l)))
4207         *p = TREE_CHAIN (l);
4208       else
4209         p = &TREE_CHAIN (l);
4210     }
4211
4212   return list;
4213 }
4214
4215 /* Return an attribute list that is the union of a1 and a2.  */
4216
4217 tree
4218 merge_attributes (tree a1, tree a2)
4219 {
4220   tree attributes;
4221
4222   /* Either one unset?  Take the set one.  */
4223
4224   if ((attributes = a1) == 0)
4225     attributes = a2;
4226
4227   /* One that completely contains the other?  Take it.  */
4228
4229   else if (a2 != 0 && ! attribute_list_contained (a1, a2))
4230     {
4231       if (attribute_list_contained (a2, a1))
4232         attributes = a2;
4233       else
4234         {
4235           /* Pick the longest list, and hang on the other list.  */
4236
4237           if (list_length (a1) < list_length (a2))
4238             attributes = a2, a2 = a1;
4239
4240           for (; a2 != 0; a2 = TREE_CHAIN (a2))
4241             {
4242               tree a;
4243               for (a = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
4244                                          attributes);
4245                    a != NULL_TREE;
4246                    a = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
4247                                          TREE_CHAIN (a)))
4248                 {
4249                   if (TREE_VALUE (a) != NULL
4250                       && TREE_CODE (TREE_VALUE (a)) == TREE_LIST
4251                       && TREE_VALUE (a2) != NULL
4252                       && TREE_CODE (TREE_VALUE (a2)) == TREE_LIST)
4253                     {
4254                       if (simple_cst_list_equal (TREE_VALUE (a),
4255                                                  TREE_VALUE (a2)) == 1)
4256                         break;
4257                     }
4258                   else if (simple_cst_equal (TREE_VALUE (a),
4259                                              TREE_VALUE (a2)) == 1)
4260                     break;
4261                 }
4262               if (a == NULL_TREE)
4263                 {
4264                   a1 = copy_node (a2);
4265                   TREE_CHAIN (a1) = attributes;
4266                   attributes = a1;
4267                 }
4268             }
4269         }
4270     }
4271   return attributes;
4272 }
4273
4274 /* Given types T1 and T2, merge their attributes and return
4275   the result.  */
4276
4277 tree
4278 merge_type_attributes (tree t1, tree t2)
4279 {
4280   return merge_attributes (TYPE_ATTRIBUTES (t1),
4281                            TYPE_ATTRIBUTES (t2));
4282 }
4283
4284 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
4285    the result.  */
4286
4287 tree
4288 merge_decl_attributes (tree olddecl, tree newdecl)
4289 {
4290   return merge_attributes (DECL_ATTRIBUTES (olddecl),
4291                            DECL_ATTRIBUTES (newdecl));
4292 }
4293
4294 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
4295
4296 /* Specialization of merge_decl_attributes for various Windows targets.
4297
4298    This handles the following situation:
4299
4300      __declspec (dllimport) int foo;
4301      int foo;
4302
4303    The second instance of `foo' nullifies the dllimport.  */
4304
4305 tree
4306 merge_dllimport_decl_attributes (tree old, tree new_tree)
4307 {
4308   tree a;
4309   int delete_dllimport_p = 1;
4310
4311   /* What we need to do here is remove from `old' dllimport if it doesn't
4312      appear in `new'.  dllimport behaves like extern: if a declaration is
4313      marked dllimport and a definition appears later, then the object
4314      is not dllimport'd.  We also remove a `new' dllimport if the old list
4315      contains dllexport:  dllexport always overrides dllimport, regardless
4316      of the order of declaration.  */     
4317   if (!VAR_OR_FUNCTION_DECL_P (new_tree))
4318     delete_dllimport_p = 0;
4319   else if (DECL_DLLIMPORT_P (new_tree)
4320            && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
4321     { 
4322       DECL_DLLIMPORT_P (new_tree) = 0;
4323       warning (OPT_Wattributes, "%q+D already declared with dllexport attribute: "
4324               "dllimport ignored", new_tree);
4325     }
4326   else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new_tree))
4327     {
4328       /* Warn about overriding a symbol that has already been used, e.g.:
4329            extern int __attribute__ ((dllimport)) foo;
4330            int* bar () {return &foo;}
4331            int foo;
4332       */
4333       if (TREE_USED (old))
4334         {
4335           warning (0, "%q+D redeclared without dllimport attribute "
4336                    "after being referenced with dll linkage", new_tree);
4337           /* If we have used a variable's address with dllimport linkage,
4338               keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
4339               decl may already have had TREE_CONSTANT computed.
4340               We still remove the attribute so that assembler code refers
4341               to '&foo rather than '_imp__foo'.  */
4342           if (TREE_CODE (old) == VAR_DECL && TREE_ADDRESSABLE (old))
4343             DECL_DLLIMPORT_P (new_tree) = 1;
4344         }
4345
4346       /* Let an inline definition silently override the external reference,
4347          but otherwise warn about attribute inconsistency.  */ 
4348       else if (TREE_CODE (new_tree) == VAR_DECL
4349                || !DECL_DECLARED_INLINE_P (new_tree))
4350         warning (OPT_Wattributes, "%q+D redeclared without dllimport attribute: "
4351                   "previous dllimport ignored", new_tree);
4352     }
4353   else
4354     delete_dllimport_p = 0;
4355
4356   a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new_tree));
4357
4358   if (delete_dllimport_p) 
4359     {
4360       tree prev, t;
4361       const size_t attr_len = strlen ("dllimport"); 
4362      
4363       /* Scan the list for dllimport and delete it.  */
4364       for (prev = NULL_TREE, t = a; t; prev = t, t = TREE_CHAIN (t))
4365         {
4366           if (is_attribute_with_length_p ("dllimport", attr_len,
4367                                           TREE_PURPOSE (t)))
4368             {
4369               if (prev == NULL_TREE)
4370                 a = TREE_CHAIN (a);
4371               else
4372                 TREE_CHAIN (prev) = TREE_CHAIN (t);
4373               break;
4374             }
4375         }
4376     }
4377
4378   return a;
4379 }
4380
4381 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
4382    struct attribute_spec.handler.  */
4383
4384 tree
4385 handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
4386                       bool *no_add_attrs)
4387 {
4388   tree node = *pnode;
4389   bool is_dllimport;
4390
4391   /* These attributes may apply to structure and union types being created,
4392      but otherwise should pass to the declaration involved.  */
4393   if (!DECL_P (node))
4394     {
4395       if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
4396                    | (int) ATTR_FLAG_ARRAY_NEXT))
4397         {
4398           *no_add_attrs = true;
4399           return tree_cons (name, args, NULL_TREE);
4400         }
4401       if (TREE_CODE (node) == RECORD_TYPE
4402           || TREE_CODE (node) == UNION_TYPE)
4403         {
4404           node = TYPE_NAME (node);
4405           if (!node)
4406             return NULL_TREE;
4407         }
4408       else
4409         {
4410           warning (OPT_Wattributes, "%qE attribute ignored",
4411                    name);
4412           *no_add_attrs = true;
4413           return NULL_TREE;
4414         }
4415     }
4416
4417   if (TREE_CODE (node) != FUNCTION_DECL
4418       && TREE_CODE (node) != VAR_DECL
4419       && TREE_CODE (node) != TYPE_DECL)
4420     {
4421       *no_add_attrs = true;
4422       warning (OPT_Wattributes, "%qE attribute ignored",
4423                name);
4424       return NULL_TREE;
4425     }
4426
4427   if (TREE_CODE (node) == TYPE_DECL
4428       && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE
4429       && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE)
4430     {
4431       *no_add_attrs = true;
4432       warning (OPT_Wattributes, "%qE attribute ignored",
4433                name);
4434       return NULL_TREE;
4435     }
4436
4437   is_dllimport = is_attribute_p ("dllimport", name);
4438
4439   /* Report error on dllimport ambiguities seen now before they cause
4440      any damage.  */
4441   if (is_dllimport)
4442     {
4443       /* Honor any target-specific overrides. */ 
4444       if (!targetm.valid_dllimport_attribute_p (node))
4445         *no_add_attrs = true;
4446
4447      else if (TREE_CODE (node) == FUNCTION_DECL
4448                 && DECL_DECLARED_INLINE_P (node))
4449         {
4450           warning (OPT_Wattributes, "inline function %q+D declared as "
4451                   " dllimport: attribute ignored", node); 
4452           *no_add_attrs = true;
4453         }
4454       /* Like MS, treat definition of dllimported variables and
4455          non-inlined functions on declaration as syntax errors. */
4456      else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
4457         {
4458           error ("function %q+D definition is marked dllimport", node);
4459           *no_add_attrs = true;
4460         }
4461
4462      else if (TREE_CODE (node) == VAR_DECL)
4463         {
4464           if (DECL_INITIAL (node))
4465             {
4466               error ("variable %q+D definition is marked dllimport",
4467                      node);
4468               *no_add_attrs = true;
4469             }
4470
4471           /* `extern' needn't be specified with dllimport.
4472              Specify `extern' now and hope for the best.  Sigh.  */
4473           DECL_EXTERNAL (node) = 1;
4474           /* Also, implicitly give dllimport'd variables declared within
4475              a function global scope, unless declared static.  */
4476           if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
4477             TREE_PUBLIC (node) = 1;
4478         }
4479
4480       if (*no_add_attrs == false)
4481         DECL_DLLIMPORT_P (node) = 1;
4482     }
4483   else if (TREE_CODE (node) == FUNCTION_DECL
4484            && DECL_DECLARED_INLINE_P (node))
4485     /* An exported function, even if inline, must be emitted.  */
4486     DECL_EXTERNAL (node) = 0;
4487
4488   /*  Report error if symbol is not accessible at global scope.  */
4489   if (!TREE_PUBLIC (node)
4490       && (TREE_CODE (node) == VAR_DECL
4491           || TREE_CODE (node) == FUNCTION_DECL))
4492     {
4493       error ("external linkage required for symbol %q+D because of "
4494              "%qE attribute", node, name);
4495       *no_add_attrs = true;
4496     }
4497
4498   /* A dllexport'd entity must have default visibility so that other
4499      program units (shared libraries or the main executable) can see
4500      it.  A dllimport'd entity must have default visibility so that
4501      the linker knows that undefined references within this program
4502      unit can be resolved by the dynamic linker.  */
4503   if (!*no_add_attrs)
4504     {
4505       if (DECL_VISIBILITY_SPECIFIED (node)
4506           && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT)
4507         error ("%qE implies default visibility, but %qD has already "
4508                "been declared with a different visibility", 
4509                name, node);
4510       DECL_VISIBILITY (node) = VISIBILITY_DEFAULT;
4511       DECL_VISIBILITY_SPECIFIED (node) = 1;
4512     }
4513
4514   return NULL_TREE;
4515 }
4516
4517 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES  */
4518 \f
4519 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
4520    of the various TYPE_QUAL values.  */
4521
4522 static void
4523 set_type_quals (tree type, int type_quals)
4524 {
4525   TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
4526   TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
4527   TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
4528 }
4529
4530 /* Returns true iff CAND is equivalent to BASE with TYPE_QUALS.  */
4531
4532 bool
4533 check_qualified_type (const_tree cand, const_tree base, int type_quals)
4534 {
4535   return (TYPE_QUALS (cand) == type_quals
4536           && TYPE_NAME (cand) == TYPE_NAME (base)
4537           /* Apparently this is needed for Objective-C.  */
4538           && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
4539           && attribute_list_equal (TYPE_ATTRIBUTES (cand),
4540                                    TYPE_ATTRIBUTES (base)));
4541 }
4542
4543 /* Return a version of the TYPE, qualified as indicated by the
4544    TYPE_QUALS, if one exists.  If no qualified version exists yet,
4545    return NULL_TREE.  */
4546
4547 tree
4548 get_qualified_type (tree type, int type_quals)
4549 {
4550   tree t;
4551
4552   if (TYPE_QUALS (type) == type_quals)
4553     return type;
4554
4555   /* Search the chain of variants to see if there is already one there just
4556      like the one we need to have.  If so, use that existing one.  We must
4557      preserve the TYPE_NAME, since there is code that depends on this.  */
4558   for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
4559     if (check_qualified_type (t, type, type_quals))
4560       return t;
4561
4562   return NULL_TREE;
4563 }
4564
4565 /* Like get_qualified_type, but creates the type if it does not
4566    exist.  This function never returns NULL_TREE.  */
4567
4568 tree
4569 build_qualified_type (tree type, int type_quals)
4570 {
4571   tree t;
4572
4573   /* See if we already have the appropriate qualified variant.  */
4574   t = get_qualified_type (type, type_quals);
4575
4576   /* If not, build it.  */
4577   if (!t)
4578     {
4579       t = build_variant_type_copy (type);
4580       set_type_quals (t, type_quals);
4581
4582       if (TYPE_STRUCTURAL_EQUALITY_P (type))
4583         /* Propagate structural equality. */
4584         SET_TYPE_STRUCTURAL_EQUALITY (t);
4585       else if (TYPE_CANONICAL (type) != type)
4586         /* Build the underlying canonical type, since it is different
4587            from TYPE. */
4588         TYPE_CANONICAL (t) = build_qualified_type (TYPE_CANONICAL (type),
4589                                                    type_quals);
4590       else
4591         /* T is its own canonical type. */
4592         TYPE_CANONICAL (t) = t;
4593       
4594     }
4595
4596   return t;
4597 }
4598
4599 /* Create a new distinct copy of TYPE.  The new type is made its own
4600    MAIN_VARIANT. If TYPE requires structural equality checks, the
4601    resulting type requires structural equality checks; otherwise, its
4602    TYPE_CANONICAL points to itself. */
4603
4604 tree
4605 build_distinct_type_copy (tree type)
4606 {
4607   tree t = copy_node (type);
4608   
4609   TYPE_POINTER_TO (t) = 0;
4610   TYPE_REFERENCE_TO (t) = 0;
4611
4612   /* Set the canonical type either to a new equivalence class, or
4613      propagate the need for structural equality checks. */
4614   if (TYPE_STRUCTURAL_EQUALITY_P (type))
4615     SET_TYPE_STRUCTURAL_EQUALITY (t);
4616   else
4617     TYPE_CANONICAL (t) = t;
4618
4619   /* Make it its own variant.  */
4620   TYPE_MAIN_VARIANT (t) = t;
4621   TYPE_NEXT_VARIANT (t) = 0;
4622
4623   /* Note that it is now possible for TYPE_MIN_VALUE to be a value
4624      whose TREE_TYPE is not t.  This can also happen in the Ada
4625      frontend when using subtypes.  */
4626
4627   return t;
4628 }
4629
4630 /* Create a new variant of TYPE, equivalent but distinct.  This is so
4631    the caller can modify it. TYPE_CANONICAL for the return type will
4632    be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
4633    are considered equal by the language itself (or that both types
4634    require structural equality checks). */
4635
4636 tree
4637 build_variant_type_copy (tree type)
4638 {
4639   tree t, m = TYPE_MAIN_VARIANT (type);
4640
4641   t = build_distinct_type_copy (type);
4642
4643   /* Since we're building a variant, assume that it is a non-semantic
4644      variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
4645   TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
4646   
4647   /* Add the new type to the chain of variants of TYPE.  */
4648   TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
4649   TYPE_NEXT_VARIANT (m) = t;
4650   TYPE_MAIN_VARIANT (t) = m;
4651
4652   return t;
4653 }
4654 \f
4655 /* Return true if the from tree in both tree maps are equal.  */
4656
4657 int
4658 tree_map_base_eq (const void *va, const void *vb)
4659 {
4660   const struct tree_map_base  *const a = (const struct tree_map_base *) va,
4661     *const b = (const struct tree_map_base *) vb;
4662   return (a->from == b->from);
4663 }
4664
4665 /* Hash a from tree in a tree_map.  */
4666
4667 unsigned int
4668 tree_map_base_hash (const void *item)
4669 {
4670   return htab_hash_pointer (((const struct tree_map_base *)item)->from);
4671 }
4672
4673 /* Return true if this tree map structure is marked for garbage collection
4674    purposes.  We simply return true if the from tree is marked, so that this
4675    structure goes away when the from tree goes away.  */
4676
4677 int
4678 tree_map_base_marked_p (const void *p)
4679 {
4680   return ggc_marked_p (((const struct tree_map_base *) p)->from);
4681 }
4682
4683 unsigned int
4684 tree_map_hash (const void *item)
4685 {
4686   return (((const struct tree_map *) item)->hash);
4687 }
4688
4689 /* Return the initialization priority for DECL.  */
4690
4691 priority_type
4692 decl_init_priority_lookup (tree decl)
4693 {
4694   struct tree_priority_map *h;
4695   struct tree_map_base in;
4696
4697   gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
4698   in.from = decl;
4699   h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
4700   return h ? h->init : DEFAULT_INIT_PRIORITY;
4701 }
4702
4703 /* Return the finalization priority for DECL.  */
4704
4705 priority_type
4706 decl_fini_priority_lookup (tree decl)
4707 {
4708   struct tree_priority_map *h;
4709   struct tree_map_base in;
4710
4711   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4712   in.from = decl;
4713   h = (struct tree_priority_map *) htab_find (init_priority_for_decl, &in);
4714   return h ? h->fini : DEFAULT_INIT_PRIORITY;
4715 }
4716
4717 /* Return the initialization and finalization priority information for
4718    DECL.  If there is no previous priority information, a freshly
4719    allocated structure is returned.  */
4720
4721 static struct tree_priority_map *
4722 decl_priority_info (tree decl)
4723 {
4724   struct tree_priority_map in;
4725   struct tree_priority_map *h;
4726   void **loc;
4727
4728   in.base.from = decl;
4729   loc = htab_find_slot (init_priority_for_decl, &in, INSERT);
4730   h = (struct tree_priority_map *) *loc;
4731   if (!h)
4732     {
4733       h = GGC_CNEW (struct tree_priority_map);
4734       *loc = h;
4735       h->base.from = decl;
4736       h->init = DEFAULT_INIT_PRIORITY;
4737       h->fini = DEFAULT_INIT_PRIORITY;
4738     }
4739
4740   return h;
4741 }
4742
4743 /* Set the initialization priority for DECL to PRIORITY.  */
4744
4745 void
4746 decl_init_priority_insert (tree decl, priority_type priority)
4747 {
4748   struct tree_priority_map *h;
4749
4750   gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
4751   h = decl_priority_info (decl);
4752   h->init = priority;
4753 }  
4754
4755 /* Set the finalization priority for DECL to PRIORITY.  */
4756
4757 void
4758 decl_fini_priority_insert (tree decl, priority_type priority)
4759 {
4760   struct tree_priority_map *h;
4761
4762   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
4763   h = decl_priority_info (decl);
4764   h->fini = priority;
4765 }  
4766
4767 /* Print out the statistics for the DECL_DEBUG_EXPR hash table.  */
4768
4769 static void
4770 print_debug_expr_statistics (void)
4771 {
4772   fprintf (stderr, "DECL_DEBUG_EXPR  hash: size %ld, %ld elements, %f collisions\n",
4773            (long) htab_size (debug_expr_for_decl),
4774            (long) htab_elements (debug_expr_for_decl),
4775            htab_collisions (debug_expr_for_decl));
4776 }
4777
4778 /* Print out the statistics for the DECL_VALUE_EXPR hash table.  */
4779
4780 static void
4781 print_value_expr_statistics (void)
4782 {
4783   fprintf (stderr, "DECL_VALUE_EXPR  hash: size %ld, %ld elements, %f collisions\n",
4784            (long) htab_size (value_expr_for_decl),
4785            (long) htab_elements (value_expr_for_decl),
4786            htab_collisions (value_expr_for_decl));
4787 }
4788
4789 /* Lookup a debug expression for FROM, and return it if we find one.  */
4790
4791 tree 
4792 decl_debug_expr_lookup (tree from)
4793 {
4794   struct tree_map *h, in;
4795   in.base.from = from;
4796
4797   h = (struct tree_map *) htab_find_with_hash (debug_expr_for_decl, &in,
4798                                                htab_hash_pointer (from));
4799   if (h)
4800     return h->to;
4801   return NULL_TREE;
4802 }
4803
4804 /* Insert a mapping FROM->TO in the debug expression hashtable.  */
4805
4806 void
4807 decl_debug_expr_insert (tree from, tree to)
4808 {
4809   struct tree_map *h;
4810   void **loc;
4811
4812   h = GGC_NEW (struct tree_map);
4813   h->hash = htab_hash_pointer (from);
4814   h->base.from = from;
4815   h->to = to;
4816   loc = htab_find_slot_with_hash (debug_expr_for_decl, h, h->hash, INSERT);
4817   *(struct tree_map **) loc = h;
4818 }  
4819
4820 /* Lookup a value expression for FROM, and return it if we find one.  */
4821
4822 tree 
4823 decl_value_expr_lookup (tree from)
4824 {
4825   struct tree_map *h, in;
4826   in.base.from = from;
4827
4828   h = (struct tree_map *) htab_find_with_hash (value_expr_for_decl, &in,
4829                                                htab_hash_pointer (from));
4830   if (h)
4831     return h->to;
4832   return NULL_TREE;
4833 }
4834
4835 /* Insert a mapping FROM->TO in the value expression hashtable.  */
4836
4837 void
4838 decl_value_expr_insert (tree from, tree to)
4839 {
4840   struct tree_map *h;
4841   void **loc;
4842
4843   h = GGC_NEW (struct tree_map);
4844   h->hash = htab_hash_pointer (from);
4845   h->base.from = from;
4846   h->to = to;
4847   loc = htab_find_slot_with_hash (value_expr_for_decl, h, h->hash, INSERT);
4848   *(struct tree_map **) loc = h;
4849 }
4850
4851 /* Hashing of types so that we don't make duplicates.
4852    The entry point is `type_hash_canon'.  */
4853
4854 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
4855    with types in the TREE_VALUE slots), by adding the hash codes
4856    of the individual types.  */
4857
4858 static unsigned int
4859 type_hash_list (const_tree list, hashval_t hashcode)
4860 {
4861   const_tree tail;
4862
4863   for (tail = list; tail; tail = TREE_CHAIN (tail))
4864     if (TREE_VALUE (tail) != error_mark_node)
4865       hashcode = iterative_hash_object (TYPE_HASH (TREE_VALUE (tail)),
4866                                         hashcode);
4867
4868   return hashcode;
4869 }
4870
4871 /* These are the Hashtable callback functions.  */
4872
4873 /* Returns true iff the types are equivalent.  */
4874
4875 static int
4876 type_hash_eq (const void *va, const void *vb)
4877 {
4878   const struct type_hash *const a = (const struct type_hash *) va,
4879     *const b = (const struct type_hash *) vb;
4880
4881   /* First test the things that are the same for all types.  */
4882   if (a->hash != b->hash
4883       || TREE_CODE (a->type) != TREE_CODE (b->type)
4884       || TREE_TYPE (a->type) != TREE_TYPE (b->type)
4885       || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
4886                                  TYPE_ATTRIBUTES (b->type))
4887       || TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
4888       || TYPE_MODE (a->type) != TYPE_MODE (b->type)
4889       || (TREE_CODE (a->type) != COMPLEX_TYPE 
4890           && TYPE_NAME (a->type) != TYPE_NAME (b->type)))
4891     return 0;
4892
4893   switch (TREE_CODE (a->type))
4894     {
4895     case VOID_TYPE:
4896     case COMPLEX_TYPE:
4897     case POINTER_TYPE:
4898     case REFERENCE_TYPE:
4899       return 1;
4900
4901     case VECTOR_TYPE:
4902       return TYPE_VECTOR_SUBPARTS (a->type) == TYPE_VECTOR_SUBPARTS (b->type);
4903
4904     case ENUMERAL_TYPE:
4905       if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
4906           && !(TYPE_VALUES (a->type)
4907                && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
4908                && TYPE_VALUES (b->type)
4909                && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
4910                && type_list_equal (TYPE_VALUES (a->type),
4911                                    TYPE_VALUES (b->type))))
4912         return 0;
4913
4914       /* ... fall through ... */
4915
4916     case INTEGER_TYPE:
4917     case REAL_TYPE:
4918     case BOOLEAN_TYPE:
4919       return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
4920                || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
4921                                       TYPE_MAX_VALUE (b->type)))
4922               && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
4923                   || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
4924                                          TYPE_MIN_VALUE (b->type))));
4925
4926     case FIXED_POINT_TYPE:
4927       return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
4928
4929     case OFFSET_TYPE:
4930       return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
4931
4932     case METHOD_TYPE:
4933       return (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
4934               && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
4935                   || (TYPE_ARG_TYPES (a->type)
4936                       && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
4937                       && TYPE_ARG_TYPES (b->type)
4938                       && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
4939                       && type_list_equal (TYPE_ARG_TYPES (a->type),
4940                                           TYPE_ARG_TYPES (b->type)))));
4941
4942     case ARRAY_TYPE:
4943       return TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type);
4944
4945     case RECORD_TYPE:
4946     case UNION_TYPE:
4947     case QUAL_UNION_TYPE:
4948       return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
4949               || (TYPE_FIELDS (a->type)
4950                   && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
4951                   && TYPE_FIELDS (b->type)
4952                   && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
4953                   && type_list_equal (TYPE_FIELDS (a->type),
4954                                       TYPE_FIELDS (b->type))));
4955
4956     case FUNCTION_TYPE:
4957       if (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
4958           || (TYPE_ARG_TYPES (a->type)
4959               && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
4960               && TYPE_ARG_TYPES (b->type)
4961               && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
4962               && type_list_equal (TYPE_ARG_TYPES (a->type),
4963                                   TYPE_ARG_TYPES (b->type))))
4964         break;
4965       return 0;
4966
4967     default:
4968       return 0;
4969     }
4970
4971   if (lang_hooks.types.type_hash_eq != NULL)
4972     return lang_hooks.types.type_hash_eq (a->type, b->type);
4973
4974   return 1;
4975 }
4976
4977 /* Return the cached hash value.  */
4978
4979 static hashval_t
4980 type_hash_hash (const void *item)
4981 {
4982   return ((const struct type_hash *) item)->hash;
4983 }
4984
4985 /* Look in the type hash table for a type isomorphic to TYPE.
4986    If one is found, return it.  Otherwise return 0.  */
4987
4988 tree
4989 type_hash_lookup (hashval_t hashcode, tree type)
4990 {
4991   struct type_hash *h, in;
4992
4993   /* The TYPE_ALIGN field of a type is set by layout_type(), so we
4994      must call that routine before comparing TYPE_ALIGNs.  */
4995   layout_type (type);
4996
4997   in.hash = hashcode;
4998   in.type = type;
4999
5000   h = (struct type_hash *) htab_find_with_hash (type_hash_table, &in,
5001                                                 hashcode);
5002   if (h)
5003     return h->type;
5004   return NULL_TREE;
5005 }
5006
5007 /* Add an entry to the type-hash-table
5008    for a type TYPE whose hash code is HASHCODE.  */
5009
5010 void
5011 type_hash_add (hashval_t hashcode, tree type)
5012 {
5013   struct type_hash *h;
5014   void **loc;
5015
5016   h = GGC_NEW (struct type_hash);
5017   h->hash = hashcode;
5018   h->type = type;
5019   loc = htab_find_slot_with_hash (type_hash_table, h, hashcode, INSERT);
5020   *loc = (void *)h;
5021 }
5022
5023 /* Given TYPE, and HASHCODE its hash code, return the canonical
5024    object for an identical type if one already exists.
5025    Otherwise, return TYPE, and record it as the canonical object.
5026
5027    To use this function, first create a type of the sort you want.
5028    Then compute its hash code from the fields of the type that
5029    make it different from other similar types.
5030    Then call this function and use the value.  */
5031
5032 tree
5033 type_hash_canon (unsigned int hashcode, tree type)
5034 {
5035   tree t1;
5036
5037   /* The hash table only contains main variants, so ensure that's what we're
5038      being passed.  */
5039   gcc_assert (TYPE_MAIN_VARIANT (type) == type);
5040
5041   if (!lang_hooks.types.hash_types)
5042     return type;
5043
5044   /* See if the type is in the hash table already.  If so, return it.
5045      Otherwise, add the type.  */
5046   t1 = type_hash_lookup (hashcode, type);
5047   if (t1 != 0)
5048     {
5049 #ifdef GATHER_STATISTICS
5050       tree_node_counts[(int) t_kind]--;
5051       tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type);
5052 #endif
5053       return t1;
5054     }
5055   else
5056     {
5057       type_hash_add (hashcode, type);
5058       return type;
5059     }
5060 }
5061
5062 /* See if the data pointed to by the type hash table is marked.  We consider
5063    it marked if the type is marked or if a debug type number or symbol
5064    table entry has been made for the type.  This reduces the amount of
5065    debugging output and eliminates that dependency of the debug output on
5066    the number of garbage collections.  */
5067
5068 static int
5069 type_hash_marked_p (const void *p)
5070 {
5071   const_tree const type = ((const struct type_hash *) p)->type;
5072
5073   return ggc_marked_p (type) || TYPE_SYMTAB_POINTER (type);
5074 }
5075
5076 static void
5077 print_type_hash_statistics (void)
5078 {
5079   fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
5080            (long) htab_size (type_hash_table),
5081            (long) htab_elements (type_hash_table),
5082            htab_collisions (type_hash_table));
5083 }
5084
5085 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
5086    with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
5087    by adding the hash codes of the individual attributes.  */
5088
5089 static unsigned int
5090 attribute_hash_list (const_tree list, hashval_t hashcode)
5091 {
5092   const_tree tail;
5093
5094   for (tail = list; tail; tail = TREE_CHAIN (tail))
5095     /* ??? Do we want to add in TREE_VALUE too? */
5096     hashcode = iterative_hash_object
5097       (IDENTIFIER_HASH_VALUE (TREE_PURPOSE (tail)), hashcode);
5098   return hashcode;
5099 }
5100
5101 /* Given two lists of attributes, return true if list l2 is
5102    equivalent to l1.  */
5103
5104 int
5105 attribute_list_equal (const_tree l1, const_tree l2)
5106 {
5107   return attribute_list_contained (l1, l2)
5108          && attribute_list_contained (l2, l1);
5109 }
5110
5111 /* Given two lists of attributes, return true if list L2 is
5112    completely contained within L1.  */
5113 /* ??? This would be faster if attribute names were stored in a canonicalized
5114    form.  Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
5115    must be used to show these elements are equivalent (which they are).  */
5116 /* ??? It's not clear that attributes with arguments will always be handled
5117    correctly.  */
5118
5119 int
5120 attribute_list_contained (const_tree l1, const_tree l2)
5121 {
5122   const_tree t1, t2;
5123
5124   /* First check the obvious, maybe the lists are identical.  */
5125   if (l1 == l2)
5126     return 1;
5127
5128   /* Maybe the lists are similar.  */
5129   for (t1 = l1, t2 = l2;
5130        t1 != 0 && t2 != 0
5131         && TREE_PURPOSE (t1) == TREE_PURPOSE (t2)
5132         && TREE_VALUE (t1) == TREE_VALUE (t2);
5133        t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2));
5134
5135   /* Maybe the lists are equal.  */
5136   if (t1 == 0 && t2 == 0)
5137     return 1;
5138
5139   for (; t2 != 0; t2 = TREE_CHAIN (t2))
5140     {
5141       const_tree attr;
5142       /* This CONST_CAST is okay because lookup_attribute does not
5143          modify its argument and the return value is assigned to a
5144          const_tree.  */
5145       for (attr = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (t2)),
5146                                     CONST_CAST_TREE(l1));
5147            attr != NULL_TREE;
5148            attr = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (t2)),
5149                                     TREE_CHAIN (attr)))
5150         {
5151           if (TREE_VALUE (t2) != NULL
5152               && TREE_CODE (TREE_VALUE (t2)) == TREE_LIST
5153               && TREE_VALUE (attr) != NULL
5154               && TREE_CODE (TREE_VALUE (attr)) == TREE_LIST)
5155             {
5156               if (simple_cst_list_equal (TREE_VALUE (t2),
5157                                          TREE_VALUE (attr)) == 1)
5158                 break;
5159             }
5160           else if (simple_cst_equal (TREE_VALUE (t2), TREE_VALUE (attr)) == 1)
5161             break;
5162         }
5163
5164       if (attr == 0)
5165         return 0;
5166     }
5167
5168   return 1;
5169 }
5170
5171 /* Given two lists of types
5172    (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
5173    return 1 if the lists contain the same types in the same order.
5174    Also, the TREE_PURPOSEs must match.  */
5175
5176 int
5177 type_list_equal (const_tree l1, const_tree l2)
5178 {
5179   const_tree t1, t2;
5180
5181   for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
5182     if (TREE_VALUE (t1) != TREE_VALUE (t2)
5183         || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
5184             && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
5185                   && (TREE_TYPE (TREE_PURPOSE (t1))
5186                       == TREE_TYPE (TREE_PURPOSE (t2))))))
5187       return 0;
5188
5189   return t1 == t2;
5190 }
5191
5192 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
5193    given by TYPE.  If the argument list accepts variable arguments,
5194    then this function counts only the ordinary arguments.  */
5195
5196 int
5197 type_num_arguments (const_tree type)
5198 {
5199   int i = 0;
5200   tree t;
5201
5202   for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
5203     /* If the function does not take a variable number of arguments,
5204        the last element in the list will have type `void'.  */
5205     if (VOID_TYPE_P (TREE_VALUE (t)))
5206       break;
5207     else
5208       ++i;
5209
5210   return i;
5211 }
5212
5213 /* Nonzero if integer constants T1 and T2
5214    represent the same constant value.  */
5215
5216 int
5217 tree_int_cst_equal (const_tree t1, const_tree t2)
5218 {
5219   if (t1 == t2)
5220     return 1;
5221
5222   if (t1 == 0 || t2 == 0)
5223     return 0;
5224
5225   if (TREE_CODE (t1) == INTEGER_CST
5226       && TREE_CODE (t2) == INTEGER_CST
5227       && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
5228       && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
5229     return 1;
5230
5231   return 0;
5232 }
5233
5234 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
5235    The precise way of comparison depends on their data type.  */
5236
5237 int
5238 tree_int_cst_lt (const_tree t1, const_tree t2)
5239 {
5240   if (t1 == t2)
5241     return 0;
5242
5243   if (TYPE_UNSIGNED (TREE_TYPE (t1)) != TYPE_UNSIGNED (TREE_TYPE (t2)))
5244     {
5245       int t1_sgn = tree_int_cst_sgn (t1);
5246       int t2_sgn = tree_int_cst_sgn (t2);
5247
5248       if (t1_sgn < t2_sgn)
5249         return 1;
5250       else if (t1_sgn > t2_sgn)
5251         return 0;
5252       /* Otherwise, both are non-negative, so we compare them as
5253          unsigned just in case one of them would overflow a signed
5254          type.  */
5255     }
5256   else if (!TYPE_UNSIGNED (TREE_TYPE (t1)))
5257     return INT_CST_LT (t1, t2);
5258
5259   return INT_CST_LT_UNSIGNED (t1, t2);
5260 }
5261
5262 /* Returns -1 if T1 < T2, 0 if T1 == T2, and 1 if T1 > T2.  */
5263
5264 int
5265 tree_int_cst_compare (const_tree t1, const_tree t2)
5266 {
5267   if (tree_int_cst_lt (t1, t2))
5268     return -1;
5269   else if (tree_int_cst_lt (t2, t1))
5270     return 1;
5271   else
5272     return 0;
5273 }
5274
5275 /* Return 1 if T is an INTEGER_CST that can be manipulated efficiently on
5276    the host.  If POS is zero, the value can be represented in a single
5277    HOST_WIDE_INT.  If POS is nonzero, the value must be non-negative and can
5278    be represented in a single unsigned HOST_WIDE_INT.  */
5279
5280 int
5281 host_integerp (const_tree t, int pos)
5282 {
5283   return (TREE_CODE (t) == INTEGER_CST
5284           && ((TREE_INT_CST_HIGH (t) == 0
5285                && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) >= 0)
5286               || (! pos && TREE_INT_CST_HIGH (t) == -1
5287                   && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0
5288                   && (!TYPE_UNSIGNED (TREE_TYPE (t))
5289                       || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
5290                           && TYPE_IS_SIZETYPE (TREE_TYPE (t)))))
5291               || (pos && TREE_INT_CST_HIGH (t) == 0)));
5292 }
5293
5294 /* Return the HOST_WIDE_INT least significant bits of T if it is an
5295    INTEGER_CST and there is no overflow.  POS is nonzero if the result must
5296    be non-negative.  We must be able to satisfy the above conditions.  */
5297
5298 HOST_WIDE_INT
5299 tree_low_cst (const_tree t, int pos)
5300 {
5301   gcc_assert (host_integerp (t, pos));
5302   return TREE_INT_CST_LOW (t);
5303 }
5304
5305 /* Return the most significant bit of the integer constant T.  */
5306
5307 int
5308 tree_int_cst_msb (const_tree t)
5309 {
5310   int prec;
5311   HOST_WIDE_INT h;
5312   unsigned HOST_WIDE_INT l;
5313
5314   /* Note that using TYPE_PRECISION here is wrong.  We care about the
5315      actual bits, not the (arbitrary) range of the type.  */
5316   prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (t))) - 1;
5317   rshift_double (TREE_INT_CST_LOW (t), TREE_INT_CST_HIGH (t), prec,
5318                  2 * HOST_BITS_PER_WIDE_INT, &l, &h, 0);
5319   return (l & 1) == 1;
5320 }
5321
5322 /* Return an indication of the sign of the integer constant T.
5323    The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
5324    Note that -1 will never be returned if T's type is unsigned.  */
5325
5326 int
5327 tree_int_cst_sgn (const_tree t)
5328 {
5329   if (TREE_INT_CST_LOW (t) == 0 && TREE_INT_CST_HIGH (t) == 0)
5330     return 0;
5331   else if (TYPE_UNSIGNED (TREE_TYPE (t)))
5332     return 1;
5333   else if (TREE_INT_CST_HIGH (t) < 0)
5334     return -1;
5335   else
5336     return 1;
5337 }
5338
5339 /* Return the minimum number of bits needed to represent VALUE in a
5340    signed or unsigned type, UNSIGNEDP says which.  */
5341
5342 unsigned int
5343 tree_int_cst_min_precision (tree value, bool unsignedp)
5344 {
5345   int log;
5346
5347   /* If the value is negative, compute its negative minus 1.  The latter
5348      adjustment is because the absolute value of the largest negative value
5349      is one larger than the largest positive value.  This is equivalent to
5350      a bit-wise negation, so use that operation instead.  */
5351
5352   if (tree_int_cst_sgn (value) < 0)
5353     value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
5354
5355   /* Return the number of bits needed, taking into account the fact
5356      that we need one more bit for a signed than unsigned type.  */
5357
5358   if (integer_zerop (value))
5359     log = 0;
5360   else
5361     log = tree_floor_log2 (value);
5362
5363   return log + 1 + !unsignedp;
5364 }
5365
5366 /* Compare two constructor-element-type constants.  Return 1 if the lists
5367    are known to be equal; otherwise return 0.  */
5368
5369 int
5370 simple_cst_list_equal (const_tree l1, const_tree l2)
5371 {
5372   while (l1 != NULL_TREE && l2 != NULL_TREE)
5373     {
5374       if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
5375         return 0;
5376
5377       l1 = TREE_CHAIN (l1);
5378       l2 = TREE_CHAIN (l2);
5379     }
5380
5381   return l1 == l2;
5382 }
5383
5384 /* Return truthvalue of whether T1 is the same tree structure as T2.
5385    Return 1 if they are the same.
5386    Return 0 if they are understandably different.
5387    Return -1 if either contains tree structure not understood by
5388    this function.  */
5389
5390 int
5391 simple_cst_equal (const_tree t1, const_tree t2)
5392 {
5393   enum tree_code code1, code2;
5394   int cmp;
5395   int i;
5396
5397   if (t1 == t2)
5398     return 1;
5399   if (t1 == 0 || t2 == 0)
5400     return 0;
5401
5402   code1 = TREE_CODE (t1);
5403   code2 = TREE_CODE (t2);
5404
5405   if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
5406     {
5407       if (CONVERT_EXPR_CODE_P (code2)
5408           || code2 == NON_LVALUE_EXPR)
5409         return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
5410       else
5411         return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
5412     }
5413
5414   else if (CONVERT_EXPR_CODE_P (code2)
5415            || code2 == NON_LVALUE_EXPR)
5416     return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
5417
5418   if (code1 != code2)
5419     return 0;
5420
5421   switch (code1)
5422     {
5423     case INTEGER_CST:
5424       return (TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
5425               && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2));
5426
5427     case REAL_CST:
5428       return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
5429
5430     case FIXED_CST:
5431       return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
5432
5433     case STRING_CST:
5434       return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
5435               && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
5436                          TREE_STRING_LENGTH (t1)));
5437
5438     case CONSTRUCTOR:
5439       {
5440         unsigned HOST_WIDE_INT idx;
5441         VEC(constructor_elt, gc) *v1 = CONSTRUCTOR_ELTS (t1);
5442         VEC(constructor_elt, gc) *v2 = CONSTRUCTOR_ELTS (t2);
5443
5444         if (VEC_length (constructor_elt, v1) != VEC_length (constructor_elt, v2))
5445           return false;
5446
5447         for (idx = 0; idx < VEC_length (constructor_elt, v1); ++idx)
5448           /* ??? Should we handle also fields here? */
5449           if (!simple_cst_equal (VEC_index (constructor_elt, v1, idx)->value,
5450                                  VEC_index (constructor_elt, v2, idx)->value))
5451             return false;
5452         return true;
5453       }
5454
5455     case SAVE_EXPR:
5456       return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
5457
5458     case CALL_EXPR:
5459       cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
5460       if (cmp <= 0)
5461         return cmp;
5462       if (call_expr_nargs (t1) != call_expr_nargs (t2))
5463         return 0;
5464       {
5465         const_tree arg1, arg2;
5466         const_call_expr_arg_iterator iter1, iter2;
5467         for (arg1 = first_const_call_expr_arg (t1, &iter1),
5468                arg2 = first_const_call_expr_arg (t2, &iter2);
5469              arg1 && arg2;
5470              arg1 = next_const_call_expr_arg (&iter1),
5471                arg2 = next_const_call_expr_arg (&iter2))
5472           {
5473             cmp = simple_cst_equal (arg1, arg2);
5474             if (cmp <= 0)
5475               return cmp;
5476           }
5477         return arg1 == arg2;
5478       }
5479
5480     case TARGET_EXPR:
5481       /* Special case: if either target is an unallocated VAR_DECL,
5482          it means that it's going to be unified with whatever the
5483          TARGET_EXPR is really supposed to initialize, so treat it
5484          as being equivalent to anything.  */
5485       if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
5486            && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
5487            && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
5488           || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
5489               && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
5490               && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
5491         cmp = 1;
5492       else
5493         cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
5494
5495       if (cmp <= 0)
5496         return cmp;
5497
5498       return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
5499
5500     case WITH_CLEANUP_EXPR:
5501       cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
5502       if (cmp <= 0)
5503         return cmp;
5504
5505       return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
5506
5507     case COMPONENT_REF:
5508       if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
5509         return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
5510
5511       return 0;
5512
5513     case VAR_DECL:
5514     case PARM_DECL:
5515     case CONST_DECL:
5516     case FUNCTION_DECL:
5517       return 0;
5518
5519     default:
5520       break;
5521     }
5522
5523   /* This general rule works for most tree codes.  All exceptions should be
5524      handled above.  If this is a language-specific tree code, we can't
5525      trust what might be in the operand, so say we don't know
5526      the situation.  */
5527   if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
5528     return -1;
5529
5530   switch (TREE_CODE_CLASS (code1))
5531     {
5532     case tcc_unary:
5533     case tcc_binary:
5534     case tcc_comparison:
5535     case tcc_expression:
5536     case tcc_reference:
5537     case tcc_statement:
5538       cmp = 1;
5539       for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
5540         {
5541           cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
5542           if (cmp <= 0)
5543             return cmp;
5544         }
5545
5546       return cmp;
5547
5548     default:
5549       return -1;
5550     }
5551 }
5552
5553 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
5554    Return -1, 0, or 1 if the value of T is less than, equal to, or greater
5555    than U, respectively.  */
5556
5557 int
5558 compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
5559 {
5560   if (tree_int_cst_sgn (t) < 0)
5561     return -1;
5562   else if (TREE_INT_CST_HIGH (t) != 0)
5563     return 1;
5564   else if (TREE_INT_CST_LOW (t) == u)
5565     return 0;
5566   else if (TREE_INT_CST_LOW (t) < u)
5567     return -1;
5568   else
5569     return 1;
5570 }
5571
5572 /* Return true if CODE represents an associative tree code.  Otherwise
5573    return false.  */
5574 bool
5575 associative_tree_code (enum tree_code code)
5576 {
5577   switch (code)
5578     {
5579     case BIT_IOR_EXPR:
5580     case BIT_AND_EXPR:
5581     case BIT_XOR_EXPR:
5582     case PLUS_EXPR:
5583     case MULT_EXPR:
5584     case MIN_EXPR:
5585     case MAX_EXPR:
5586       return true;
5587
5588     default:
5589       break;
5590     }
5591   return false;
5592 }
5593
5594 /* Return true if CODE represents a commutative tree code.  Otherwise
5595    return false.  */
5596 bool
5597 commutative_tree_code (enum tree_code code)
5598 {
5599   switch (code)
5600     {
5601     case PLUS_EXPR:
5602     case MULT_EXPR:
5603     case MIN_EXPR:
5604     case MAX_EXPR:
5605     case BIT_IOR_EXPR:
5606     case BIT_XOR_EXPR:
5607     case BIT_AND_EXPR:
5608     case NE_EXPR:
5609     case EQ_EXPR:
5610     case UNORDERED_EXPR:
5611     case ORDERED_EXPR:
5612     case UNEQ_EXPR:
5613     case LTGT_EXPR:
5614     case TRUTH_AND_EXPR:
5615     case TRUTH_XOR_EXPR:
5616     case TRUTH_OR_EXPR:
5617       return true;
5618
5619     default:
5620       break;
5621     }
5622   return false;
5623 }
5624
5625 /* Generate a hash value for an expression.  This can be used iteratively
5626    by passing a previous result as the VAL argument.
5627
5628    This function is intended to produce the same hash for expressions which
5629    would compare equal using operand_equal_p.  */
5630
5631 hashval_t
5632 iterative_hash_expr (const_tree t, hashval_t val)
5633 {
5634   int i;
5635   enum tree_code code;
5636   char tclass;
5637
5638   if (t == NULL_TREE)
5639     return iterative_hash_hashval_t (0, val);
5640
5641   code = TREE_CODE (t);
5642
5643   switch (code)
5644     {
5645     /* Alas, constants aren't shared, so we can't rely on pointer
5646        identity.  */
5647     case INTEGER_CST:
5648       val = iterative_hash_host_wide_int (TREE_INT_CST_LOW (t), val);
5649       return iterative_hash_host_wide_int (TREE_INT_CST_HIGH (t), val);
5650     case REAL_CST:
5651       {
5652         unsigned int val2 = real_hash (TREE_REAL_CST_PTR (t));
5653
5654         return iterative_hash_hashval_t (val2, val);
5655       }
5656     case FIXED_CST:
5657       {
5658         unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
5659
5660         return iterative_hash_hashval_t (val2, val);
5661       }
5662     case STRING_CST:
5663       return iterative_hash (TREE_STRING_POINTER (t),
5664                              TREE_STRING_LENGTH (t), val);
5665     case COMPLEX_CST:
5666       val = iterative_hash_expr (TREE_REALPART (t), val);
5667       return iterative_hash_expr (TREE_IMAGPART (t), val);
5668     case VECTOR_CST:
5669       return iterative_hash_expr (TREE_VECTOR_CST_ELTS (t), val);
5670
5671     case SSA_NAME:
5672       /* we can just compare by pointer.  */
5673       return iterative_hash_host_wide_int (SSA_NAME_VERSION (t), val);
5674
5675     case TREE_LIST:
5676       /* A list of expressions, for a CALL_EXPR or as the elements of a
5677          VECTOR_CST.  */
5678       for (; t; t = TREE_CHAIN (t))
5679         val = iterative_hash_expr (TREE_VALUE (t), val);
5680       return val;
5681     case CONSTRUCTOR:
5682       {
5683         unsigned HOST_WIDE_INT idx;
5684         tree field, value;
5685         FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
5686           {
5687             val = iterative_hash_expr (field, val);
5688             val = iterative_hash_expr (value, val);
5689           }
5690         return val;
5691       }
5692     case FUNCTION_DECL:
5693       /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
5694          Otherwise nodes that compare equal according to operand_equal_p might
5695          get different hash codes.  However, don't do this for machine specific
5696          or front end builtins, since the function code is overloaded in those
5697          cases.  */
5698       if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
5699           && built_in_decls[DECL_FUNCTION_CODE (t)])
5700         {
5701           t = built_in_decls[DECL_FUNCTION_CODE (t)];
5702           code = TREE_CODE (t);
5703         }
5704       /* FALL THROUGH */
5705     default:
5706       tclass = TREE_CODE_CLASS (code);
5707
5708       if (tclass == tcc_declaration)
5709         {
5710           /* DECL's have a unique ID */
5711           val = iterative_hash_host_wide_int (DECL_UID (t), val);
5712         }
5713       else
5714         {
5715           gcc_assert (IS_EXPR_CODE_CLASS (tclass));
5716           
5717           val = iterative_hash_object (code, val);
5718
5719           /* Don't hash the type, that can lead to having nodes which
5720              compare equal according to operand_equal_p, but which
5721              have different hash codes.  */
5722           if (CONVERT_EXPR_CODE_P (code)
5723               || code == NON_LVALUE_EXPR)
5724             {
5725               /* Make sure to include signness in the hash computation.  */
5726               val += TYPE_UNSIGNED (TREE_TYPE (t));
5727               val = iterative_hash_expr (TREE_OPERAND (t, 0), val);
5728             }
5729
5730           else if (commutative_tree_code (code))
5731             {
5732               /* It's a commutative expression.  We want to hash it the same
5733                  however it appears.  We do this by first hashing both operands
5734                  and then rehashing based on the order of their independent
5735                  hashes.  */
5736               hashval_t one = iterative_hash_expr (TREE_OPERAND (t, 0), 0);
5737               hashval_t two = iterative_hash_expr (TREE_OPERAND (t, 1), 0);
5738               hashval_t t;
5739
5740               if (one > two)
5741                 t = one, one = two, two = t;
5742
5743               val = iterative_hash_hashval_t (one, val);
5744               val = iterative_hash_hashval_t (two, val);
5745             }
5746           else
5747             for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
5748               val = iterative_hash_expr (TREE_OPERAND (t, i), val);
5749         }
5750       return val;
5751       break;
5752     }
5753 }
5754
5755 /* Generate a hash value for a pair of expressions.  This can be used
5756    iteratively by passing a previous result as the VAL argument.
5757
5758    The same hash value is always returned for a given pair of expressions,
5759    regardless of the order in which they are presented.  This is useful in
5760    hashing the operands of commutative functions.  */
5761
5762 hashval_t
5763 iterative_hash_exprs_commutative (const_tree t1,
5764                                   const_tree t2, hashval_t val)
5765 {
5766   hashval_t one = iterative_hash_expr (t1, 0);
5767   hashval_t two = iterative_hash_expr (t2, 0);
5768   hashval_t t;
5769
5770   if (one > two)
5771     t = one, one = two, two = t;
5772   val = iterative_hash_hashval_t (one, val);
5773   val = iterative_hash_hashval_t (two, val);
5774
5775   return val;
5776 }
5777 \f
5778 /* Constructors for pointer, array and function types.
5779    (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
5780    constructed by language-dependent code, not here.)  */
5781
5782 /* Construct, lay out and return the type of pointers to TO_TYPE with
5783    mode MODE.  If CAN_ALIAS_ALL is TRUE, indicate this type can
5784    reference all of memory. If such a type has already been
5785    constructed, reuse it.  */
5786
5787 tree
5788 build_pointer_type_for_mode (tree to_type, enum machine_mode mode,
5789                              bool can_alias_all)
5790 {
5791   tree t;
5792
5793   if (to_type == error_mark_node)
5794     return error_mark_node;
5795
5796   /* If the pointed-to type has the may_alias attribute set, force
5797      a TYPE_REF_CAN_ALIAS_ALL pointer to be generated.  */
5798   if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
5799     can_alias_all = true;
5800
5801   /* In some cases, languages will have things that aren't a POINTER_TYPE
5802      (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
5803      In that case, return that type without regard to the rest of our
5804      operands.
5805
5806      ??? This is a kludge, but consistent with the way this function has
5807      always operated and there doesn't seem to be a good way to avoid this
5808      at the moment.  */
5809   if (TYPE_POINTER_TO (to_type) != 0
5810       && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
5811     return TYPE_POINTER_TO (to_type);
5812
5813   /* First, if we already have a type for pointers to TO_TYPE and it's
5814      the proper mode, use it.  */
5815   for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
5816     if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
5817       return t;
5818
5819   t = make_node (POINTER_TYPE);
5820
5821   TREE_TYPE (t) = to_type;
5822   SET_TYPE_MODE (t, mode);
5823   TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
5824   TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
5825   TYPE_POINTER_TO (to_type) = t;
5826
5827   if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
5828     SET_TYPE_STRUCTURAL_EQUALITY (t);
5829   else if (TYPE_CANONICAL (to_type) != to_type)
5830     TYPE_CANONICAL (t)
5831       = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
5832                                      mode, can_alias_all);
5833
5834   /* Lay out the type.  This function has many callers that are concerned
5835      with expression-construction, and this simplifies them all.  */
5836   layout_type (t);
5837
5838   return t;
5839 }
5840
5841 /* By default build pointers in ptr_mode.  */
5842
5843 tree
5844 build_pointer_type (tree to_type)
5845 {
5846   return build_pointer_type_for_mode (to_type, ptr_mode, false);
5847 }
5848
5849 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE.  */
5850
5851 tree
5852 build_reference_type_for_mode (tree to_type, enum machine_mode mode,
5853                                bool can_alias_all)
5854 {
5855   tree t;
5856
5857   if (to_type == error_mark_node)
5858     return error_mark_node;
5859
5860   /* If the pointed-to type has the may_alias attribute set, force
5861      a TYPE_REF_CAN_ALIAS_ALL pointer to be generated.  */
5862   if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
5863     can_alias_all = true;
5864
5865   /* In some cases, languages will have things that aren't a REFERENCE_TYPE
5866      (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
5867      In that case, return that type without regard to the rest of our
5868      operands.
5869
5870      ??? This is a kludge, but consistent with the way this function has
5871      always operated and there doesn't seem to be a good way to avoid this
5872      at the moment.  */
5873   if (TYPE_REFERENCE_TO (to_type) != 0
5874       && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
5875     return TYPE_REFERENCE_TO (to_type);
5876
5877   /* First, if we already have a type for pointers to TO_TYPE and it's
5878      the proper mode, use it.  */
5879   for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
5880     if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
5881       return t;
5882
5883   t = make_node (REFERENCE_TYPE);
5884
5885   TREE_TYPE (t) = to_type;
5886   SET_TYPE_MODE (t, mode);
5887   TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
5888   TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
5889   TYPE_REFERENCE_TO (to_type) = t;
5890
5891   if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
5892     SET_TYPE_STRUCTURAL_EQUALITY (t);
5893   else if (TYPE_CANONICAL (to_type) != to_type)
5894     TYPE_CANONICAL (t) 
5895       = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
5896                                        mode, can_alias_all);
5897
5898   layout_type (t);
5899
5900   return t;
5901 }
5902
5903
5904 /* Build the node for the type of references-to-TO_TYPE by default
5905    in ptr_mode.  */
5906
5907 tree
5908 build_reference_type (tree to_type)
5909 {
5910   return build_reference_type_for_mode (to_type, ptr_mode, false);
5911 }
5912
5913 /* Build a type that is compatible with t but has no cv quals anywhere
5914    in its type, thus
5915
5916    const char *const *const *  ->  char ***.  */
5917
5918 tree
5919 build_type_no_quals (tree t)
5920 {
5921   switch (TREE_CODE (t))
5922     {
5923     case POINTER_TYPE:
5924       return build_pointer_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
5925                                           TYPE_MODE (t),
5926                                           TYPE_REF_CAN_ALIAS_ALL (t));
5927     case REFERENCE_TYPE:
5928       return
5929         build_reference_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
5930                                        TYPE_MODE (t),
5931                                        TYPE_REF_CAN_ALIAS_ALL (t));
5932     default:
5933       return TYPE_MAIN_VARIANT (t);
5934     }
5935 }
5936
5937 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
5938    MAXVAL should be the maximum value in the domain
5939    (one less than the length of the array).
5940
5941    The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
5942    We don't enforce this limit, that is up to caller (e.g. language front end).
5943    The limit exists because the result is a signed type and we don't handle
5944    sizes that use more than one HOST_WIDE_INT.  */
5945
5946 tree
5947 build_index_type (tree maxval)
5948 {
5949   tree itype = make_node (INTEGER_TYPE);
5950
5951   TREE_TYPE (itype) = sizetype;
5952   TYPE_PRECISION (itype) = TYPE_PRECISION (sizetype);
5953   TYPE_MIN_VALUE (itype) = size_zero_node;
5954   TYPE_MAX_VALUE (itype) = fold_convert (sizetype, maxval);
5955   SET_TYPE_MODE (itype, TYPE_MODE (sizetype));
5956   TYPE_SIZE (itype) = TYPE_SIZE (sizetype);
5957   TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (sizetype);
5958   TYPE_ALIGN (itype) = TYPE_ALIGN (sizetype);
5959   TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (sizetype);
5960
5961   if (host_integerp (maxval, 1))
5962     return type_hash_canon (tree_low_cst (maxval, 1), itype);
5963   else
5964     {
5965       /* Since we cannot hash this type, we need to compare it using
5966          structural equality checks. */
5967       SET_TYPE_STRUCTURAL_EQUALITY (itype);
5968       return itype;
5969     }
5970 }
5971
5972 /* Builds a signed or unsigned integer type of precision PRECISION.
5973    Used for C bitfields whose precision does not match that of
5974    built-in target types.  */
5975 tree
5976 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
5977                                 int unsignedp)
5978 {
5979   tree itype = make_node (INTEGER_TYPE);
5980
5981   TYPE_PRECISION (itype) = precision;
5982
5983   if (unsignedp)
5984     fixup_unsigned_type (itype);
5985   else
5986     fixup_signed_type (itype);
5987
5988   if (host_integerp (TYPE_MAX_VALUE (itype), 1))
5989     return type_hash_canon (tree_low_cst (TYPE_MAX_VALUE (itype), 1), itype);
5990
5991   return itype;
5992 }
5993
5994 /* Create a range of some discrete type TYPE (an INTEGER_TYPE,
5995    ENUMERAL_TYPE or BOOLEAN_TYPE), with low bound LOWVAL and
5996    high bound HIGHVAL.  If TYPE is NULL, sizetype is used.  */
5997
5998 tree
5999 build_range_type (tree type, tree lowval, tree highval)
6000 {
6001   tree itype = make_node (INTEGER_TYPE);
6002
6003   TREE_TYPE (itype) = type;
6004   if (type == NULL_TREE)
6005     type = sizetype;
6006
6007   TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
6008   TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
6009
6010   TYPE_PRECISION (itype) = TYPE_PRECISION (type);
6011   SET_TYPE_MODE (itype, TYPE_MODE (type));
6012   TYPE_SIZE (itype) = TYPE_SIZE (type);
6013   TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
6014   TYPE_ALIGN (itype) = TYPE_ALIGN (type);
6015   TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
6016
6017   if (host_integerp (lowval, 0) && highval != 0 && host_integerp (highval, 0))
6018     return type_hash_canon (tree_low_cst (highval, 0)
6019                             - tree_low_cst (lowval, 0),
6020                             itype);
6021   else
6022     return itype;
6023 }
6024
6025 /* Return true if the debug information for TYPE, a subtype, should be emitted
6026    as a subrange type.  If so, set LOWVAL to the low bound and HIGHVAL to the
6027    high bound, respectively.  Sometimes doing so unnecessarily obfuscates the
6028    debug info and doesn't reflect the source code.  */
6029
6030 bool
6031 subrange_type_for_debug_p (const_tree type, tree *lowval, tree *highval)
6032 {
6033   tree base_type = TREE_TYPE (type), low, high;
6034
6035   /* Subrange types have a base type which is an integral type.  */
6036   if (!INTEGRAL_TYPE_P (base_type))
6037     return false;
6038
6039   /* Get the real bounds of the subtype.  */
6040   if (lang_hooks.types.get_subrange_bounds)
6041     lang_hooks.types.get_subrange_bounds (type, &low, &high);
6042   else
6043     {
6044       low = TYPE_MIN_VALUE (type);
6045       high = TYPE_MAX_VALUE (type);
6046     }
6047
6048   /* If the type and its base type have the same representation and the same
6049      name, then the type is not a subrange but a copy of the base type.  */
6050   if ((TREE_CODE (base_type) == INTEGER_TYPE
6051        || TREE_CODE (base_type) == BOOLEAN_TYPE)
6052       && int_size_in_bytes (type) == int_size_in_bytes (base_type)
6053       && tree_int_cst_equal (low, TYPE_MIN_VALUE (base_type))
6054       && tree_int_cst_equal (high, TYPE_MAX_VALUE (base_type)))
6055     {
6056       tree type_name = TYPE_NAME (type);
6057       tree base_type_name = TYPE_NAME (base_type);
6058
6059       if (type_name && TREE_CODE (type_name) == TYPE_DECL)
6060         type_name = DECL_NAME (type_name);
6061
6062       if (base_type_name && TREE_CODE (base_type_name) == TYPE_DECL)
6063         base_type_name = DECL_NAME (base_type_name);
6064
6065       if (type_name == base_type_name)
6066         return false;
6067     }
6068
6069   if (lowval)
6070     *lowval = low;
6071   if (highval)
6072     *highval = high;
6073   return true;
6074 }
6075
6076 /* Just like build_index_type, but takes lowval and highval instead
6077    of just highval (maxval).  */
6078
6079 tree
6080 build_index_2_type (tree lowval, tree highval)
6081 {
6082   return build_range_type (sizetype, lowval, highval);
6083 }
6084
6085 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
6086    and number of elements specified by the range of values of INDEX_TYPE.
6087    If such a type has already been constructed, reuse it.  */
6088
6089 tree
6090 build_array_type (tree elt_type, tree index_type)
6091 {
6092   tree t;
6093   hashval_t hashcode = 0;
6094
6095   if (TREE_CODE (elt_type) == FUNCTION_TYPE)
6096     {
6097       error ("arrays of functions are not meaningful");
6098       elt_type = integer_type_node;
6099     }
6100
6101   t = make_node (ARRAY_TYPE);
6102   TREE_TYPE (t) = elt_type;
6103   TYPE_DOMAIN (t) = index_type;
6104   
6105   if (index_type == 0)
6106     {
6107       tree save = t;
6108       hashcode = iterative_hash_object (TYPE_HASH (elt_type), hashcode);
6109       t = type_hash_canon (hashcode, t);
6110       if (save == t)
6111         layout_type (t);
6112
6113       if (TYPE_CANONICAL (t) == t)
6114         {
6115           if (TYPE_STRUCTURAL_EQUALITY_P (elt_type))
6116             SET_TYPE_STRUCTURAL_EQUALITY (t);
6117           else if (TYPE_CANONICAL (elt_type) != elt_type)
6118             TYPE_CANONICAL (t) 
6119               = build_array_type (TYPE_CANONICAL (elt_type), index_type);
6120         }
6121
6122       return t;
6123     }
6124
6125   hashcode = iterative_hash_object (TYPE_HASH (elt_type), hashcode);
6126   hashcode = iterative_hash_object (TYPE_HASH (index_type), hashcode);
6127   t = type_hash_canon (hashcode, t);
6128
6129   if (!COMPLETE_TYPE_P (t))
6130     layout_type (t);
6131
6132   if (TYPE_CANONICAL (t) == t)
6133     {
6134       if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
6135           || TYPE_STRUCTURAL_EQUALITY_P (index_type))
6136         SET_TYPE_STRUCTURAL_EQUALITY (t);
6137       else if (TYPE_CANONICAL (elt_type) != elt_type
6138                || TYPE_CANONICAL (index_type) != index_type)
6139         TYPE_CANONICAL (t) 
6140           = build_array_type (TYPE_CANONICAL (elt_type),
6141                               TYPE_CANONICAL (index_type));
6142     }
6143
6144   return t;
6145 }
6146
6147 /* Recursively examines the array elements of TYPE, until a non-array
6148    element type is found.  */
6149
6150 tree
6151 strip_array_types (tree type)
6152 {
6153   while (TREE_CODE (type) == ARRAY_TYPE)
6154     type = TREE_TYPE (type);
6155
6156   return type;
6157 }
6158
6159 /* Computes the canonical argument types from the argument type list
6160    ARGTYPES. 
6161
6162    Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
6163    on entry to this function, or if any of the ARGTYPES are
6164    structural.
6165
6166    Upon return, *ANY_NONCANONICAL_P will be true iff either it was
6167    true on entry to this function, or if any of the ARGTYPES are
6168    non-canonical.
6169
6170    Returns a canonical argument list, which may be ARGTYPES when the
6171    canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
6172    true) or would not differ from ARGTYPES.  */
6173
6174 static tree 
6175 maybe_canonicalize_argtypes(tree argtypes, 
6176                             bool *any_structural_p,
6177                             bool *any_noncanonical_p)
6178 {
6179   tree arg;
6180   bool any_noncanonical_argtypes_p = false;
6181   
6182   for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
6183     {
6184       if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
6185         /* Fail gracefully by stating that the type is structural.  */
6186         *any_structural_p = true;
6187       else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
6188         *any_structural_p = true;
6189       else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
6190                || TREE_PURPOSE (arg))
6191         /* If the argument has a default argument, we consider it
6192            non-canonical even though the type itself is canonical.
6193            That way, different variants of function and method types
6194            with default arguments will all point to the variant with
6195            no defaults as their canonical type.  */
6196         any_noncanonical_argtypes_p = true;
6197     }
6198
6199   if (*any_structural_p)
6200     return argtypes;
6201
6202   if (any_noncanonical_argtypes_p)
6203     {
6204       /* Build the canonical list of argument types.  */
6205       tree canon_argtypes = NULL_TREE;
6206       bool is_void = false;
6207
6208       for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
6209         {
6210           if (arg == void_list_node)
6211             is_void = true;
6212           else
6213             canon_argtypes = tree_cons (NULL_TREE,
6214                                         TYPE_CANONICAL (TREE_VALUE (arg)),
6215                                         canon_argtypes);
6216         }
6217
6218       canon_argtypes = nreverse (canon_argtypes);
6219       if (is_void)
6220         canon_argtypes = chainon (canon_argtypes, void_list_node);
6221
6222       /* There is a non-canonical type.  */
6223       *any_noncanonical_p = true;
6224       return canon_argtypes;
6225     }
6226
6227   /* The canonical argument types are the same as ARGTYPES.  */
6228   return argtypes;
6229 }
6230
6231 /* Construct, lay out and return
6232    the type of functions returning type VALUE_TYPE
6233    given arguments of types ARG_TYPES.
6234    ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
6235    are data type nodes for the arguments of the function.
6236    If such a type has already been constructed, reuse it.  */
6237
6238 tree
6239 build_function_type (tree value_type, tree arg_types)
6240 {
6241   tree t;
6242   hashval_t hashcode = 0;
6243   bool any_structural_p, any_noncanonical_p;
6244   tree canon_argtypes;
6245
6246   if (TREE_CODE (value_type) == FUNCTION_TYPE)
6247     {
6248       error ("function return type cannot be function");
6249       value_type = integer_type_node;
6250     }
6251
6252   /* Make a node of the sort we want.  */
6253   t = make_node (FUNCTION_TYPE);
6254   TREE_TYPE (t) = value_type;
6255   TYPE_ARG_TYPES (t) = arg_types;
6256
6257   /* If we already have such a type, use the old one.  */
6258   hashcode = iterative_hash_object (TYPE_HASH (value_type), hashcode);
6259   hashcode = type_hash_list (arg_types, hashcode);
6260   t = type_hash_canon (hashcode, t);
6261
6262   /* Set up the canonical type. */
6263   any_structural_p   = TYPE_STRUCTURAL_EQUALITY_P (value_type);
6264   any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
6265   canon_argtypes = maybe_canonicalize_argtypes (arg_types, 
6266                                                 &any_structural_p,
6267                                                 &any_noncanonical_p);
6268   if (any_structural_p)
6269     SET_TYPE_STRUCTURAL_EQUALITY (t);
6270   else if (any_noncanonical_p)
6271     TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
6272                                               canon_argtypes);
6273       
6274   if (!COMPLETE_TYPE_P (t))
6275     layout_type (t);
6276   return t;
6277 }
6278
6279 /* Build variant of function type ORIG_TYPE skipping ARGS_TO_SKIP.  */
6280
6281 tree
6282 build_function_type_skip_args (tree orig_type, bitmap args_to_skip)
6283 {
6284   tree new_type = NULL;
6285   tree args, new_args = NULL, t;
6286   tree new_reversed;
6287   int i = 0;
6288
6289   for (args = TYPE_ARG_TYPES (orig_type); args && args != void_list_node;
6290        args = TREE_CHAIN (args), i++)
6291     if (!bitmap_bit_p (args_to_skip, i))
6292       new_args = tree_cons (NULL_TREE, TREE_VALUE (args), new_args);
6293
6294   new_reversed = nreverse (new_args);
6295   if (args)
6296     {
6297       if (new_reversed)
6298         TREE_CHAIN (new_args) = void_list_node;
6299       else
6300         new_reversed = void_list_node;
6301     }
6302     gcc_assert (new_reversed);
6303
6304   /* Use copy_node to preserve as much as possible from original type
6305      (debug info, attribute lists etc.)
6306      Exception is METHOD_TYPEs must have THIS argument.
6307      When we are asked to remove it, we need to build new FUNCTION_TYPE
6308      instead.  */
6309   if (TREE_CODE (orig_type) != METHOD_TYPE
6310       || !bitmap_bit_p (args_to_skip, 0))
6311     {
6312       new_type = copy_node (orig_type);
6313       TYPE_ARG_TYPES (new_type) = new_reversed;
6314     }
6315   else
6316     {
6317       new_type
6318         = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type),
6319                                                          new_reversed));
6320       TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
6321     }
6322
6323   /* This is a new type, not a copy of an old type.  Need to reassociate
6324      variants.  We can handle everything except the main variant lazily.  */
6325   t = TYPE_MAIN_VARIANT (orig_type);
6326   if (orig_type != t)
6327     {
6328       TYPE_MAIN_VARIANT (new_type) = t;
6329       TYPE_NEXT_VARIANT (new_type) = TYPE_NEXT_VARIANT (t);
6330       TYPE_NEXT_VARIANT (t) = new_type;
6331     }
6332   else
6333     {
6334       TYPE_MAIN_VARIANT (new_type) = new_type;
6335       TYPE_NEXT_VARIANT (new_type) = NULL;
6336     }
6337   return new_type;
6338 }
6339
6340 /* Build variant of function type ORIG_TYPE skipping ARGS_TO_SKIP.  
6341   
6342    Arguments from DECL_ARGUMENTS list can't be removed now, since they are
6343    linked by TREE_CHAIN directly.  It is caller responsibility to eliminate
6344    them when they are being duplicated (i.e. copy_arguments_for_versioning).  */
6345
6346 tree
6347 build_function_decl_skip_args (tree orig_decl, bitmap args_to_skip)
6348 {
6349   tree new_decl = copy_node (orig_decl);
6350   tree new_type;
6351
6352   new_type = TREE_TYPE (orig_decl);
6353   if (prototype_p (new_type))
6354     new_type = build_function_type_skip_args (new_type, args_to_skip);
6355   TREE_TYPE (new_decl) = new_type;
6356
6357   /* For declarations setting DECL_VINDEX (i.e. methods)
6358      we expect first argument to be THIS pointer.   */
6359   if (bitmap_bit_p (args_to_skip, 0))
6360     DECL_VINDEX (new_decl) = NULL_TREE;
6361   return new_decl;
6362 }
6363
6364 /* Build a function type.  The RETURN_TYPE is the type returned by the
6365    function. If VAARGS is set, no void_type_node is appended to the
6366    the list. ARGP muse be alway be terminated be a NULL_TREE.  */
6367
6368 static tree
6369 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
6370 {
6371   tree t, args, last;
6372
6373   t = va_arg (argp, tree);
6374   for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
6375     args = tree_cons (NULL_TREE, t, args);
6376
6377   if (vaargs)
6378     {
6379       last = args;
6380       if (args != NULL_TREE)
6381         args = nreverse (args);
6382       gcc_assert (args != NULL_TREE && last != void_list_node);
6383     }
6384   else if (args == NULL_TREE)
6385     args = void_list_node;
6386   else
6387     {
6388       last = args;
6389       args = nreverse (args);
6390       TREE_CHAIN (last) = void_list_node;
6391     }
6392   args = build_function_type (return_type, args);
6393
6394   return args;
6395 }
6396
6397 /* Build a function type.  The RETURN_TYPE is the type returned by the
6398    function.  If additional arguments are provided, they are
6399    additional argument types.  The list of argument types must always
6400    be terminated by NULL_TREE.  */
6401
6402 tree
6403 build_function_type_list (tree return_type, ...)
6404 {
6405   tree args;
6406   va_list p;
6407
6408   va_start (p, return_type);
6409   args = build_function_type_list_1 (false, return_type, p);
6410   va_end (p);
6411   return args;
6412 }
6413
6414 /* Build a variable argument function type.  The RETURN_TYPE is the
6415    type returned by the function.  If additional arguments are provided,
6416    they are additional argument types.  The list of argument types must
6417    always be terminated by NULL_TREE.  */
6418
6419 tree
6420 build_varargs_function_type_list (tree return_type, ...)
6421 {
6422   tree args;
6423   va_list p;
6424
6425   va_start (p, return_type);
6426   args = build_function_type_list_1 (true, return_type, p);
6427   va_end (p);
6428
6429   return args;
6430 }
6431
6432 /* Build a METHOD_TYPE for a member of BASETYPE.  The RETTYPE (a TYPE)
6433    and ARGTYPES (a TREE_LIST) are the return type and arguments types
6434    for the method.  An implicit additional parameter (of type
6435    pointer-to-BASETYPE) is added to the ARGTYPES.  */
6436
6437 tree
6438 build_method_type_directly (tree basetype,
6439                             tree rettype,
6440                             tree argtypes)
6441 {
6442   tree t;
6443   tree ptype;
6444   int hashcode = 0;
6445   bool any_structural_p, any_noncanonical_p;
6446   tree canon_argtypes;
6447
6448   /* Make a node of the sort we want.  */
6449   t = make_node (METHOD_TYPE);
6450
6451   TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
6452   TREE_TYPE (t) = rettype;
6453   ptype = build_pointer_type (basetype);
6454
6455   /* The actual arglist for this function includes a "hidden" argument
6456      which is "this".  Put it into the list of argument types.  */
6457   argtypes = tree_cons (NULL_TREE, ptype, argtypes);
6458   TYPE_ARG_TYPES (t) = argtypes;
6459
6460   /* If we already have such a type, use the old one.  */
6461   hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
6462   hashcode = iterative_hash_object (TYPE_HASH (rettype), hashcode);
6463   hashcode = type_hash_list (argtypes, hashcode);
6464   t = type_hash_canon (hashcode, t);
6465
6466   /* Set up the canonical type. */
6467   any_structural_p
6468     = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
6469        || TYPE_STRUCTURAL_EQUALITY_P (rettype));
6470   any_noncanonical_p
6471     = (TYPE_CANONICAL (basetype) != basetype
6472        || TYPE_CANONICAL (rettype) != rettype);
6473   canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
6474                                                 &any_structural_p,
6475                                                 &any_noncanonical_p);
6476   if (any_structural_p)
6477     SET_TYPE_STRUCTURAL_EQUALITY (t);
6478   else if (any_noncanonical_p)
6479     TYPE_CANONICAL (t) 
6480       = build_method_type_directly (TYPE_CANONICAL (basetype),
6481                                     TYPE_CANONICAL (rettype),
6482                                     canon_argtypes);
6483   if (!COMPLETE_TYPE_P (t))
6484     layout_type (t);
6485
6486   return t;
6487 }
6488
6489 /* Construct, lay out and return the type of methods belonging to class
6490    BASETYPE and whose arguments and values are described by TYPE.
6491    If that type exists already, reuse it.
6492    TYPE must be a FUNCTION_TYPE node.  */
6493
6494 tree
6495 build_method_type (tree basetype, tree type)
6496 {
6497   gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
6498
6499   return build_method_type_directly (basetype,
6500                                      TREE_TYPE (type),
6501                                      TYPE_ARG_TYPES (type));
6502 }
6503
6504 /* Construct, lay out and return the type of offsets to a value
6505    of type TYPE, within an object of type BASETYPE.
6506    If a suitable offset type exists already, reuse it.  */
6507
6508 tree
6509 build_offset_type (tree basetype, tree type)
6510 {
6511   tree t;
6512   hashval_t hashcode = 0;
6513
6514   /* Make a node of the sort we want.  */
6515   t = make_node (OFFSET_TYPE);
6516
6517   TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
6518   TREE_TYPE (t) = type;
6519
6520   /* If we already have such a type, use the old one.  */
6521   hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
6522   hashcode = iterative_hash_object (TYPE_HASH (type), hashcode);
6523   t = type_hash_canon (hashcode, t);
6524
6525   if (!COMPLETE_TYPE_P (t))
6526     layout_type (t);
6527
6528   if (TYPE_CANONICAL (t) == t)
6529     {
6530       if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
6531           || TYPE_STRUCTURAL_EQUALITY_P (type))
6532         SET_TYPE_STRUCTURAL_EQUALITY (t);
6533       else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
6534                || TYPE_CANONICAL (type) != type)
6535         TYPE_CANONICAL (t) 
6536           = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
6537                                TYPE_CANONICAL (type));
6538     }
6539
6540   return t;
6541 }
6542
6543 /* Create a complex type whose components are COMPONENT_TYPE.  */
6544
6545 tree
6546 build_complex_type (tree component_type)
6547 {
6548   tree t;
6549   hashval_t hashcode;
6550
6551   gcc_assert (INTEGRAL_TYPE_P (component_type)
6552               || SCALAR_FLOAT_TYPE_P (component_type)
6553               || FIXED_POINT_TYPE_P (component_type));
6554
6555   /* Make a node of the sort we want.  */
6556   t = make_node (COMPLEX_TYPE);
6557
6558   TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
6559
6560   /* If we already have such a type, use the old one.  */
6561   hashcode = iterative_hash_object (TYPE_HASH (component_type), 0);
6562   t = type_hash_canon (hashcode, t);
6563
6564   if (!COMPLETE_TYPE_P (t))
6565     layout_type (t);
6566
6567   if (TYPE_CANONICAL (t) == t)
6568     {
6569       if (TYPE_STRUCTURAL_EQUALITY_P (component_type))
6570         SET_TYPE_STRUCTURAL_EQUALITY (t);
6571       else if (TYPE_CANONICAL (component_type) != component_type)
6572         TYPE_CANONICAL (t) 
6573           = build_complex_type (TYPE_CANONICAL (component_type));
6574     }
6575
6576   /* We need to create a name, since complex is a fundamental type.  */
6577   if (! TYPE_NAME (t))
6578     {
6579       const char *name;
6580       if (component_type == char_type_node)
6581         name = "complex char";
6582       else if (component_type == signed_char_type_node)
6583         name = "complex signed char";
6584       else if (component_type == unsigned_char_type_node)
6585         name = "complex unsigned char";
6586       else if (component_type == short_integer_type_node)
6587         name = "complex short int";
6588       else if (component_type == short_unsigned_type_node)
6589         name = "complex short unsigned int";
6590       else if (component_type == integer_type_node)
6591         name = "complex int";
6592       else if (component_type == unsigned_type_node)
6593         name = "complex unsigned int";
6594       else if (component_type == long_integer_type_node)
6595         name = "complex long int";
6596       else if (component_type == long_unsigned_type_node)
6597         name = "complex long unsigned int";
6598       else if (component_type == long_long_integer_type_node)
6599         name = "complex long long int";
6600       else if (component_type == long_long_unsigned_type_node)
6601         name = "complex long long unsigned int";
6602       else
6603         name = 0;
6604
6605       if (name != 0)
6606         TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
6607                                     get_identifier (name), t);
6608     }
6609
6610   return build_qualified_type (t, TYPE_QUALS (component_type));
6611 }
6612
6613 /* If TYPE is a real or complex floating-point type and the target
6614    does not directly support arithmetic on TYPE then return the wider
6615    type to be used for arithmetic on TYPE.  Otherwise, return
6616    NULL_TREE.  */
6617
6618 tree
6619 excess_precision_type (tree type)
6620 {
6621   if (flag_excess_precision != EXCESS_PRECISION_FAST)
6622     {
6623       int flt_eval_method = TARGET_FLT_EVAL_METHOD;
6624       switch (TREE_CODE (type))
6625         {
6626         case REAL_TYPE:
6627           switch (flt_eval_method)
6628             {
6629             case 1:
6630               if (TYPE_MODE (type) == TYPE_MODE (float_type_node))
6631                 return double_type_node;
6632               break;
6633             case 2:
6634               if (TYPE_MODE (type) == TYPE_MODE (float_type_node)
6635                   || TYPE_MODE (type) == TYPE_MODE (double_type_node))
6636                 return long_double_type_node;
6637               break;
6638             default:
6639               gcc_unreachable ();
6640             }
6641           break;
6642         case COMPLEX_TYPE:
6643           if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
6644             return NULL_TREE;
6645           switch (flt_eval_method)
6646             {
6647             case 1:
6648               if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node))
6649                 return complex_double_type_node;
6650               break;
6651             case 2:
6652               if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node)
6653                   || (TYPE_MODE (TREE_TYPE (type))
6654                       == TYPE_MODE (double_type_node)))
6655                 return complex_long_double_type_node;
6656               break;
6657             default:
6658               gcc_unreachable ();
6659             }
6660           break;
6661         default:
6662           break;
6663         }
6664     }
6665   return NULL_TREE;
6666 }
6667 \f
6668 /* Return OP, stripped of any conversions to wider types as much as is safe.
6669    Converting the value back to OP's type makes a value equivalent to OP.
6670
6671    If FOR_TYPE is nonzero, we return a value which, if converted to
6672    type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
6673
6674    OP must have integer, real or enumeral type.  Pointers are not allowed!
6675
6676    There are some cases where the obvious value we could return
6677    would regenerate to OP if converted to OP's type,
6678    but would not extend like OP to wider types.
6679    If FOR_TYPE indicates such extension is contemplated, we eschew such values.
6680    For example, if OP is (unsigned short)(signed char)-1,
6681    we avoid returning (signed char)-1 if FOR_TYPE is int,
6682    even though extending that to an unsigned short would regenerate OP,
6683    since the result of extending (signed char)-1 to (int)
6684    is different from (int) OP.  */
6685
6686 tree
6687 get_unwidened (tree op, tree for_type)
6688 {
6689   /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension.  */
6690   tree type = TREE_TYPE (op);
6691   unsigned final_prec
6692     = TYPE_PRECISION (for_type != 0 ? for_type : type);
6693   int uns
6694     = (for_type != 0 && for_type != type
6695        && final_prec > TYPE_PRECISION (type)
6696        && TYPE_UNSIGNED (type));
6697   tree win = op;
6698
6699   while (CONVERT_EXPR_P (op))
6700     {
6701       int bitschange;
6702
6703       /* TYPE_PRECISION on vector types has different meaning
6704          (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
6705          so avoid them here.  */
6706       if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
6707         break;
6708
6709       bitschange = TYPE_PRECISION (TREE_TYPE (op))
6710                    - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
6711
6712       /* Truncations are many-one so cannot be removed.
6713          Unless we are later going to truncate down even farther.  */
6714       if (bitschange < 0
6715           && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
6716         break;
6717
6718       /* See what's inside this conversion.  If we decide to strip it,
6719          we will set WIN.  */
6720       op = TREE_OPERAND (op, 0);
6721
6722       /* If we have not stripped any zero-extensions (uns is 0),
6723          we can strip any kind of extension.
6724          If we have previously stripped a zero-extension,
6725          only zero-extensions can safely be stripped.
6726          Any extension can be stripped if the bits it would produce
6727          are all going to be discarded later by truncating to FOR_TYPE.  */
6728
6729       if (bitschange > 0)
6730         {
6731           if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
6732             win = op;
6733           /* TYPE_UNSIGNED says whether this is a zero-extension.
6734              Let's avoid computing it if it does not affect WIN
6735              and if UNS will not be needed again.  */
6736           if ((uns
6737                || CONVERT_EXPR_P (op))
6738               && TYPE_UNSIGNED (TREE_TYPE (op)))
6739             {
6740               uns = 1;
6741               win = op;
6742             }
6743         }
6744     }
6745
6746   return win;
6747 }
6748 \f
6749 /* Return OP or a simpler expression for a narrower value
6750    which can be sign-extended or zero-extended to give back OP.
6751    Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
6752    or 0 if the value should be sign-extended.  */
6753
6754 tree
6755 get_narrower (tree op, int *unsignedp_ptr)
6756 {
6757   int uns = 0;
6758   int first = 1;
6759   tree win = op;
6760   bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
6761
6762   while (TREE_CODE (op) == NOP_EXPR)
6763     {
6764       int bitschange
6765         = (TYPE_PRECISION (TREE_TYPE (op))
6766            - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
6767
6768       /* Truncations are many-one so cannot be removed.  */
6769       if (bitschange < 0)
6770         break;
6771
6772       /* See what's inside this conversion.  If we decide to strip it,
6773          we will set WIN.  */
6774
6775       if (bitschange > 0)
6776         {
6777           op = TREE_OPERAND (op, 0);
6778           /* An extension: the outermost one can be stripped,
6779              but remember whether it is zero or sign extension.  */
6780           if (first)
6781             uns = TYPE_UNSIGNED (TREE_TYPE (op));
6782           /* Otherwise, if a sign extension has been stripped,
6783              only sign extensions can now be stripped;
6784              if a zero extension has been stripped, only zero-extensions.  */
6785           else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
6786             break;
6787           first = 0;
6788         }
6789       else /* bitschange == 0 */
6790         {
6791           /* A change in nominal type can always be stripped, but we must
6792              preserve the unsignedness.  */
6793           if (first)
6794             uns = TYPE_UNSIGNED (TREE_TYPE (op));
6795           first = 0;
6796           op = TREE_OPERAND (op, 0);
6797           /* Keep trying to narrow, but don't assign op to win if it
6798              would turn an integral type into something else.  */
6799           if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
6800             continue;
6801         }
6802
6803       win = op;
6804     }
6805
6806   if (TREE_CODE (op) == COMPONENT_REF
6807       /* Since type_for_size always gives an integer type.  */
6808       && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
6809       && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
6810       /* Ensure field is laid out already.  */
6811       && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
6812       && host_integerp (DECL_SIZE (TREE_OPERAND (op, 1)), 1))
6813     {
6814       unsigned HOST_WIDE_INT innerprec
6815         = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
6816       int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
6817                        || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
6818       tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
6819
6820       /* We can get this structure field in a narrower type that fits it,
6821          but the resulting extension to its nominal type (a fullword type)
6822          must satisfy the same conditions as for other extensions.
6823
6824          Do this only for fields that are aligned (not bit-fields),
6825          because when bit-field insns will be used there is no
6826          advantage in doing this.  */
6827
6828       if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
6829           && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
6830           && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
6831           && type != 0)
6832         {
6833           if (first)
6834             uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
6835           win = fold_convert (type, op);
6836         }
6837     }
6838
6839   *unsignedp_ptr = uns;
6840   return win;
6841 }
6842 \f
6843 /* Nonzero if integer constant C has a value that is permissible
6844    for type TYPE (an INTEGER_TYPE).  */
6845
6846 int
6847 int_fits_type_p (const_tree c, const_tree type)
6848 {
6849   tree type_low_bound, type_high_bound;
6850   bool ok_for_low_bound, ok_for_high_bound, unsc;
6851   double_int dc, dd;
6852
6853   dc = tree_to_double_int (c);
6854   unsc = TYPE_UNSIGNED (TREE_TYPE (c));
6855
6856   if (TREE_CODE (TREE_TYPE (c)) == INTEGER_TYPE
6857       && TYPE_IS_SIZETYPE (TREE_TYPE (c))
6858       && unsc)
6859     /* So c is an unsigned integer whose type is sizetype and type is not.
6860        sizetype'd integers are sign extended even though they are
6861        unsigned. If the integer value fits in the lower end word of c,
6862        and if the higher end word has all its bits set to 1, that
6863        means the higher end bits are set to 1 only for sign extension.
6864        So let's convert c into an equivalent zero extended unsigned
6865        integer.  */
6866     dc = double_int_zext (dc, TYPE_PRECISION (TREE_TYPE (c)));
6867
6868 retry:
6869   type_low_bound = TYPE_MIN_VALUE (type);
6870   type_high_bound = TYPE_MAX_VALUE (type);
6871
6872   /* If at least one bound of the type is a constant integer, we can check
6873      ourselves and maybe make a decision. If no such decision is possible, but
6874      this type is a subtype, try checking against that.  Otherwise, use
6875      fit_double_type, which checks against the precision.
6876
6877      Compute the status for each possibly constant bound, and return if we see
6878      one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
6879      for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
6880      for "constant known to fit".  */
6881
6882   /* Check if c >= type_low_bound.  */
6883   if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
6884     {
6885       dd = tree_to_double_int (type_low_bound);
6886       if (TREE_CODE (type) == INTEGER_TYPE
6887           && TYPE_IS_SIZETYPE (type)
6888           && TYPE_UNSIGNED (type))
6889         dd = double_int_zext (dd, TYPE_PRECISION (type));
6890       if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_low_bound)))
6891         {
6892           int c_neg = (!unsc && double_int_negative_p (dc));
6893           int t_neg = (unsc && double_int_negative_p (dd));
6894
6895           if (c_neg && !t_neg)
6896             return 0;
6897           if ((c_neg || !t_neg) && double_int_ucmp (dc, dd) < 0)
6898             return 0;
6899         }
6900       else if (double_int_cmp (dc, dd, unsc) < 0)
6901         return 0;
6902       ok_for_low_bound = true;
6903     }
6904   else
6905     ok_for_low_bound = false;
6906
6907   /* Check if c <= type_high_bound.  */
6908   if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
6909     {
6910       dd = tree_to_double_int (type_high_bound);
6911       if (TREE_CODE (type) == INTEGER_TYPE
6912           && TYPE_IS_SIZETYPE (type)
6913           && TYPE_UNSIGNED (type))
6914         dd = double_int_zext (dd, TYPE_PRECISION (type));
6915       if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_high_bound)))
6916         {
6917           int c_neg = (!unsc && double_int_negative_p (dc));
6918           int t_neg = (unsc && double_int_negative_p (dd));
6919
6920           if (t_neg && !c_neg)
6921             return 0;
6922           if ((t_neg || !c_neg) && double_int_ucmp (dc, dd) > 0)
6923             return 0;
6924         }
6925       else if (double_int_cmp (dc, dd, unsc) > 0)
6926         return 0;
6927       ok_for_high_bound = true;
6928     }
6929   else
6930     ok_for_high_bound = false;
6931
6932   /* If the constant fits both bounds, the result is known.  */
6933   if (ok_for_low_bound && ok_for_high_bound)
6934     return 1;
6935
6936   /* Perform some generic filtering which may allow making a decision
6937      even if the bounds are not constant.  First, negative integers
6938      never fit in unsigned types, */
6939   if (TYPE_UNSIGNED (type) && !unsc && double_int_negative_p (dc))
6940     return 0;
6941
6942   /* Second, narrower types always fit in wider ones.  */
6943   if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
6944     return 1;
6945
6946   /* Third, unsigned integers with top bit set never fit signed types.  */
6947   if (! TYPE_UNSIGNED (type) && unsc)
6948     {
6949       int prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (c))) - 1;
6950       if (prec < HOST_BITS_PER_WIDE_INT)
6951         {
6952           if (((((unsigned HOST_WIDE_INT) 1) << prec) & dc.low) != 0)
6953             return 0;
6954         }
6955       else if (((((unsigned HOST_WIDE_INT) 1)
6956                  << (prec - HOST_BITS_PER_WIDE_INT)) & dc.high) != 0)
6957         return 0;
6958     }
6959
6960   /* If we haven't been able to decide at this point, there nothing more we
6961      can check ourselves here.  Look at the base type if we have one and it
6962      has the same precision.  */
6963   if (TREE_CODE (type) == INTEGER_TYPE
6964       && TREE_TYPE (type) != 0
6965       && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
6966     {
6967       type = TREE_TYPE (type);
6968       goto retry;
6969     }
6970
6971   /* Or to fit_double_type, if nothing else.  */
6972   return !fit_double_type (dc.low, dc.high, &dc.low, &dc.high, type);
6973 }
6974
6975 /* Stores bounds of an integer TYPE in MIN and MAX.  If TYPE has non-constant
6976    bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
6977    represented (assuming two's-complement arithmetic) within the bit
6978    precision of the type are returned instead.  */
6979
6980 void
6981 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
6982 {
6983   if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
6984       && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
6985     mpz_set_double_int (min, tree_to_double_int (TYPE_MIN_VALUE (type)),
6986                         TYPE_UNSIGNED (type));
6987   else
6988     {
6989       if (TYPE_UNSIGNED (type))
6990         mpz_set_ui (min, 0);
6991       else
6992         {
6993           double_int mn;
6994           mn = double_int_mask (TYPE_PRECISION (type) - 1);
6995           mn = double_int_sext (double_int_add (mn, double_int_one),
6996                                 TYPE_PRECISION (type));
6997           mpz_set_double_int (min, mn, false);
6998         }
6999     }
7000
7001   if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type) 
7002       && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
7003     mpz_set_double_int (max, tree_to_double_int (TYPE_MAX_VALUE (type)),
7004                         TYPE_UNSIGNED (type));
7005   else
7006     {
7007       if (TYPE_UNSIGNED (type))
7008         mpz_set_double_int (max, double_int_mask (TYPE_PRECISION (type)),
7009                             true);
7010       else
7011         mpz_set_double_int (max, double_int_mask (TYPE_PRECISION (type) - 1),
7012                             true);
7013     }
7014 }
7015
7016 /* Return true if VAR is an automatic variable defined in function FN.  */
7017
7018 bool
7019 auto_var_in_fn_p (const_tree var, const_tree fn)
7020 {
7021   return (DECL_P (var) && DECL_CONTEXT (var) == fn
7022           && (((TREE_CODE (var) == VAR_DECL || TREE_CODE (var) == PARM_DECL)
7023                && ! TREE_STATIC (var))
7024               || TREE_CODE (var) == LABEL_DECL
7025               || TREE_CODE (var) == RESULT_DECL));
7026 }
7027
7028 /* Subprogram of following function.  Called by walk_tree.
7029
7030    Return *TP if it is an automatic variable or parameter of the
7031    function passed in as DATA.  */
7032
7033 static tree
7034 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
7035 {
7036   tree fn = (tree) data;
7037
7038   if (TYPE_P (*tp))
7039     *walk_subtrees = 0;
7040
7041   else if (DECL_P (*tp)
7042            && auto_var_in_fn_p (*tp, fn))
7043     return *tp;
7044
7045   return NULL_TREE;
7046 }
7047
7048 /* Returns true if T is, contains, or refers to a type with variable
7049    size.  For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
7050    arguments, but not the return type.  If FN is nonzero, only return
7051    true if a modifier of the type or position of FN is a variable or
7052    parameter inside FN.
7053
7054    This concept is more general than that of C99 'variably modified types':
7055    in C99, a struct type is never variably modified because a VLA may not
7056    appear as a structure member.  However, in GNU C code like:
7057
7058      struct S { int i[f()]; };
7059
7060    is valid, and other languages may define similar constructs.  */
7061
7062 bool
7063 variably_modified_type_p (tree type, tree fn)
7064 {
7065   tree t;
7066
7067 /* Test if T is either variable (if FN is zero) or an expression containing
7068    a variable in FN.  */
7069 #define RETURN_TRUE_IF_VAR(T)                                           \
7070   do { tree _t = (T);                                                   \
7071     if (_t && _t != error_mark_node && TREE_CODE (_t) != INTEGER_CST    \
7072         && (!fn || walk_tree (&_t, find_var_from_fn, fn, NULL)))        \
7073       return true;  } while (0)
7074
7075   if (type == error_mark_node)
7076     return false;
7077
7078   /* If TYPE itself has variable size, it is variably modified.  */
7079   RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
7080   RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
7081
7082   switch (TREE_CODE (type))
7083     {
7084     case POINTER_TYPE:
7085     case REFERENCE_TYPE:
7086     case VECTOR_TYPE:
7087       if (variably_modified_type_p (TREE_TYPE (type), fn))
7088         return true;
7089       break;
7090
7091     case FUNCTION_TYPE:
7092     case METHOD_TYPE:
7093       /* If TYPE is a function type, it is variably modified if the
7094          return type is variably modified.  */
7095       if (variably_modified_type_p (TREE_TYPE (type), fn))
7096           return true;
7097       break;
7098
7099     case INTEGER_TYPE:
7100     case REAL_TYPE:
7101     case FIXED_POINT_TYPE:
7102     case ENUMERAL_TYPE:
7103     case BOOLEAN_TYPE:
7104       /* Scalar types are variably modified if their end points
7105          aren't constant.  */
7106       RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
7107       RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
7108       break;
7109
7110     case RECORD_TYPE:
7111     case UNION_TYPE:
7112     case QUAL_UNION_TYPE:
7113       /* We can't see if any of the fields are variably-modified by the
7114          definition we normally use, since that would produce infinite
7115          recursion via pointers.  */
7116       /* This is variably modified if some field's type is.  */
7117       for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t))
7118         if (TREE_CODE (t) == FIELD_DECL)
7119           {
7120             RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
7121             RETURN_TRUE_IF_VAR (DECL_SIZE (t));
7122             RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
7123
7124             if (TREE_CODE (type) == QUAL_UNION_TYPE)
7125               RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
7126           }
7127         break;
7128
7129     case ARRAY_TYPE:
7130       /* Do not call ourselves to avoid infinite recursion.  This is
7131          variably modified if the element type is.  */
7132       RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
7133       RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
7134       break;
7135
7136     default:
7137       break;
7138     }
7139
7140   /* The current language may have other cases to check, but in general,
7141      all other types are not variably modified.  */
7142   return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
7143
7144 #undef RETURN_TRUE_IF_VAR
7145 }
7146
7147 /* Given a DECL or TYPE, return the scope in which it was declared, or
7148    NULL_TREE if there is no containing scope.  */
7149
7150 tree
7151 get_containing_scope (const_tree t)
7152 {
7153   return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
7154 }
7155
7156 /* Return the innermost context enclosing DECL that is
7157    a FUNCTION_DECL, or zero if none.  */
7158
7159 tree
7160 decl_function_context (const_tree decl)
7161 {
7162   tree context;
7163
7164   if (TREE_CODE (decl) == ERROR_MARK)
7165     return 0;
7166
7167   /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
7168      where we look up the function at runtime.  Such functions always take
7169      a first argument of type 'pointer to real context'.
7170
7171      C++ should really be fixed to use DECL_CONTEXT for the real context,
7172      and use something else for the "virtual context".  */
7173   else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
7174     context
7175       = TYPE_MAIN_VARIANT
7176         (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
7177   else
7178     context = DECL_CONTEXT (decl);
7179
7180   while (context && TREE_CODE (context) != FUNCTION_DECL)
7181     {
7182       if (TREE_CODE (context) == BLOCK)
7183         context = BLOCK_SUPERCONTEXT (context);
7184       else
7185         context = get_containing_scope (context);
7186     }
7187
7188   return context;
7189 }
7190
7191 /* Return the innermost context enclosing DECL that is
7192    a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
7193    TYPE_DECLs and FUNCTION_DECLs are transparent to this function.  */
7194
7195 tree
7196 decl_type_context (const_tree decl)
7197 {
7198   tree context = DECL_CONTEXT (decl);
7199
7200   while (context)
7201     switch (TREE_CODE (context))
7202       {
7203       case NAMESPACE_DECL:
7204       case TRANSLATION_UNIT_DECL:
7205         return NULL_TREE;
7206
7207       case RECORD_TYPE:
7208       case UNION_TYPE:
7209       case QUAL_UNION_TYPE:
7210         return context;
7211
7212       case TYPE_DECL:
7213       case FUNCTION_DECL:
7214         context = DECL_CONTEXT (context);
7215         break;
7216
7217       case BLOCK:
7218         context = BLOCK_SUPERCONTEXT (context);
7219         break;
7220
7221       default:
7222         gcc_unreachable ();
7223       }
7224
7225   return NULL_TREE;
7226 }
7227
7228 /* CALL is a CALL_EXPR.  Return the declaration for the function
7229    called, or NULL_TREE if the called function cannot be
7230    determined.  */
7231
7232 tree
7233 get_callee_fndecl (const_tree call)
7234 {
7235   tree addr;
7236
7237   if (call == error_mark_node)
7238     return error_mark_node;
7239
7240   /* It's invalid to call this function with anything but a
7241      CALL_EXPR.  */
7242   gcc_assert (TREE_CODE (call) == CALL_EXPR);
7243
7244   /* The first operand to the CALL is the address of the function
7245      called.  */
7246   addr = CALL_EXPR_FN (call);
7247
7248   STRIP_NOPS (addr);
7249
7250   /* If this is a readonly function pointer, extract its initial value.  */
7251   if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
7252       && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
7253       && DECL_INITIAL (addr))
7254     addr = DECL_INITIAL (addr);
7255
7256   /* If the address is just `&f' for some function `f', then we know
7257      that `f' is being called.  */
7258   if (TREE_CODE (addr) == ADDR_EXPR
7259       && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
7260     return TREE_OPERAND (addr, 0);
7261
7262   /* We couldn't figure out what was being called.  */
7263   return NULL_TREE;
7264 }
7265
7266 /* Print debugging information about tree nodes generated during the compile,
7267    and any language-specific information.  */
7268
7269 void
7270 dump_tree_statistics (void)
7271 {
7272 #ifdef GATHER_STATISTICS
7273   int i;
7274   int total_nodes, total_bytes;
7275 #endif
7276
7277   fprintf (stderr, "\n??? tree nodes created\n\n");
7278 #ifdef GATHER_STATISTICS
7279   fprintf (stderr, "Kind                   Nodes      Bytes\n");
7280   fprintf (stderr, "---------------------------------------\n");
7281   total_nodes = total_bytes = 0;
7282   for (i = 0; i < (int) all_kinds; i++)
7283     {
7284       fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
7285                tree_node_counts[i], tree_node_sizes[i]);
7286       total_nodes += tree_node_counts[i];
7287       total_bytes += tree_node_sizes[i];
7288     }
7289   fprintf (stderr, "---------------------------------------\n");
7290   fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
7291   fprintf (stderr, "---------------------------------------\n");
7292   ssanames_print_statistics ();
7293   phinodes_print_statistics ();
7294 #else
7295   fprintf (stderr, "(No per-node statistics)\n");
7296 #endif
7297   print_type_hash_statistics ();
7298   print_debug_expr_statistics ();
7299   print_value_expr_statistics ();
7300   lang_hooks.print_statistics ();
7301 }
7302 \f
7303 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
7304
7305 /* Generate a crc32 of a string.  */
7306
7307 unsigned
7308 crc32_string (unsigned chksum, const char *string)
7309 {
7310   do
7311     {
7312       unsigned value = *string << 24;
7313       unsigned ix;
7314
7315       for (ix = 8; ix--; value <<= 1)
7316         {
7317           unsigned feedback;
7318
7319           feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
7320           chksum <<= 1;
7321           chksum ^= feedback;
7322         }
7323     }
7324   while (*string++);
7325   return chksum;
7326 }
7327
7328 /* P is a string that will be used in a symbol.  Mask out any characters
7329    that are not valid in that context.  */
7330
7331 void
7332 clean_symbol_name (char *p)
7333 {
7334   for (; *p; p++)
7335     if (! (ISALNUM (*p)
7336 #ifndef NO_DOLLAR_IN_LABEL      /* this for `$'; unlikely, but... -- kr */
7337             || *p == '$'
7338 #endif
7339 #ifndef NO_DOT_IN_LABEL         /* this for `.'; unlikely, but...  */
7340             || *p == '.'
7341 #endif
7342            ))
7343       *p = '_';
7344 }
7345
7346 /* Generate a name for a special-purpose function function.
7347    The generated name may need to be unique across the whole link.
7348    TYPE is some string to identify the purpose of this function to the
7349    linker or collect2; it must start with an uppercase letter,
7350    one of:
7351    I - for constructors
7352    D - for destructors
7353    N - for C++ anonymous namespaces
7354    F - for DWARF unwind frame information.  */
7355
7356 tree
7357 get_file_function_name (const char *type)
7358 {
7359   char *buf;
7360   const char *p;
7361   char *q;
7362
7363   /* If we already have a name we know to be unique, just use that.  */
7364   if (first_global_object_name)
7365     p = q = ASTRDUP (first_global_object_name);
7366   /* If the target is handling the constructors/destructors, they
7367      will be local to this file and the name is only necessary for
7368      debugging purposes.  */
7369   else if ((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
7370     {
7371       const char *file = main_input_filename;
7372       if (! file)
7373         file = input_filename;
7374       /* Just use the file's basename, because the full pathname
7375          might be quite long.  */
7376       p = strrchr (file, '/');
7377       if (p)
7378         p++;
7379       else
7380         p = file;
7381       p = q = ASTRDUP (p);
7382     }
7383   else
7384     {
7385       /* Otherwise, the name must be unique across the entire link.
7386          We don't have anything that we know to be unique to this translation
7387          unit, so use what we do have and throw in some randomness.  */
7388       unsigned len;
7389       const char *name = weak_global_object_name;
7390       const char *file = main_input_filename;
7391
7392       if (! name)
7393         name = "";
7394       if (! file)
7395         file = input_filename;
7396
7397       len = strlen (file);
7398       q = (char *) alloca (9 * 2 + len + 1);
7399       memcpy (q, file, len + 1);
7400
7401       sprintf (q + len, "_%08X_%08X", crc32_string (0, name),
7402                crc32_string (0, get_random_seed (false)));
7403
7404       p = q;
7405     }
7406
7407   clean_symbol_name (q);
7408   buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
7409                          + strlen (type));
7410
7411   /* Set up the name of the file-level functions we may need.
7412      Use a global object (which is already required to be unique over
7413      the program) rather than the file name (which imposes extra
7414      constraints).  */
7415   sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
7416
7417   return get_identifier (buf);
7418 }
7419 \f
7420 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
7421
7422 /* Complain that the tree code of NODE does not match the expected 0
7423    terminated list of trailing codes. The trailing code list can be
7424    empty, for a more vague error message.  FILE, LINE, and FUNCTION
7425    are of the caller.  */
7426
7427 void
7428 tree_check_failed (const_tree node, const char *file,
7429                    int line, const char *function, ...)
7430 {
7431   va_list args;
7432   const char *buffer;
7433   unsigned length = 0;
7434   int code;
7435
7436   va_start (args, function);
7437   while ((code = va_arg (args, int)))
7438     length += 4 + strlen (tree_code_name[code]);
7439   va_end (args);
7440   if (length)
7441     {
7442       char *tmp;
7443       va_start (args, function);
7444       length += strlen ("expected ");
7445       buffer = tmp = (char *) alloca (length);
7446       length = 0;
7447       while ((code = va_arg (args, int)))
7448         {
7449           const char *prefix = length ? " or " : "expected ";
7450           
7451           strcpy (tmp + length, prefix);
7452           length += strlen (prefix);
7453           strcpy (tmp + length, tree_code_name[code]);
7454           length += strlen (tree_code_name[code]);
7455         }
7456       va_end (args);
7457     }
7458   else
7459     buffer = "unexpected node";
7460
7461   internal_error ("tree check: %s, have %s in %s, at %s:%d",
7462                   buffer, tree_code_name[TREE_CODE (node)],
7463                   function, trim_filename (file), line);
7464 }
7465
7466 /* Complain that the tree code of NODE does match the expected 0
7467    terminated list of trailing codes. FILE, LINE, and FUNCTION are of
7468    the caller.  */
7469
7470 void
7471 tree_not_check_failed (const_tree node, const char *file,
7472                        int line, const char *function, ...)
7473 {
7474   va_list args;
7475   char *buffer;
7476   unsigned length = 0;
7477   int code;
7478
7479   va_start (args, function);
7480   while ((code = va_arg (args, int)))
7481     length += 4 + strlen (tree_code_name[code]);
7482   va_end (args);
7483   va_start (args, function);
7484   buffer = (char *) alloca (length);
7485   length = 0;
7486   while ((code = va_arg (args, int)))
7487     {
7488       if (length)
7489         {
7490           strcpy (buffer + length, " or ");
7491           length += 4;
7492         }
7493       strcpy (buffer + length, tree_code_name[code]);
7494       length += strlen (tree_code_name[code]);
7495     }
7496   va_end (args);
7497
7498   internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
7499                   buffer, tree_code_name[TREE_CODE (node)],
7500                   function, trim_filename (file), line);
7501 }
7502
7503 /* Similar to tree_check_failed, except that we check for a class of tree
7504    code, given in CL.  */
7505
7506 void
7507 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
7508                          const char *file, int line, const char *function)
7509 {
7510   internal_error
7511     ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
7512      TREE_CODE_CLASS_STRING (cl),
7513      TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
7514      tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
7515 }
7516
7517 /* Similar to tree_check_failed, except that instead of specifying a
7518    dozen codes, use the knowledge that they're all sequential.  */
7519
7520 void
7521 tree_range_check_failed (const_tree node, const char *file, int line,
7522                          const char *function, enum tree_code c1,
7523                          enum tree_code c2)
7524 {
7525   char *buffer;
7526   unsigned length = 0;
7527   unsigned int c;
7528
7529   for (c = c1; c <= c2; ++c)
7530     length += 4 + strlen (tree_code_name[c]);
7531
7532   length += strlen ("expected ");
7533   buffer = (char *) alloca (length);
7534   length = 0;
7535
7536   for (c = c1; c <= c2; ++c)
7537     {
7538       const char *prefix = length ? " or " : "expected ";
7539
7540       strcpy (buffer + length, prefix);
7541       length += strlen (prefix);
7542       strcpy (buffer + length, tree_code_name[c]);
7543       length += strlen (tree_code_name[c]);
7544     }
7545
7546   internal_error ("tree check: %s, have %s in %s, at %s:%d",
7547                   buffer, tree_code_name[TREE_CODE (node)],
7548                   function, trim_filename (file), line);
7549 }
7550
7551
7552 /* Similar to tree_check_failed, except that we check that a tree does
7553    not have the specified code, given in CL.  */
7554
7555 void
7556 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
7557                              const char *file, int line, const char *function)
7558 {
7559   internal_error
7560     ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
7561      TREE_CODE_CLASS_STRING (cl),
7562      TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
7563      tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
7564 }
7565
7566
7567 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes.  */
7568
7569 void
7570 omp_clause_check_failed (const_tree node, const char *file, int line,
7571                          const char *function, enum omp_clause_code code)
7572 {
7573   internal_error ("tree check: expected omp_clause %s, have %s in %s, at %s:%d",
7574                   omp_clause_code_name[code], tree_code_name[TREE_CODE (node)],
7575                   function, trim_filename (file), line);
7576 }
7577
7578
7579 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes.  */
7580
7581 void
7582 omp_clause_range_check_failed (const_tree node, const char *file, int line,
7583                                const char *function, enum omp_clause_code c1,
7584                                enum omp_clause_code c2)
7585 {
7586   char *buffer;
7587   unsigned length = 0;
7588   unsigned int c;
7589
7590   for (c = c1; c <= c2; ++c)
7591     length += 4 + strlen (omp_clause_code_name[c]);
7592
7593   length += strlen ("expected ");
7594   buffer = (char *) alloca (length);
7595   length = 0;
7596
7597   for (c = c1; c <= c2; ++c)
7598     {
7599       const char *prefix = length ? " or " : "expected ";
7600
7601       strcpy (buffer + length, prefix);
7602       length += strlen (prefix);
7603       strcpy (buffer + length, omp_clause_code_name[c]);
7604       length += strlen (omp_clause_code_name[c]);
7605     }
7606
7607   internal_error ("tree check: %s, have %s in %s, at %s:%d",
7608                   buffer, omp_clause_code_name[TREE_CODE (node)],
7609                   function, trim_filename (file), line);
7610 }
7611
7612
7613 #undef DEFTREESTRUCT
7614 #define DEFTREESTRUCT(VAL, NAME) NAME,
7615
7616 static const char *ts_enum_names[] = {
7617 #include "treestruct.def"
7618 };
7619 #undef DEFTREESTRUCT
7620
7621 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
7622
7623 /* Similar to tree_class_check_failed, except that we check for
7624    whether CODE contains the tree structure identified by EN.  */
7625
7626 void
7627 tree_contains_struct_check_failed (const_tree node, 
7628                                    const enum tree_node_structure_enum en,
7629                                    const char *file, int line, 
7630                                    const char *function)
7631 {
7632   internal_error
7633     ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
7634      TS_ENUM_NAME(en),
7635      tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
7636 }
7637
7638
7639 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
7640    (dynamically sized) vector.  */
7641
7642 void
7643 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
7644                            const char *function)
7645 {
7646   internal_error
7647     ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
7648      idx + 1, len, function, trim_filename (file), line);
7649 }
7650
7651 /* Similar to above, except that the check is for the bounds of the operand
7652    vector of an expression node EXP.  */
7653
7654 void
7655 tree_operand_check_failed (int idx, const_tree exp, const char *file,
7656                            int line, const char *function)
7657 {
7658   int code = TREE_CODE (exp);
7659   internal_error
7660     ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
7661      idx + 1, tree_code_name[code], TREE_OPERAND_LENGTH (exp),
7662      function, trim_filename (file), line);
7663 }
7664
7665 /* Similar to above, except that the check is for the number of
7666    operands of an OMP_CLAUSE node.  */
7667
7668 void
7669 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
7670                                  int line, const char *function)
7671 {
7672   internal_error
7673     ("tree check: accessed operand %d of omp_clause %s with %d operands "
7674      "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
7675      omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
7676      trim_filename (file), line);
7677 }
7678 #endif /* ENABLE_TREE_CHECKING */
7679 \f
7680 /* Create a new vector type node holding SUBPARTS units of type INNERTYPE,
7681    and mapped to the machine mode MODE.  Initialize its fields and build
7682    the information necessary for debugging output.  */
7683
7684 static tree
7685 make_vector_type (tree innertype, int nunits, enum machine_mode mode)
7686 {
7687   tree t;
7688   hashval_t hashcode = 0;
7689
7690   /* Build a main variant, based on the main variant of the inner type, then
7691      use it to build the variant we return.  */
7692   if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
7693       && TYPE_MAIN_VARIANT (innertype) != innertype)
7694     return build_type_attribute_qual_variant (
7695             make_vector_type (TYPE_MAIN_VARIANT (innertype), nunits, mode),
7696             TYPE_ATTRIBUTES (innertype),
7697             TYPE_QUALS (innertype));
7698
7699   t = make_node (VECTOR_TYPE);
7700   TREE_TYPE (t) = TYPE_MAIN_VARIANT (innertype);
7701   SET_TYPE_VECTOR_SUBPARTS (t, nunits);
7702   SET_TYPE_MODE (t, mode);
7703   TYPE_READONLY (t) = TYPE_READONLY (innertype);
7704   TYPE_VOLATILE (t) = TYPE_VOLATILE (innertype);
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 (mode, hashcode);
7735   hashcode = iterative_hash_object (TYPE_HASH (innertype), hashcode);
7736   return type_hash_canon (hashcode, t);
7737 }
7738
7739 static tree
7740 make_or_reuse_type (unsigned size, int unsignedp)
7741 {
7742   if (size == INT_TYPE_SIZE)
7743     return unsignedp ? unsigned_type_node : integer_type_node;
7744   if (size == CHAR_TYPE_SIZE)
7745     return unsignedp ? unsigned_char_type_node : signed_char_type_node;
7746   if (size == SHORT_TYPE_SIZE)
7747     return unsignedp ? short_unsigned_type_node : short_integer_type_node;
7748   if (size == LONG_TYPE_SIZE)
7749     return unsignedp ? long_unsigned_type_node : long_integer_type_node;
7750   if (size == LONG_LONG_TYPE_SIZE)
7751     return (unsignedp ? long_long_unsigned_type_node
7752             : long_long_integer_type_node);
7753
7754   if (unsignedp)
7755     return make_unsigned_type (size);
7756   else
7757     return make_signed_type (size);
7758 }
7759
7760 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP.  */
7761
7762 static tree
7763 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
7764 {
7765   if (satp)
7766     {
7767       if (size == SHORT_FRACT_TYPE_SIZE)
7768         return unsignedp ? sat_unsigned_short_fract_type_node
7769                          : sat_short_fract_type_node;
7770       if (size == FRACT_TYPE_SIZE)
7771         return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
7772       if (size == LONG_FRACT_TYPE_SIZE)
7773         return unsignedp ? sat_unsigned_long_fract_type_node
7774                          : sat_long_fract_type_node;
7775       if (size == LONG_LONG_FRACT_TYPE_SIZE)
7776         return unsignedp ? sat_unsigned_long_long_fract_type_node
7777                          : sat_long_long_fract_type_node;
7778     }
7779   else
7780     {
7781       if (size == SHORT_FRACT_TYPE_SIZE)
7782         return unsignedp ? unsigned_short_fract_type_node
7783                          : short_fract_type_node;
7784       if (size == FRACT_TYPE_SIZE)
7785         return unsignedp ? unsigned_fract_type_node : fract_type_node;
7786       if (size == LONG_FRACT_TYPE_SIZE)
7787         return unsignedp ? unsigned_long_fract_type_node
7788                          : long_fract_type_node;
7789       if (size == LONG_LONG_FRACT_TYPE_SIZE)
7790         return unsignedp ? unsigned_long_long_fract_type_node
7791                          : long_long_fract_type_node;
7792     }
7793
7794   return make_fract_type (size, unsignedp, satp);
7795 }
7796
7797 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP.  */
7798
7799 static tree
7800 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
7801 {
7802   if (satp)
7803     {
7804       if (size == SHORT_ACCUM_TYPE_SIZE)
7805         return unsignedp ? sat_unsigned_short_accum_type_node
7806                          : sat_short_accum_type_node;
7807       if (size == ACCUM_TYPE_SIZE)
7808         return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
7809       if (size == LONG_ACCUM_TYPE_SIZE)
7810         return unsignedp ? sat_unsigned_long_accum_type_node
7811                          : sat_long_accum_type_node;
7812       if (size == LONG_LONG_ACCUM_TYPE_SIZE)
7813         return unsignedp ? sat_unsigned_long_long_accum_type_node
7814                          : sat_long_long_accum_type_node;
7815     }
7816   else
7817     {
7818       if (size == SHORT_ACCUM_TYPE_SIZE)
7819         return unsignedp ? unsigned_short_accum_type_node
7820                          : short_accum_type_node;
7821       if (size == ACCUM_TYPE_SIZE)
7822         return unsignedp ? unsigned_accum_type_node : accum_type_node;
7823       if (size == LONG_ACCUM_TYPE_SIZE)
7824         return unsignedp ? unsigned_long_accum_type_node
7825                          : long_accum_type_node;
7826       if (size == LONG_LONG_ACCUM_TYPE_SIZE)
7827         return unsignedp ? unsigned_long_long_accum_type_node
7828                          : long_long_accum_type_node;
7829     }
7830
7831   return make_accum_type (size, unsignedp, satp);
7832 }
7833
7834 /* Create nodes for all integer types (and error_mark_node) using the sizes
7835    of C datatypes.  The caller should call set_sizetype soon after calling
7836    this function to select one of the types as sizetype.  */
7837
7838 void
7839 build_common_tree_nodes (bool signed_char, bool signed_sizetype)
7840 {
7841   error_mark_node = make_node (ERROR_MARK);
7842   TREE_TYPE (error_mark_node) = error_mark_node;
7843
7844   initialize_sizetypes (signed_sizetype);
7845
7846   /* Define both `signed char' and `unsigned char'.  */
7847   signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
7848   TYPE_STRING_FLAG (signed_char_type_node) = 1;
7849   unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
7850   TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
7851
7852   /* Define `char', which is like either `signed char' or `unsigned char'
7853      but not the same as either.  */
7854   char_type_node
7855     = (signed_char
7856        ? make_signed_type (CHAR_TYPE_SIZE)
7857        : make_unsigned_type (CHAR_TYPE_SIZE));
7858   TYPE_STRING_FLAG (char_type_node) = 1;
7859
7860   short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
7861   short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
7862   integer_type_node = make_signed_type (INT_TYPE_SIZE);
7863   unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
7864   long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
7865   long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
7866   long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
7867   long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
7868
7869   /* Define a boolean type.  This type only represents boolean values but
7870      may be larger than char depending on the value of BOOL_TYPE_SIZE.
7871      Front ends which want to override this size (i.e. Java) can redefine
7872      boolean_type_node before calling build_common_tree_nodes_2.  */
7873   boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
7874   TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
7875   TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
7876   TYPE_PRECISION (boolean_type_node) = 1;
7877
7878   /* Fill in the rest of the sized types.  Reuse existing type nodes
7879      when possible.  */
7880   intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
7881   intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
7882   intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
7883   intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
7884   intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
7885
7886   unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
7887   unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
7888   unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
7889   unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
7890   unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
7891
7892   access_public_node = get_identifier ("public");
7893   access_protected_node = get_identifier ("protected");
7894   access_private_node = get_identifier ("private");
7895 }
7896
7897 /* Call this function after calling build_common_tree_nodes and set_sizetype.
7898    It will create several other common tree nodes.  */
7899
7900 void
7901 build_common_tree_nodes_2 (int short_double)
7902 {
7903   /* Define these next since types below may used them.  */
7904   integer_zero_node = build_int_cst (NULL_TREE, 0);
7905   integer_one_node = build_int_cst (NULL_TREE, 1);
7906   integer_minus_one_node = build_int_cst (NULL_TREE, -1);
7907
7908   size_zero_node = size_int (0);
7909   size_one_node = size_int (1);
7910   bitsize_zero_node = bitsize_int (0);
7911   bitsize_one_node = bitsize_int (1);
7912   bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
7913
7914   boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
7915   boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
7916
7917   void_type_node = make_node (VOID_TYPE);
7918   layout_type (void_type_node);
7919
7920   /* We are not going to have real types in C with less than byte alignment,
7921      so we might as well not have any types that claim to have it.  */
7922   TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
7923   TYPE_USER_ALIGN (void_type_node) = 0;
7924
7925   null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
7926   layout_type (TREE_TYPE (null_pointer_node));
7927
7928   ptr_type_node = build_pointer_type (void_type_node);
7929   const_ptr_type_node
7930     = build_pointer_type (build_type_variant (void_type_node, 1, 0));
7931   fileptr_type_node = ptr_type_node;
7932
7933   float_type_node = make_node (REAL_TYPE);
7934   TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
7935   layout_type (float_type_node);
7936
7937   double_type_node = make_node (REAL_TYPE);
7938   if (short_double)
7939     TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
7940   else
7941     TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
7942   layout_type (double_type_node);
7943
7944   long_double_type_node = make_node (REAL_TYPE);
7945   TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
7946   layout_type (long_double_type_node);
7947
7948   float_ptr_type_node = build_pointer_type (float_type_node);
7949   double_ptr_type_node = build_pointer_type (double_type_node);
7950   long_double_ptr_type_node = build_pointer_type (long_double_type_node);
7951   integer_ptr_type_node = build_pointer_type (integer_type_node);
7952
7953   /* Fixed size integer types.  */
7954   uint32_type_node = build_nonstandard_integer_type (32, true);
7955   uint64_type_node = build_nonstandard_integer_type (64, true);
7956
7957   /* Decimal float types. */
7958   dfloat32_type_node = make_node (REAL_TYPE);
7959   TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE; 
7960   layout_type (dfloat32_type_node);
7961   SET_TYPE_MODE (dfloat32_type_node, SDmode);
7962   dfloat32_ptr_type_node = build_pointer_type (dfloat32_type_node);
7963
7964   dfloat64_type_node = make_node (REAL_TYPE);
7965   TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
7966   layout_type (dfloat64_type_node);
7967   SET_TYPE_MODE (dfloat64_type_node, DDmode);
7968   dfloat64_ptr_type_node = build_pointer_type (dfloat64_type_node);
7969
7970   dfloat128_type_node = make_node (REAL_TYPE);
7971   TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE; 
7972   layout_type (dfloat128_type_node);
7973   SET_TYPE_MODE (dfloat128_type_node, TDmode);
7974   dfloat128_ptr_type_node = build_pointer_type (dfloat128_type_node);
7975
7976   complex_integer_type_node = build_complex_type (integer_type_node);
7977   complex_float_type_node = build_complex_type (float_type_node);
7978   complex_double_type_node = build_complex_type (double_type_node);
7979   complex_long_double_type_node = build_complex_type (long_double_type_node);
7980
7981 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned.  */
7982 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
7983   sat_ ## KIND ## _type_node = \
7984     make_sat_signed_ ## KIND ## _type (SIZE); \
7985   sat_unsigned_ ## KIND ## _type_node = \
7986     make_sat_unsigned_ ## KIND ## _type (SIZE); \
7987   KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
7988   unsigned_ ## KIND ## _type_node = \
7989     make_unsigned_ ## KIND ## _type (SIZE);
7990
7991 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
7992   sat_ ## WIDTH ## KIND ## _type_node = \
7993     make_sat_signed_ ## KIND ## _type (SIZE); \
7994   sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
7995     make_sat_unsigned_ ## KIND ## _type (SIZE); \
7996   WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
7997   unsigned_ ## WIDTH ## KIND ## _type_node = \
7998     make_unsigned_ ## KIND ## _type (SIZE);
7999
8000 /* Make fixed-point type nodes based on four different widths.  */
8001 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
8002   MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
8003   MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
8004   MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
8005   MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
8006
8007 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned.  */
8008 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
8009   NAME ## _type_node = \
8010     make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
8011   u ## NAME ## _type_node = \
8012     make_or_reuse_unsigned_ ## KIND ## _type \
8013       (GET_MODE_BITSIZE (U ## MODE ## mode)); \
8014   sat_ ## NAME ## _type_node = \
8015     make_or_reuse_sat_signed_ ## KIND ## _type \
8016       (GET_MODE_BITSIZE (MODE ## mode)); \
8017   sat_u ## NAME ## _type_node = \
8018     make_or_reuse_sat_unsigned_ ## KIND ## _type \
8019       (GET_MODE_BITSIZE (U ## MODE ## mode));
8020
8021   /* Fixed-point type and mode nodes.  */
8022   MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
8023   MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
8024   MAKE_FIXED_MODE_NODE (fract, qq, QQ)
8025   MAKE_FIXED_MODE_NODE (fract, hq, HQ)
8026   MAKE_FIXED_MODE_NODE (fract, sq, SQ)
8027   MAKE_FIXED_MODE_NODE (fract, dq, DQ)
8028   MAKE_FIXED_MODE_NODE (fract, tq, TQ)
8029   MAKE_FIXED_MODE_NODE (accum, ha, HA)
8030   MAKE_FIXED_MODE_NODE (accum, sa, SA)
8031   MAKE_FIXED_MODE_NODE (accum, da, DA)
8032   MAKE_FIXED_MODE_NODE (accum, ta, TA)
8033
8034   {
8035     tree t = targetm.build_builtin_va_list ();
8036
8037     /* Many back-ends define record types without setting TYPE_NAME.
8038        If we copied the record type here, we'd keep the original
8039        record type without a name.  This breaks name mangling.  So,
8040        don't copy record types and let c_common_nodes_and_builtins()
8041        declare the type to be __builtin_va_list.  */
8042     if (TREE_CODE (t) != RECORD_TYPE)
8043       t = build_variant_type_copy (t);
8044     
8045     va_list_type_node = t;
8046   }
8047 }
8048
8049 /* A subroutine of build_common_builtin_nodes.  Define a builtin function.  */
8050
8051 static void
8052 local_define_builtin (const char *name, tree type, enum built_in_function code,
8053                       const char *library_name, int ecf_flags)
8054 {
8055   tree decl;
8056
8057   decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
8058                                library_name, NULL_TREE);
8059   if (ecf_flags & ECF_CONST)
8060     TREE_READONLY (decl) = 1;
8061   if (ecf_flags & ECF_PURE)
8062     DECL_PURE_P (decl) = 1;
8063   if (ecf_flags & ECF_LOOPING_CONST_OR_PURE)
8064     DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
8065   if (ecf_flags & ECF_NORETURN)
8066     TREE_THIS_VOLATILE (decl) = 1;
8067   if (ecf_flags & ECF_NOTHROW)
8068     TREE_NOTHROW (decl) = 1;
8069   if (ecf_flags & ECF_MALLOC)
8070     DECL_IS_MALLOC (decl) = 1;
8071
8072   built_in_decls[code] = decl;
8073   implicit_built_in_decls[code] = decl;
8074 }
8075
8076 /* Call this function after instantiating all builtins that the language
8077    front end cares about.  This will build the rest of the builtins that
8078    are relied upon by the tree optimizers and the middle-end.  */
8079
8080 void
8081 build_common_builtin_nodes (void)
8082 {
8083   tree tmp, ftype;
8084
8085   if (built_in_decls[BUILT_IN_MEMCPY] == NULL
8086       || built_in_decls[BUILT_IN_MEMMOVE] == NULL)
8087     {
8088       tmp = tree_cons (NULL_TREE, size_type_node, void_list_node);
8089       tmp = tree_cons (NULL_TREE, const_ptr_type_node, tmp);
8090       tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
8091       ftype = build_function_type (ptr_type_node, tmp);
8092
8093       if (built_in_decls[BUILT_IN_MEMCPY] == NULL)
8094         local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
8095                               "memcpy", ECF_NOTHROW);
8096       if (built_in_decls[BUILT_IN_MEMMOVE] == NULL)
8097         local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
8098                               "memmove", ECF_NOTHROW);
8099     }
8100
8101   if (built_in_decls[BUILT_IN_MEMCMP] == NULL)
8102     {
8103       tmp = tree_cons (NULL_TREE, size_type_node, void_list_node);
8104       tmp = tree_cons (NULL_TREE, const_ptr_type_node, tmp);
8105       tmp = tree_cons (NULL_TREE, const_ptr_type_node, tmp);
8106       ftype = build_function_type (integer_type_node, tmp);
8107       local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
8108                             "memcmp", ECF_PURE | ECF_NOTHROW);
8109     }
8110
8111   if (built_in_decls[BUILT_IN_MEMSET] == NULL)
8112     {
8113       tmp = tree_cons (NULL_TREE, size_type_node, void_list_node);
8114       tmp = tree_cons (NULL_TREE, integer_type_node, tmp);
8115       tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
8116       ftype = build_function_type (ptr_type_node, tmp);
8117       local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
8118                             "memset", ECF_NOTHROW);
8119     }
8120
8121   if (built_in_decls[BUILT_IN_ALLOCA] == NULL)
8122     {
8123       tmp = tree_cons (NULL_TREE, size_type_node, void_list_node);
8124       ftype = build_function_type (ptr_type_node, tmp);
8125       local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
8126                             "alloca", ECF_NOTHROW | ECF_MALLOC);
8127     }
8128
8129   tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8130   tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
8131   tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
8132   ftype = build_function_type (void_type_node, tmp);
8133   local_define_builtin ("__builtin_init_trampoline", ftype,
8134                         BUILT_IN_INIT_TRAMPOLINE,
8135                         "__builtin_init_trampoline", ECF_NOTHROW);
8136
8137   tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8138   ftype = build_function_type (ptr_type_node, tmp);
8139   local_define_builtin ("__builtin_adjust_trampoline", ftype,
8140                         BUILT_IN_ADJUST_TRAMPOLINE,
8141                         "__builtin_adjust_trampoline",
8142                         ECF_CONST | ECF_NOTHROW);
8143
8144   tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8145   tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
8146   ftype = build_function_type (void_type_node, tmp);
8147   local_define_builtin ("__builtin_nonlocal_goto", ftype,
8148                         BUILT_IN_NONLOCAL_GOTO,
8149                         "__builtin_nonlocal_goto",
8150                         ECF_NORETURN | ECF_NOTHROW);
8151
8152   tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8153   tmp = tree_cons (NULL_TREE, ptr_type_node, tmp);
8154   ftype = build_function_type (void_type_node, tmp);
8155   local_define_builtin ("__builtin_setjmp_setup", ftype,
8156                         BUILT_IN_SETJMP_SETUP,
8157                         "__builtin_setjmp_setup", ECF_NOTHROW);
8158
8159   tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8160   ftype = build_function_type (ptr_type_node, tmp);
8161   local_define_builtin ("__builtin_setjmp_dispatcher", ftype,
8162                         BUILT_IN_SETJMP_DISPATCHER,
8163                         "__builtin_setjmp_dispatcher",
8164                         ECF_PURE | ECF_NOTHROW);
8165
8166   tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8167   ftype = build_function_type (void_type_node, tmp);
8168   local_define_builtin ("__builtin_setjmp_receiver", ftype,
8169                         BUILT_IN_SETJMP_RECEIVER,
8170                         "__builtin_setjmp_receiver", ECF_NOTHROW);
8171
8172   ftype = build_function_type (ptr_type_node, void_list_node);
8173   local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
8174                         "__builtin_stack_save", ECF_NOTHROW);
8175
8176   tmp = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8177   ftype = build_function_type (void_type_node, tmp);
8178   local_define_builtin ("__builtin_stack_restore", ftype,
8179                         BUILT_IN_STACK_RESTORE,
8180                         "__builtin_stack_restore", ECF_NOTHROW);
8181
8182   ftype = build_function_type (void_type_node, void_list_node);
8183   local_define_builtin ("__builtin_profile_func_enter", ftype,
8184                         BUILT_IN_PROFILE_FUNC_ENTER, "profile_func_enter", 0);
8185   local_define_builtin ("__builtin_profile_func_exit", ftype,
8186                         BUILT_IN_PROFILE_FUNC_EXIT, "profile_func_exit", 0);
8187
8188   /* Complex multiplication and division.  These are handled as builtins
8189      rather than optabs because emit_library_call_value doesn't support
8190      complex.  Further, we can do slightly better with folding these 
8191      beasties if the real and complex parts of the arguments are separate.  */
8192   {
8193     int mode;
8194
8195     for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
8196       {
8197         char mode_name_buf[4], *q;
8198         const char *p;
8199         enum built_in_function mcode, dcode;
8200         tree type, inner_type;
8201
8202         type = lang_hooks.types.type_for_mode ((enum machine_mode) mode, 0);
8203         if (type == NULL)
8204           continue;
8205         inner_type = TREE_TYPE (type);
8206
8207         tmp = tree_cons (NULL_TREE, inner_type, void_list_node);
8208         tmp = tree_cons (NULL_TREE, inner_type, tmp);
8209         tmp = tree_cons (NULL_TREE, inner_type, tmp);
8210         tmp = tree_cons (NULL_TREE, inner_type, tmp);
8211         ftype = build_function_type (type, tmp);
8212
8213         mcode = ((enum built_in_function)
8214                  (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
8215         dcode = ((enum built_in_function)
8216                  (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
8217
8218         for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
8219           *q = TOLOWER (*p);
8220         *q = '\0';
8221
8222         built_in_names[mcode] = concat ("__mul", mode_name_buf, "3", NULL);
8223         local_define_builtin (built_in_names[mcode], ftype, mcode,
8224                               built_in_names[mcode], ECF_CONST | ECF_NOTHROW);
8225
8226         built_in_names[dcode] = concat ("__div", mode_name_buf, "3", NULL);
8227         local_define_builtin (built_in_names[dcode], ftype, dcode,
8228                               built_in_names[dcode], ECF_CONST | ECF_NOTHROW);
8229       }
8230   }
8231 }
8232
8233 /* HACK.  GROSS.  This is absolutely disgusting.  I wish there was a
8234    better way.
8235
8236    If we requested a pointer to a vector, build up the pointers that
8237    we stripped off while looking for the inner type.  Similarly for
8238    return values from functions.
8239
8240    The argument TYPE is the top of the chain, and BOTTOM is the
8241    new type which we will point to.  */
8242
8243 tree
8244 reconstruct_complex_type (tree type, tree bottom)
8245 {
8246   tree inner, outer;
8247   
8248   if (TREE_CODE (type) == POINTER_TYPE)
8249     {
8250       inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
8251       outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
8252                                            TYPE_REF_CAN_ALIAS_ALL (type));
8253     }
8254   else if (TREE_CODE (type) == REFERENCE_TYPE)
8255     {
8256       inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
8257       outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
8258                                              TYPE_REF_CAN_ALIAS_ALL (type));
8259     }
8260   else if (TREE_CODE (type) == ARRAY_TYPE)
8261     {
8262       inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
8263       outer = build_array_type (inner, TYPE_DOMAIN (type));
8264     }
8265   else if (TREE_CODE (type) == FUNCTION_TYPE)
8266     {
8267       inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
8268       outer = build_function_type (inner, TYPE_ARG_TYPES (type));
8269     }
8270   else if (TREE_CODE (type) == METHOD_TYPE)
8271     {
8272       inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
8273       /* The build_method_type_directly() routine prepends 'this' to argument list,
8274          so we must compensate by getting rid of it.  */
8275       outer 
8276         = build_method_type_directly 
8277             (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
8278              inner,
8279              TREE_CHAIN (TYPE_ARG_TYPES (type)));
8280     }
8281   else if (TREE_CODE (type) == OFFSET_TYPE)
8282     {
8283       inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
8284       outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
8285     }
8286   else
8287     return bottom;
8288
8289   return build_qualified_type (outer, TYPE_QUALS (type));
8290 }
8291
8292 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
8293    the inner type.  */
8294 tree
8295 build_vector_type_for_mode (tree innertype, enum machine_mode mode)
8296 {
8297   int nunits;
8298
8299   switch (GET_MODE_CLASS (mode))
8300     {
8301     case MODE_VECTOR_INT:
8302     case MODE_VECTOR_FLOAT:
8303     case MODE_VECTOR_FRACT:
8304     case MODE_VECTOR_UFRACT:
8305     case MODE_VECTOR_ACCUM:
8306     case MODE_VECTOR_UACCUM:
8307       nunits = GET_MODE_NUNITS (mode);
8308       break;
8309
8310     case MODE_INT:
8311       /* Check that there are no leftover bits.  */
8312       gcc_assert (GET_MODE_BITSIZE (mode)
8313                   % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
8314
8315       nunits = GET_MODE_BITSIZE (mode)
8316                / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
8317       break;
8318
8319     default:
8320       gcc_unreachable ();
8321     }
8322
8323   return make_vector_type (innertype, nunits, mode);
8324 }
8325
8326 /* Similarly, but takes the inner type and number of units, which must be
8327    a power of two.  */
8328
8329 tree
8330 build_vector_type (tree innertype, int nunits)
8331 {
8332   return make_vector_type (innertype, nunits, VOIDmode);
8333 }
8334
8335 /* Similarly, but takes the inner type and number of units, which must be
8336    a power of two.  */
8337
8338 tree
8339 build_opaque_vector_type (tree innertype, int nunits)
8340 {
8341   tree t;
8342   innertype = build_distinct_type_copy (innertype);
8343   t = make_vector_type (innertype, nunits, VOIDmode);
8344   TYPE_VECTOR_OPAQUE (t) = true;
8345   return t;
8346 }
8347
8348
8349 /* Build RESX_EXPR with given REGION_NUMBER.  */
8350 tree
8351 build_resx (int region_number)
8352 {
8353   tree t;
8354   t = build1 (RESX_EXPR, void_type_node,
8355               build_int_cst (NULL_TREE, region_number));
8356   return t;
8357 }
8358
8359 /* Given an initializer INIT, return TRUE if INIT is zero or some
8360    aggregate of zeros.  Otherwise return FALSE.  */
8361 bool
8362 initializer_zerop (const_tree init)
8363 {
8364   tree elt;
8365
8366   STRIP_NOPS (init);
8367
8368   switch (TREE_CODE (init))
8369     {
8370     case INTEGER_CST:
8371       return integer_zerop (init);
8372
8373     case REAL_CST:
8374       /* ??? Note that this is not correct for C4X float formats.  There,
8375          a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
8376          negative exponent.  */
8377       return real_zerop (init)
8378         && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
8379
8380     case FIXED_CST:
8381       return fixed_zerop (init);
8382
8383     case COMPLEX_CST:
8384       return integer_zerop (init)
8385         || (real_zerop (init)
8386             && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
8387             && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
8388
8389     case VECTOR_CST:
8390       for (elt = TREE_VECTOR_CST_ELTS (init); elt; elt = TREE_CHAIN (elt))
8391         if (!initializer_zerop (TREE_VALUE (elt)))
8392           return false;
8393       return true;
8394
8395     case CONSTRUCTOR:
8396       {
8397         unsigned HOST_WIDE_INT idx;
8398
8399         FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
8400           if (!initializer_zerop (elt))
8401             return false;
8402         return true;
8403       }
8404
8405     default:
8406       return false;
8407     }
8408 }
8409
8410 /* Build an empty statement at location LOC.  */
8411
8412 tree
8413 build_empty_stmt (location_t loc)
8414 {
8415   tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
8416   SET_EXPR_LOCATION (t, loc);
8417   return t;
8418 }
8419
8420
8421 /* Build an OpenMP clause with code CODE.  LOC is the location of the
8422    clause.  */
8423
8424 tree
8425 build_omp_clause (location_t loc, enum omp_clause_code code)
8426 {
8427   tree t;
8428   int size, length;
8429
8430   length = omp_clause_num_ops[code];
8431   size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
8432
8433   t = GGC_NEWVAR (union tree_node, size);
8434   memset (t, 0, size);
8435   TREE_SET_CODE (t, OMP_CLAUSE);
8436   OMP_CLAUSE_SET_CODE (t, code);
8437   OMP_CLAUSE_LOCATION (t) = loc;
8438
8439 #ifdef GATHER_STATISTICS
8440   tree_node_counts[(int) omp_clause_kind]++;
8441   tree_node_sizes[(int) omp_clause_kind] += size;
8442 #endif
8443   
8444   return t;
8445 }
8446
8447 /* Build a tcc_vl_exp object with code CODE and room for LEN operands.  LEN
8448    includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
8449    Except for the CODE and operand count field, other storage for the
8450    object is initialized to zeros.  */
8451
8452 tree
8453 build_vl_exp_stat (enum tree_code code, int len MEM_STAT_DECL)
8454 {
8455   tree t;
8456   int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
8457
8458   gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
8459   gcc_assert (len >= 1);
8460
8461 #ifdef GATHER_STATISTICS
8462   tree_node_counts[(int) e_kind]++;
8463   tree_node_sizes[(int) e_kind] += length;
8464 #endif
8465
8466   t = (tree) ggc_alloc_zone_pass_stat (length, &tree_zone);
8467
8468   memset (t, 0, length);
8469
8470   TREE_SET_CODE (t, code);
8471
8472   /* Can't use TREE_OPERAND to store the length because if checking is
8473      enabled, it will try to check the length before we store it.  :-P  */
8474   t->exp.operands[0] = build_int_cst (sizetype, len);
8475
8476   return t;
8477 }
8478
8479
8480 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE
8481    and FN and a null static chain slot.  ARGLIST is a TREE_LIST of the
8482    arguments.  */
8483
8484 tree
8485 build_call_list (tree return_type, tree fn, tree arglist)
8486 {
8487   tree t;
8488   int i;
8489
8490   t = build_vl_exp (CALL_EXPR, list_length (arglist) + 3);
8491   TREE_TYPE (t) = return_type;
8492   CALL_EXPR_FN (t) = fn;
8493   CALL_EXPR_STATIC_CHAIN (t) = NULL_TREE;
8494   for (i = 0; arglist; arglist = TREE_CHAIN (arglist), i++)
8495     CALL_EXPR_ARG (t, i) = TREE_VALUE (arglist);
8496   process_call_operands (t);
8497   return t;
8498 }
8499
8500 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
8501    FN and a null static chain slot.  NARGS is the number of call arguments
8502    which are specified as "..." arguments.  */
8503
8504 tree
8505 build_call_nary (tree return_type, tree fn, int nargs, ...)
8506 {
8507   tree ret;
8508   va_list args;
8509   va_start (args, nargs);
8510   ret = build_call_valist (return_type, fn, nargs, args);
8511   va_end (args);
8512   return ret;
8513 }
8514
8515 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
8516    FN and a null static chain slot.  NARGS is the number of call arguments
8517    which are specified as a va_list ARGS.  */
8518
8519 tree
8520 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
8521 {
8522   tree t;
8523   int i;
8524
8525   t = build_vl_exp (CALL_EXPR, nargs + 3);
8526   TREE_TYPE (t) = return_type;
8527   CALL_EXPR_FN (t) = fn;
8528   CALL_EXPR_STATIC_CHAIN (t) = NULL_TREE;
8529   for (i = 0; i < nargs; i++)
8530     CALL_EXPR_ARG (t, i) = va_arg (args, tree);
8531   process_call_operands (t);
8532   return t;
8533 }
8534
8535 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
8536    FN and a null static chain slot.  NARGS is the number of call arguments
8537    which are specified as a tree array ARGS.  */
8538
8539 tree
8540 build_call_array (tree return_type, tree fn, int nargs, const tree *args)
8541 {
8542   tree t;
8543   int i;
8544
8545   t = build_vl_exp (CALL_EXPR, nargs + 3);
8546   TREE_TYPE (t) = return_type;
8547   CALL_EXPR_FN (t) = fn;
8548   CALL_EXPR_STATIC_CHAIN (t) = NULL_TREE;
8549   for (i = 0; i < nargs; i++)
8550     CALL_EXPR_ARG (t, i) = args[i];
8551   process_call_operands (t);
8552   return t;
8553 }
8554
8555 /* Like build_call_array, but takes a VEC.  */
8556
8557 tree
8558 build_call_vec (tree return_type, tree fn, VEC(tree,gc) *args)
8559 {
8560   tree ret, t;
8561   unsigned int ix;
8562
8563   ret = build_vl_exp (CALL_EXPR, VEC_length (tree, args) + 3);
8564   TREE_TYPE (ret) = return_type;
8565   CALL_EXPR_FN (ret) = fn;
8566   CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
8567   for (ix = 0; VEC_iterate (tree, args, ix, t); ++ix)
8568     CALL_EXPR_ARG (ret, ix) = t;
8569   process_call_operands (ret);
8570   return ret;
8571 }
8572
8573
8574 /* Returns true if it is possible to prove that the index of
8575    an array access REF (an ARRAY_REF expression) falls into the
8576    array bounds.  */
8577
8578 bool
8579 in_array_bounds_p (tree ref)
8580 {
8581   tree idx = TREE_OPERAND (ref, 1);
8582   tree min, max;
8583
8584   if (TREE_CODE (idx) != INTEGER_CST)
8585     return false;
8586
8587   min = array_ref_low_bound (ref);
8588   max = array_ref_up_bound (ref);
8589   if (!min
8590       || !max
8591       || TREE_CODE (min) != INTEGER_CST
8592       || TREE_CODE (max) != INTEGER_CST)
8593     return false;
8594
8595   if (tree_int_cst_lt (idx, min)
8596       || tree_int_cst_lt (max, idx))
8597     return false;
8598
8599   return true;
8600 }
8601
8602 /* Returns true if it is possible to prove that the range of
8603    an array access REF (an ARRAY_RANGE_REF expression) falls
8604    into the array bounds.  */
8605
8606 bool
8607 range_in_array_bounds_p (tree ref)
8608 {
8609   tree domain_type = TYPE_DOMAIN (TREE_TYPE (ref));
8610   tree range_min, range_max, min, max;
8611
8612   range_min = TYPE_MIN_VALUE (domain_type);
8613   range_max = TYPE_MAX_VALUE (domain_type);
8614   if (!range_min
8615       || !range_max
8616       || TREE_CODE (range_min) != INTEGER_CST
8617       || TREE_CODE (range_max) != INTEGER_CST)
8618     return false;
8619
8620   min = array_ref_low_bound (ref);
8621   max = array_ref_up_bound (ref);
8622   if (!min
8623       || !max
8624       || TREE_CODE (min) != INTEGER_CST
8625       || TREE_CODE (max) != INTEGER_CST)
8626     return false;
8627
8628   if (tree_int_cst_lt (range_min, min)
8629       || tree_int_cst_lt (max, range_max))
8630     return false;
8631
8632   return true;
8633 }
8634
8635 /* Return true if T (assumed to be a DECL) must be assigned a memory
8636    location.  */
8637
8638 bool
8639 needs_to_live_in_memory (const_tree t)
8640 {
8641   if (TREE_CODE (t) == SSA_NAME)
8642     t = SSA_NAME_VAR (t);
8643
8644   return (TREE_ADDRESSABLE (t)
8645           || is_global_var (t)
8646           || (TREE_CODE (t) == RESULT_DECL
8647               && aggregate_value_p (t, current_function_decl)));
8648 }
8649
8650 /* There are situations in which a language considers record types
8651    compatible which have different field lists.  Decide if two fields
8652    are compatible.  It is assumed that the parent records are compatible.  */
8653
8654 bool
8655 fields_compatible_p (const_tree f1, const_tree f2)
8656 {
8657   if (!operand_equal_p (DECL_FIELD_BIT_OFFSET (f1),
8658                         DECL_FIELD_BIT_OFFSET (f2), OEP_ONLY_CONST))
8659     return false;
8660
8661   if (!operand_equal_p (DECL_FIELD_OFFSET (f1),
8662                         DECL_FIELD_OFFSET (f2), OEP_ONLY_CONST))
8663     return false;
8664
8665   if (!types_compatible_p (TREE_TYPE (f1), TREE_TYPE (f2)))
8666     return false;
8667
8668   return true;
8669 }
8670
8671 /* Locate within RECORD a field that is compatible with ORIG_FIELD.  */
8672
8673 tree
8674 find_compatible_field (tree record, tree orig_field)
8675 {
8676   tree f;
8677
8678   for (f = TYPE_FIELDS (record); f ; f = TREE_CHAIN (f))
8679     if (TREE_CODE (f) == FIELD_DECL
8680         && fields_compatible_p (f, orig_field))
8681       return f;
8682
8683   /* ??? Why isn't this on the main fields list?  */
8684   f = TYPE_VFIELD (record);
8685   if (f && TREE_CODE (f) == FIELD_DECL
8686       && fields_compatible_p (f, orig_field))
8687     return f;
8688
8689   /* ??? We should abort here, but Java appears to do Bad Things
8690      with inherited fields.  */
8691   return orig_field;
8692 }
8693
8694 /* Return value of a constant X and sign-extend it.  */
8695
8696 HOST_WIDE_INT
8697 int_cst_value (const_tree x)
8698 {
8699   unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
8700   unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
8701
8702   /* Make sure the sign-extended value will fit in a HOST_WIDE_INT.  */
8703   gcc_assert (TREE_INT_CST_HIGH (x) == 0
8704               || TREE_INT_CST_HIGH (x) == -1);
8705
8706   if (bits < HOST_BITS_PER_WIDE_INT)
8707     {
8708       bool negative = ((val >> (bits - 1)) & 1) != 0;
8709       if (negative)
8710         val |= (~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1;
8711       else
8712         val &= ~((~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1);
8713     }
8714
8715   return val;
8716 }
8717
8718 /* Return value of a constant X and sign-extend it.  */
8719
8720 HOST_WIDEST_INT
8721 widest_int_cst_value (const_tree x)
8722 {
8723   unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
8724   unsigned HOST_WIDEST_INT val = TREE_INT_CST_LOW (x);
8725
8726 #if HOST_BITS_PER_WIDEST_INT > HOST_BITS_PER_WIDE_INT
8727   gcc_assert (HOST_BITS_PER_WIDEST_INT >= 2 * HOST_BITS_PER_WIDE_INT);
8728   val |= (((unsigned HOST_WIDEST_INT) TREE_INT_CST_HIGH (x))
8729           << HOST_BITS_PER_WIDE_INT);
8730 #else
8731   /* Make sure the sign-extended value will fit in a HOST_WIDE_INT.  */
8732   gcc_assert (TREE_INT_CST_HIGH (x) == 0
8733               || TREE_INT_CST_HIGH (x) == -1);
8734 #endif
8735
8736   if (bits < HOST_BITS_PER_WIDEST_INT)
8737     {
8738       bool negative = ((val >> (bits - 1)) & 1) != 0;
8739       if (negative)
8740         val |= (~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1;
8741       else
8742         val &= ~((~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1);
8743     }
8744
8745   return val;
8746 }
8747
8748 /* If TYPE is an integral type, return an equivalent type which is
8749     unsigned iff UNSIGNEDP is true.  If TYPE is not an integral type,
8750     return TYPE itself.  */
8751
8752 tree
8753 signed_or_unsigned_type_for (int unsignedp, tree type)
8754 {
8755   tree t = type;
8756   if (POINTER_TYPE_P (type))
8757     t = size_type_node;
8758
8759   if (!INTEGRAL_TYPE_P (t) || TYPE_UNSIGNED (t) == unsignedp)
8760     return t;
8761   
8762   return lang_hooks.types.type_for_size (TYPE_PRECISION (t), unsignedp);
8763 }
8764
8765 /* Returns unsigned variant of TYPE.  */
8766
8767 tree
8768 unsigned_type_for (tree type)
8769 {
8770   return signed_or_unsigned_type_for (1, type);
8771 }
8772
8773 /* Returns signed variant of TYPE.  */
8774
8775 tree
8776 signed_type_for (tree type)
8777 {
8778   return signed_or_unsigned_type_for (0, type);
8779 }
8780
8781 /* Returns the largest value obtainable by casting something in INNER type to
8782    OUTER type.  */
8783
8784 tree
8785 upper_bound_in_type (tree outer, tree inner)
8786 {
8787   unsigned HOST_WIDE_INT lo, hi;
8788   unsigned int det = 0;
8789   unsigned oprec = TYPE_PRECISION (outer);
8790   unsigned iprec = TYPE_PRECISION (inner);
8791   unsigned prec;
8792
8793   /* Compute a unique number for every combination.  */
8794   det |= (oprec > iprec) ? 4 : 0;
8795   det |= TYPE_UNSIGNED (outer) ? 2 : 0;
8796   det |= TYPE_UNSIGNED (inner) ? 1 : 0;
8797
8798   /* Determine the exponent to use.  */
8799   switch (det)
8800     {
8801     case 0:
8802     case 1:
8803       /* oprec <= iprec, outer: signed, inner: don't care.  */
8804       prec = oprec - 1;
8805       break;
8806     case 2:
8807     case 3:
8808       /* oprec <= iprec, outer: unsigned, inner: don't care.  */
8809       prec = oprec;
8810       break;
8811     case 4:
8812       /* oprec > iprec, outer: signed, inner: signed.  */
8813       prec = iprec - 1;
8814       break;
8815     case 5:
8816       /* oprec > iprec, outer: signed, inner: unsigned.  */
8817       prec = iprec;
8818       break;
8819     case 6:
8820       /* oprec > iprec, outer: unsigned, inner: signed.  */
8821       prec = oprec;
8822       break;
8823     case 7:
8824       /* oprec > iprec, outer: unsigned, inner: unsigned.  */
8825       prec = iprec;
8826       break;
8827     default:
8828       gcc_unreachable ();
8829     }
8830
8831   /* Compute 2^^prec - 1.  */
8832   if (prec <= HOST_BITS_PER_WIDE_INT)
8833     {
8834       hi = 0;
8835       lo = ((~(unsigned HOST_WIDE_INT) 0)
8836             >> (HOST_BITS_PER_WIDE_INT - prec));
8837     }
8838   else
8839     {
8840       hi = ((~(unsigned HOST_WIDE_INT) 0)
8841             >> (2 * HOST_BITS_PER_WIDE_INT - prec));
8842       lo = ~(unsigned HOST_WIDE_INT) 0;
8843     }
8844
8845   return build_int_cst_wide (outer, lo, hi);
8846 }
8847
8848 /* Returns the smallest value obtainable by casting something in INNER type to
8849    OUTER type.  */
8850
8851 tree
8852 lower_bound_in_type (tree outer, tree inner)
8853 {
8854   unsigned HOST_WIDE_INT lo, hi;
8855   unsigned oprec = TYPE_PRECISION (outer);
8856   unsigned iprec = TYPE_PRECISION (inner);
8857
8858   /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
8859      and obtain 0.  */
8860   if (TYPE_UNSIGNED (outer)
8861       /* If we are widening something of an unsigned type, OUTER type
8862          contains all values of INNER type.  In particular, both INNER
8863          and OUTER types have zero in common.  */
8864       || (oprec > iprec && TYPE_UNSIGNED (inner)))
8865     lo = hi = 0;
8866   else
8867     {
8868       /* If we are widening a signed type to another signed type, we
8869          want to obtain -2^^(iprec-1).  If we are keeping the
8870          precision or narrowing to a signed type, we want to obtain
8871          -2^(oprec-1).  */
8872       unsigned prec = oprec > iprec ? iprec : oprec;
8873
8874       if (prec <= HOST_BITS_PER_WIDE_INT)
8875         {
8876           hi = ~(unsigned HOST_WIDE_INT) 0;
8877           lo = (~(unsigned HOST_WIDE_INT) 0) << (prec - 1);
8878         }
8879       else
8880         {
8881           hi = ((~(unsigned HOST_WIDE_INT) 0)
8882                 << (prec - HOST_BITS_PER_WIDE_INT - 1));
8883           lo = 0;
8884         }
8885     }
8886
8887   return build_int_cst_wide (outer, lo, hi);
8888 }
8889
8890 /* Return nonzero if two operands that are suitable for PHI nodes are
8891    necessarily equal.  Specifically, both ARG0 and ARG1 must be either
8892    SSA_NAME or invariant.  Note that this is strictly an optimization.
8893    That is, callers of this function can directly call operand_equal_p
8894    and get the same result, only slower.  */
8895
8896 int
8897 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
8898 {
8899   if (arg0 == arg1)
8900     return 1;
8901   if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
8902     return 0;
8903   return operand_equal_p (arg0, arg1, 0);
8904 }
8905
8906 /* Returns number of zeros at the end of binary representation of X.
8907    
8908    ??? Use ffs if available?  */
8909
8910 tree
8911 num_ending_zeros (const_tree x)
8912 {
8913   unsigned HOST_WIDE_INT fr, nfr;
8914   unsigned num, abits;
8915   tree type = TREE_TYPE (x);
8916
8917   if (TREE_INT_CST_LOW (x) == 0)
8918     {
8919       num = HOST_BITS_PER_WIDE_INT;
8920       fr = TREE_INT_CST_HIGH (x);
8921     }
8922   else
8923     {
8924       num = 0;
8925       fr = TREE_INT_CST_LOW (x);
8926     }
8927
8928   for (abits = HOST_BITS_PER_WIDE_INT / 2; abits; abits /= 2)
8929     {
8930       nfr = fr >> abits;
8931       if (nfr << abits == fr)
8932         {
8933           num += abits;
8934           fr = nfr;
8935         }
8936     }
8937
8938   if (num > TYPE_PRECISION (type))
8939     num = TYPE_PRECISION (type);
8940
8941   return build_int_cst_type (type, num);
8942 }
8943
8944
8945 #define WALK_SUBTREE(NODE)                              \
8946   do                                                    \
8947     {                                                   \
8948       result = walk_tree_1 (&(NODE), func, data, pset, lh);     \
8949       if (result)                                       \
8950         return result;                                  \
8951     }                                                   \
8952   while (0)
8953
8954 /* This is a subroutine of walk_tree that walks field of TYPE that are to
8955    be walked whenever a type is seen in the tree.  Rest of operands and return
8956    value are as for walk_tree.  */
8957
8958 static tree
8959 walk_type_fields (tree type, walk_tree_fn func, void *data,
8960                   struct pointer_set_t *pset, walk_tree_lh lh)
8961 {
8962   tree result = NULL_TREE;
8963
8964   switch (TREE_CODE (type))
8965     {
8966     case POINTER_TYPE:
8967     case REFERENCE_TYPE:
8968       /* We have to worry about mutually recursive pointers.  These can't
8969          be written in C.  They can in Ada.  It's pathological, but
8970          there's an ACATS test (c38102a) that checks it.  Deal with this
8971          by checking if we're pointing to another pointer, that one
8972          points to another pointer, that one does too, and we have no htab.
8973          If so, get a hash table.  We check three levels deep to avoid
8974          the cost of the hash table if we don't need one.  */
8975       if (POINTER_TYPE_P (TREE_TYPE (type))
8976           && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
8977           && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
8978           && !pset)
8979         {
8980           result = walk_tree_without_duplicates (&TREE_TYPE (type),
8981                                                  func, data);
8982           if (result)
8983             return result;
8984
8985           break;
8986         }
8987
8988       /* ... fall through ... */
8989
8990     case COMPLEX_TYPE:
8991       WALK_SUBTREE (TREE_TYPE (type));
8992       break;
8993
8994     case METHOD_TYPE:
8995       WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
8996
8997       /* Fall through.  */
8998
8999     case FUNCTION_TYPE:
9000       WALK_SUBTREE (TREE_TYPE (type));
9001       {
9002         tree arg;
9003
9004         /* We never want to walk into default arguments.  */
9005         for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
9006           WALK_SUBTREE (TREE_VALUE (arg));
9007       }
9008       break;
9009
9010     case ARRAY_TYPE:
9011       /* Don't follow this nodes's type if a pointer for fear that
9012          we'll have infinite recursion.  If we have a PSET, then we
9013          need not fear.  */
9014       if (pset
9015           || (!POINTER_TYPE_P (TREE_TYPE (type))
9016               && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
9017         WALK_SUBTREE (TREE_TYPE (type));
9018       WALK_SUBTREE (TYPE_DOMAIN (type));
9019       break;
9020
9021     case OFFSET_TYPE:
9022       WALK_SUBTREE (TREE_TYPE (type));
9023       WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
9024       break;
9025
9026     default:
9027       break;
9028     }
9029
9030   return NULL_TREE;
9031 }
9032
9033 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal.  FUNC is
9034    called with the DATA and the address of each sub-tree.  If FUNC returns a
9035    non-NULL value, the traversal is stopped, and the value returned by FUNC
9036    is returned.  If PSET is non-NULL it is used to record the nodes visited,
9037    and to avoid visiting a node more than once.  */
9038
9039 tree
9040 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
9041              struct pointer_set_t *pset, walk_tree_lh lh)
9042 {
9043   enum tree_code code;
9044   int walk_subtrees;
9045   tree result;
9046
9047 #define WALK_SUBTREE_TAIL(NODE)                         \
9048   do                                                    \
9049     {                                                   \
9050        tp = & (NODE);                                   \
9051        goto tail_recurse;                               \
9052     }                                                   \
9053   while (0)
9054
9055  tail_recurse:
9056   /* Skip empty subtrees.  */
9057   if (!*tp)
9058     return NULL_TREE;
9059
9060   /* Don't walk the same tree twice, if the user has requested
9061      that we avoid doing so.  */
9062   if (pset && pointer_set_insert (pset, *tp))
9063     return NULL_TREE;
9064
9065   /* Call the function.  */
9066   walk_subtrees = 1;
9067   result = (*func) (tp, &walk_subtrees, data);
9068
9069   /* If we found something, return it.  */
9070   if (result)
9071     return result;
9072
9073   code = TREE_CODE (*tp);
9074
9075   /* Even if we didn't, FUNC may have decided that there was nothing
9076      interesting below this point in the tree.  */
9077   if (!walk_subtrees)
9078     {
9079       /* But we still need to check our siblings.  */
9080       if (code == TREE_LIST)
9081         WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
9082       else if (code == OMP_CLAUSE)
9083         WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
9084       else
9085         return NULL_TREE;
9086     }
9087
9088   if (lh)
9089     {
9090       result = (*lh) (tp, &walk_subtrees, func, data, pset);
9091       if (result || !walk_subtrees)
9092         return result;
9093     }
9094
9095   switch (code)
9096     {
9097     case ERROR_MARK:
9098     case IDENTIFIER_NODE:
9099     case INTEGER_CST:
9100     case REAL_CST:
9101     case FIXED_CST:
9102     case VECTOR_CST:
9103     case STRING_CST:
9104     case BLOCK:
9105     case PLACEHOLDER_EXPR:
9106     case SSA_NAME:
9107     case FIELD_DECL:
9108     case RESULT_DECL:
9109       /* None of these have subtrees other than those already walked
9110          above.  */
9111       break;
9112
9113     case TREE_LIST:
9114       WALK_SUBTREE (TREE_VALUE (*tp));
9115       WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
9116       break;
9117
9118     case TREE_VEC:
9119       {
9120         int len = TREE_VEC_LENGTH (*tp);
9121
9122         if (len == 0)
9123           break;
9124
9125         /* Walk all elements but the first.  */
9126         while (--len)
9127           WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
9128
9129         /* Now walk the first one as a tail call.  */
9130         WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
9131       }
9132
9133     case COMPLEX_CST:
9134       WALK_SUBTREE (TREE_REALPART (*tp));
9135       WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
9136
9137     case CONSTRUCTOR:
9138       {
9139         unsigned HOST_WIDE_INT idx;
9140         constructor_elt *ce;
9141
9142         for (idx = 0;
9143              VEC_iterate(constructor_elt, CONSTRUCTOR_ELTS (*tp), idx, ce);
9144              idx++)
9145           WALK_SUBTREE (ce->value);
9146       }
9147       break;
9148
9149     case SAVE_EXPR:
9150       WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
9151
9152     case BIND_EXPR:
9153       {
9154         tree decl;
9155         for (decl = BIND_EXPR_VARS (*tp); decl; decl = TREE_CHAIN (decl))
9156           {
9157             /* Walk the DECL_INITIAL and DECL_SIZE.  We don't want to walk
9158                into declarations that are just mentioned, rather than
9159                declared; they don't really belong to this part of the tree.
9160                And, we can see cycles: the initializer for a declaration
9161                can refer to the declaration itself.  */
9162             WALK_SUBTREE (DECL_INITIAL (decl));
9163             WALK_SUBTREE (DECL_SIZE (decl));
9164             WALK_SUBTREE (DECL_SIZE_UNIT (decl));
9165           }
9166         WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
9167       }
9168
9169     case STATEMENT_LIST:
9170       {
9171         tree_stmt_iterator i;
9172         for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
9173           WALK_SUBTREE (*tsi_stmt_ptr (i));
9174       }
9175       break;
9176
9177     case OMP_CLAUSE:
9178       switch (OMP_CLAUSE_CODE (*tp))
9179         {
9180         case OMP_CLAUSE_PRIVATE:
9181         case OMP_CLAUSE_SHARED:
9182         case OMP_CLAUSE_FIRSTPRIVATE:
9183         case OMP_CLAUSE_COPYIN:
9184         case OMP_CLAUSE_COPYPRIVATE:
9185         case OMP_CLAUSE_IF:
9186         case OMP_CLAUSE_NUM_THREADS:
9187         case OMP_CLAUSE_SCHEDULE:
9188           WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0));
9189           /* FALLTHRU */
9190
9191         case OMP_CLAUSE_NOWAIT:
9192         case OMP_CLAUSE_ORDERED:
9193         case OMP_CLAUSE_DEFAULT:
9194         case OMP_CLAUSE_UNTIED:
9195           WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
9196
9197         case OMP_CLAUSE_LASTPRIVATE:
9198           WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
9199           WALK_SUBTREE (OMP_CLAUSE_LASTPRIVATE_STMT (*tp));
9200           WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
9201
9202         case OMP_CLAUSE_COLLAPSE:
9203           {
9204             int i;
9205             for (i = 0; i < 3; i++)
9206               WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
9207             WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
9208           }
9209
9210         case OMP_CLAUSE_REDUCTION:
9211           {
9212             int i;
9213             for (i = 0; i < 4; i++)
9214               WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
9215             WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
9216           }
9217
9218         default:
9219           gcc_unreachable ();
9220         }
9221       break;
9222
9223     case TARGET_EXPR:
9224       {
9225         int i, len;
9226
9227         /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
9228            But, we only want to walk once.  */
9229         len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
9230         for (i = 0; i < len; ++i)
9231           WALK_SUBTREE (TREE_OPERAND (*tp, i));
9232         WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
9233       }
9234
9235     case DECL_EXPR:
9236       /* If this is a TYPE_DECL, walk into the fields of the type that it's
9237          defining.  We only want to walk into these fields of a type in this
9238          case and not in the general case of a mere reference to the type.
9239
9240          The criterion is as follows: if the field can be an expression, it
9241          must be walked only here.  This should be in keeping with the fields
9242          that are directly gimplified in gimplify_type_sizes in order for the
9243          mark/copy-if-shared/unmark machinery of the gimplifier to work with
9244          variable-sized types.
9245   
9246          Note that DECLs get walked as part of processing the BIND_EXPR.  */
9247       if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
9248         {
9249           tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
9250           if (TREE_CODE (*type_p) == ERROR_MARK)
9251             return NULL_TREE;
9252
9253           /* Call the function for the type.  See if it returns anything or
9254              doesn't want us to continue.  If we are to continue, walk both
9255              the normal fields and those for the declaration case.  */
9256           result = (*func) (type_p, &walk_subtrees, data);
9257           if (result || !walk_subtrees)
9258             return result;
9259
9260           result = walk_type_fields (*type_p, func, data, pset, lh);
9261           if (result)
9262             return result;
9263
9264           /* If this is a record type, also walk the fields.  */
9265           if (TREE_CODE (*type_p) == RECORD_TYPE
9266               || TREE_CODE (*type_p) == UNION_TYPE
9267               || TREE_CODE (*type_p) == QUAL_UNION_TYPE)
9268             {
9269               tree field;
9270
9271               for (field = TYPE_FIELDS (*type_p); field;
9272                    field = TREE_CHAIN (field))
9273                 {
9274                   /* We'd like to look at the type of the field, but we can
9275                      easily get infinite recursion.  So assume it's pointed
9276                      to elsewhere in the tree.  Also, ignore things that
9277                      aren't fields.  */
9278                   if (TREE_CODE (field) != FIELD_DECL)
9279                     continue;
9280
9281                   WALK_SUBTREE (DECL_FIELD_OFFSET (field));
9282                   WALK_SUBTREE (DECL_SIZE (field));
9283                   WALK_SUBTREE (DECL_SIZE_UNIT (field));
9284                   if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
9285                     WALK_SUBTREE (DECL_QUALIFIER (field));
9286                 }
9287             }
9288
9289           /* Same for scalar types.  */
9290           else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
9291                    || TREE_CODE (*type_p) == ENUMERAL_TYPE
9292                    || TREE_CODE (*type_p) == INTEGER_TYPE
9293                    || TREE_CODE (*type_p) == FIXED_POINT_TYPE
9294                    || TREE_CODE (*type_p) == REAL_TYPE)
9295             {
9296               WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
9297               WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
9298             }
9299
9300           WALK_SUBTREE (TYPE_SIZE (*type_p));
9301           WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
9302         }
9303       /* FALLTHRU */
9304
9305     default:
9306       if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
9307         {
9308           int i, len;
9309
9310           /* Walk over all the sub-trees of this operand.  */
9311           len = TREE_OPERAND_LENGTH (*tp);
9312
9313           /* Go through the subtrees.  We need to do this in forward order so
9314              that the scope of a FOR_EXPR is handled properly.  */
9315           if (len)
9316             {
9317               for (i = 0; i < len - 1; ++i)
9318                 WALK_SUBTREE (TREE_OPERAND (*tp, i));
9319               WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
9320             }
9321         }
9322       /* If this is a type, walk the needed fields in the type.  */
9323       else if (TYPE_P (*tp))
9324         return walk_type_fields (*tp, func, data, pset, lh);
9325       break;
9326     }
9327
9328   /* We didn't find what we were looking for.  */
9329   return NULL_TREE;
9330
9331 #undef WALK_SUBTREE_TAIL
9332 }
9333 #undef WALK_SUBTREE
9334
9335 /* Like walk_tree, but does not walk duplicate nodes more than once.  */
9336
9337 tree
9338 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
9339                                 walk_tree_lh lh)
9340 {
9341   tree result;
9342   struct pointer_set_t *pset;
9343
9344   pset = pointer_set_create ();
9345   result = walk_tree_1 (tp, func, data, pset, lh);
9346   pointer_set_destroy (pset);
9347   return result;
9348 }
9349
9350
9351 tree *
9352 tree_block (tree t)
9353 {
9354   char const c = TREE_CODE_CLASS (TREE_CODE (t));
9355
9356   if (IS_EXPR_CODE_CLASS (c))
9357     return &t->exp.block;
9358   gcc_unreachable ();
9359   return NULL;
9360 }
9361
9362 /* Build and return a TREE_LIST of arguments in the CALL_EXPR exp.
9363    FIXME: don't use this function.  It exists for compatibility with
9364    the old representation of CALL_EXPRs where a list was used to hold the
9365    arguments.  Places that currently extract the arglist from a CALL_EXPR
9366    ought to be rewritten to use the CALL_EXPR itself.  */
9367 tree
9368 call_expr_arglist (tree exp)
9369 {
9370   tree arglist = NULL_TREE;
9371   int i;
9372   for (i = call_expr_nargs (exp) - 1; i >= 0; i--)
9373     arglist = tree_cons (NULL_TREE, CALL_EXPR_ARG (exp, i), arglist);
9374   return arglist;
9375 }
9376
9377
9378 /* Create a nameless artificial label and put it in the current
9379    function context.  The label has a location of LOC.  Returns the
9380    newly created label.  */
9381
9382 tree
9383 create_artificial_label (location_t loc)
9384 {
9385   tree lab = build_decl (loc,
9386                          LABEL_DECL, NULL_TREE, void_type_node);
9387
9388   DECL_ARTIFICIAL (lab) = 1;
9389   DECL_IGNORED_P (lab) = 1;
9390   DECL_CONTEXT (lab) = current_function_decl;
9391   return lab;
9392 }
9393
9394 /*  Given a tree, try to return a useful variable name that we can use
9395     to prefix a temporary that is being assigned the value of the tree.
9396     I.E. given  <temp> = &A, return A.  */
9397
9398 const char *
9399 get_name (tree t)
9400 {
9401   tree stripped_decl;
9402
9403   stripped_decl = t;
9404   STRIP_NOPS (stripped_decl);
9405   if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
9406     return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
9407   else
9408     {
9409       switch (TREE_CODE (stripped_decl))
9410         {
9411         case ADDR_EXPR:
9412           return get_name (TREE_OPERAND (stripped_decl, 0));
9413         default:
9414           return NULL;
9415         }
9416     }
9417 }
9418
9419 /* Return true if TYPE has a variable argument list.  */
9420
9421 bool
9422 stdarg_p (tree fntype)
9423 {
9424   function_args_iterator args_iter;
9425   tree n = NULL_TREE, t;
9426
9427   if (!fntype)
9428     return false;
9429
9430   FOREACH_FUNCTION_ARGS(fntype, t, args_iter)
9431     {
9432       n = t;
9433     }
9434
9435   return n != NULL_TREE && n != void_type_node;
9436 }
9437
9438 /* Return true if TYPE has a prototype.  */
9439
9440 bool
9441 prototype_p (tree fntype)
9442 {
9443   tree t;
9444
9445   gcc_assert (fntype != NULL_TREE);
9446
9447   t = TYPE_ARG_TYPES (fntype);
9448   return (t != NULL_TREE);
9449 }
9450
9451 /* If BLOCK is inlined from an __attribute__((__artificial__))
9452    routine, return pointer to location from where it has been
9453    called.  */
9454 location_t *
9455 block_nonartificial_location (tree block)
9456 {
9457   location_t *ret = NULL;
9458
9459   while (block && TREE_CODE (block) == BLOCK
9460          && BLOCK_ABSTRACT_ORIGIN (block))
9461     {
9462       tree ao = BLOCK_ABSTRACT_ORIGIN (block);
9463
9464       while (TREE_CODE (ao) == BLOCK
9465              && BLOCK_ABSTRACT_ORIGIN (ao)
9466              && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
9467         ao = BLOCK_ABSTRACT_ORIGIN (ao);
9468
9469       if (TREE_CODE (ao) == FUNCTION_DECL)
9470         {
9471           /* If AO is an artificial inline, point RET to the
9472              call site locus at which it has been inlined and continue
9473              the loop, in case AO's caller is also an artificial
9474              inline.  */
9475           if (DECL_DECLARED_INLINE_P (ao)
9476               && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
9477             ret = &BLOCK_SOURCE_LOCATION (block);
9478           else
9479             break;
9480         }
9481       else if (TREE_CODE (ao) != BLOCK)
9482         break;
9483
9484       block = BLOCK_SUPERCONTEXT (block);
9485     }
9486   return ret;
9487 }
9488
9489
9490 /* If EXP is inlined from an __attribute__((__artificial__))
9491    function, return the location of the original call expression.  */
9492
9493 location_t
9494 tree_nonartificial_location (tree exp)
9495 {
9496   location_t *loc = block_nonartificial_location (TREE_BLOCK (exp));
9497
9498   if (loc)
9499     return *loc;
9500   else
9501     return EXPR_LOCATION (exp);
9502 }
9503
9504
9505 /* These are the hash table functions for the hash table of OPTIMIZATION_NODEq
9506    nodes.  */
9507
9508 /* Return the hash code code X, an OPTIMIZATION_NODE or TARGET_OPTION code.  */
9509
9510 static hashval_t
9511 cl_option_hash_hash (const void *x)
9512 {
9513   const_tree const t = (const_tree) x;
9514   const char *p;
9515   size_t i;
9516   size_t len = 0;
9517   hashval_t hash = 0;
9518
9519   if (TREE_CODE (t) == OPTIMIZATION_NODE)
9520     {
9521       p = (const char *)TREE_OPTIMIZATION (t);
9522       len = sizeof (struct cl_optimization);
9523     }
9524
9525   else if (TREE_CODE (t) == TARGET_OPTION_NODE)
9526     {
9527       p = (const char *)TREE_TARGET_OPTION (t);
9528       len = sizeof (struct cl_target_option);
9529     }
9530
9531   else
9532     gcc_unreachable ();
9533
9534   /* assume most opt flags are just 0/1, some are 2-3, and a few might be
9535      something else.  */
9536   for (i = 0; i < len; i++)
9537     if (p[i])
9538       hash = (hash << 4) ^ ((i << 2) | p[i]);
9539
9540   return hash;
9541 }
9542
9543 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
9544    TARGET_OPTION tree node) is the same as that given by *Y, which is the
9545    same.  */
9546
9547 static int
9548 cl_option_hash_eq (const void *x, const void *y)
9549 {
9550   const_tree const xt = (const_tree) x;
9551   const_tree const yt = (const_tree) y;
9552   const char *xp;
9553   const char *yp;
9554   size_t len;
9555
9556   if (TREE_CODE (xt) != TREE_CODE (yt))
9557     return 0;
9558
9559   if (TREE_CODE (xt) == OPTIMIZATION_NODE)
9560     {
9561       xp = (const char *)TREE_OPTIMIZATION (xt);
9562       yp = (const char *)TREE_OPTIMIZATION (yt);
9563       len = sizeof (struct cl_optimization);
9564     }
9565
9566   else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
9567     {
9568       xp = (const char *)TREE_TARGET_OPTION (xt);
9569       yp = (const char *)TREE_TARGET_OPTION (yt);
9570       len = sizeof (struct cl_target_option);
9571     }
9572
9573   else
9574     gcc_unreachable ();
9575
9576   return (memcmp (xp, yp, len) == 0);
9577 }
9578
9579 /* Build an OPTIMIZATION_NODE based on the current options.  */
9580
9581 tree
9582 build_optimization_node (void)
9583 {
9584   tree t;
9585   void **slot;
9586
9587   /* Use the cache of optimization nodes.  */
9588
9589   cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node));
9590
9591   slot = htab_find_slot (cl_option_hash_table, cl_optimization_node, INSERT);
9592   t = (tree) *slot;
9593   if (!t)
9594     {
9595       /* Insert this one into the hash table.  */
9596       t = cl_optimization_node;
9597       *slot = t;
9598
9599       /* Make a new node for next time round.  */
9600       cl_optimization_node = make_node (OPTIMIZATION_NODE);
9601     }
9602
9603   return t;
9604 }
9605
9606 /* Build a TARGET_OPTION_NODE based on the current options.  */
9607
9608 tree
9609 build_target_option_node (void)
9610 {
9611   tree t;
9612   void **slot;
9613
9614   /* Use the cache of optimization nodes.  */
9615
9616   cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node));
9617
9618   slot = htab_find_slot (cl_option_hash_table, cl_target_option_node, INSERT);
9619   t = (tree) *slot;
9620   if (!t)
9621     {
9622       /* Insert this one into the hash table.  */
9623       t = cl_target_option_node;
9624       *slot = t;
9625
9626       /* Make a new node for next time round.  */
9627       cl_target_option_node = make_node (TARGET_OPTION_NODE);
9628     }
9629
9630   return t;
9631 }
9632
9633 /* Determine the "ultimate origin" of a block.  The block may be an inlined
9634    instance of an inlined instance of a block which is local to an inline
9635    function, so we have to trace all of the way back through the origin chain
9636    to find out what sort of node actually served as the original seed for the
9637    given block.  */
9638
9639 tree
9640 block_ultimate_origin (const_tree block)
9641 {
9642   tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
9643
9644   /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
9645      nodes in the function to point to themselves; ignore that if
9646      we're trying to output the abstract instance of this function.  */
9647   if (BLOCK_ABSTRACT (block) && immediate_origin == block)
9648     return NULL_TREE;
9649
9650   if (immediate_origin == NULL_TREE)
9651     return NULL_TREE;
9652   else
9653     {
9654       tree ret_val;
9655       tree lookahead = immediate_origin;
9656
9657       do
9658         {
9659           ret_val = lookahead;
9660           lookahead = (TREE_CODE (ret_val) == BLOCK
9661                        ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
9662         }
9663       while (lookahead != NULL && lookahead != ret_val);
9664
9665       /* The block's abstract origin chain may not be the *ultimate* origin of
9666          the block. It could lead to a DECL that has an abstract origin set.
9667          If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
9668          will give us if it has one).  Note that DECL's abstract origins are
9669          supposed to be the most distant ancestor (or so decl_ultimate_origin
9670          claims), so we don't need to loop following the DECL origins.  */
9671       if (DECL_P (ret_val))
9672         return DECL_ORIGIN (ret_val);
9673
9674       return ret_val;
9675     }
9676 }
9677
9678 /* Return true if T1 and T2 are equivalent lists.  */
9679
9680 bool
9681 list_equal_p (const_tree t1, const_tree t2)
9682 {
9683   for (; t1 && t2; t1 = TREE_CHAIN (t1) , t2 = TREE_CHAIN (t2))
9684     if (TREE_VALUE (t1) != TREE_VALUE (t2))
9685       return false;
9686   return !t1 && !t2;
9687 }
9688
9689 /* Return true iff conversion in EXP generates no instruction.  Mark
9690    it inline so that we fully inline into the stripping functions even
9691    though we have two uses of this function.  */
9692
9693 static inline bool
9694 tree_nop_conversion (const_tree exp)
9695 {
9696   tree outer_type, inner_type;
9697
9698   if (!CONVERT_EXPR_P (exp)
9699       && TREE_CODE (exp) != NON_LVALUE_EXPR)
9700     return false;
9701   if (TREE_OPERAND (exp, 0) == error_mark_node)
9702     return false;
9703
9704   outer_type = TREE_TYPE (exp);
9705   inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
9706
9707   /* Use precision rather then machine mode when we can, which gives
9708      the correct answer even for submode (bit-field) types.  */
9709   if ((INTEGRAL_TYPE_P (outer_type)
9710        || POINTER_TYPE_P (outer_type)
9711        || TREE_CODE (outer_type) == OFFSET_TYPE)
9712       && (INTEGRAL_TYPE_P (inner_type)
9713           || POINTER_TYPE_P (inner_type)
9714           || TREE_CODE (inner_type) == OFFSET_TYPE))
9715     return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
9716
9717   /* Otherwise fall back on comparing machine modes (e.g. for
9718      aggregate types, floats).  */
9719   return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
9720 }
9721
9722 /* Return true iff conversion in EXP generates no instruction.  Don't
9723    consider conversions changing the signedness.  */
9724
9725 static bool
9726 tree_sign_nop_conversion (const_tree exp)
9727 {
9728   tree outer_type, inner_type;
9729
9730   if (!tree_nop_conversion (exp))
9731     return false;
9732
9733   outer_type = TREE_TYPE (exp);
9734   inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
9735
9736   return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
9737           && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
9738 }
9739
9740 /* Strip conversions from EXP according to tree_nop_conversion and
9741    return the resulting expression.  */
9742
9743 tree
9744 tree_strip_nop_conversions (tree exp)
9745 {
9746   while (tree_nop_conversion (exp))
9747     exp = TREE_OPERAND (exp, 0);
9748   return exp;
9749 }
9750
9751 /* Strip conversions from EXP according to tree_sign_nop_conversion
9752    and return the resulting expression.  */
9753
9754 tree
9755 tree_strip_sign_nop_conversions (tree exp)
9756 {
9757   while (tree_sign_nop_conversion (exp))
9758     exp = TREE_OPERAND (exp, 0);
9759   return exp;
9760 }
9761
9762
9763 #include "gt-tree.h"