OSDN Git Service

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