OSDN Git Service

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