tree.c (decl_init_priority_insert): Don't create entry for default priority.
[platform/upstream/gcc.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   if (priority == DEFAULT_INIT_PRIORITY)
5917     return;
5918   h = decl_priority_info (decl);
5919   h->init = priority;
5920 }
5921
5922 /* Set the finalization priority for DECL to PRIORITY.  */
5923
5924 void
5925 decl_fini_priority_insert (tree decl, priority_type priority)
5926 {
5927   struct tree_priority_map *h;
5928
5929   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
5930   if (priority == DEFAULT_INIT_PRIORITY)
5931     return;
5932   h = decl_priority_info (decl);
5933   h->fini = priority;
5934 }
5935
5936 /* Print out the statistics for the DECL_DEBUG_EXPR hash table.  */
5937
5938 static void
5939 print_debug_expr_statistics (void)
5940 {
5941   fprintf (stderr, "DECL_DEBUG_EXPR  hash: size %ld, %ld elements, %f collisions\n",
5942            (long) htab_size (debug_expr_for_decl),
5943            (long) htab_elements (debug_expr_for_decl),
5944            htab_collisions (debug_expr_for_decl));
5945 }
5946
5947 /* Print out the statistics for the DECL_VALUE_EXPR hash table.  */
5948
5949 static void
5950 print_value_expr_statistics (void)
5951 {
5952   fprintf (stderr, "DECL_VALUE_EXPR  hash: size %ld, %ld elements, %f collisions\n",
5953            (long) htab_size (value_expr_for_decl),
5954            (long) htab_elements (value_expr_for_decl),
5955            htab_collisions (value_expr_for_decl));
5956 }
5957
5958 /* Lookup a debug expression for FROM, and return it if we find one.  */
5959
5960 tree
5961 decl_debug_expr_lookup (tree from)
5962 {
5963   struct tree_decl_map *h, in;
5964   in.base.from = from;
5965
5966   h = (struct tree_decl_map *)
5967       htab_find_with_hash (debug_expr_for_decl, &in, DECL_UID (from));
5968   if (h)
5969     return h->to;
5970   return NULL_TREE;
5971 }
5972
5973 /* Insert a mapping FROM->TO in the debug expression hashtable.  */
5974
5975 void
5976 decl_debug_expr_insert (tree from, tree to)
5977 {
5978   struct tree_decl_map *h;
5979   void **loc;
5980
5981   h = ggc_alloc_tree_decl_map ();
5982   h->base.from = from;
5983   h->to = to;
5984   loc = htab_find_slot_with_hash (debug_expr_for_decl, h, DECL_UID (from),
5985                                   INSERT);
5986   *(struct tree_decl_map **) loc = h;
5987 }
5988
5989 /* Lookup a value expression for FROM, and return it if we find one.  */
5990
5991 tree
5992 decl_value_expr_lookup (tree from)
5993 {
5994   struct tree_decl_map *h, in;
5995   in.base.from = from;
5996
5997   h = (struct tree_decl_map *)
5998       htab_find_with_hash (value_expr_for_decl, &in, DECL_UID (from));
5999   if (h)
6000     return h->to;
6001   return NULL_TREE;
6002 }
6003
6004 /* Insert a mapping FROM->TO in the value expression hashtable.  */
6005
6006 void
6007 decl_value_expr_insert (tree from, tree to)
6008 {
6009   struct tree_decl_map *h;
6010   void **loc;
6011
6012   h = ggc_alloc_tree_decl_map ();
6013   h->base.from = from;
6014   h->to = to;
6015   loc = htab_find_slot_with_hash (value_expr_for_decl, h, DECL_UID (from),
6016                                   INSERT);
6017   *(struct tree_decl_map **) loc = h;
6018 }
6019
6020 /* Hashing of types so that we don't make duplicates.
6021    The entry point is `type_hash_canon'.  */
6022
6023 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
6024    with types in the TREE_VALUE slots), by adding the hash codes
6025    of the individual types.  */
6026
6027 static unsigned int
6028 type_hash_list (const_tree list, hashval_t hashcode)
6029 {
6030   const_tree tail;
6031
6032   for (tail = list; tail; tail = TREE_CHAIN (tail))
6033     if (TREE_VALUE (tail) != error_mark_node)
6034       hashcode = iterative_hash_object (TYPE_HASH (TREE_VALUE (tail)),
6035                                         hashcode);
6036
6037   return hashcode;
6038 }
6039
6040 /* These are the Hashtable callback functions.  */
6041
6042 /* Returns true iff the types are equivalent.  */
6043
6044 static int
6045 type_hash_eq (const void *va, const void *vb)
6046 {
6047   const struct type_hash *const a = (const struct type_hash *) va,
6048     *const b = (const struct type_hash *) vb;
6049
6050   /* First test the things that are the same for all types.  */
6051   if (a->hash != b->hash
6052       || TREE_CODE (a->type) != TREE_CODE (b->type)
6053       || TREE_TYPE (a->type) != TREE_TYPE (b->type)
6054       || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
6055                                  TYPE_ATTRIBUTES (b->type))
6056       || (TREE_CODE (a->type) != COMPLEX_TYPE
6057           && TYPE_NAME (a->type) != TYPE_NAME (b->type)))
6058     return 0;
6059
6060   /* Be careful about comparing arrays before and after the element type
6061      has been completed; don't compare TYPE_ALIGN unless both types are
6062      complete.  */
6063   if (COMPLETE_TYPE_P (a->type) && COMPLETE_TYPE_P (b->type)
6064       && (TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
6065           || TYPE_MODE (a->type) != TYPE_MODE (b->type)))
6066     return 0;
6067
6068   switch (TREE_CODE (a->type))
6069     {
6070     case VOID_TYPE:
6071     case COMPLEX_TYPE:
6072     case POINTER_TYPE:
6073     case REFERENCE_TYPE:
6074       return 1;
6075
6076     case VECTOR_TYPE:
6077       return TYPE_VECTOR_SUBPARTS (a->type) == TYPE_VECTOR_SUBPARTS (b->type);
6078
6079     case ENUMERAL_TYPE:
6080       if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
6081           && !(TYPE_VALUES (a->type)
6082                && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
6083                && TYPE_VALUES (b->type)
6084                && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
6085                && type_list_equal (TYPE_VALUES (a->type),
6086                                    TYPE_VALUES (b->type))))
6087         return 0;
6088
6089       /* ... fall through ... */
6090
6091     case INTEGER_TYPE:
6092     case REAL_TYPE:
6093     case BOOLEAN_TYPE:
6094       return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
6095                || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
6096                                       TYPE_MAX_VALUE (b->type)))
6097               && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
6098                   || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
6099                                          TYPE_MIN_VALUE (b->type))));
6100
6101     case FIXED_POINT_TYPE:
6102       return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
6103
6104     case OFFSET_TYPE:
6105       return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
6106
6107     case METHOD_TYPE:
6108       if (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
6109           && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6110               || (TYPE_ARG_TYPES (a->type)
6111                   && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6112                   && TYPE_ARG_TYPES (b->type)
6113                   && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6114                   && type_list_equal (TYPE_ARG_TYPES (a->type),
6115                                       TYPE_ARG_TYPES (b->type)))))
6116         break;
6117       return 0;
6118     case ARRAY_TYPE:
6119       return TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type);
6120
6121     case RECORD_TYPE:
6122     case UNION_TYPE:
6123     case QUAL_UNION_TYPE:
6124       return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
6125               || (TYPE_FIELDS (a->type)
6126                   && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
6127                   && TYPE_FIELDS (b->type)
6128                   && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
6129                   && type_list_equal (TYPE_FIELDS (a->type),
6130                                       TYPE_FIELDS (b->type))));
6131
6132     case FUNCTION_TYPE:
6133       if (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
6134           || (TYPE_ARG_TYPES (a->type)
6135               && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
6136               && TYPE_ARG_TYPES (b->type)
6137               && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
6138               && type_list_equal (TYPE_ARG_TYPES (a->type),
6139                                   TYPE_ARG_TYPES (b->type))))
6140         break;
6141       return 0;
6142
6143     default:
6144       return 0;
6145     }
6146
6147   if (lang_hooks.types.type_hash_eq != NULL)
6148     return lang_hooks.types.type_hash_eq (a->type, b->type);
6149
6150   return 1;
6151 }
6152
6153 /* Return the cached hash value.  */
6154
6155 static hashval_t
6156 type_hash_hash (const void *item)
6157 {
6158   return ((const struct type_hash *) item)->hash;
6159 }
6160
6161 /* Look in the type hash table for a type isomorphic to TYPE.
6162    If one is found, return it.  Otherwise return 0.  */
6163
6164 tree
6165 type_hash_lookup (hashval_t hashcode, tree type)
6166 {
6167   struct type_hash *h, in;
6168
6169   /* The TYPE_ALIGN field of a type is set by layout_type(), so we
6170      must call that routine before comparing TYPE_ALIGNs.  */
6171   layout_type (type);
6172
6173   in.hash = hashcode;
6174   in.type = type;
6175
6176   h = (struct type_hash *) htab_find_with_hash (type_hash_table, &in,
6177                                                 hashcode);
6178   if (h)
6179     return h->type;
6180   return NULL_TREE;
6181 }
6182
6183 /* Add an entry to the type-hash-table
6184    for a type TYPE whose hash code is HASHCODE.  */
6185
6186 void
6187 type_hash_add (hashval_t hashcode, tree type)
6188 {
6189   struct type_hash *h;
6190   void **loc;
6191
6192   h = ggc_alloc_type_hash ();
6193   h->hash = hashcode;
6194   h->type = type;
6195   loc = htab_find_slot_with_hash (type_hash_table, h, hashcode, INSERT);
6196   *loc = (void *)h;
6197 }
6198
6199 /* Given TYPE, and HASHCODE its hash code, return the canonical
6200    object for an identical type if one already exists.
6201    Otherwise, return TYPE, and record it as the canonical object.
6202
6203    To use this function, first create a type of the sort you want.
6204    Then compute its hash code from the fields of the type that
6205    make it different from other similar types.
6206    Then call this function and use the value.  */
6207
6208 tree
6209 type_hash_canon (unsigned int hashcode, tree type)
6210 {
6211   tree t1;
6212
6213   /* The hash table only contains main variants, so ensure that's what we're
6214      being passed.  */
6215   gcc_assert (TYPE_MAIN_VARIANT (type) == type);
6216
6217   /* See if the type is in the hash table already.  If so, return it.
6218      Otherwise, add the type.  */
6219   t1 = type_hash_lookup (hashcode, type);
6220   if (t1 != 0)
6221     {
6222 #ifdef GATHER_STATISTICS
6223       tree_node_counts[(int) t_kind]--;
6224       tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type);
6225 #endif
6226       return t1;
6227     }
6228   else
6229     {
6230       type_hash_add (hashcode, type);
6231       return type;
6232     }
6233 }
6234
6235 /* See if the data pointed to by the type hash table is marked.  We consider
6236    it marked if the type is marked or if a debug type number or symbol
6237    table entry has been made for the type.  */
6238
6239 static int
6240 type_hash_marked_p (const void *p)
6241 {
6242   const_tree const type = ((const struct type_hash *) p)->type;
6243
6244   return ggc_marked_p (type);
6245 }
6246
6247 static void
6248 print_type_hash_statistics (void)
6249 {
6250   fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
6251            (long) htab_size (type_hash_table),
6252            (long) htab_elements (type_hash_table),
6253            htab_collisions (type_hash_table));
6254 }
6255
6256 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
6257    with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
6258    by adding the hash codes of the individual attributes.  */
6259
6260 static unsigned int
6261 attribute_hash_list (const_tree list, hashval_t hashcode)
6262 {
6263   const_tree tail;
6264
6265   for (tail = list; tail; tail = TREE_CHAIN (tail))
6266     /* ??? Do we want to add in TREE_VALUE too? */
6267     hashcode = iterative_hash_object
6268       (IDENTIFIER_HASH_VALUE (TREE_PURPOSE (tail)), hashcode);
6269   return hashcode;
6270 }
6271
6272 /* Given two lists of attributes, return true if list l2 is
6273    equivalent to l1.  */
6274
6275 int
6276 attribute_list_equal (const_tree l1, const_tree l2)
6277 {
6278   return attribute_list_contained (l1, l2)
6279          && attribute_list_contained (l2, l1);
6280 }
6281
6282 /* Given two lists of attributes, return true if list L2 is
6283    completely contained within L1.  */
6284 /* ??? This would be faster if attribute names were stored in a canonicalized
6285    form.  Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
6286    must be used to show these elements are equivalent (which they are).  */
6287 /* ??? It's not clear that attributes with arguments will always be handled
6288    correctly.  */
6289
6290 int
6291 attribute_list_contained (const_tree l1, const_tree l2)
6292 {
6293   const_tree t1, t2;
6294
6295   /* First check the obvious, maybe the lists are identical.  */
6296   if (l1 == l2)
6297     return 1;
6298
6299   /* Maybe the lists are similar.  */
6300   for (t1 = l1, t2 = l2;
6301        t1 != 0 && t2 != 0
6302         && TREE_PURPOSE (t1) == TREE_PURPOSE (t2)
6303         && TREE_VALUE (t1) == TREE_VALUE (t2);
6304        t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2));
6305
6306   /* Maybe the lists are equal.  */
6307   if (t1 == 0 && t2 == 0)
6308     return 1;
6309
6310   for (; t2 != 0; t2 = TREE_CHAIN (t2))
6311     {
6312       const_tree attr;
6313       /* This CONST_CAST is okay because lookup_attribute does not
6314          modify its argument and the return value is assigned to a
6315          const_tree.  */
6316       for (attr = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (t2)),
6317                                     CONST_CAST_TREE(l1));
6318            attr != NULL_TREE && !attribute_value_equal (t2, attr);
6319            attr = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (t2)),
6320                                     TREE_CHAIN (attr)))
6321         ;
6322
6323       if (attr == NULL_TREE)
6324         return 0;
6325     }
6326
6327   return 1;
6328 }
6329
6330 /* Given two lists of types
6331    (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
6332    return 1 if the lists contain the same types in the same order.
6333    Also, the TREE_PURPOSEs must match.  */
6334
6335 int
6336 type_list_equal (const_tree l1, const_tree l2)
6337 {
6338   const_tree t1, t2;
6339
6340   for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6341     if (TREE_VALUE (t1) != TREE_VALUE (t2)
6342         || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
6343             && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
6344                   && (TREE_TYPE (TREE_PURPOSE (t1))
6345                       == TREE_TYPE (TREE_PURPOSE (t2))))))
6346       return 0;
6347
6348   return t1 == t2;
6349 }
6350
6351 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
6352    given by TYPE.  If the argument list accepts variable arguments,
6353    then this function counts only the ordinary arguments.  */
6354
6355 int
6356 type_num_arguments (const_tree type)
6357 {
6358   int i = 0;
6359   tree t;
6360
6361   for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
6362     /* If the function does not take a variable number of arguments,
6363        the last element in the list will have type `void'.  */
6364     if (VOID_TYPE_P (TREE_VALUE (t)))
6365       break;
6366     else
6367       ++i;
6368
6369   return i;
6370 }
6371
6372 /* Nonzero if integer constants T1 and T2
6373    represent the same constant value.  */
6374
6375 int
6376 tree_int_cst_equal (const_tree t1, const_tree t2)
6377 {
6378   if (t1 == t2)
6379     return 1;
6380
6381   if (t1 == 0 || t2 == 0)
6382     return 0;
6383
6384   if (TREE_CODE (t1) == INTEGER_CST
6385       && TREE_CODE (t2) == INTEGER_CST
6386       && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
6387       && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
6388     return 1;
6389
6390   return 0;
6391 }
6392
6393 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
6394    The precise way of comparison depends on their data type.  */
6395
6396 int
6397 tree_int_cst_lt (const_tree t1, const_tree t2)
6398 {
6399   if (t1 == t2)
6400     return 0;
6401
6402   if (TYPE_UNSIGNED (TREE_TYPE (t1)) != TYPE_UNSIGNED (TREE_TYPE (t2)))
6403     {
6404       int t1_sgn = tree_int_cst_sgn (t1);
6405       int t2_sgn = tree_int_cst_sgn (t2);
6406
6407       if (t1_sgn < t2_sgn)
6408         return 1;
6409       else if (t1_sgn > t2_sgn)
6410         return 0;
6411       /* Otherwise, both are non-negative, so we compare them as
6412          unsigned just in case one of them would overflow a signed
6413          type.  */
6414     }
6415   else if (!TYPE_UNSIGNED (TREE_TYPE (t1)))
6416     return INT_CST_LT (t1, t2);
6417
6418   return INT_CST_LT_UNSIGNED (t1, t2);
6419 }
6420
6421 /* Returns -1 if T1 < T2, 0 if T1 == T2, and 1 if T1 > T2.  */
6422
6423 int
6424 tree_int_cst_compare (const_tree t1, const_tree t2)
6425 {
6426   if (tree_int_cst_lt (t1, t2))
6427     return -1;
6428   else if (tree_int_cst_lt (t2, t1))
6429     return 1;
6430   else
6431     return 0;
6432 }
6433
6434 /* Return 1 if T is an INTEGER_CST that can be manipulated efficiently on
6435    the host.  If POS is zero, the value can be represented in a single
6436    HOST_WIDE_INT.  If POS is nonzero, the value must be non-negative and can
6437    be represented in a single unsigned HOST_WIDE_INT.  */
6438
6439 int
6440 host_integerp (const_tree t, int pos)
6441 {
6442   if (t == NULL_TREE)
6443     return 0;
6444
6445   return (TREE_CODE (t) == INTEGER_CST
6446           && ((TREE_INT_CST_HIGH (t) == 0
6447                && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) >= 0)
6448               || (! pos && TREE_INT_CST_HIGH (t) == -1
6449                   && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0
6450                   && (!TYPE_UNSIGNED (TREE_TYPE (t))
6451                       || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
6452                           && TYPE_IS_SIZETYPE (TREE_TYPE (t)))))
6453               || (pos && TREE_INT_CST_HIGH (t) == 0)));
6454 }
6455
6456 /* Return the HOST_WIDE_INT least significant bits of T if it is an
6457    INTEGER_CST and there is no overflow.  POS is nonzero if the result must
6458    be non-negative.  We must be able to satisfy the above conditions.  */
6459
6460 HOST_WIDE_INT
6461 tree_low_cst (const_tree t, int pos)
6462 {
6463   gcc_assert (host_integerp (t, pos));
6464   return TREE_INT_CST_LOW (t);
6465 }
6466
6467 /* Return the most significant bit of the integer constant T.  */
6468
6469 int
6470 tree_int_cst_msb (const_tree t)
6471 {
6472   int prec;
6473   HOST_WIDE_INT h;
6474   unsigned HOST_WIDE_INT l;
6475
6476   /* Note that using TYPE_PRECISION here is wrong.  We care about the
6477      actual bits, not the (arbitrary) range of the type.  */
6478   prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (t))) - 1;
6479   rshift_double (TREE_INT_CST_LOW (t), TREE_INT_CST_HIGH (t), prec,
6480                  2 * HOST_BITS_PER_WIDE_INT, &l, &h, 0);
6481   return (l & 1) == 1;
6482 }
6483
6484 /* Return an indication of the sign of the integer constant T.
6485    The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
6486    Note that -1 will never be returned if T's type is unsigned.  */
6487
6488 int
6489 tree_int_cst_sgn (const_tree t)
6490 {
6491   if (TREE_INT_CST_LOW (t) == 0 && TREE_INT_CST_HIGH (t) == 0)
6492     return 0;
6493   else if (TYPE_UNSIGNED (TREE_TYPE (t)))
6494     return 1;
6495   else if (TREE_INT_CST_HIGH (t) < 0)
6496     return -1;
6497   else
6498     return 1;
6499 }
6500
6501 /* Return the minimum number of bits needed to represent VALUE in a
6502    signed or unsigned type, UNSIGNEDP says which.  */
6503
6504 unsigned int
6505 tree_int_cst_min_precision (tree value, bool unsignedp)
6506 {
6507   int log;
6508
6509   /* If the value is negative, compute its negative minus 1.  The latter
6510      adjustment is because the absolute value of the largest negative value
6511      is one larger than the largest positive value.  This is equivalent to
6512      a bit-wise negation, so use that operation instead.  */
6513
6514   if (tree_int_cst_sgn (value) < 0)
6515     value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
6516
6517   /* Return the number of bits needed, taking into account the fact
6518      that we need one more bit for a signed than unsigned type.  */
6519
6520   if (integer_zerop (value))
6521     log = 0;
6522   else
6523     log = tree_floor_log2 (value);
6524
6525   return log + 1 + !unsignedp;
6526 }
6527
6528 /* Compare two constructor-element-type constants.  Return 1 if the lists
6529    are known to be equal; otherwise return 0.  */
6530
6531 int
6532 simple_cst_list_equal (const_tree l1, const_tree l2)
6533 {
6534   while (l1 != NULL_TREE && l2 != NULL_TREE)
6535     {
6536       if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
6537         return 0;
6538
6539       l1 = TREE_CHAIN (l1);
6540       l2 = TREE_CHAIN (l2);
6541     }
6542
6543   return l1 == l2;
6544 }
6545
6546 /* Return truthvalue of whether T1 is the same tree structure as T2.
6547    Return 1 if they are the same.
6548    Return 0 if they are understandably different.
6549    Return -1 if either contains tree structure not understood by
6550    this function.  */
6551
6552 int
6553 simple_cst_equal (const_tree t1, const_tree t2)
6554 {
6555   enum tree_code code1, code2;
6556   int cmp;
6557   int i;
6558
6559   if (t1 == t2)
6560     return 1;
6561   if (t1 == 0 || t2 == 0)
6562     return 0;
6563
6564   code1 = TREE_CODE (t1);
6565   code2 = TREE_CODE (t2);
6566
6567   if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
6568     {
6569       if (CONVERT_EXPR_CODE_P (code2)
6570           || code2 == NON_LVALUE_EXPR)
6571         return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6572       else
6573         return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
6574     }
6575
6576   else if (CONVERT_EXPR_CODE_P (code2)
6577            || code2 == NON_LVALUE_EXPR)
6578     return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
6579
6580   if (code1 != code2)
6581     return 0;
6582
6583   switch (code1)
6584     {
6585     case INTEGER_CST:
6586       return (TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
6587               && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2));
6588
6589     case REAL_CST:
6590       return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
6591
6592     case FIXED_CST:
6593       return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
6594
6595     case STRING_CST:
6596       return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
6597               && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
6598                          TREE_STRING_LENGTH (t1)));
6599
6600     case CONSTRUCTOR:
6601       {
6602         unsigned HOST_WIDE_INT idx;
6603         VEC(constructor_elt, gc) *v1 = CONSTRUCTOR_ELTS (t1);
6604         VEC(constructor_elt, gc) *v2 = CONSTRUCTOR_ELTS (t2);
6605
6606         if (VEC_length (constructor_elt, v1) != VEC_length (constructor_elt, v2))
6607           return false;
6608
6609         for (idx = 0; idx < VEC_length (constructor_elt, v1); ++idx)
6610           /* ??? Should we handle also fields here? */
6611           if (!simple_cst_equal (VEC_index (constructor_elt, v1, idx)->value,
6612                                  VEC_index (constructor_elt, v2, idx)->value))
6613             return false;
6614         return true;
6615       }
6616
6617     case SAVE_EXPR:
6618       return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6619
6620     case CALL_EXPR:
6621       cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
6622       if (cmp <= 0)
6623         return cmp;
6624       if (call_expr_nargs (t1) != call_expr_nargs (t2))
6625         return 0;
6626       {
6627         const_tree arg1, arg2;
6628         const_call_expr_arg_iterator iter1, iter2;
6629         for (arg1 = first_const_call_expr_arg (t1, &iter1),
6630                arg2 = first_const_call_expr_arg (t2, &iter2);
6631              arg1 && arg2;
6632              arg1 = next_const_call_expr_arg (&iter1),
6633                arg2 = next_const_call_expr_arg (&iter2))
6634           {
6635             cmp = simple_cst_equal (arg1, arg2);
6636             if (cmp <= 0)
6637               return cmp;
6638           }
6639         return arg1 == arg2;
6640       }
6641
6642     case TARGET_EXPR:
6643       /* Special case: if either target is an unallocated VAR_DECL,
6644          it means that it's going to be unified with whatever the
6645          TARGET_EXPR is really supposed to initialize, so treat it
6646          as being equivalent to anything.  */
6647       if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
6648            && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
6649            && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
6650           || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
6651               && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
6652               && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
6653         cmp = 1;
6654       else
6655         cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6656
6657       if (cmp <= 0)
6658         return cmp;
6659
6660       return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
6661
6662     case WITH_CLEANUP_EXPR:
6663       cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6664       if (cmp <= 0)
6665         return cmp;
6666
6667       return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
6668
6669     case COMPONENT_REF:
6670       if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
6671         return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
6672
6673       return 0;
6674
6675     case VAR_DECL:
6676     case PARM_DECL:
6677     case CONST_DECL:
6678     case FUNCTION_DECL:
6679       return 0;
6680
6681     default:
6682       break;
6683     }
6684
6685   /* This general rule works for most tree codes.  All exceptions should be
6686      handled above.  If this is a language-specific tree code, we can't
6687      trust what might be in the operand, so say we don't know
6688      the situation.  */
6689   if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
6690     return -1;
6691
6692   switch (TREE_CODE_CLASS (code1))
6693     {
6694     case tcc_unary:
6695     case tcc_binary:
6696     case tcc_comparison:
6697     case tcc_expression:
6698     case tcc_reference:
6699     case tcc_statement:
6700       cmp = 1;
6701       for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
6702         {
6703           cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
6704           if (cmp <= 0)
6705             return cmp;
6706         }
6707
6708       return cmp;
6709
6710     default:
6711       return -1;
6712     }
6713 }
6714
6715 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
6716    Return -1, 0, or 1 if the value of T is less than, equal to, or greater
6717    than U, respectively.  */
6718
6719 int
6720 compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
6721 {
6722   if (tree_int_cst_sgn (t) < 0)
6723     return -1;
6724   else if (TREE_INT_CST_HIGH (t) != 0)
6725     return 1;
6726   else if (TREE_INT_CST_LOW (t) == u)
6727     return 0;
6728   else if (TREE_INT_CST_LOW (t) < u)
6729     return -1;
6730   else
6731     return 1;
6732 }
6733
6734 /* Return true if CODE represents an associative tree code.  Otherwise
6735    return false.  */
6736 bool
6737 associative_tree_code (enum tree_code code)
6738 {
6739   switch (code)
6740     {
6741     case BIT_IOR_EXPR:
6742     case BIT_AND_EXPR:
6743     case BIT_XOR_EXPR:
6744     case PLUS_EXPR:
6745     case MULT_EXPR:
6746     case MIN_EXPR:
6747     case MAX_EXPR:
6748       return true;
6749
6750     default:
6751       break;
6752     }
6753   return false;
6754 }
6755
6756 /* Return true if CODE represents a commutative tree code.  Otherwise
6757    return false.  */
6758 bool
6759 commutative_tree_code (enum tree_code code)
6760 {
6761   switch (code)
6762     {
6763     case PLUS_EXPR:
6764     case MULT_EXPR:
6765     case MIN_EXPR:
6766     case MAX_EXPR:
6767     case BIT_IOR_EXPR:
6768     case BIT_XOR_EXPR:
6769     case BIT_AND_EXPR:
6770     case NE_EXPR:
6771     case EQ_EXPR:
6772     case UNORDERED_EXPR:
6773     case ORDERED_EXPR:
6774     case UNEQ_EXPR:
6775     case LTGT_EXPR:
6776     case TRUTH_AND_EXPR:
6777     case TRUTH_XOR_EXPR:
6778     case TRUTH_OR_EXPR:
6779       return true;
6780
6781     default:
6782       break;
6783     }
6784   return false;
6785 }
6786
6787 /* Return true if CODE represents a ternary tree code for which the
6788    first two operands are commutative.  Otherwise return false.  */
6789 bool
6790 commutative_ternary_tree_code (enum tree_code code)
6791 {
6792   switch (code)
6793     {
6794     case WIDEN_MULT_PLUS_EXPR:
6795     case WIDEN_MULT_MINUS_EXPR:
6796       return true;
6797
6798     default:
6799       break;
6800     }
6801   return false;
6802 }
6803
6804 /* Generate a hash value for an expression.  This can be used iteratively
6805    by passing a previous result as the VAL argument.
6806
6807    This function is intended to produce the same hash for expressions which
6808    would compare equal using operand_equal_p.  */
6809
6810 hashval_t
6811 iterative_hash_expr (const_tree t, hashval_t val)
6812 {
6813   int i;
6814   enum tree_code code;
6815   char tclass;
6816
6817   if (t == NULL_TREE)
6818     return iterative_hash_hashval_t (0, val);
6819
6820   code = TREE_CODE (t);
6821
6822   switch (code)
6823     {
6824     /* Alas, constants aren't shared, so we can't rely on pointer
6825        identity.  */
6826     case INTEGER_CST:
6827       val = iterative_hash_host_wide_int (TREE_INT_CST_LOW (t), val);
6828       return iterative_hash_host_wide_int (TREE_INT_CST_HIGH (t), val);
6829     case REAL_CST:
6830       {
6831         unsigned int val2 = real_hash (TREE_REAL_CST_PTR (t));
6832
6833         return iterative_hash_hashval_t (val2, val);
6834       }
6835     case FIXED_CST:
6836       {
6837         unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
6838
6839         return iterative_hash_hashval_t (val2, val);
6840       }
6841     case STRING_CST:
6842       return iterative_hash (TREE_STRING_POINTER (t),
6843                              TREE_STRING_LENGTH (t), val);
6844     case COMPLEX_CST:
6845       val = iterative_hash_expr (TREE_REALPART (t), val);
6846       return iterative_hash_expr (TREE_IMAGPART (t), val);
6847     case VECTOR_CST:
6848       return iterative_hash_expr (TREE_VECTOR_CST_ELTS (t), val);
6849     case SSA_NAME:
6850       /* We can just compare by pointer.  */
6851       return iterative_hash_host_wide_int (SSA_NAME_VERSION (t), val);
6852     case PLACEHOLDER_EXPR:
6853       /* The node itself doesn't matter.  */
6854       return val;
6855     case TREE_LIST:
6856       /* A list of expressions, for a CALL_EXPR or as the elements of a
6857          VECTOR_CST.  */
6858       for (; t; t = TREE_CHAIN (t))
6859         val = iterative_hash_expr (TREE_VALUE (t), val);
6860       return val;
6861     case CONSTRUCTOR:
6862       {
6863         unsigned HOST_WIDE_INT idx;
6864         tree field, value;
6865         FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
6866           {
6867             val = iterative_hash_expr (field, val);
6868             val = iterative_hash_expr (value, val);
6869           }
6870         return val;
6871       }
6872     case MEM_REF:
6873       {
6874         /* The type of the second operand is relevant, except for
6875            its top-level qualifiers.  */
6876         tree type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (t, 1)));
6877
6878         val = iterative_hash_object (TYPE_HASH (type), val);
6879
6880         /* We could use the standard hash computation from this point
6881            on.  */
6882         val = iterative_hash_object (code, val);
6883         val = iterative_hash_expr (TREE_OPERAND (t, 1), val);
6884         val = iterative_hash_expr (TREE_OPERAND (t, 0), val);
6885         return val;
6886       }
6887     case FUNCTION_DECL:
6888       /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
6889          Otherwise nodes that compare equal according to operand_equal_p might
6890          get different hash codes.  However, don't do this for machine specific
6891          or front end builtins, since the function code is overloaded in those
6892          cases.  */
6893       if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
6894           && built_in_decls[DECL_FUNCTION_CODE (t)])
6895         {
6896           t = built_in_decls[DECL_FUNCTION_CODE (t)];
6897           code = TREE_CODE (t);
6898         }
6899       /* FALL THROUGH */
6900     default:
6901       tclass = TREE_CODE_CLASS (code);
6902
6903       if (tclass == tcc_declaration)
6904         {
6905           /* DECL's have a unique ID */
6906           val = iterative_hash_host_wide_int (DECL_UID (t), val);
6907         }
6908       else
6909         {
6910           gcc_assert (IS_EXPR_CODE_CLASS (tclass));
6911
6912           val = iterative_hash_object (code, val);
6913
6914           /* Don't hash the type, that can lead to having nodes which
6915              compare equal according to operand_equal_p, but which
6916              have different hash codes.  */
6917           if (CONVERT_EXPR_CODE_P (code)
6918               || code == NON_LVALUE_EXPR)
6919             {
6920               /* Make sure to include signness in the hash computation.  */
6921               val += TYPE_UNSIGNED (TREE_TYPE (t));
6922               val = iterative_hash_expr (TREE_OPERAND (t, 0), val);
6923             }
6924
6925           else if (commutative_tree_code (code))
6926             {
6927               /* It's a commutative expression.  We want to hash it the same
6928                  however it appears.  We do this by first hashing both operands
6929                  and then rehashing based on the order of their independent
6930                  hashes.  */
6931               hashval_t one = iterative_hash_expr (TREE_OPERAND (t, 0), 0);
6932               hashval_t two = iterative_hash_expr (TREE_OPERAND (t, 1), 0);
6933               hashval_t t;
6934
6935               if (one > two)
6936                 t = one, one = two, two = t;
6937
6938               val = iterative_hash_hashval_t (one, val);
6939               val = iterative_hash_hashval_t (two, val);
6940             }
6941           else
6942             for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
6943               val = iterative_hash_expr (TREE_OPERAND (t, i), val);
6944         }
6945       return val;
6946       break;
6947     }
6948 }
6949
6950 /* Generate a hash value for a pair of expressions.  This can be used
6951    iteratively by passing a previous result as the VAL argument.
6952
6953    The same hash value is always returned for a given pair of expressions,
6954    regardless of the order in which they are presented.  This is useful in
6955    hashing the operands of commutative functions.  */
6956
6957 hashval_t
6958 iterative_hash_exprs_commutative (const_tree t1,
6959                                   const_tree t2, hashval_t val)
6960 {
6961   hashval_t one = iterative_hash_expr (t1, 0);
6962   hashval_t two = iterative_hash_expr (t2, 0);
6963   hashval_t t;
6964
6965   if (one > two)
6966     t = one, one = two, two = t;
6967   val = iterative_hash_hashval_t (one, val);
6968   val = iterative_hash_hashval_t (two, val);
6969
6970   return val;
6971 }
6972 \f
6973 /* Constructors for pointer, array and function types.
6974    (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
6975    constructed by language-dependent code, not here.)  */
6976
6977 /* Construct, lay out and return the type of pointers to TO_TYPE with
6978    mode MODE.  If CAN_ALIAS_ALL is TRUE, indicate this type can
6979    reference all of memory. If such a type has already been
6980    constructed, reuse it.  */
6981
6982 tree
6983 build_pointer_type_for_mode (tree to_type, enum machine_mode mode,
6984                              bool can_alias_all)
6985 {
6986   tree t;
6987
6988   if (to_type == error_mark_node)
6989     return error_mark_node;
6990
6991   /* If the pointed-to type has the may_alias attribute set, force
6992      a TYPE_REF_CAN_ALIAS_ALL pointer to be generated.  */
6993   if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
6994     can_alias_all = true;
6995
6996   /* In some cases, languages will have things that aren't a POINTER_TYPE
6997      (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
6998      In that case, return that type without regard to the rest of our
6999      operands.
7000
7001      ??? This is a kludge, but consistent with the way this function has
7002      always operated and there doesn't seem to be a good way to avoid this
7003      at the moment.  */
7004   if (TYPE_POINTER_TO (to_type) != 0
7005       && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
7006     return TYPE_POINTER_TO (to_type);
7007
7008   /* First, if we already have a type for pointers to TO_TYPE and it's
7009      the proper mode, use it.  */
7010   for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
7011     if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7012       return t;
7013
7014   t = make_node (POINTER_TYPE);
7015
7016   TREE_TYPE (t) = to_type;
7017   SET_TYPE_MODE (t, mode);
7018   TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7019   TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
7020   TYPE_POINTER_TO (to_type) = t;
7021
7022   if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7023     SET_TYPE_STRUCTURAL_EQUALITY (t);
7024   else if (TYPE_CANONICAL (to_type) != to_type)
7025     TYPE_CANONICAL (t)
7026       = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
7027                                      mode, can_alias_all);
7028
7029   /* Lay out the type.  This function has many callers that are concerned
7030      with expression-construction, and this simplifies them all.  */
7031   layout_type (t);
7032
7033   return t;
7034 }
7035
7036 /* By default build pointers in ptr_mode.  */
7037
7038 tree
7039 build_pointer_type (tree to_type)
7040 {
7041   addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7042                                               : TYPE_ADDR_SPACE (to_type);
7043   enum machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7044   return build_pointer_type_for_mode (to_type, pointer_mode, false);
7045 }
7046
7047 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE.  */
7048
7049 tree
7050 build_reference_type_for_mode (tree to_type, enum machine_mode mode,
7051                                bool can_alias_all)
7052 {
7053   tree t;
7054
7055   if (to_type == error_mark_node)
7056     return error_mark_node;
7057
7058   /* If the pointed-to type has the may_alias attribute set, force
7059      a TYPE_REF_CAN_ALIAS_ALL pointer to be generated.  */
7060   if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7061     can_alias_all = true;
7062
7063   /* In some cases, languages will have things that aren't a REFERENCE_TYPE
7064      (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
7065      In that case, return that type without regard to the rest of our
7066      operands.
7067
7068      ??? This is a kludge, but consistent with the way this function has
7069      always operated and there doesn't seem to be a good way to avoid this
7070      at the moment.  */
7071   if (TYPE_REFERENCE_TO (to_type) != 0
7072       && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
7073     return TYPE_REFERENCE_TO (to_type);
7074
7075   /* First, if we already have a type for pointers to TO_TYPE and it's
7076      the proper mode, use it.  */
7077   for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
7078     if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7079       return t;
7080
7081   t = make_node (REFERENCE_TYPE);
7082
7083   TREE_TYPE (t) = to_type;
7084   SET_TYPE_MODE (t, mode);
7085   TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7086   TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
7087   TYPE_REFERENCE_TO (to_type) = t;
7088
7089   if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7090     SET_TYPE_STRUCTURAL_EQUALITY (t);
7091   else if (TYPE_CANONICAL (to_type) != to_type)
7092     TYPE_CANONICAL (t)
7093       = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
7094                                        mode, can_alias_all);
7095
7096   layout_type (t);
7097
7098   return t;
7099 }
7100
7101
7102 /* Build the node for the type of references-to-TO_TYPE by default
7103    in ptr_mode.  */
7104
7105 tree
7106 build_reference_type (tree to_type)
7107 {
7108   addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7109                                               : TYPE_ADDR_SPACE (to_type);
7110   enum machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7111   return build_reference_type_for_mode (to_type, pointer_mode, false);
7112 }
7113
7114 /* Build a type that is compatible with t but has no cv quals anywhere
7115    in its type, thus
7116
7117    const char *const *const *  ->  char ***.  */
7118
7119 tree
7120 build_type_no_quals (tree t)
7121 {
7122   switch (TREE_CODE (t))
7123     {
7124     case POINTER_TYPE:
7125       return build_pointer_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
7126                                           TYPE_MODE (t),
7127                                           TYPE_REF_CAN_ALIAS_ALL (t));
7128     case REFERENCE_TYPE:
7129       return
7130         build_reference_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
7131                                        TYPE_MODE (t),
7132                                        TYPE_REF_CAN_ALIAS_ALL (t));
7133     default:
7134       return TYPE_MAIN_VARIANT (t);
7135     }
7136 }
7137
7138 #define MAX_INT_CACHED_PREC \
7139   (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
7140 static GTY(()) tree nonstandard_integer_type_cache[2 * MAX_INT_CACHED_PREC + 2];
7141
7142 /* Builds a signed or unsigned integer type of precision PRECISION.
7143    Used for C bitfields whose precision does not match that of
7144    built-in target types.  */
7145 tree
7146 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
7147                                 int unsignedp)
7148 {
7149   tree itype, ret;
7150
7151   if (unsignedp)
7152     unsignedp = MAX_INT_CACHED_PREC + 1;
7153     
7154   if (precision <= MAX_INT_CACHED_PREC)
7155     {
7156       itype = nonstandard_integer_type_cache[precision + unsignedp];
7157       if (itype)
7158         return itype;
7159     }
7160
7161   itype = make_node (INTEGER_TYPE);
7162   TYPE_PRECISION (itype) = precision;
7163
7164   if (unsignedp)
7165     fixup_unsigned_type (itype);
7166   else
7167     fixup_signed_type (itype);
7168
7169   ret = itype;
7170   if (host_integerp (TYPE_MAX_VALUE (itype), 1))
7171     ret = type_hash_canon (tree_low_cst (TYPE_MAX_VALUE (itype), 1), itype);
7172   if (precision <= MAX_INT_CACHED_PREC)
7173     nonstandard_integer_type_cache[precision + unsignedp] = ret;
7174
7175   return ret;
7176 }
7177
7178 /* Create a range of some discrete type TYPE (an INTEGER_TYPE, ENUMERAL_TYPE
7179    or BOOLEAN_TYPE) with low bound LOWVAL and high bound HIGHVAL.  If SHARED
7180    is true, reuse such a type that has already been constructed.  */
7181
7182 static tree
7183 build_range_type_1 (tree type, tree lowval, tree highval, bool shared)
7184 {
7185   tree itype = make_node (INTEGER_TYPE);
7186   hashval_t hashcode = 0;
7187
7188   TREE_TYPE (itype) = type;
7189
7190   TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
7191   TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
7192
7193   TYPE_PRECISION (itype) = TYPE_PRECISION (type);
7194   SET_TYPE_MODE (itype, TYPE_MODE (type));
7195   TYPE_SIZE (itype) = TYPE_SIZE (type);
7196   TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
7197   TYPE_ALIGN (itype) = TYPE_ALIGN (type);
7198   TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
7199
7200   if (!shared)
7201     return itype;
7202
7203   if ((TYPE_MIN_VALUE (itype)
7204        && TREE_CODE (TYPE_MIN_VALUE (itype)) != INTEGER_CST)
7205       || (TYPE_MAX_VALUE (itype)
7206           && TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST))
7207     {
7208       /* Since we cannot reliably merge this type, we need to compare it using
7209          structural equality checks.  */
7210       SET_TYPE_STRUCTURAL_EQUALITY (itype);
7211       return itype;
7212     }
7213
7214   hashcode = iterative_hash_expr (TYPE_MIN_VALUE (itype), hashcode);
7215   hashcode = iterative_hash_expr (TYPE_MAX_VALUE (itype), hashcode);
7216   hashcode = iterative_hash_hashval_t (TYPE_HASH (type), hashcode);
7217   itype = type_hash_canon (hashcode, itype);
7218
7219   return itype;
7220 }
7221
7222 /* Wrapper around build_range_type_1 with SHARED set to true.  */
7223
7224 tree
7225 build_range_type (tree type, tree lowval, tree highval)
7226 {
7227   return build_range_type_1 (type, lowval, highval, true);
7228 }
7229
7230 /* Wrapper around build_range_type_1 with SHARED set to false.  */
7231
7232 tree
7233 build_nonshared_range_type (tree type, tree lowval, tree highval)
7234 {
7235   return build_range_type_1 (type, lowval, highval, false);
7236 }
7237
7238 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
7239    MAXVAL should be the maximum value in the domain
7240    (one less than the length of the array).
7241
7242    The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
7243    We don't enforce this limit, that is up to caller (e.g. language front end).
7244    The limit exists because the result is a signed type and we don't handle
7245    sizes that use more than one HOST_WIDE_INT.  */
7246
7247 tree
7248 build_index_type (tree maxval)
7249 {
7250   return build_range_type (sizetype, size_zero_node, maxval);
7251 }
7252
7253 /* Return true if the debug information for TYPE, a subtype, should be emitted
7254    as a subrange type.  If so, set LOWVAL to the low bound and HIGHVAL to the
7255    high bound, respectively.  Sometimes doing so unnecessarily obfuscates the
7256    debug info and doesn't reflect the source code.  */
7257
7258 bool
7259 subrange_type_for_debug_p (const_tree type, tree *lowval, tree *highval)
7260 {
7261   tree base_type = TREE_TYPE (type), low, high;
7262
7263   /* Subrange types have a base type which is an integral type.  */
7264   if (!INTEGRAL_TYPE_P (base_type))
7265     return false;
7266
7267   /* Get the real bounds of the subtype.  */
7268   if (lang_hooks.types.get_subrange_bounds)
7269     lang_hooks.types.get_subrange_bounds (type, &low, &high);
7270   else
7271     {
7272       low = TYPE_MIN_VALUE (type);
7273       high = TYPE_MAX_VALUE (type);
7274     }
7275
7276   /* If the type and its base type have the same representation and the same
7277      name, then the type is not a subrange but a copy of the base type.  */
7278   if ((TREE_CODE (base_type) == INTEGER_TYPE
7279        || TREE_CODE (base_type) == BOOLEAN_TYPE)
7280       && int_size_in_bytes (type) == int_size_in_bytes (base_type)
7281       && tree_int_cst_equal (low, TYPE_MIN_VALUE (base_type))
7282       && tree_int_cst_equal (high, TYPE_MAX_VALUE (base_type)))
7283     {
7284       tree type_name = TYPE_NAME (type);
7285       tree base_type_name = TYPE_NAME (base_type);
7286
7287       if (type_name && TREE_CODE (type_name) == TYPE_DECL)
7288         type_name = DECL_NAME (type_name);
7289
7290       if (base_type_name && TREE_CODE (base_type_name) == TYPE_DECL)
7291         base_type_name = DECL_NAME (base_type_name);
7292
7293       if (type_name == base_type_name)
7294         return false;
7295     }
7296
7297   if (lowval)
7298     *lowval = low;
7299   if (highval)
7300     *highval = high;
7301   return true;
7302 }
7303
7304 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
7305    and number of elements specified by the range of values of INDEX_TYPE.
7306    If SHARED is true, reuse such a type that has already been constructed.  */
7307
7308 static tree
7309 build_array_type_1 (tree elt_type, tree index_type, bool shared)
7310 {
7311   tree t;
7312
7313   if (TREE_CODE (elt_type) == FUNCTION_TYPE)
7314     {
7315       error ("arrays of functions are not meaningful");
7316       elt_type = integer_type_node;
7317     }
7318
7319   t = make_node (ARRAY_TYPE);
7320   TREE_TYPE (t) = elt_type;
7321   TYPE_DOMAIN (t) = index_type;
7322   TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (elt_type);
7323   layout_type (t);
7324
7325   /* If the element type is incomplete at this point we get marked for
7326      structural equality.  Do not record these types in the canonical
7327      type hashtable.  */
7328   if (TYPE_STRUCTURAL_EQUALITY_P (t))
7329     return t;
7330
7331   if (shared)
7332     {
7333       hashval_t hashcode = iterative_hash_object (TYPE_HASH (elt_type), 0);
7334       if (index_type)
7335         hashcode = iterative_hash_object (TYPE_HASH (index_type), hashcode);
7336       t = type_hash_canon (hashcode, t);
7337     }
7338
7339   if (TYPE_CANONICAL (t) == t)
7340     {
7341       if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
7342           || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
7343         SET_TYPE_STRUCTURAL_EQUALITY (t);
7344       else if (TYPE_CANONICAL (elt_type) != elt_type
7345                || (index_type && TYPE_CANONICAL (index_type) != index_type))
7346         TYPE_CANONICAL (t)
7347           = build_array_type_1 (TYPE_CANONICAL (elt_type),
7348                                 index_type
7349                                 ? TYPE_CANONICAL (index_type) : NULL_TREE,
7350                                 shared);
7351     }
7352
7353   return t;
7354 }
7355
7356 /* Wrapper around build_array_type_1 with SHARED set to true.  */
7357
7358 tree
7359 build_array_type (tree elt_type, tree index_type)
7360 {
7361   return build_array_type_1 (elt_type, index_type, true);
7362 }
7363
7364 /* Wrapper around build_array_type_1 with SHARED set to false.  */
7365
7366 tree
7367 build_nonshared_array_type (tree elt_type, tree index_type)
7368 {
7369   return build_array_type_1 (elt_type, index_type, false);
7370 }
7371
7372 /* Recursively examines the array elements of TYPE, until a non-array
7373    element type is found.  */
7374
7375 tree
7376 strip_array_types (tree type)
7377 {
7378   while (TREE_CODE (type) == ARRAY_TYPE)
7379     type = TREE_TYPE (type);
7380
7381   return type;
7382 }
7383
7384 /* Computes the canonical argument types from the argument type list
7385    ARGTYPES.
7386
7387    Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
7388    on entry to this function, or if any of the ARGTYPES are
7389    structural.
7390
7391    Upon return, *ANY_NONCANONICAL_P will be true iff either it was
7392    true on entry to this function, or if any of the ARGTYPES are
7393    non-canonical.
7394
7395    Returns a canonical argument list, which may be ARGTYPES when the
7396    canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
7397    true) or would not differ from ARGTYPES.  */
7398
7399 static tree
7400 maybe_canonicalize_argtypes(tree argtypes,
7401                             bool *any_structural_p,
7402                             bool *any_noncanonical_p)
7403 {
7404   tree arg;
7405   bool any_noncanonical_argtypes_p = false;
7406
7407   for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
7408     {
7409       if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
7410         /* Fail gracefully by stating that the type is structural.  */
7411         *any_structural_p = true;
7412       else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
7413         *any_structural_p = true;
7414       else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
7415                || TREE_PURPOSE (arg))
7416         /* If the argument has a default argument, we consider it
7417            non-canonical even though the type itself is canonical.
7418            That way, different variants of function and method types
7419            with default arguments will all point to the variant with
7420            no defaults as their canonical type.  */
7421         any_noncanonical_argtypes_p = true;
7422     }
7423
7424   if (*any_structural_p)
7425     return argtypes;
7426
7427   if (any_noncanonical_argtypes_p)
7428     {
7429       /* Build the canonical list of argument types.  */
7430       tree canon_argtypes = NULL_TREE;
7431       bool is_void = false;
7432
7433       for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
7434         {
7435           if (arg == void_list_node)
7436             is_void = true;
7437           else
7438             canon_argtypes = tree_cons (NULL_TREE,
7439                                         TYPE_CANONICAL (TREE_VALUE (arg)),
7440                                         canon_argtypes);
7441         }
7442
7443       canon_argtypes = nreverse (canon_argtypes);
7444       if (is_void)
7445         canon_argtypes = chainon (canon_argtypes, void_list_node);
7446
7447       /* There is a non-canonical type.  */
7448       *any_noncanonical_p = true;
7449       return canon_argtypes;
7450     }
7451
7452   /* The canonical argument types are the same as ARGTYPES.  */
7453   return argtypes;
7454 }
7455
7456 /* Construct, lay out and return
7457    the type of functions returning type VALUE_TYPE
7458    given arguments of types ARG_TYPES.
7459    ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
7460    are data type nodes for the arguments of the function.
7461    If such a type has already been constructed, reuse it.  */
7462
7463 tree
7464 build_function_type (tree value_type, tree arg_types)
7465 {
7466   tree t;
7467   hashval_t hashcode = 0;
7468   bool any_structural_p, any_noncanonical_p;
7469   tree canon_argtypes;
7470
7471   if (TREE_CODE (value_type) == FUNCTION_TYPE)
7472     {
7473       error ("function return type cannot be function");
7474       value_type = integer_type_node;
7475     }
7476
7477   /* Make a node of the sort we want.  */
7478   t = make_node (FUNCTION_TYPE);
7479   TREE_TYPE (t) = value_type;
7480   TYPE_ARG_TYPES (t) = arg_types;
7481
7482   /* If we already have such a type, use the old one.  */
7483   hashcode = iterative_hash_object (TYPE_HASH (value_type), hashcode);
7484   hashcode = type_hash_list (arg_types, hashcode);
7485   t = type_hash_canon (hashcode, t);
7486
7487   /* Set up the canonical type. */
7488   any_structural_p   = TYPE_STRUCTURAL_EQUALITY_P (value_type);
7489   any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
7490   canon_argtypes = maybe_canonicalize_argtypes (arg_types,
7491                                                 &any_structural_p,
7492                                                 &any_noncanonical_p);
7493   if (any_structural_p)
7494     SET_TYPE_STRUCTURAL_EQUALITY (t);
7495   else if (any_noncanonical_p)
7496     TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
7497                                               canon_argtypes);
7498
7499   if (!COMPLETE_TYPE_P (t))
7500     layout_type (t);
7501   return t;
7502 }
7503
7504 /* Build variant of function type ORIG_TYPE skipping ARGS_TO_SKIP.  */
7505
7506 tree
7507 build_function_type_skip_args (tree orig_type, bitmap args_to_skip)
7508 {
7509   tree new_type = NULL;
7510   tree args, new_args = NULL, t;
7511   tree new_reversed;
7512   int i = 0;
7513
7514   for (args = TYPE_ARG_TYPES (orig_type); args && args != void_list_node;
7515        args = TREE_CHAIN (args), i++)
7516     if (!bitmap_bit_p (args_to_skip, i))
7517       new_args = tree_cons (NULL_TREE, TREE_VALUE (args), new_args);
7518
7519   new_reversed = nreverse (new_args);
7520   if (args)
7521     {
7522       if (new_reversed)
7523         TREE_CHAIN (new_args) = void_list_node;
7524       else
7525         new_reversed = void_list_node;
7526     }
7527
7528   /* Use copy_node to preserve as much as possible from original type
7529      (debug info, attribute lists etc.)
7530      Exception is METHOD_TYPEs must have THIS argument.
7531      When we are asked to remove it, we need to build new FUNCTION_TYPE
7532      instead.  */
7533   if (TREE_CODE (orig_type) != METHOD_TYPE
7534       || !bitmap_bit_p (args_to_skip, 0))
7535     {
7536       new_type = build_distinct_type_copy (orig_type);
7537       TYPE_ARG_TYPES (new_type) = new_reversed;
7538     }
7539   else
7540     {
7541       new_type
7542         = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type),
7543                                                          new_reversed));
7544       TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
7545     }
7546
7547   /* This is a new type, not a copy of an old type.  Need to reassociate
7548      variants.  We can handle everything except the main variant lazily.  */
7549   t = TYPE_MAIN_VARIANT (orig_type);
7550   if (orig_type != t)
7551     {
7552       TYPE_MAIN_VARIANT (new_type) = t;
7553       TYPE_NEXT_VARIANT (new_type) = TYPE_NEXT_VARIANT (t);
7554       TYPE_NEXT_VARIANT (t) = new_type;
7555     }
7556   else
7557     {
7558       TYPE_MAIN_VARIANT (new_type) = new_type;
7559       TYPE_NEXT_VARIANT (new_type) = NULL;
7560     }
7561   return new_type;
7562 }
7563
7564 /* Build variant of function type ORIG_TYPE skipping ARGS_TO_SKIP.
7565
7566    Arguments from DECL_ARGUMENTS list can't be removed now, since they are
7567    linked by TREE_CHAIN directly.  The caller is responsible for eliminating
7568    them when they are being duplicated (i.e. copy_arguments_for_versioning).  */
7569
7570 tree
7571 build_function_decl_skip_args (tree orig_decl, bitmap args_to_skip)
7572 {
7573   tree new_decl = copy_node (orig_decl);
7574   tree new_type;
7575
7576   new_type = TREE_TYPE (orig_decl);
7577   if (prototype_p (new_type))
7578     new_type = build_function_type_skip_args (new_type, args_to_skip);
7579   TREE_TYPE (new_decl) = new_type;
7580
7581   /* For declarations setting DECL_VINDEX (i.e. methods)
7582      we expect first argument to be THIS pointer.   */
7583   if (bitmap_bit_p (args_to_skip, 0))
7584     DECL_VINDEX (new_decl) = NULL_TREE;
7585
7586   /* When signature changes, we need to clear builtin info.  */
7587   if (DECL_BUILT_IN (new_decl) && !bitmap_empty_p (args_to_skip))
7588     {
7589       DECL_BUILT_IN_CLASS (new_decl) = NOT_BUILT_IN;
7590       DECL_FUNCTION_CODE (new_decl) = (enum built_in_function) 0;
7591     }
7592   return new_decl;
7593 }
7594
7595 /* Build a function type.  The RETURN_TYPE is the type returned by the
7596    function.  If VAARGS is set, no void_type_node is appended to the
7597    the list.  ARGP must be always be terminated be a NULL_TREE.  */
7598
7599 static tree
7600 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
7601 {
7602   tree t, args, last;
7603
7604   t = va_arg (argp, tree);
7605   for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
7606     args = tree_cons (NULL_TREE, t, args);
7607
7608   if (vaargs)
7609     {
7610       last = args;
7611       if (args != NULL_TREE)
7612         args = nreverse (args);
7613       gcc_assert (last != void_list_node);
7614     }
7615   else if (args == NULL_TREE)
7616     args = void_list_node;
7617   else
7618     {
7619       last = args;
7620       args = nreverse (args);
7621       TREE_CHAIN (last) = void_list_node;
7622     }
7623   args = build_function_type (return_type, args);
7624
7625   return args;
7626 }
7627
7628 /* Build a function type.  The RETURN_TYPE is the type returned by the
7629    function.  If additional arguments are provided, they are
7630    additional argument types.  The list of argument types must always
7631    be terminated by NULL_TREE.  */
7632
7633 tree
7634 build_function_type_list (tree return_type, ...)
7635 {
7636   tree args;
7637   va_list p;
7638
7639   va_start (p, return_type);
7640   args = build_function_type_list_1 (false, return_type, p);
7641   va_end (p);
7642   return args;
7643 }
7644
7645 /* Build a variable argument function type.  The RETURN_TYPE is the
7646    type returned by the function.  If additional arguments are provided,
7647    they are additional argument types.  The list of argument types must
7648    always be terminated by NULL_TREE.  */
7649
7650 tree
7651 build_varargs_function_type_list (tree return_type, ...)
7652 {
7653   tree args;
7654   va_list p;
7655
7656   va_start (p, return_type);
7657   args = build_function_type_list_1 (true, return_type, p);
7658   va_end (p);
7659
7660   return args;
7661 }
7662
7663 /* Build a METHOD_TYPE for a member of BASETYPE.  The RETTYPE (a TYPE)
7664    and ARGTYPES (a TREE_LIST) are the return type and arguments types
7665    for the method.  An implicit additional parameter (of type
7666    pointer-to-BASETYPE) is added to the ARGTYPES.  */
7667
7668 tree
7669 build_method_type_directly (tree basetype,
7670                             tree rettype,
7671                             tree argtypes)
7672 {
7673   tree t;
7674   tree ptype;
7675   int hashcode = 0;
7676   bool any_structural_p, any_noncanonical_p;
7677   tree canon_argtypes;
7678
7679   /* Make a node of the sort we want.  */
7680   t = make_node (METHOD_TYPE);
7681
7682   TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
7683   TREE_TYPE (t) = rettype;
7684   ptype = build_pointer_type (basetype);
7685
7686   /* The actual arglist for this function includes a "hidden" argument
7687      which is "this".  Put it into the list of argument types.  */
7688   argtypes = tree_cons (NULL_TREE, ptype, argtypes);
7689   TYPE_ARG_TYPES (t) = argtypes;
7690
7691   /* If we already have such a type, use the old one.  */
7692   hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
7693   hashcode = iterative_hash_object (TYPE_HASH (rettype), hashcode);
7694   hashcode = type_hash_list (argtypes, hashcode);
7695   t = type_hash_canon (hashcode, t);
7696
7697   /* Set up the canonical type. */
7698   any_structural_p
7699     = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
7700        || TYPE_STRUCTURAL_EQUALITY_P (rettype));
7701   any_noncanonical_p
7702     = (TYPE_CANONICAL (basetype) != basetype
7703        || TYPE_CANONICAL (rettype) != rettype);
7704   canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
7705                                                 &any_structural_p,
7706                                                 &any_noncanonical_p);
7707   if (any_structural_p)
7708     SET_TYPE_STRUCTURAL_EQUALITY (t);
7709   else if (any_noncanonical_p)
7710     TYPE_CANONICAL (t)
7711       = build_method_type_directly (TYPE_CANONICAL (basetype),
7712                                     TYPE_CANONICAL (rettype),
7713                                     canon_argtypes);
7714   if (!COMPLETE_TYPE_P (t))
7715     layout_type (t);
7716
7717   return t;
7718 }
7719
7720 /* Construct, lay out and return the type of methods belonging to class
7721    BASETYPE and whose arguments and values are described by TYPE.
7722    If that type exists already, reuse it.
7723    TYPE must be a FUNCTION_TYPE node.  */
7724
7725 tree
7726 build_method_type (tree basetype, tree type)
7727 {
7728   gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
7729
7730   return build_method_type_directly (basetype,
7731                                      TREE_TYPE (type),
7732                                      TYPE_ARG_TYPES (type));
7733 }
7734
7735 /* Construct, lay out and return the type of offsets to a value
7736    of type TYPE, within an object of type BASETYPE.
7737    If a suitable offset type exists already, reuse it.  */
7738
7739 tree
7740 build_offset_type (tree basetype, tree type)
7741 {
7742   tree t;
7743   hashval_t hashcode = 0;
7744
7745   /* Make a node of the sort we want.  */
7746   t = make_node (OFFSET_TYPE);
7747
7748   TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
7749   TREE_TYPE (t) = type;
7750
7751   /* If we already have such a type, use the old one.  */
7752   hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
7753   hashcode = iterative_hash_object (TYPE_HASH (type), hashcode);
7754   t = type_hash_canon (hashcode, t);
7755
7756   if (!COMPLETE_TYPE_P (t))
7757     layout_type (t);
7758
7759   if (TYPE_CANONICAL (t) == t)
7760     {
7761       if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
7762           || TYPE_STRUCTURAL_EQUALITY_P (type))
7763         SET_TYPE_STRUCTURAL_EQUALITY (t);
7764       else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
7765                || TYPE_CANONICAL (type) != type)
7766         TYPE_CANONICAL (t)
7767           = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
7768                                TYPE_CANONICAL (type));
7769     }
7770
7771   return t;
7772 }
7773
7774 /* Create a complex type whose components are COMPONENT_TYPE.  */
7775
7776 tree
7777 build_complex_type (tree component_type)
7778 {
7779   tree t;
7780   hashval_t hashcode;
7781
7782   gcc_assert (INTEGRAL_TYPE_P (component_type)
7783               || SCALAR_FLOAT_TYPE_P (component_type)
7784               || FIXED_POINT_TYPE_P (component_type));
7785
7786   /* Make a node of the sort we want.  */
7787   t = make_node (COMPLEX_TYPE);
7788
7789   TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
7790
7791   /* If we already have such a type, use the old one.  */
7792   hashcode = iterative_hash_object (TYPE_HASH (component_type), 0);
7793   t = type_hash_canon (hashcode, t);
7794
7795   if (!COMPLETE_TYPE_P (t))
7796     layout_type (t);
7797
7798   if (TYPE_CANONICAL (t) == t)
7799     {
7800       if (TYPE_STRUCTURAL_EQUALITY_P (component_type))
7801         SET_TYPE_STRUCTURAL_EQUALITY (t);
7802       else if (TYPE_CANONICAL (component_type) != component_type)
7803         TYPE_CANONICAL (t)
7804           = build_complex_type (TYPE_CANONICAL (component_type));
7805     }
7806
7807   /* We need to create a name, since complex is a fundamental type.  */
7808   if (! TYPE_NAME (t))
7809     {
7810       const char *name;
7811       if (component_type == char_type_node)
7812         name = "complex char";
7813       else if (component_type == signed_char_type_node)
7814         name = "complex signed char";
7815       else if (component_type == unsigned_char_type_node)
7816         name = "complex unsigned char";
7817       else if (component_type == short_integer_type_node)
7818         name = "complex short int";
7819       else if (component_type == short_unsigned_type_node)
7820         name = "complex short unsigned int";
7821       else if (component_type == integer_type_node)
7822         name = "complex int";
7823       else if (component_type == unsigned_type_node)
7824         name = "complex unsigned int";
7825       else if (component_type == long_integer_type_node)
7826         name = "complex long int";
7827       else if (component_type == long_unsigned_type_node)
7828         name = "complex long unsigned int";
7829       else if (component_type == long_long_integer_type_node)
7830         name = "complex long long int";
7831       else if (component_type == long_long_unsigned_type_node)
7832         name = "complex long long unsigned int";
7833       else
7834         name = 0;
7835
7836       if (name != 0)
7837         TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
7838                                     get_identifier (name), t);
7839     }
7840
7841   return build_qualified_type (t, TYPE_QUALS (component_type));
7842 }
7843
7844 /* If TYPE is a real or complex floating-point type and the target
7845    does not directly support arithmetic on TYPE then return the wider
7846    type to be used for arithmetic on TYPE.  Otherwise, return
7847    NULL_TREE.  */
7848
7849 tree
7850 excess_precision_type (tree type)
7851 {
7852   if (flag_excess_precision != EXCESS_PRECISION_FAST)
7853     {
7854       int flt_eval_method = TARGET_FLT_EVAL_METHOD;
7855       switch (TREE_CODE (type))
7856         {
7857         case REAL_TYPE:
7858           switch (flt_eval_method)
7859             {
7860             case 1:
7861               if (TYPE_MODE (type) == TYPE_MODE (float_type_node))
7862                 return double_type_node;
7863               break;
7864             case 2:
7865               if (TYPE_MODE (type) == TYPE_MODE (float_type_node)
7866                   || TYPE_MODE (type) == TYPE_MODE (double_type_node))
7867                 return long_double_type_node;
7868               break;
7869             default:
7870               gcc_unreachable ();
7871             }
7872           break;
7873         case COMPLEX_TYPE:
7874           if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
7875             return NULL_TREE;
7876           switch (flt_eval_method)
7877             {
7878             case 1:
7879               if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node))
7880                 return complex_double_type_node;
7881               break;
7882             case 2:
7883               if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node)
7884                   || (TYPE_MODE (TREE_TYPE (type))
7885                       == TYPE_MODE (double_type_node)))
7886                 return complex_long_double_type_node;
7887               break;
7888             default:
7889               gcc_unreachable ();
7890             }
7891           break;
7892         default:
7893           break;
7894         }
7895     }
7896   return NULL_TREE;
7897 }
7898 \f
7899 /* Return OP, stripped of any conversions to wider types as much as is safe.
7900    Converting the value back to OP's type makes a value equivalent to OP.
7901
7902    If FOR_TYPE is nonzero, we return a value which, if converted to
7903    type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
7904
7905    OP must have integer, real or enumeral type.  Pointers are not allowed!
7906
7907    There are some cases where the obvious value we could return
7908    would regenerate to OP if converted to OP's type,
7909    but would not extend like OP to wider types.
7910    If FOR_TYPE indicates such extension is contemplated, we eschew such values.
7911    For example, if OP is (unsigned short)(signed char)-1,
7912    we avoid returning (signed char)-1 if FOR_TYPE is int,
7913    even though extending that to an unsigned short would regenerate OP,
7914    since the result of extending (signed char)-1 to (int)
7915    is different from (int) OP.  */
7916
7917 tree
7918 get_unwidened (tree op, tree for_type)
7919 {
7920   /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension.  */
7921   tree type = TREE_TYPE (op);
7922   unsigned final_prec
7923     = TYPE_PRECISION (for_type != 0 ? for_type : type);
7924   int uns
7925     = (for_type != 0 && for_type != type
7926        && final_prec > TYPE_PRECISION (type)
7927        && TYPE_UNSIGNED (type));
7928   tree win = op;
7929
7930   while (CONVERT_EXPR_P (op))
7931     {
7932       int bitschange;
7933
7934       /* TYPE_PRECISION on vector types has different meaning
7935          (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
7936          so avoid them here.  */
7937       if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
7938         break;
7939
7940       bitschange = TYPE_PRECISION (TREE_TYPE (op))
7941                    - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
7942
7943       /* Truncations are many-one so cannot be removed.
7944          Unless we are later going to truncate down even farther.  */
7945       if (bitschange < 0
7946           && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
7947         break;
7948
7949       /* See what's inside this conversion.  If we decide to strip it,
7950          we will set WIN.  */
7951       op = TREE_OPERAND (op, 0);
7952
7953       /* If we have not stripped any zero-extensions (uns is 0),
7954          we can strip any kind of extension.
7955          If we have previously stripped a zero-extension,
7956          only zero-extensions can safely be stripped.
7957          Any extension can be stripped if the bits it would produce
7958          are all going to be discarded later by truncating to FOR_TYPE.  */
7959
7960       if (bitschange > 0)
7961         {
7962           if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
7963             win = op;
7964           /* TYPE_UNSIGNED says whether this is a zero-extension.
7965              Let's avoid computing it if it does not affect WIN
7966              and if UNS will not be needed again.  */
7967           if ((uns
7968                || CONVERT_EXPR_P (op))
7969               && TYPE_UNSIGNED (TREE_TYPE (op)))
7970             {
7971               uns = 1;
7972               win = op;
7973             }
7974         }
7975     }
7976
7977   /* If we finally reach a constant see if it fits in for_type and
7978      in that case convert it.  */
7979   if (for_type
7980       && TREE_CODE (win) == INTEGER_CST
7981       && TREE_TYPE (win) != for_type
7982       && int_fits_type_p (win, for_type))
7983     win = fold_convert (for_type, win);
7984
7985   return win;
7986 }
7987 \f
7988 /* Return OP or a simpler expression for a narrower value
7989    which can be sign-extended or zero-extended to give back OP.
7990    Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
7991    or 0 if the value should be sign-extended.  */
7992
7993 tree
7994 get_narrower (tree op, int *unsignedp_ptr)
7995 {
7996   int uns = 0;
7997   int first = 1;
7998   tree win = op;
7999   bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
8000
8001   while (TREE_CODE (op) == NOP_EXPR)
8002     {
8003       int bitschange
8004         = (TYPE_PRECISION (TREE_TYPE (op))
8005            - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
8006
8007       /* Truncations are many-one so cannot be removed.  */
8008       if (bitschange < 0)
8009         break;
8010
8011       /* See what's inside this conversion.  If we decide to strip it,
8012          we will set WIN.  */
8013
8014       if (bitschange > 0)
8015         {
8016           op = TREE_OPERAND (op, 0);
8017           /* An extension: the outermost one can be stripped,
8018              but remember whether it is zero or sign extension.  */
8019           if (first)
8020             uns = TYPE_UNSIGNED (TREE_TYPE (op));
8021           /* Otherwise, if a sign extension has been stripped,
8022              only sign extensions can now be stripped;
8023              if a zero extension has been stripped, only zero-extensions.  */
8024           else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
8025             break;
8026           first = 0;
8027         }
8028       else /* bitschange == 0 */
8029         {
8030           /* A change in nominal type can always be stripped, but we must
8031              preserve the unsignedness.  */
8032           if (first)
8033             uns = TYPE_UNSIGNED (TREE_TYPE (op));
8034           first = 0;
8035           op = TREE_OPERAND (op, 0);
8036           /* Keep trying to narrow, but don't assign op to win if it
8037              would turn an integral type into something else.  */
8038           if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
8039             continue;
8040         }
8041
8042       win = op;
8043     }
8044
8045   if (TREE_CODE (op) == COMPONENT_REF
8046       /* Since type_for_size always gives an integer type.  */
8047       && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
8048       && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
8049       /* Ensure field is laid out already.  */
8050       && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
8051       && host_integerp (DECL_SIZE (TREE_OPERAND (op, 1)), 1))
8052     {
8053       unsigned HOST_WIDE_INT innerprec
8054         = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
8055       int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
8056                        || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
8057       tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
8058
8059       /* We can get this structure field in a narrower type that fits it,
8060          but the resulting extension to its nominal type (a fullword type)
8061          must satisfy the same conditions as for other extensions.
8062
8063          Do this only for fields that are aligned (not bit-fields),
8064          because when bit-field insns will be used there is no
8065          advantage in doing this.  */
8066
8067       if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
8068           && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
8069           && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
8070           && type != 0)
8071         {
8072           if (first)
8073             uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
8074           win = fold_convert (type, op);
8075         }
8076     }
8077
8078   *unsignedp_ptr = uns;
8079   return win;
8080 }
8081 \f
8082 /* Returns true if integer constant C has a value that is permissible
8083    for type TYPE (an INTEGER_TYPE).  */
8084
8085 bool
8086 int_fits_type_p (const_tree c, const_tree type)
8087 {
8088   tree type_low_bound, type_high_bound;
8089   bool ok_for_low_bound, ok_for_high_bound, unsc;
8090   double_int dc, dd;
8091
8092   dc = tree_to_double_int (c);
8093   unsc = TYPE_UNSIGNED (TREE_TYPE (c));
8094
8095   if (TREE_CODE (TREE_TYPE (c)) == INTEGER_TYPE
8096       && TYPE_IS_SIZETYPE (TREE_TYPE (c))
8097       && unsc)
8098     /* So c is an unsigned integer whose type is sizetype and type is not.
8099        sizetype'd integers are sign extended even though they are
8100        unsigned. If the integer value fits in the lower end word of c,
8101        and if the higher end word has all its bits set to 1, that
8102        means the higher end bits are set to 1 only for sign extension.
8103        So let's convert c into an equivalent zero extended unsigned
8104        integer.  */
8105     dc = double_int_zext (dc, TYPE_PRECISION (TREE_TYPE (c)));
8106
8107 retry:
8108   type_low_bound = TYPE_MIN_VALUE (type);
8109   type_high_bound = TYPE_MAX_VALUE (type);
8110
8111   /* If at least one bound of the type is a constant integer, we can check
8112      ourselves and maybe make a decision. If no such decision is possible, but
8113      this type is a subtype, try checking against that.  Otherwise, use
8114      double_int_fits_to_tree_p, which checks against the precision.
8115
8116      Compute the status for each possibly constant bound, and return if we see
8117      one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
8118      for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
8119      for "constant known to fit".  */
8120
8121   /* Check if c >= type_low_bound.  */
8122   if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
8123     {
8124       dd = tree_to_double_int (type_low_bound);
8125       if (TREE_CODE (type) == INTEGER_TYPE
8126           && TYPE_IS_SIZETYPE (type)
8127           && TYPE_UNSIGNED (type))
8128         dd = double_int_zext (dd, TYPE_PRECISION (type));
8129       if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_low_bound)))
8130         {
8131           int c_neg = (!unsc && double_int_negative_p (dc));
8132           int t_neg = (unsc && double_int_negative_p (dd));
8133
8134           if (c_neg && !t_neg)
8135             return false;
8136           if ((c_neg || !t_neg) && double_int_ucmp (dc, dd) < 0)
8137             return false;
8138         }
8139       else if (double_int_cmp (dc, dd, unsc) < 0)
8140         return false;
8141       ok_for_low_bound = true;
8142     }
8143   else
8144     ok_for_low_bound = false;
8145
8146   /* Check if c <= type_high_bound.  */
8147   if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
8148     {
8149       dd = tree_to_double_int (type_high_bound);
8150       if (TREE_CODE (type) == INTEGER_TYPE
8151           && TYPE_IS_SIZETYPE (type)
8152           && TYPE_UNSIGNED (type))
8153         dd = double_int_zext (dd, TYPE_PRECISION (type));
8154       if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_high_bound)))
8155         {
8156           int c_neg = (!unsc && double_int_negative_p (dc));
8157           int t_neg = (unsc && double_int_negative_p (dd));
8158
8159           if (t_neg && !c_neg)
8160             return false;
8161           if ((t_neg || !c_neg) && double_int_ucmp (dc, dd) > 0)
8162             return false;
8163         }
8164       else if (double_int_cmp (dc, dd, unsc) > 0)
8165         return false;
8166       ok_for_high_bound = true;
8167     }
8168   else
8169     ok_for_high_bound = false;
8170
8171   /* If the constant fits both bounds, the result is known.  */
8172   if (ok_for_low_bound && ok_for_high_bound)
8173     return true;
8174
8175   /* Perform some generic filtering which may allow making a decision
8176      even if the bounds are not constant.  First, negative integers
8177      never fit in unsigned types, */
8178   if (TYPE_UNSIGNED (type) && !unsc && double_int_negative_p (dc))
8179     return false;
8180
8181   /* Second, narrower types always fit in wider ones.  */
8182   if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
8183     return true;
8184
8185   /* Third, unsigned integers with top bit set never fit signed types.  */
8186   if (! TYPE_UNSIGNED (type) && unsc)
8187     {
8188       int prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (c))) - 1;
8189       if (prec < HOST_BITS_PER_WIDE_INT)
8190         {
8191           if (((((unsigned HOST_WIDE_INT) 1) << prec) & dc.low) != 0)
8192             return false;
8193         }
8194       else if (((((unsigned HOST_WIDE_INT) 1)
8195                  << (prec - HOST_BITS_PER_WIDE_INT)) & dc.high) != 0)
8196         return false;
8197     }
8198
8199   /* If we haven't been able to decide at this point, there nothing more we
8200      can check ourselves here.  Look at the base type if we have one and it
8201      has the same precision.  */
8202   if (TREE_CODE (type) == INTEGER_TYPE
8203       && TREE_TYPE (type) != 0
8204       && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
8205     {
8206       type = TREE_TYPE (type);
8207       goto retry;
8208     }
8209
8210   /* Or to double_int_fits_to_tree_p, if nothing else.  */
8211   return double_int_fits_to_tree_p (type, dc);
8212 }
8213
8214 /* Stores bounds of an integer TYPE in MIN and MAX.  If TYPE has non-constant
8215    bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
8216    represented (assuming two's-complement arithmetic) within the bit
8217    precision of the type are returned instead.  */
8218
8219 void
8220 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
8221 {
8222   if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
8223       && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
8224     mpz_set_double_int (min, tree_to_double_int (TYPE_MIN_VALUE (type)),
8225                         TYPE_UNSIGNED (type));
8226   else
8227     {
8228       if (TYPE_UNSIGNED (type))
8229         mpz_set_ui (min, 0);
8230       else
8231         {
8232           double_int mn;
8233           mn = double_int_mask (TYPE_PRECISION (type) - 1);
8234           mn = double_int_sext (double_int_add (mn, double_int_one),
8235                                 TYPE_PRECISION (type));
8236           mpz_set_double_int (min, mn, false);
8237         }
8238     }
8239
8240   if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
8241       && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
8242     mpz_set_double_int (max, tree_to_double_int (TYPE_MAX_VALUE (type)),
8243                         TYPE_UNSIGNED (type));
8244   else
8245     {
8246       if (TYPE_UNSIGNED (type))
8247         mpz_set_double_int (max, double_int_mask (TYPE_PRECISION (type)),
8248                             true);
8249       else
8250         mpz_set_double_int (max, double_int_mask (TYPE_PRECISION (type) - 1),
8251                             true);
8252     }
8253 }
8254
8255 /* Return true if VAR is an automatic variable defined in function FN.  */
8256
8257 bool
8258 auto_var_in_fn_p (const_tree var, const_tree fn)
8259 {
8260   return (DECL_P (var) && DECL_CONTEXT (var) == fn
8261           && ((((TREE_CODE (var) == VAR_DECL && ! DECL_EXTERNAL (var))
8262                 || TREE_CODE (var) == PARM_DECL)
8263                && ! TREE_STATIC (var))
8264               || TREE_CODE (var) == LABEL_DECL
8265               || TREE_CODE (var) == RESULT_DECL));
8266 }
8267
8268 /* Subprogram of following function.  Called by walk_tree.
8269
8270    Return *TP if it is an automatic variable or parameter of the
8271    function passed in as DATA.  */
8272
8273 static tree
8274 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
8275 {
8276   tree fn = (tree) data;
8277
8278   if (TYPE_P (*tp))
8279     *walk_subtrees = 0;
8280
8281   else if (DECL_P (*tp)
8282            && auto_var_in_fn_p (*tp, fn))
8283     return *tp;
8284
8285   return NULL_TREE;
8286 }
8287
8288 /* Returns true if T is, contains, or refers to a type with variable
8289    size.  For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
8290    arguments, but not the return type.  If FN is nonzero, only return
8291    true if a modifier of the type or position of FN is a variable or
8292    parameter inside FN.
8293
8294    This concept is more general than that of C99 'variably modified types':
8295    in C99, a struct type is never variably modified because a VLA may not
8296    appear as a structure member.  However, in GNU C code like:
8297
8298      struct S { int i[f()]; };
8299
8300    is valid, and other languages may define similar constructs.  */
8301
8302 bool
8303 variably_modified_type_p (tree type, tree fn)
8304 {
8305   tree t;
8306
8307 /* Test if T is either variable (if FN is zero) or an expression containing
8308    a variable in FN.  */
8309 #define RETURN_TRUE_IF_VAR(T)                                           \
8310   do { tree _t = (T);                                                   \
8311     if (_t && _t != error_mark_node && TREE_CODE (_t) != INTEGER_CST    \
8312         && (!fn || walk_tree (&_t, find_var_from_fn, fn, NULL)))        \
8313       return true;  } while (0)
8314
8315   if (type == error_mark_node)
8316     return false;
8317
8318   /* If TYPE itself has variable size, it is variably modified.  */
8319   RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
8320   RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
8321
8322   switch (TREE_CODE (type))
8323     {
8324     case POINTER_TYPE:
8325     case REFERENCE_TYPE:
8326     case VECTOR_TYPE:
8327       if (variably_modified_type_p (TREE_TYPE (type), fn))
8328         return true;
8329       break;
8330
8331     case FUNCTION_TYPE:
8332     case METHOD_TYPE:
8333       /* If TYPE is a function type, it is variably modified if the
8334          return type is variably modified.  */
8335       if (variably_modified_type_p (TREE_TYPE (type), fn))
8336           return true;
8337       break;
8338
8339     case INTEGER_TYPE:
8340     case REAL_TYPE:
8341     case FIXED_POINT_TYPE:
8342     case ENUMERAL_TYPE:
8343     case BOOLEAN_TYPE:
8344       /* Scalar types are variably modified if their end points
8345          aren't constant.  */
8346       RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
8347       RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
8348       break;
8349
8350     case RECORD_TYPE:
8351     case UNION_TYPE:
8352     case QUAL_UNION_TYPE:
8353       /* We can't see if any of the fields are variably-modified by the
8354          definition we normally use, since that would produce infinite
8355          recursion via pointers.  */
8356       /* This is variably modified if some field's type is.  */
8357       for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
8358         if (TREE_CODE (t) == FIELD_DECL)
8359           {
8360             RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
8361             RETURN_TRUE_IF_VAR (DECL_SIZE (t));
8362             RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
8363
8364             if (TREE_CODE (type) == QUAL_UNION_TYPE)
8365               RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
8366           }
8367         break;
8368
8369     case ARRAY_TYPE:
8370       /* Do not call ourselves to avoid infinite recursion.  This is
8371          variably modified if the element type is.  */
8372       RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
8373       RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
8374       break;
8375
8376     default:
8377       break;
8378     }
8379
8380   /* The current language may have other cases to check, but in general,
8381      all other types are not variably modified.  */
8382   return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
8383
8384 #undef RETURN_TRUE_IF_VAR
8385 }
8386
8387 /* Given a DECL or TYPE, return the scope in which it was declared, or
8388    NULL_TREE if there is no containing scope.  */
8389
8390 tree
8391 get_containing_scope (const_tree t)
8392 {
8393   return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
8394 }
8395
8396 /* Return the innermost context enclosing DECL that is
8397    a FUNCTION_DECL, or zero if none.  */
8398
8399 tree
8400 decl_function_context (const_tree decl)
8401 {
8402   tree context;
8403
8404   if (TREE_CODE (decl) == ERROR_MARK)
8405     return 0;
8406
8407   /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
8408      where we look up the function at runtime.  Such functions always take
8409      a first argument of type 'pointer to real context'.
8410
8411      C++ should really be fixed to use DECL_CONTEXT for the real context,
8412      and use something else for the "virtual context".  */
8413   else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
8414     context
8415       = TYPE_MAIN_VARIANT
8416         (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
8417   else
8418     context = DECL_CONTEXT (decl);
8419
8420   while (context && TREE_CODE (context) != FUNCTION_DECL)
8421     {
8422       if (TREE_CODE (context) == BLOCK)
8423         context = BLOCK_SUPERCONTEXT (context);
8424       else
8425         context = get_containing_scope (context);
8426     }
8427
8428   return context;
8429 }
8430
8431 /* Return the innermost context enclosing DECL that is
8432    a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
8433    TYPE_DECLs and FUNCTION_DECLs are transparent to this function.  */
8434
8435 tree
8436 decl_type_context (const_tree decl)
8437 {
8438   tree context = DECL_CONTEXT (decl);
8439
8440   while (context)
8441     switch (TREE_CODE (context))
8442       {
8443       case NAMESPACE_DECL:
8444       case TRANSLATION_UNIT_DECL:
8445         return NULL_TREE;
8446
8447       case RECORD_TYPE:
8448       case UNION_TYPE:
8449       case QUAL_UNION_TYPE:
8450         return context;
8451
8452       case TYPE_DECL:
8453       case FUNCTION_DECL:
8454         context = DECL_CONTEXT (context);
8455         break;
8456
8457       case BLOCK:
8458         context = BLOCK_SUPERCONTEXT (context);
8459         break;
8460
8461       default:
8462         gcc_unreachable ();
8463       }
8464
8465   return NULL_TREE;
8466 }
8467
8468 /* CALL is a CALL_EXPR.  Return the declaration for the function
8469    called, or NULL_TREE if the called function cannot be
8470    determined.  */
8471
8472 tree
8473 get_callee_fndecl (const_tree call)
8474 {
8475   tree addr;
8476
8477   if (call == error_mark_node)
8478     return error_mark_node;
8479
8480   /* It's invalid to call this function with anything but a
8481      CALL_EXPR.  */
8482   gcc_assert (TREE_CODE (call) == CALL_EXPR);
8483
8484   /* The first operand to the CALL is the address of the function
8485      called.  */
8486   addr = CALL_EXPR_FN (call);
8487
8488   STRIP_NOPS (addr);
8489
8490   /* If this is a readonly function pointer, extract its initial value.  */
8491   if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
8492       && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
8493       && DECL_INITIAL (addr))
8494     addr = DECL_INITIAL (addr);
8495
8496   /* If the address is just `&f' for some function `f', then we know
8497      that `f' is being called.  */
8498   if (TREE_CODE (addr) == ADDR_EXPR
8499       && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
8500     return TREE_OPERAND (addr, 0);
8501
8502   /* We couldn't figure out what was being called.  */
8503   return NULL_TREE;
8504 }
8505
8506 /* Print debugging information about tree nodes generated during the compile,
8507    and any language-specific information.  */
8508
8509 void
8510 dump_tree_statistics (void)
8511 {
8512 #ifdef GATHER_STATISTICS
8513   int i;
8514   int total_nodes, total_bytes;
8515 #endif
8516
8517   fprintf (stderr, "\n??? tree nodes created\n\n");
8518 #ifdef GATHER_STATISTICS
8519   fprintf (stderr, "Kind                   Nodes      Bytes\n");
8520   fprintf (stderr, "---------------------------------------\n");
8521   total_nodes = total_bytes = 0;
8522   for (i = 0; i < (int) all_kinds; i++)
8523     {
8524       fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
8525                tree_node_counts[i], tree_node_sizes[i]);
8526       total_nodes += tree_node_counts[i];
8527       total_bytes += tree_node_sizes[i];
8528     }
8529   fprintf (stderr, "---------------------------------------\n");
8530   fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
8531   fprintf (stderr, "---------------------------------------\n");
8532   ssanames_print_statistics ();
8533   phinodes_print_statistics ();
8534 #else
8535   fprintf (stderr, "(No per-node statistics)\n");
8536 #endif
8537   print_type_hash_statistics ();
8538   print_debug_expr_statistics ();
8539   print_value_expr_statistics ();
8540   lang_hooks.print_statistics ();
8541 }
8542 \f
8543 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
8544
8545 /* Generate a crc32 of a string.  */
8546
8547 unsigned
8548 crc32_string (unsigned chksum, const char *string)
8549 {
8550   do
8551     {
8552       unsigned value = *string << 24;
8553       unsigned ix;
8554
8555       for (ix = 8; ix--; value <<= 1)
8556         {
8557           unsigned feedback;
8558
8559           feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
8560           chksum <<= 1;
8561           chksum ^= feedback;
8562         }
8563     }
8564   while (*string++);
8565   return chksum;
8566 }
8567
8568 /* P is a string that will be used in a symbol.  Mask out any characters
8569    that are not valid in that context.  */
8570
8571 void
8572 clean_symbol_name (char *p)
8573 {
8574   for (; *p; p++)
8575     if (! (ISALNUM (*p)
8576 #ifndef NO_DOLLAR_IN_LABEL      /* this for `$'; unlikely, but... -- kr */
8577             || *p == '$'
8578 #endif
8579 #ifndef NO_DOT_IN_LABEL         /* this for `.'; unlikely, but...  */
8580             || *p == '.'
8581 #endif
8582            ))
8583       *p = '_';
8584 }
8585
8586 /* Generate a name for a special-purpose function function.
8587    The generated name may need to be unique across the whole link.
8588    TYPE is some string to identify the purpose of this function to the
8589    linker or collect2; it must start with an uppercase letter,
8590    one of:
8591    I - for constructors
8592    D - for destructors
8593    N - for C++ anonymous namespaces
8594    F - for DWARF unwind frame information.  */
8595
8596 tree
8597 get_file_function_name (const char *type)
8598 {
8599   char *buf;
8600   const char *p;
8601   char *q;
8602
8603   /* If we already have a name we know to be unique, just use that.  */
8604   if (first_global_object_name)
8605     p = q = ASTRDUP (first_global_object_name);
8606   /* If the target is handling the constructors/destructors, they
8607      will be local to this file and the name is only necessary for
8608      debugging purposes. 
8609      We also assign sub_I and sub_D sufixes to constructors called from
8610      the global static constructors.  These are always local.  */
8611   else if (((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
8612            || (strncmp (type, "sub_", 4) == 0
8613                && (type[4] == 'I' || type[4] == 'D')))
8614     {
8615       const char *file = main_input_filename;
8616       if (! file)
8617         file = input_filename;
8618       /* Just use the file's basename, because the full pathname
8619          might be quite long.  */
8620       p = q = ASTRDUP (lbasename (file));
8621     }
8622   else
8623     {
8624       /* Otherwise, the name must be unique across the entire link.
8625          We don't have anything that we know to be unique to this translation
8626          unit, so use what we do have and throw in some randomness.  */
8627       unsigned len;
8628       const char *name = weak_global_object_name;
8629       const char *file = main_input_filename;
8630
8631       if (! name)
8632         name = "";
8633       if (! file)
8634         file = input_filename;
8635
8636       len = strlen (file);
8637       q = (char *) alloca (9 * 2 + len + 1);
8638       memcpy (q, file, len + 1);
8639
8640       sprintf (q + len, "_%08X_%08X", crc32_string (0, name),
8641                crc32_string (0, get_random_seed (false)));
8642
8643       p = q;
8644     }
8645
8646   clean_symbol_name (q);
8647   buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
8648                          + strlen (type));
8649
8650   /* Set up the name of the file-level functions we may need.
8651      Use a global object (which is already required to be unique over
8652      the program) rather than the file name (which imposes extra
8653      constraints).  */
8654   sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
8655
8656   return get_identifier (buf);
8657 }
8658 \f
8659 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
8660
8661 /* Complain that the tree code of NODE does not match the expected 0
8662    terminated list of trailing codes. The trailing code list can be
8663    empty, for a more vague error message.  FILE, LINE, and FUNCTION
8664    are of the caller.  */
8665
8666 void
8667 tree_check_failed (const_tree node, const char *file,
8668                    int line, const char *function, ...)
8669 {
8670   va_list args;
8671   const char *buffer;
8672   unsigned length = 0;
8673   int code;
8674
8675   va_start (args, function);
8676   while ((code = va_arg (args, int)))
8677     length += 4 + strlen (tree_code_name[code]);
8678   va_end (args);
8679   if (length)
8680     {
8681       char *tmp;
8682       va_start (args, function);
8683       length += strlen ("expected ");
8684       buffer = tmp = (char *) alloca (length);
8685       length = 0;
8686       while ((code = va_arg (args, int)))
8687         {
8688           const char *prefix = length ? " or " : "expected ";
8689
8690           strcpy (tmp + length, prefix);
8691           length += strlen (prefix);
8692           strcpy (tmp + length, tree_code_name[code]);
8693           length += strlen (tree_code_name[code]);
8694         }
8695       va_end (args);
8696     }
8697   else
8698     buffer = "unexpected node";
8699
8700   internal_error ("tree check: %s, have %s in %s, at %s:%d",
8701                   buffer, tree_code_name[TREE_CODE (node)],
8702                   function, trim_filename (file), line);
8703 }
8704
8705 /* Complain that the tree code of NODE does match the expected 0
8706    terminated list of trailing codes. FILE, LINE, and FUNCTION are of
8707    the caller.  */
8708
8709 void
8710 tree_not_check_failed (const_tree node, const char *file,
8711                        int line, const char *function, ...)
8712 {
8713   va_list args;
8714   char *buffer;
8715   unsigned length = 0;
8716   int code;
8717
8718   va_start (args, function);
8719   while ((code = va_arg (args, int)))
8720     length += 4 + strlen (tree_code_name[code]);
8721   va_end (args);
8722   va_start (args, function);
8723   buffer = (char *) alloca (length);
8724   length = 0;
8725   while ((code = va_arg (args, int)))
8726     {
8727       if (length)
8728         {
8729           strcpy (buffer + length, " or ");
8730           length += 4;
8731         }
8732       strcpy (buffer + length, tree_code_name[code]);
8733       length += strlen (tree_code_name[code]);
8734     }
8735   va_end (args);
8736
8737   internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
8738                   buffer, tree_code_name[TREE_CODE (node)],
8739                   function, trim_filename (file), line);
8740 }
8741
8742 /* Similar to tree_check_failed, except that we check for a class of tree
8743    code, given in CL.  */
8744
8745 void
8746 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
8747                          const char *file, int line, const char *function)
8748 {
8749   internal_error
8750     ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
8751      TREE_CODE_CLASS_STRING (cl),
8752      TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
8753      tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
8754 }
8755
8756 /* Similar to tree_check_failed, except that instead of specifying a
8757    dozen codes, use the knowledge that they're all sequential.  */
8758
8759 void
8760 tree_range_check_failed (const_tree node, const char *file, int line,
8761                          const char *function, enum tree_code c1,
8762                          enum tree_code c2)
8763 {
8764   char *buffer;
8765   unsigned length = 0;
8766   unsigned int c;
8767
8768   for (c = c1; c <= c2; ++c)
8769     length += 4 + strlen (tree_code_name[c]);
8770
8771   length += strlen ("expected ");
8772   buffer = (char *) alloca (length);
8773   length = 0;
8774
8775   for (c = c1; c <= c2; ++c)
8776     {
8777       const char *prefix = length ? " or " : "expected ";
8778
8779       strcpy (buffer + length, prefix);
8780       length += strlen (prefix);
8781       strcpy (buffer + length, tree_code_name[c]);
8782       length += strlen (tree_code_name[c]);
8783     }
8784
8785   internal_error ("tree check: %s, have %s in %s, at %s:%d",
8786                   buffer, tree_code_name[TREE_CODE (node)],
8787                   function, trim_filename (file), line);
8788 }
8789
8790
8791 /* Similar to tree_check_failed, except that we check that a tree does
8792    not have the specified code, given in CL.  */
8793
8794 void
8795 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
8796                              const char *file, int line, const char *function)
8797 {
8798   internal_error
8799     ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
8800      TREE_CODE_CLASS_STRING (cl),
8801      TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
8802      tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
8803 }
8804
8805
8806 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes.  */
8807
8808 void
8809 omp_clause_check_failed (const_tree node, const char *file, int line,
8810                          const char *function, enum omp_clause_code code)
8811 {
8812   internal_error ("tree check: expected omp_clause %s, have %s in %s, at %s:%d",
8813                   omp_clause_code_name[code], tree_code_name[TREE_CODE (node)],
8814                   function, trim_filename (file), line);
8815 }
8816
8817
8818 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes.  */
8819
8820 void
8821 omp_clause_range_check_failed (const_tree node, const char *file, int line,
8822                                const char *function, enum omp_clause_code c1,
8823                                enum omp_clause_code c2)
8824 {
8825   char *buffer;
8826   unsigned length = 0;
8827   unsigned int c;
8828
8829   for (c = c1; c <= c2; ++c)
8830     length += 4 + strlen (omp_clause_code_name[c]);
8831
8832   length += strlen ("expected ");
8833   buffer = (char *) alloca (length);
8834   length = 0;
8835
8836   for (c = c1; c <= c2; ++c)
8837     {
8838       const char *prefix = length ? " or " : "expected ";
8839
8840       strcpy (buffer + length, prefix);
8841       length += strlen (prefix);
8842       strcpy (buffer + length, omp_clause_code_name[c]);
8843       length += strlen (omp_clause_code_name[c]);
8844     }
8845
8846   internal_error ("tree check: %s, have %s in %s, at %s:%d",
8847                   buffer, omp_clause_code_name[TREE_CODE (node)],
8848                   function, trim_filename (file), line);
8849 }
8850
8851
8852 #undef DEFTREESTRUCT
8853 #define DEFTREESTRUCT(VAL, NAME) NAME,
8854
8855 static const char *ts_enum_names[] = {
8856 #include "treestruct.def"
8857 };
8858 #undef DEFTREESTRUCT
8859
8860 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
8861
8862 /* Similar to tree_class_check_failed, except that we check for
8863    whether CODE contains the tree structure identified by EN.  */
8864
8865 void
8866 tree_contains_struct_check_failed (const_tree node,
8867                                    const enum tree_node_structure_enum en,
8868                                    const char *file, int line,
8869                                    const char *function)
8870 {
8871   internal_error
8872     ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
8873      TS_ENUM_NAME(en),
8874      tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
8875 }
8876
8877
8878 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
8879    (dynamically sized) vector.  */
8880
8881 void
8882 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
8883                            const char *function)
8884 {
8885   internal_error
8886     ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
8887      idx + 1, len, function, trim_filename (file), line);
8888 }
8889
8890 /* Similar to above, except that the check is for the bounds of the operand
8891    vector of an expression node EXP.  */
8892
8893 void
8894 tree_operand_check_failed (int idx, const_tree exp, const char *file,
8895                            int line, const char *function)
8896 {
8897   int code = TREE_CODE (exp);
8898   internal_error
8899     ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
8900      idx + 1, tree_code_name[code], TREE_OPERAND_LENGTH (exp),
8901      function, trim_filename (file), line);
8902 }
8903
8904 /* Similar to above, except that the check is for the number of
8905    operands of an OMP_CLAUSE node.  */
8906
8907 void
8908 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
8909                                  int line, const char *function)
8910 {
8911   internal_error
8912     ("tree check: accessed operand %d of omp_clause %s with %d operands "
8913      "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
8914      omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
8915      trim_filename (file), line);
8916 }
8917 #endif /* ENABLE_TREE_CHECKING */
8918 \f
8919 /* Create a new vector type node holding SUBPARTS units of type INNERTYPE,
8920    and mapped to the machine mode MODE.  Initialize its fields and build
8921    the information necessary for debugging output.  */
8922
8923 static tree
8924 make_vector_type (tree innertype, int nunits, enum machine_mode mode)
8925 {
8926   tree t;
8927   hashval_t hashcode = 0;
8928
8929   t = make_node (VECTOR_TYPE);
8930   TREE_TYPE (t) = TYPE_MAIN_VARIANT (innertype);
8931   SET_TYPE_VECTOR_SUBPARTS (t, nunits);
8932   SET_TYPE_MODE (t, mode);
8933
8934   if (TYPE_STRUCTURAL_EQUALITY_P (innertype))
8935     SET_TYPE_STRUCTURAL_EQUALITY (t);
8936   else if (TYPE_CANONICAL (innertype) != innertype
8937            || mode != VOIDmode)
8938     TYPE_CANONICAL (t)
8939       = make_vector_type (TYPE_CANONICAL (innertype), nunits, VOIDmode);
8940
8941   layout_type (t);
8942
8943   hashcode = iterative_hash_host_wide_int (VECTOR_TYPE, hashcode);
8944   hashcode = iterative_hash_host_wide_int (nunits, hashcode);
8945   hashcode = iterative_hash_host_wide_int (mode, hashcode);
8946   hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (t)), hashcode);
8947   t = type_hash_canon (hashcode, t);
8948
8949   /* We have built a main variant, based on the main variant of the
8950      inner type. Use it to build the variant we return.  */
8951   if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
8952       && TREE_TYPE (t) != innertype)
8953     return build_type_attribute_qual_variant (t,
8954                                               TYPE_ATTRIBUTES (innertype),
8955                                               TYPE_QUALS (innertype));
8956
8957   return t;
8958 }
8959
8960 static tree
8961 make_or_reuse_type (unsigned size, int unsignedp)
8962 {
8963   if (size == INT_TYPE_SIZE)
8964     return unsignedp ? unsigned_type_node : integer_type_node;
8965   if (size == CHAR_TYPE_SIZE)
8966     return unsignedp ? unsigned_char_type_node : signed_char_type_node;
8967   if (size == SHORT_TYPE_SIZE)
8968     return unsignedp ? short_unsigned_type_node : short_integer_type_node;
8969   if (size == LONG_TYPE_SIZE)
8970     return unsignedp ? long_unsigned_type_node : long_integer_type_node;
8971   if (size == LONG_LONG_TYPE_SIZE)
8972     return (unsignedp ? long_long_unsigned_type_node
8973             : long_long_integer_type_node);
8974   if (size == 128 && int128_integer_type_node)
8975     return (unsignedp ? int128_unsigned_type_node
8976             : int128_integer_type_node);
8977
8978   if (unsignedp)
8979     return make_unsigned_type (size);
8980   else
8981     return make_signed_type (size);
8982 }
8983
8984 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP.  */
8985
8986 static tree
8987 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
8988 {
8989   if (satp)
8990     {
8991       if (size == SHORT_FRACT_TYPE_SIZE)
8992         return unsignedp ? sat_unsigned_short_fract_type_node
8993                          : sat_short_fract_type_node;
8994       if (size == FRACT_TYPE_SIZE)
8995         return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
8996       if (size == LONG_FRACT_TYPE_SIZE)
8997         return unsignedp ? sat_unsigned_long_fract_type_node
8998                          : sat_long_fract_type_node;
8999       if (size == LONG_LONG_FRACT_TYPE_SIZE)
9000         return unsignedp ? sat_unsigned_long_long_fract_type_node
9001                          : sat_long_long_fract_type_node;
9002     }
9003   else
9004     {
9005       if (size == SHORT_FRACT_TYPE_SIZE)
9006         return unsignedp ? unsigned_short_fract_type_node
9007                          : short_fract_type_node;
9008       if (size == FRACT_TYPE_SIZE)
9009         return unsignedp ? unsigned_fract_type_node : fract_type_node;
9010       if (size == LONG_FRACT_TYPE_SIZE)
9011         return unsignedp ? unsigned_long_fract_type_node
9012                          : long_fract_type_node;
9013       if (size == LONG_LONG_FRACT_TYPE_SIZE)
9014         return unsignedp ? unsigned_long_long_fract_type_node
9015                          : long_long_fract_type_node;
9016     }
9017
9018   return make_fract_type (size, unsignedp, satp);
9019 }
9020
9021 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP.  */
9022
9023 static tree
9024 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
9025 {
9026   if (satp)
9027     {
9028       if (size == SHORT_ACCUM_TYPE_SIZE)
9029         return unsignedp ? sat_unsigned_short_accum_type_node
9030                          : sat_short_accum_type_node;
9031       if (size == ACCUM_TYPE_SIZE)
9032         return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
9033       if (size == LONG_ACCUM_TYPE_SIZE)
9034         return unsignedp ? sat_unsigned_long_accum_type_node
9035                          : sat_long_accum_type_node;
9036       if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9037         return unsignedp ? sat_unsigned_long_long_accum_type_node
9038                          : sat_long_long_accum_type_node;
9039     }
9040   else
9041     {
9042       if (size == SHORT_ACCUM_TYPE_SIZE)
9043         return unsignedp ? unsigned_short_accum_type_node
9044                          : short_accum_type_node;
9045       if (size == ACCUM_TYPE_SIZE)
9046         return unsignedp ? unsigned_accum_type_node : accum_type_node;
9047       if (size == LONG_ACCUM_TYPE_SIZE)
9048         return unsignedp ? unsigned_long_accum_type_node
9049                          : long_accum_type_node;
9050       if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9051         return unsignedp ? unsigned_long_long_accum_type_node
9052                          : long_long_accum_type_node;
9053     }
9054
9055   return make_accum_type (size, unsignedp, satp);
9056 }
9057
9058 /* Create nodes for all integer types (and error_mark_node) using the sizes
9059    of C datatypes.  The caller should call set_sizetype soon after calling
9060    this function to select one of the types as sizetype.  */
9061
9062 void
9063 build_common_tree_nodes (bool signed_char)
9064 {
9065   error_mark_node = make_node (ERROR_MARK);
9066   TREE_TYPE (error_mark_node) = error_mark_node;
9067
9068   initialize_sizetypes ();
9069
9070   /* Define both `signed char' and `unsigned char'.  */
9071   signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
9072   TYPE_STRING_FLAG (signed_char_type_node) = 1;
9073   unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
9074   TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
9075
9076   /* Define `char', which is like either `signed char' or `unsigned char'
9077      but not the same as either.  */
9078   char_type_node
9079     = (signed_char
9080        ? make_signed_type (CHAR_TYPE_SIZE)
9081        : make_unsigned_type (CHAR_TYPE_SIZE));
9082   TYPE_STRING_FLAG (char_type_node) = 1;
9083
9084   short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
9085   short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
9086   integer_type_node = make_signed_type (INT_TYPE_SIZE);
9087   unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
9088   long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
9089   long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
9090   long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
9091   long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
9092 #if HOST_BITS_PER_WIDE_INT >= 64
9093     /* TODO: This isn't correct, but as logic depends at the moment on
9094        host's instead of target's wide-integer.
9095        If there is a target not supporting TImode, but has an 128-bit
9096        integer-scalar register, this target check needs to be adjusted. */
9097     if (targetm.scalar_mode_supported_p (TImode))
9098       {
9099         int128_integer_type_node = make_signed_type (128);
9100         int128_unsigned_type_node = make_unsigned_type (128);
9101       }
9102 #endif
9103   /* Define a boolean type.  This type only represents boolean values but
9104      may be larger than char depending on the value of BOOL_TYPE_SIZE.
9105      Front ends which want to override this size (i.e. Java) can redefine
9106      boolean_type_node before calling build_common_tree_nodes_2.  */
9107   boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
9108   TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
9109   TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
9110   TYPE_PRECISION (boolean_type_node) = 1;
9111
9112   /* Fill in the rest of the sized types.  Reuse existing type nodes
9113      when possible.  */
9114   intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
9115   intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
9116   intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
9117   intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
9118   intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
9119
9120   unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
9121   unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
9122   unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
9123   unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
9124   unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
9125
9126   access_public_node = get_identifier ("public");
9127   access_protected_node = get_identifier ("protected");
9128   access_private_node = get_identifier ("private");
9129 }
9130
9131 /* Call this function after calling build_common_tree_nodes and set_sizetype.
9132    It will create several other common tree nodes.  */
9133
9134 void
9135 build_common_tree_nodes_2 (int short_double)
9136 {
9137   /* Define these next since types below may used them.  */
9138   integer_zero_node = build_int_cst (integer_type_node, 0);
9139   integer_one_node = build_int_cst (integer_type_node, 1);
9140   integer_three_node = build_int_cst (integer_type_node, 3);
9141   integer_minus_one_node = build_int_cst (integer_type_node, -1);
9142
9143   size_zero_node = size_int (0);
9144   size_one_node = size_int (1);
9145   bitsize_zero_node = bitsize_int (0);
9146   bitsize_one_node = bitsize_int (1);
9147   bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
9148
9149   boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
9150   boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
9151
9152   void_type_node = make_node (VOID_TYPE);
9153   layout_type (void_type_node);
9154
9155   /* We are not going to have real types in C with less than byte alignment,
9156      so we might as well not have any types that claim to have it.  */
9157   TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
9158   TYPE_USER_ALIGN (void_type_node) = 0;
9159
9160   null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
9161   layout_type (TREE_TYPE (null_pointer_node));
9162
9163   ptr_type_node = build_pointer_type (void_type_node);
9164   const_ptr_type_node
9165     = build_pointer_type (build_type_variant (void_type_node, 1, 0));
9166   fileptr_type_node = ptr_type_node;
9167
9168   float_type_node = make_node (REAL_TYPE);
9169   TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
9170   layout_type (float_type_node);
9171
9172   double_type_node = make_node (REAL_TYPE);
9173   if (short_double)
9174     TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
9175   else
9176     TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
9177   layout_type (double_type_node);
9178
9179   long_double_type_node = make_node (REAL_TYPE);
9180   TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
9181   layout_type (long_double_type_node);
9182
9183   float_ptr_type_node = build_pointer_type (float_type_node);
9184   double_ptr_type_node = build_pointer_type (double_type_node);
9185   long_double_ptr_type_node = build_pointer_type (long_double_type_node);
9186   integer_ptr_type_node = build_pointer_type (integer_type_node);
9187
9188   /* Fixed size integer types.  */
9189   uint32_type_node = build_nonstandard_integer_type (32, true);
9190   uint64_type_node = build_nonstandard_integer_type (64, true);
9191
9192   /* Decimal float types. */
9193   dfloat32_type_node = make_node (REAL_TYPE);
9194   TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
9195   layout_type (dfloat32_type_node);
9196   SET_TYPE_MODE (dfloat32_type_node, SDmode);
9197   dfloat32_ptr_type_node = build_pointer_type (dfloat32_type_node);
9198
9199   dfloat64_type_node = make_node (REAL_TYPE);
9200   TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
9201   layout_type (dfloat64_type_node);
9202   SET_TYPE_MODE (dfloat64_type_node, DDmode);
9203   dfloat64_ptr_type_node = build_pointer_type (dfloat64_type_node);
9204
9205   dfloat128_type_node = make_node (REAL_TYPE);
9206   TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
9207   layout_type (dfloat128_type_node);
9208   SET_TYPE_MODE (dfloat128_type_node, TDmode);
9209   dfloat128_ptr_type_node = build_pointer_type (dfloat128_type_node);
9210
9211   complex_integer_type_node = build_complex_type (integer_type_node);
9212   complex_float_type_node = build_complex_type (float_type_node);
9213   complex_double_type_node = build_complex_type (double_type_node);
9214   complex_long_double_type_node = build_complex_type (long_double_type_node);
9215
9216 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned.  */
9217 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
9218   sat_ ## KIND ## _type_node = \
9219     make_sat_signed_ ## KIND ## _type (SIZE); \
9220   sat_unsigned_ ## KIND ## _type_node = \
9221     make_sat_unsigned_ ## KIND ## _type (SIZE); \
9222   KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9223   unsigned_ ## KIND ## _type_node = \
9224     make_unsigned_ ## KIND ## _type (SIZE);
9225
9226 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
9227   sat_ ## WIDTH ## KIND ## _type_node = \
9228     make_sat_signed_ ## KIND ## _type (SIZE); \
9229   sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
9230     make_sat_unsigned_ ## KIND ## _type (SIZE); \
9231   WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9232   unsigned_ ## WIDTH ## KIND ## _type_node = \
9233     make_unsigned_ ## KIND ## _type (SIZE);
9234
9235 /* Make fixed-point type nodes based on four different widths.  */
9236 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
9237   MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
9238   MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
9239   MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
9240   MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
9241
9242 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned.  */
9243 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
9244   NAME ## _type_node = \
9245     make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
9246   u ## NAME ## _type_node = \
9247     make_or_reuse_unsigned_ ## KIND ## _type \
9248       (GET_MODE_BITSIZE (U ## MODE ## mode)); \
9249   sat_ ## NAME ## _type_node = \
9250     make_or_reuse_sat_signed_ ## KIND ## _type \
9251       (GET_MODE_BITSIZE (MODE ## mode)); \
9252   sat_u ## NAME ## _type_node = \
9253     make_or_reuse_sat_unsigned_ ## KIND ## _type \
9254       (GET_MODE_BITSIZE (U ## MODE ## mode));
9255
9256   /* Fixed-point type and mode nodes.  */
9257   MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
9258   MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
9259   MAKE_FIXED_MODE_NODE (fract, qq, QQ)
9260   MAKE_FIXED_MODE_NODE (fract, hq, HQ)
9261   MAKE_FIXED_MODE_NODE (fract, sq, SQ)
9262   MAKE_FIXED_MODE_NODE (fract, dq, DQ)
9263   MAKE_FIXED_MODE_NODE (fract, tq, TQ)
9264   MAKE_FIXED_MODE_NODE (accum, ha, HA)
9265   MAKE_FIXED_MODE_NODE (accum, sa, SA)
9266   MAKE_FIXED_MODE_NODE (accum, da, DA)
9267   MAKE_FIXED_MODE_NODE (accum, ta, TA)
9268
9269   {
9270     tree t = targetm.build_builtin_va_list ();
9271
9272     /* Many back-ends define record types without setting TYPE_NAME.
9273        If we copied the record type here, we'd keep the original
9274        record type without a name.  This breaks name mangling.  So,
9275        don't copy record types and let c_common_nodes_and_builtins()
9276        declare the type to be __builtin_va_list.  */
9277     if (TREE_CODE (t) != RECORD_TYPE)
9278       t = build_variant_type_copy (t);
9279
9280     va_list_type_node = t;
9281   }
9282 }
9283
9284 /* A subroutine of build_common_builtin_nodes.  Define a builtin function.  */
9285
9286 static void
9287 local_define_builtin (const char *name, tree type, enum built_in_function code,
9288                       const char *library_name, int ecf_flags)
9289 {
9290   tree decl;
9291
9292   decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
9293                                library_name, NULL_TREE);
9294   if (ecf_flags & ECF_CONST)
9295     TREE_READONLY (decl) = 1;
9296   if (ecf_flags & ECF_PURE)
9297     DECL_PURE_P (decl) = 1;
9298   if (ecf_flags & ECF_LOOPING_CONST_OR_PURE)
9299     DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
9300   if (ecf_flags & ECF_NORETURN)
9301     TREE_THIS_VOLATILE (decl) = 1;
9302   if (ecf_flags & ECF_NOTHROW)
9303     TREE_NOTHROW (decl) = 1;
9304   if (ecf_flags & ECF_MALLOC)
9305     DECL_IS_MALLOC (decl) = 1;
9306   if (ecf_flags & ECF_LEAF)
9307     DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("leaf"),
9308                                         NULL, DECL_ATTRIBUTES (decl));
9309
9310   built_in_decls[code] = decl;
9311   implicit_built_in_decls[code] = decl;
9312 }
9313
9314 /* Call this function after instantiating all builtins that the language
9315    front end cares about.  This will build the rest of the builtins that
9316    are relied upon by the tree optimizers and the middle-end.  */
9317
9318 void
9319 build_common_builtin_nodes (void)
9320 {
9321   tree tmp, ftype;
9322
9323   if (built_in_decls[BUILT_IN_MEMCPY] == NULL
9324       || built_in_decls[BUILT_IN_MEMMOVE] == NULL)
9325     {
9326       ftype = build_function_type_list (ptr_type_node,
9327                                         ptr_type_node, const_ptr_type_node,
9328                                         size_type_node, NULL_TREE);
9329
9330       if (built_in_decls[BUILT_IN_MEMCPY] == NULL)
9331         local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
9332                               "memcpy", ECF_NOTHROW | ECF_LEAF);
9333       if (built_in_decls[BUILT_IN_MEMMOVE] == NULL)
9334         local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
9335                               "memmove", ECF_NOTHROW | ECF_LEAF);
9336     }
9337
9338   if (built_in_decls[BUILT_IN_MEMCMP] == NULL)
9339     {
9340       ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
9341                                         const_ptr_type_node, size_type_node,
9342                                         NULL_TREE);
9343       local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
9344                             "memcmp", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9345     }
9346
9347   if (built_in_decls[BUILT_IN_MEMSET] == NULL)
9348     {
9349       ftype = build_function_type_list (ptr_type_node,
9350                                         ptr_type_node, integer_type_node,
9351                                         size_type_node, NULL_TREE);
9352       local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
9353                             "memset", ECF_NOTHROW | ECF_LEAF);
9354     }
9355
9356   if (built_in_decls[BUILT_IN_ALLOCA] == NULL)
9357     {
9358       ftype = build_function_type_list (ptr_type_node,
9359                                         size_type_node, NULL_TREE);
9360       local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
9361                             "alloca", ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
9362     }
9363
9364   /* If we're checking the stack, `alloca' can throw.  */
9365   if (flag_stack_check)
9366     TREE_NOTHROW (built_in_decls[BUILT_IN_ALLOCA]) = 0;
9367
9368   ftype = build_function_type_list (void_type_node,
9369                                     ptr_type_node, ptr_type_node,
9370                                     ptr_type_node, NULL_TREE);
9371   local_define_builtin ("__builtin_init_trampoline", ftype,
9372                         BUILT_IN_INIT_TRAMPOLINE,
9373                         "__builtin_init_trampoline", ECF_NOTHROW | ECF_LEAF);
9374
9375   ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
9376   local_define_builtin ("__builtin_adjust_trampoline", ftype,
9377                         BUILT_IN_ADJUST_TRAMPOLINE,
9378                         "__builtin_adjust_trampoline",
9379                         ECF_CONST | ECF_NOTHROW);
9380
9381   ftype = build_function_type_list (void_type_node,
9382                                     ptr_type_node, ptr_type_node, NULL_TREE);
9383   local_define_builtin ("__builtin_nonlocal_goto", ftype,
9384                         BUILT_IN_NONLOCAL_GOTO,
9385                         "__builtin_nonlocal_goto",
9386                         ECF_NORETURN | ECF_NOTHROW);
9387
9388   ftype = build_function_type_list (void_type_node,
9389                                     ptr_type_node, ptr_type_node, NULL_TREE);
9390   local_define_builtin ("__builtin_setjmp_setup", ftype,
9391                         BUILT_IN_SETJMP_SETUP,
9392                         "__builtin_setjmp_setup", ECF_NOTHROW);
9393
9394   ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
9395   local_define_builtin ("__builtin_setjmp_dispatcher", ftype,
9396                         BUILT_IN_SETJMP_DISPATCHER,
9397                         "__builtin_setjmp_dispatcher",
9398                         ECF_PURE | ECF_NOTHROW);
9399
9400   ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9401   local_define_builtin ("__builtin_setjmp_receiver", ftype,
9402                         BUILT_IN_SETJMP_RECEIVER,
9403                         "__builtin_setjmp_receiver", ECF_NOTHROW);
9404
9405   ftype = build_function_type_list (ptr_type_node, NULL_TREE);
9406   local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
9407                         "__builtin_stack_save", ECF_NOTHROW | ECF_LEAF);
9408
9409   ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9410   local_define_builtin ("__builtin_stack_restore", ftype,
9411                         BUILT_IN_STACK_RESTORE,
9412                         "__builtin_stack_restore", ECF_NOTHROW | ECF_LEAF);
9413
9414   /* If there's a possibility that we might use the ARM EABI, build the
9415     alternate __cxa_end_cleanup node used to resume from C++ and Java.  */
9416   if (targetm.arm_eabi_unwinder)
9417     {
9418       ftype = build_function_type_list (void_type_node, NULL_TREE);
9419       local_define_builtin ("__builtin_cxa_end_cleanup", ftype,
9420                             BUILT_IN_CXA_END_CLEANUP,
9421                             "__cxa_end_cleanup", ECF_NORETURN | ECF_LEAF);
9422     }
9423
9424   ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9425   local_define_builtin ("__builtin_unwind_resume", ftype,
9426                         BUILT_IN_UNWIND_RESUME,
9427                         ((targetm.except_unwind_info (&global_options)
9428                           == UI_SJLJ)
9429                          ? "_Unwind_SjLj_Resume" : "_Unwind_Resume"),
9430                         ECF_NORETURN);
9431
9432   /* The exception object and filter values from the runtime.  The argument
9433      must be zero before exception lowering, i.e. from the front end.  After
9434      exception lowering, it will be the region number for the exception
9435      landing pad.  These functions are PURE instead of CONST to prevent
9436      them from being hoisted past the exception edge that will initialize
9437      its value in the landing pad.  */
9438   ftype = build_function_type_list (ptr_type_node,
9439                                     integer_type_node, NULL_TREE);
9440   local_define_builtin ("__builtin_eh_pointer", ftype, BUILT_IN_EH_POINTER,
9441                         "__builtin_eh_pointer", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9442
9443   tmp = lang_hooks.types.type_for_mode (targetm.eh_return_filter_mode (), 0);
9444   ftype = build_function_type_list (tmp, integer_type_node, NULL_TREE);
9445   local_define_builtin ("__builtin_eh_filter", ftype, BUILT_IN_EH_FILTER,
9446                         "__builtin_eh_filter", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9447
9448   ftype = build_function_type_list (void_type_node,
9449                                     integer_type_node, integer_type_node,
9450                                     NULL_TREE);
9451   local_define_builtin ("__builtin_eh_copy_values", ftype,
9452                         BUILT_IN_EH_COPY_VALUES,
9453                         "__builtin_eh_copy_values", ECF_NOTHROW);
9454
9455   /* Complex multiplication and division.  These are handled as builtins
9456      rather than optabs because emit_library_call_value doesn't support
9457      complex.  Further, we can do slightly better with folding these
9458      beasties if the real and complex parts of the arguments are separate.  */
9459   {
9460     int mode;
9461
9462     for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
9463       {
9464         char mode_name_buf[4], *q;
9465         const char *p;
9466         enum built_in_function mcode, dcode;
9467         tree type, inner_type;
9468
9469         type = lang_hooks.types.type_for_mode ((enum machine_mode) mode, 0);
9470         if (type == NULL)
9471           continue;
9472         inner_type = TREE_TYPE (type);
9473
9474         ftype = build_function_type_list (type, inner_type, inner_type,
9475                                           inner_type, inner_type, NULL_TREE);
9476
9477         mcode = ((enum built_in_function)
9478                  (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
9479         dcode = ((enum built_in_function)
9480                  (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
9481
9482         for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
9483           *q = TOLOWER (*p);
9484         *q = '\0';
9485
9486         built_in_names[mcode] = concat ("__mul", mode_name_buf, "3", NULL);
9487         local_define_builtin (built_in_names[mcode], ftype, mcode,
9488                               built_in_names[mcode], ECF_CONST | ECF_NOTHROW | ECF_LEAF);
9489
9490         built_in_names[dcode] = concat ("__div", mode_name_buf, "3", NULL);
9491         local_define_builtin (built_in_names[dcode], ftype, dcode,
9492                               built_in_names[dcode], ECF_CONST | ECF_NOTHROW | ECF_LEAF);
9493       }
9494   }
9495 }
9496
9497 /* HACK.  GROSS.  This is absolutely disgusting.  I wish there was a
9498    better way.
9499
9500    If we requested a pointer to a vector, build up the pointers that
9501    we stripped off while looking for the inner type.  Similarly for
9502    return values from functions.
9503
9504    The argument TYPE is the top of the chain, and BOTTOM is the
9505    new type which we will point to.  */
9506
9507 tree
9508 reconstruct_complex_type (tree type, tree bottom)
9509 {
9510   tree inner, outer;
9511
9512   if (TREE_CODE (type) == POINTER_TYPE)
9513     {
9514       inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9515       outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
9516                                            TYPE_REF_CAN_ALIAS_ALL (type));
9517     }
9518   else if (TREE_CODE (type) == REFERENCE_TYPE)
9519     {
9520       inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9521       outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
9522                                              TYPE_REF_CAN_ALIAS_ALL (type));
9523     }
9524   else if (TREE_CODE (type) == ARRAY_TYPE)
9525     {
9526       inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9527       outer = build_array_type (inner, TYPE_DOMAIN (type));
9528     }
9529   else if (TREE_CODE (type) == FUNCTION_TYPE)
9530     {
9531       inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9532       outer = build_function_type (inner, TYPE_ARG_TYPES (type));
9533     }
9534   else if (TREE_CODE (type) == METHOD_TYPE)
9535     {
9536       inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9537       /* The build_method_type_directly() routine prepends 'this' to argument list,
9538          so we must compensate by getting rid of it.  */
9539       outer
9540         = build_method_type_directly
9541             (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
9542              inner,
9543              TREE_CHAIN (TYPE_ARG_TYPES (type)));
9544     }
9545   else if (TREE_CODE (type) == OFFSET_TYPE)
9546     {
9547       inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9548       outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
9549     }
9550   else
9551     return bottom;
9552
9553   return build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
9554                                             TYPE_QUALS (type));
9555 }
9556
9557 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
9558    the inner type.  */
9559 tree
9560 build_vector_type_for_mode (tree innertype, enum machine_mode mode)
9561 {
9562   int nunits;
9563
9564   switch (GET_MODE_CLASS (mode))
9565     {
9566     case MODE_VECTOR_INT:
9567     case MODE_VECTOR_FLOAT:
9568     case MODE_VECTOR_FRACT:
9569     case MODE_VECTOR_UFRACT:
9570     case MODE_VECTOR_ACCUM:
9571     case MODE_VECTOR_UACCUM:
9572       nunits = GET_MODE_NUNITS (mode);
9573       break;
9574
9575     case MODE_INT:
9576       /* Check that there are no leftover bits.  */
9577       gcc_assert (GET_MODE_BITSIZE (mode)
9578                   % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
9579
9580       nunits = GET_MODE_BITSIZE (mode)
9581                / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
9582       break;
9583
9584     default:
9585       gcc_unreachable ();
9586     }
9587
9588   return make_vector_type (innertype, nunits, mode);
9589 }
9590
9591 /* Similarly, but takes the inner type and number of units, which must be
9592    a power of two.  */
9593
9594 tree
9595 build_vector_type (tree innertype, int nunits)
9596 {
9597   return make_vector_type (innertype, nunits, VOIDmode);
9598 }
9599
9600 /* Similarly, but takes the inner type and number of units, which must be
9601    a power of two.  */
9602
9603 tree
9604 build_opaque_vector_type (tree innertype, int nunits)
9605 {
9606   tree t;
9607   innertype = build_distinct_type_copy (innertype);
9608   t = make_vector_type (innertype, nunits, VOIDmode);
9609   TYPE_VECTOR_OPAQUE (t) = true;
9610   return t;
9611 }
9612
9613
9614 /* Given an initializer INIT, return TRUE if INIT is zero or some
9615    aggregate of zeros.  Otherwise return FALSE.  */
9616 bool
9617 initializer_zerop (const_tree init)
9618 {
9619   tree elt;
9620
9621   STRIP_NOPS (init);
9622
9623   switch (TREE_CODE (init))
9624     {
9625     case INTEGER_CST:
9626       return integer_zerop (init);
9627
9628     case REAL_CST:
9629       /* ??? Note that this is not correct for C4X float formats.  There,
9630          a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
9631          negative exponent.  */
9632       return real_zerop (init)
9633         && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
9634
9635     case FIXED_CST:
9636       return fixed_zerop (init);
9637
9638     case COMPLEX_CST:
9639       return integer_zerop (init)
9640         || (real_zerop (init)
9641             && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
9642             && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
9643
9644     case VECTOR_CST:
9645       for (elt = TREE_VECTOR_CST_ELTS (init); elt; elt = TREE_CHAIN (elt))
9646         if (!initializer_zerop (TREE_VALUE (elt)))
9647           return false;
9648       return true;
9649
9650     case CONSTRUCTOR:
9651       {
9652         unsigned HOST_WIDE_INT idx;
9653
9654         FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
9655           if (!initializer_zerop (elt))
9656             return false;
9657         return true;
9658       }
9659
9660     case STRING_CST:
9661       {
9662         int i;
9663
9664         /* We need to loop through all elements to handle cases like
9665            "\0" and "\0foobar".  */
9666         for (i = 0; i < TREE_STRING_LENGTH (init); ++i)
9667           if (TREE_STRING_POINTER (init)[i] != '\0')
9668             return false;
9669
9670         return true;
9671       }
9672
9673     default:
9674       return false;
9675     }
9676 }
9677
9678 /* Build an empty statement at location LOC.  */
9679
9680 tree
9681 build_empty_stmt (location_t loc)
9682 {
9683   tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
9684   SET_EXPR_LOCATION (t, loc);
9685   return t;
9686 }
9687
9688
9689 /* Build an OpenMP clause with code CODE.  LOC is the location of the
9690    clause.  */
9691
9692 tree
9693 build_omp_clause (location_t loc, enum omp_clause_code code)
9694 {
9695   tree t;
9696   int size, length;
9697
9698   length = omp_clause_num_ops[code];
9699   size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
9700
9701   record_node_allocation_statistics (OMP_CLAUSE, size);
9702
9703   t = ggc_alloc_tree_node (size);
9704   memset (t, 0, size);
9705   TREE_SET_CODE (t, OMP_CLAUSE);
9706   OMP_CLAUSE_SET_CODE (t, code);
9707   OMP_CLAUSE_LOCATION (t) = loc;
9708
9709   return t;
9710 }
9711
9712 /* Build a tcc_vl_exp object with code CODE and room for LEN operands.  LEN
9713    includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
9714    Except for the CODE and operand count field, other storage for the
9715    object is initialized to zeros.  */
9716
9717 tree
9718 build_vl_exp_stat (enum tree_code code, int len MEM_STAT_DECL)
9719 {
9720   tree t;
9721   int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
9722
9723   gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
9724   gcc_assert (len >= 1);
9725
9726   record_node_allocation_statistics (code, length);
9727
9728   t = ggc_alloc_zone_cleared_tree_node_stat (&tree_zone, length PASS_MEM_STAT);
9729
9730   TREE_SET_CODE (t, code);
9731
9732   /* Can't use TREE_OPERAND to store the length because if checking is
9733      enabled, it will try to check the length before we store it.  :-P  */
9734   t->exp.operands[0] = build_int_cst (sizetype, len);
9735
9736   return t;
9737 }
9738
9739 /* Helper function for build_call_* functions; build a CALL_EXPR with
9740    indicated RETURN_TYPE, FN, and NARGS, but do not initialize any of
9741    the argument slots.  */
9742
9743 static tree
9744 build_call_1 (tree return_type, tree fn, int nargs)
9745 {
9746   tree t;
9747
9748   t = build_vl_exp (CALL_EXPR, nargs + 3);
9749   TREE_TYPE (t) = return_type;
9750   CALL_EXPR_FN (t) = fn;
9751   CALL_EXPR_STATIC_CHAIN (t) = NULL;
9752
9753   return t;
9754 }
9755
9756 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
9757    FN and a null static chain slot.  NARGS is the number of call arguments
9758    which are specified as "..." arguments.  */
9759
9760 tree
9761 build_call_nary (tree return_type, tree fn, int nargs, ...)
9762 {
9763   tree ret;
9764   va_list args;
9765   va_start (args, nargs);
9766   ret = build_call_valist (return_type, fn, nargs, args);
9767   va_end (args);
9768   return ret;
9769 }
9770
9771 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
9772    FN and a null static chain slot.  NARGS is the number of call arguments
9773    which are specified as a va_list ARGS.  */
9774
9775 tree
9776 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
9777 {
9778   tree t;
9779   int i;
9780
9781   t = build_call_1 (return_type, fn, nargs);
9782   for (i = 0; i < nargs; i++)
9783     CALL_EXPR_ARG (t, i) = va_arg (args, tree);
9784   process_call_operands (t);
9785   return t;
9786 }
9787
9788 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
9789    FN and a null static chain slot.  NARGS is the number of call arguments
9790    which are specified as a tree array ARGS.  */
9791
9792 tree
9793 build_call_array_loc (location_t loc, tree return_type, tree fn,
9794                       int nargs, const tree *args)
9795 {
9796   tree t;
9797   int i;
9798
9799   t = build_call_1 (return_type, fn, nargs);
9800   for (i = 0; i < nargs; i++)
9801     CALL_EXPR_ARG (t, i) = args[i];
9802   process_call_operands (t);
9803   SET_EXPR_LOCATION (t, loc);
9804   return t;
9805 }
9806
9807 /* Like build_call_array, but takes a VEC.  */
9808
9809 tree
9810 build_call_vec (tree return_type, tree fn, VEC(tree,gc) *args)
9811 {
9812   tree ret, t;
9813   unsigned int ix;
9814
9815   ret = build_call_1 (return_type, fn, VEC_length (tree, args));
9816   FOR_EACH_VEC_ELT (tree, args, ix, t)
9817     CALL_EXPR_ARG (ret, ix) = t;
9818   process_call_operands (ret);
9819   return ret;
9820 }
9821
9822
9823 /* Returns true if it is possible to prove that the index of
9824    an array access REF (an ARRAY_REF expression) falls into the
9825    array bounds.  */
9826
9827 bool
9828 in_array_bounds_p (tree ref)
9829 {
9830   tree idx = TREE_OPERAND (ref, 1);
9831   tree min, max;
9832
9833   if (TREE_CODE (idx) != INTEGER_CST)
9834     return false;
9835
9836   min = array_ref_low_bound (ref);
9837   max = array_ref_up_bound (ref);
9838   if (!min
9839       || !max
9840       || TREE_CODE (min) != INTEGER_CST
9841       || TREE_CODE (max) != INTEGER_CST)
9842     return false;
9843
9844   if (tree_int_cst_lt (idx, min)
9845       || tree_int_cst_lt (max, idx))
9846     return false;
9847
9848   return true;
9849 }
9850
9851 /* Returns true if it is possible to prove that the range of
9852    an array access REF (an ARRAY_RANGE_REF expression) falls
9853    into the array bounds.  */
9854
9855 bool
9856 range_in_array_bounds_p (tree ref)
9857 {
9858   tree domain_type = TYPE_DOMAIN (TREE_TYPE (ref));
9859   tree range_min, range_max, min, max;
9860
9861   range_min = TYPE_MIN_VALUE (domain_type);
9862   range_max = TYPE_MAX_VALUE (domain_type);
9863   if (!range_min
9864       || !range_max
9865       || TREE_CODE (range_min) != INTEGER_CST
9866       || TREE_CODE (range_max) != INTEGER_CST)
9867     return false;
9868
9869   min = array_ref_low_bound (ref);
9870   max = array_ref_up_bound (ref);
9871   if (!min
9872       || !max
9873       || TREE_CODE (min) != INTEGER_CST
9874       || TREE_CODE (max) != INTEGER_CST)
9875     return false;
9876
9877   if (tree_int_cst_lt (range_min, min)
9878       || tree_int_cst_lt (max, range_max))
9879     return false;
9880
9881   return true;
9882 }
9883
9884 /* Return true if T (assumed to be a DECL) must be assigned a memory
9885    location.  */
9886
9887 bool
9888 needs_to_live_in_memory (const_tree t)
9889 {
9890   if (TREE_CODE (t) == SSA_NAME)
9891     t = SSA_NAME_VAR (t);
9892
9893   return (TREE_ADDRESSABLE (t)
9894           || is_global_var (t)
9895           || (TREE_CODE (t) == RESULT_DECL
9896               && !DECL_BY_REFERENCE (t)
9897               && aggregate_value_p (t, current_function_decl)));
9898 }
9899
9900 /* Return value of a constant X and sign-extend it.  */
9901
9902 HOST_WIDE_INT
9903 int_cst_value (const_tree x)
9904 {
9905   unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
9906   unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
9907
9908   /* Make sure the sign-extended value will fit in a HOST_WIDE_INT.  */
9909   gcc_assert (TREE_INT_CST_HIGH (x) == 0
9910               || TREE_INT_CST_HIGH (x) == -1);
9911
9912   if (bits < HOST_BITS_PER_WIDE_INT)
9913     {
9914       bool negative = ((val >> (bits - 1)) & 1) != 0;
9915       if (negative)
9916         val |= (~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1;
9917       else
9918         val &= ~((~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1);
9919     }
9920
9921   return val;
9922 }
9923
9924 /* Return value of a constant X and sign-extend it.  */
9925
9926 HOST_WIDEST_INT
9927 widest_int_cst_value (const_tree x)
9928 {
9929   unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
9930   unsigned HOST_WIDEST_INT val = TREE_INT_CST_LOW (x);
9931
9932 #if HOST_BITS_PER_WIDEST_INT > HOST_BITS_PER_WIDE_INT
9933   gcc_assert (HOST_BITS_PER_WIDEST_INT >= 2 * HOST_BITS_PER_WIDE_INT);
9934   val |= (((unsigned HOST_WIDEST_INT) TREE_INT_CST_HIGH (x))
9935           << HOST_BITS_PER_WIDE_INT);
9936 #else
9937   /* Make sure the sign-extended value will fit in a HOST_WIDE_INT.  */
9938   gcc_assert (TREE_INT_CST_HIGH (x) == 0
9939               || TREE_INT_CST_HIGH (x) == -1);
9940 #endif
9941
9942   if (bits < HOST_BITS_PER_WIDEST_INT)
9943     {
9944       bool negative = ((val >> (bits - 1)) & 1) != 0;
9945       if (negative)
9946         val |= (~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1;
9947       else
9948         val &= ~((~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1);
9949     }
9950
9951   return val;
9952 }
9953
9954 /* If TYPE is an integral type, return an equivalent type which is
9955     unsigned iff UNSIGNEDP is true.  If TYPE is not an integral type,
9956     return TYPE itself.  */
9957
9958 tree
9959 signed_or_unsigned_type_for (int unsignedp, tree type)
9960 {
9961   tree t = type;
9962   if (POINTER_TYPE_P (type))
9963     {
9964       /* If the pointer points to the normal address space, use the
9965          size_type_node.  Otherwise use an appropriate size for the pointer
9966          based on the named address space it points to.  */
9967       if (!TYPE_ADDR_SPACE (TREE_TYPE (t)))
9968         t = size_type_node;
9969       else
9970         return lang_hooks.types.type_for_size (TYPE_PRECISION (t), unsignedp);
9971     }
9972
9973   if (!INTEGRAL_TYPE_P (t) || TYPE_UNSIGNED (t) == unsignedp)
9974     return t;
9975
9976   return lang_hooks.types.type_for_size (TYPE_PRECISION (t), unsignedp);
9977 }
9978
9979 /* Returns unsigned variant of TYPE.  */
9980
9981 tree
9982 unsigned_type_for (tree type)
9983 {
9984   return signed_or_unsigned_type_for (1, type);
9985 }
9986
9987 /* Returns signed variant of TYPE.  */
9988
9989 tree
9990 signed_type_for (tree type)
9991 {
9992   return signed_or_unsigned_type_for (0, type);
9993 }
9994
9995 /* Returns the largest value obtainable by casting something in INNER type to
9996    OUTER type.  */
9997
9998 tree
9999 upper_bound_in_type (tree outer, tree inner)
10000 {
10001   unsigned HOST_WIDE_INT lo, hi;
10002   unsigned int det = 0;
10003   unsigned oprec = TYPE_PRECISION (outer);
10004   unsigned iprec = TYPE_PRECISION (inner);
10005   unsigned prec;
10006
10007   /* Compute a unique number for every combination.  */
10008   det |= (oprec > iprec) ? 4 : 0;
10009   det |= TYPE_UNSIGNED (outer) ? 2 : 0;
10010   det |= TYPE_UNSIGNED (inner) ? 1 : 0;
10011
10012   /* Determine the exponent to use.  */
10013   switch (det)
10014     {
10015     case 0:
10016     case 1:
10017       /* oprec <= iprec, outer: signed, inner: don't care.  */
10018       prec = oprec - 1;
10019       break;
10020     case 2:
10021     case 3:
10022       /* oprec <= iprec, outer: unsigned, inner: don't care.  */
10023       prec = oprec;
10024       break;
10025     case 4:
10026       /* oprec > iprec, outer: signed, inner: signed.  */
10027       prec = iprec - 1;
10028       break;
10029     case 5:
10030       /* oprec > iprec, outer: signed, inner: unsigned.  */
10031       prec = iprec;
10032       break;
10033     case 6:
10034       /* oprec > iprec, outer: unsigned, inner: signed.  */
10035       prec = oprec;
10036       break;
10037     case 7:
10038       /* oprec > iprec, outer: unsigned, inner: unsigned.  */
10039       prec = iprec;
10040       break;
10041     default:
10042       gcc_unreachable ();
10043     }
10044
10045   /* Compute 2^^prec - 1.  */
10046   if (prec <= HOST_BITS_PER_WIDE_INT)
10047     {
10048       hi = 0;
10049       lo = ((~(unsigned HOST_WIDE_INT) 0)
10050             >> (HOST_BITS_PER_WIDE_INT - prec));
10051     }
10052   else
10053     {
10054       hi = ((~(unsigned HOST_WIDE_INT) 0)
10055             >> (2 * HOST_BITS_PER_WIDE_INT - prec));
10056       lo = ~(unsigned HOST_WIDE_INT) 0;
10057     }
10058
10059   return build_int_cst_wide (outer, lo, hi);
10060 }
10061
10062 /* Returns the smallest value obtainable by casting something in INNER type to
10063    OUTER type.  */
10064
10065 tree
10066 lower_bound_in_type (tree outer, tree inner)
10067 {
10068   unsigned HOST_WIDE_INT lo, hi;
10069   unsigned oprec = TYPE_PRECISION (outer);
10070   unsigned iprec = TYPE_PRECISION (inner);
10071
10072   /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
10073      and obtain 0.  */
10074   if (TYPE_UNSIGNED (outer)
10075       /* If we are widening something of an unsigned type, OUTER type
10076          contains all values of INNER type.  In particular, both INNER
10077          and OUTER types have zero in common.  */
10078       || (oprec > iprec && TYPE_UNSIGNED (inner)))
10079     lo = hi = 0;
10080   else
10081     {
10082       /* If we are widening a signed type to another signed type, we
10083          want to obtain -2^^(iprec-1).  If we are keeping the
10084          precision or narrowing to a signed type, we want to obtain
10085          -2^(oprec-1).  */
10086       unsigned prec = oprec > iprec ? iprec : oprec;
10087
10088       if (prec <= HOST_BITS_PER_WIDE_INT)
10089         {
10090           hi = ~(unsigned HOST_WIDE_INT) 0;
10091           lo = (~(unsigned HOST_WIDE_INT) 0) << (prec - 1);
10092         }
10093       else
10094         {
10095           hi = ((~(unsigned HOST_WIDE_INT) 0)
10096                 << (prec - HOST_BITS_PER_WIDE_INT - 1));
10097           lo = 0;
10098         }
10099     }
10100
10101   return build_int_cst_wide (outer, lo, hi);
10102 }
10103
10104 /* Return nonzero if two operands that are suitable for PHI nodes are
10105    necessarily equal.  Specifically, both ARG0 and ARG1 must be either
10106    SSA_NAME or invariant.  Note that this is strictly an optimization.
10107    That is, callers of this function can directly call operand_equal_p
10108    and get the same result, only slower.  */
10109
10110 int
10111 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
10112 {
10113   if (arg0 == arg1)
10114     return 1;
10115   if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
10116     return 0;
10117   return operand_equal_p (arg0, arg1, 0);
10118 }
10119
10120 /* Returns number of zeros at the end of binary representation of X.
10121
10122    ??? Use ffs if available?  */
10123
10124 tree
10125 num_ending_zeros (const_tree x)
10126 {
10127   unsigned HOST_WIDE_INT fr, nfr;
10128   unsigned num, abits;
10129   tree type = TREE_TYPE (x);
10130
10131   if (TREE_INT_CST_LOW (x) == 0)
10132     {
10133       num = HOST_BITS_PER_WIDE_INT;
10134       fr = TREE_INT_CST_HIGH (x);
10135     }
10136   else
10137     {
10138       num = 0;
10139       fr = TREE_INT_CST_LOW (x);
10140     }
10141
10142   for (abits = HOST_BITS_PER_WIDE_INT / 2; abits; abits /= 2)
10143     {
10144       nfr = fr >> abits;
10145       if (nfr << abits == fr)
10146         {
10147           num += abits;
10148           fr = nfr;
10149         }
10150     }
10151
10152   if (num > TYPE_PRECISION (type))
10153     num = TYPE_PRECISION (type);
10154
10155   return build_int_cst_type (type, num);
10156 }
10157
10158
10159 #define WALK_SUBTREE(NODE)                              \
10160   do                                                    \
10161     {                                                   \
10162       result = walk_tree_1 (&(NODE), func, data, pset, lh);     \
10163       if (result)                                       \
10164         return result;                                  \
10165     }                                                   \
10166   while (0)
10167
10168 /* This is a subroutine of walk_tree that walks field of TYPE that are to
10169    be walked whenever a type is seen in the tree.  Rest of operands and return
10170    value are as for walk_tree.  */
10171
10172 static tree
10173 walk_type_fields (tree type, walk_tree_fn func, void *data,
10174                   struct pointer_set_t *pset, walk_tree_lh lh)
10175 {
10176   tree result = NULL_TREE;
10177
10178   switch (TREE_CODE (type))
10179     {
10180     case POINTER_TYPE:
10181     case REFERENCE_TYPE:
10182       /* We have to worry about mutually recursive pointers.  These can't
10183          be written in C.  They can in Ada.  It's pathological, but
10184          there's an ACATS test (c38102a) that checks it.  Deal with this
10185          by checking if we're pointing to another pointer, that one
10186          points to another pointer, that one does too, and we have no htab.
10187          If so, get a hash table.  We check three levels deep to avoid
10188          the cost of the hash table if we don't need one.  */
10189       if (POINTER_TYPE_P (TREE_TYPE (type))
10190           && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
10191           && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
10192           && !pset)
10193         {
10194           result = walk_tree_without_duplicates (&TREE_TYPE (type),
10195                                                  func, data);
10196           if (result)
10197             return result;
10198
10199           break;
10200         }
10201
10202       /* ... fall through ... */
10203
10204     case COMPLEX_TYPE:
10205       WALK_SUBTREE (TREE_TYPE (type));
10206       break;
10207
10208     case METHOD_TYPE:
10209       WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
10210
10211       /* Fall through.  */
10212
10213     case FUNCTION_TYPE:
10214       WALK_SUBTREE (TREE_TYPE (type));
10215       {
10216         tree arg;
10217
10218         /* We never want to walk into default arguments.  */
10219         for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
10220           WALK_SUBTREE (TREE_VALUE (arg));
10221       }
10222       break;
10223
10224     case ARRAY_TYPE:
10225       /* Don't follow this nodes's type if a pointer for fear that
10226          we'll have infinite recursion.  If we have a PSET, then we
10227          need not fear.  */
10228       if (pset
10229           || (!POINTER_TYPE_P (TREE_TYPE (type))
10230               && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
10231         WALK_SUBTREE (TREE_TYPE (type));
10232       WALK_SUBTREE (TYPE_DOMAIN (type));
10233       break;
10234
10235     case OFFSET_TYPE:
10236       WALK_SUBTREE (TREE_TYPE (type));
10237       WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
10238       break;
10239
10240     default:
10241       break;
10242     }
10243
10244   return NULL_TREE;
10245 }
10246
10247 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal.  FUNC is
10248    called with the DATA and the address of each sub-tree.  If FUNC returns a
10249    non-NULL value, the traversal is stopped, and the value returned by FUNC
10250    is returned.  If PSET is non-NULL it is used to record the nodes visited,
10251    and to avoid visiting a node more than once.  */
10252
10253 tree
10254 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
10255              struct pointer_set_t *pset, walk_tree_lh lh)
10256 {
10257   enum tree_code code;
10258   int walk_subtrees;
10259   tree result;
10260
10261 #define WALK_SUBTREE_TAIL(NODE)                         \
10262   do                                                    \
10263     {                                                   \
10264        tp = & (NODE);                                   \
10265        goto tail_recurse;                               \
10266     }                                                   \
10267   while (0)
10268
10269  tail_recurse:
10270   /* Skip empty subtrees.  */
10271   if (!*tp)
10272     return NULL_TREE;
10273
10274   /* Don't walk the same tree twice, if the user has requested
10275      that we avoid doing so.  */
10276   if (pset && pointer_set_insert (pset, *tp))
10277     return NULL_TREE;
10278
10279   /* Call the function.  */
10280   walk_subtrees = 1;
10281   result = (*func) (tp, &walk_subtrees, data);
10282
10283   /* If we found something, return it.  */
10284   if (result)
10285     return result;
10286
10287   code = TREE_CODE (*tp);
10288
10289   /* Even if we didn't, FUNC may have decided that there was nothing
10290      interesting below this point in the tree.  */
10291   if (!walk_subtrees)
10292     {
10293       /* But we still need to check our siblings.  */
10294       if (code == TREE_LIST)
10295         WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
10296       else if (code == OMP_CLAUSE)
10297         WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10298       else
10299         return NULL_TREE;
10300     }
10301
10302   if (lh)
10303     {
10304       result = (*lh) (tp, &walk_subtrees, func, data, pset);
10305       if (result || !walk_subtrees)
10306         return result;
10307     }
10308
10309   switch (code)
10310     {
10311     case ERROR_MARK:
10312     case IDENTIFIER_NODE:
10313     case INTEGER_CST:
10314     case REAL_CST:
10315     case FIXED_CST:
10316     case VECTOR_CST:
10317     case STRING_CST:
10318     case BLOCK:
10319     case PLACEHOLDER_EXPR:
10320     case SSA_NAME:
10321     case FIELD_DECL:
10322     case RESULT_DECL:
10323       /* None of these have subtrees other than those already walked
10324          above.  */
10325       break;
10326
10327     case TREE_LIST:
10328       WALK_SUBTREE (TREE_VALUE (*tp));
10329       WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
10330       break;
10331
10332     case TREE_VEC:
10333       {
10334         int len = TREE_VEC_LENGTH (*tp);
10335
10336         if (len == 0)
10337           break;
10338
10339         /* Walk all elements but the first.  */
10340         while (--len)
10341           WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
10342
10343         /* Now walk the first one as a tail call.  */
10344         WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
10345       }
10346
10347     case COMPLEX_CST:
10348       WALK_SUBTREE (TREE_REALPART (*tp));
10349       WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
10350
10351     case CONSTRUCTOR:
10352       {
10353         unsigned HOST_WIDE_INT idx;
10354         constructor_elt *ce;
10355
10356         for (idx = 0;
10357              VEC_iterate(constructor_elt, CONSTRUCTOR_ELTS (*tp), idx, ce);
10358              idx++)
10359           WALK_SUBTREE (ce->value);
10360       }
10361       break;
10362
10363     case SAVE_EXPR:
10364       WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
10365
10366     case BIND_EXPR:
10367       {
10368         tree decl;
10369         for (decl = BIND_EXPR_VARS (*tp); decl; decl = DECL_CHAIN (decl))
10370           {
10371             /* Walk the DECL_INITIAL and DECL_SIZE.  We don't want to walk
10372                into declarations that are just mentioned, rather than
10373                declared; they don't really belong to this part of the tree.
10374                And, we can see cycles: the initializer for a declaration
10375                can refer to the declaration itself.  */
10376             WALK_SUBTREE (DECL_INITIAL (decl));
10377             WALK_SUBTREE (DECL_SIZE (decl));
10378             WALK_SUBTREE (DECL_SIZE_UNIT (decl));
10379           }
10380         WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
10381       }
10382
10383     case STATEMENT_LIST:
10384       {
10385         tree_stmt_iterator i;
10386         for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
10387           WALK_SUBTREE (*tsi_stmt_ptr (i));
10388       }
10389       break;
10390
10391     case OMP_CLAUSE:
10392       switch (OMP_CLAUSE_CODE (*tp))
10393         {
10394         case OMP_CLAUSE_PRIVATE:
10395         case OMP_CLAUSE_SHARED:
10396         case OMP_CLAUSE_FIRSTPRIVATE:
10397         case OMP_CLAUSE_COPYIN:
10398         case OMP_CLAUSE_COPYPRIVATE:
10399         case OMP_CLAUSE_IF:
10400         case OMP_CLAUSE_NUM_THREADS:
10401         case OMP_CLAUSE_SCHEDULE:
10402           WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0));
10403           /* FALLTHRU */
10404
10405         case OMP_CLAUSE_NOWAIT:
10406         case OMP_CLAUSE_ORDERED:
10407         case OMP_CLAUSE_DEFAULT:
10408         case OMP_CLAUSE_UNTIED:
10409           WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10410
10411         case OMP_CLAUSE_LASTPRIVATE:
10412           WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
10413           WALK_SUBTREE (OMP_CLAUSE_LASTPRIVATE_STMT (*tp));
10414           WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10415
10416         case OMP_CLAUSE_COLLAPSE:
10417           {
10418             int i;
10419             for (i = 0; i < 3; i++)
10420               WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
10421             WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10422           }
10423
10424         case OMP_CLAUSE_REDUCTION:
10425           {
10426             int i;
10427             for (i = 0; i < 4; i++)
10428               WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
10429             WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10430           }
10431
10432         default:
10433           gcc_unreachable ();
10434         }
10435       break;
10436
10437     case TARGET_EXPR:
10438       {
10439         int i, len;
10440
10441         /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
10442            But, we only want to walk once.  */
10443         len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
10444         for (i = 0; i < len; ++i)
10445           WALK_SUBTREE (TREE_OPERAND (*tp, i));
10446         WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
10447       }
10448
10449     case DECL_EXPR:
10450       /* If this is a TYPE_DECL, walk into the fields of the type that it's
10451          defining.  We only want to walk into these fields of a type in this
10452          case and not in the general case of a mere reference to the type.
10453
10454          The criterion is as follows: if the field can be an expression, it
10455          must be walked only here.  This should be in keeping with the fields
10456          that are directly gimplified in gimplify_type_sizes in order for the
10457          mark/copy-if-shared/unmark machinery of the gimplifier to work with
10458          variable-sized types.
10459
10460          Note that DECLs get walked as part of processing the BIND_EXPR.  */
10461       if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
10462         {
10463           tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
10464           if (TREE_CODE (*type_p) == ERROR_MARK)
10465             return NULL_TREE;
10466
10467           /* Call the function for the type.  See if it returns anything or
10468              doesn't want us to continue.  If we are to continue, walk both
10469              the normal fields and those for the declaration case.  */
10470           result = (*func) (type_p, &walk_subtrees, data);
10471           if (result || !walk_subtrees)
10472             return result;
10473
10474           result = walk_type_fields (*type_p, func, data, pset, lh);
10475           if (result)
10476             return result;
10477
10478           /* If this is a record type, also walk the fields.  */
10479           if (RECORD_OR_UNION_TYPE_P (*type_p))
10480             {
10481               tree field;
10482
10483               for (field = TYPE_FIELDS (*type_p); field;
10484                    field = DECL_CHAIN (field))
10485                 {
10486                   /* We'd like to look at the type of the field, but we can
10487                      easily get infinite recursion.  So assume it's pointed
10488                      to elsewhere in the tree.  Also, ignore things that
10489                      aren't fields.  */
10490                   if (TREE_CODE (field) != FIELD_DECL)
10491                     continue;
10492
10493                   WALK_SUBTREE (DECL_FIELD_OFFSET (field));
10494                   WALK_SUBTREE (DECL_SIZE (field));
10495                   WALK_SUBTREE (DECL_SIZE_UNIT (field));
10496                   if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
10497                     WALK_SUBTREE (DECL_QUALIFIER (field));
10498                 }
10499             }
10500
10501           /* Same for scalar types.  */
10502           else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
10503                    || TREE_CODE (*type_p) == ENUMERAL_TYPE
10504                    || TREE_CODE (*type_p) == INTEGER_TYPE
10505                    || TREE_CODE (*type_p) == FIXED_POINT_TYPE
10506                    || TREE_CODE (*type_p) == REAL_TYPE)
10507             {
10508               WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
10509               WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
10510             }
10511
10512           WALK_SUBTREE (TYPE_SIZE (*type_p));
10513           WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
10514         }
10515       /* FALLTHRU */
10516
10517     default:
10518       if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
10519         {
10520           int i, len;
10521
10522           /* Walk over all the sub-trees of this operand.  */
10523           len = TREE_OPERAND_LENGTH (*tp);
10524
10525           /* Go through the subtrees.  We need to do this in forward order so
10526              that the scope of a FOR_EXPR is handled properly.  */
10527           if (len)
10528             {
10529               for (i = 0; i < len - 1; ++i)
10530                 WALK_SUBTREE (TREE_OPERAND (*tp, i));
10531               WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
10532             }
10533         }
10534       /* If this is a type, walk the needed fields in the type.  */
10535       else if (TYPE_P (*tp))
10536         return walk_type_fields (*tp, func, data, pset, lh);
10537       break;
10538     }
10539
10540   /* We didn't find what we were looking for.  */
10541   return NULL_TREE;
10542
10543 #undef WALK_SUBTREE_TAIL
10544 }
10545 #undef WALK_SUBTREE
10546
10547 /* Like walk_tree, but does not walk duplicate nodes more than once.  */
10548
10549 tree
10550 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
10551                                 walk_tree_lh lh)
10552 {
10553   tree result;
10554   struct pointer_set_t *pset;
10555
10556   pset = pointer_set_create ();
10557   result = walk_tree_1 (tp, func, data, pset, lh);
10558   pointer_set_destroy (pset);
10559   return result;
10560 }
10561
10562
10563 tree *
10564 tree_block (tree t)
10565 {
10566   char const c = TREE_CODE_CLASS (TREE_CODE (t));
10567
10568   if (IS_EXPR_CODE_CLASS (c))
10569     return &t->exp.block;
10570   gcc_unreachable ();
10571   return NULL;
10572 }
10573
10574 /* Create a nameless artificial label and put it in the current
10575    function context.  The label has a location of LOC.  Returns the
10576    newly created label.  */
10577
10578 tree
10579 create_artificial_label (location_t loc)
10580 {
10581   tree lab = build_decl (loc,
10582                          LABEL_DECL, NULL_TREE, void_type_node);
10583
10584   DECL_ARTIFICIAL (lab) = 1;
10585   DECL_IGNORED_P (lab) = 1;
10586   DECL_CONTEXT (lab) = current_function_decl;
10587   return lab;
10588 }
10589
10590 /*  Given a tree, try to return a useful variable name that we can use
10591     to prefix a temporary that is being assigned the value of the tree.
10592     I.E. given  <temp> = &A, return A.  */
10593
10594 const char *
10595 get_name (tree t)
10596 {
10597   tree stripped_decl;
10598
10599   stripped_decl = t;
10600   STRIP_NOPS (stripped_decl);
10601   if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
10602     return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
10603   else
10604     {
10605       switch (TREE_CODE (stripped_decl))
10606         {
10607         case ADDR_EXPR:
10608           return get_name (TREE_OPERAND (stripped_decl, 0));
10609         default:
10610           return NULL;
10611         }
10612     }
10613 }
10614
10615 /* Return true if TYPE has a variable argument list.  */
10616
10617 bool
10618 stdarg_p (const_tree fntype)
10619 {
10620   function_args_iterator args_iter;
10621   tree n = NULL_TREE, t;
10622
10623   if (!fntype)
10624     return false;
10625
10626   FOREACH_FUNCTION_ARGS(fntype, t, args_iter)
10627     {
10628       n = t;
10629     }
10630
10631   return n != NULL_TREE && n != void_type_node;
10632 }
10633
10634 /* Return true if TYPE has a prototype.  */
10635
10636 bool
10637 prototype_p (tree fntype)
10638 {
10639   tree t;
10640
10641   gcc_assert (fntype != NULL_TREE);
10642
10643   t = TYPE_ARG_TYPES (fntype);
10644   return (t != NULL_TREE);
10645 }
10646
10647 /* If BLOCK is inlined from an __attribute__((__artificial__))
10648    routine, return pointer to location from where it has been
10649    called.  */
10650 location_t *
10651 block_nonartificial_location (tree block)
10652 {
10653   location_t *ret = NULL;
10654
10655   while (block && TREE_CODE (block) == BLOCK
10656          && BLOCK_ABSTRACT_ORIGIN (block))
10657     {
10658       tree ao = BLOCK_ABSTRACT_ORIGIN (block);
10659
10660       while (TREE_CODE (ao) == BLOCK
10661              && BLOCK_ABSTRACT_ORIGIN (ao)
10662              && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
10663         ao = BLOCK_ABSTRACT_ORIGIN (ao);
10664
10665       if (TREE_CODE (ao) == FUNCTION_DECL)
10666         {
10667           /* If AO is an artificial inline, point RET to the
10668              call site locus at which it has been inlined and continue
10669              the loop, in case AO's caller is also an artificial
10670              inline.  */
10671           if (DECL_DECLARED_INLINE_P (ao)
10672               && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
10673             ret = &BLOCK_SOURCE_LOCATION (block);
10674           else
10675             break;
10676         }
10677       else if (TREE_CODE (ao) != BLOCK)
10678         break;
10679
10680       block = BLOCK_SUPERCONTEXT (block);
10681     }
10682   return ret;
10683 }
10684
10685
10686 /* If EXP is inlined from an __attribute__((__artificial__))
10687    function, return the location of the original call expression.  */
10688
10689 location_t
10690 tree_nonartificial_location (tree exp)
10691 {
10692   location_t *loc = block_nonartificial_location (TREE_BLOCK (exp));
10693
10694   if (loc)
10695     return *loc;
10696   else
10697     return EXPR_LOCATION (exp);
10698 }
10699
10700
10701 /* These are the hash table functions for the hash table of OPTIMIZATION_NODEq
10702    nodes.  */
10703
10704 /* Return the hash code code X, an OPTIMIZATION_NODE or TARGET_OPTION code.  */
10705
10706 static hashval_t
10707 cl_option_hash_hash (const void *x)
10708 {
10709   const_tree const t = (const_tree) x;
10710   const char *p;
10711   size_t i;
10712   size_t len = 0;
10713   hashval_t hash = 0;
10714
10715   if (TREE_CODE (t) == OPTIMIZATION_NODE)
10716     {
10717       p = (const char *)TREE_OPTIMIZATION (t);
10718       len = sizeof (struct cl_optimization);
10719     }
10720
10721   else if (TREE_CODE (t) == TARGET_OPTION_NODE)
10722     {
10723       p = (const char *)TREE_TARGET_OPTION (t);
10724       len = sizeof (struct cl_target_option);
10725     }
10726
10727   else
10728     gcc_unreachable ();
10729
10730   /* assume most opt flags are just 0/1, some are 2-3, and a few might be
10731      something else.  */
10732   for (i = 0; i < len; i++)
10733     if (p[i])
10734       hash = (hash << 4) ^ ((i << 2) | p[i]);
10735
10736   return hash;
10737 }
10738
10739 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
10740    TARGET_OPTION tree node) is the same as that given by *Y, which is the
10741    same.  */
10742
10743 static int
10744 cl_option_hash_eq (const void *x, const void *y)
10745 {
10746   const_tree const xt = (const_tree) x;
10747   const_tree const yt = (const_tree) y;
10748   const char *xp;
10749   const char *yp;
10750   size_t len;
10751
10752   if (TREE_CODE (xt) != TREE_CODE (yt))
10753     return 0;
10754
10755   if (TREE_CODE (xt) == OPTIMIZATION_NODE)
10756     {
10757       xp = (const char *)TREE_OPTIMIZATION (xt);
10758       yp = (const char *)TREE_OPTIMIZATION (yt);
10759       len = sizeof (struct cl_optimization);
10760     }
10761
10762   else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
10763     {
10764       xp = (const char *)TREE_TARGET_OPTION (xt);
10765       yp = (const char *)TREE_TARGET_OPTION (yt);
10766       len = sizeof (struct cl_target_option);
10767     }
10768
10769   else
10770     gcc_unreachable ();
10771
10772   return (memcmp (xp, yp, len) == 0);
10773 }
10774
10775 /* Build an OPTIMIZATION_NODE based on the current options.  */
10776
10777 tree
10778 build_optimization_node (void)
10779 {
10780   tree t;
10781   void **slot;
10782
10783   /* Use the cache of optimization nodes.  */
10784
10785   cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node),
10786                         &global_options);
10787
10788   slot = htab_find_slot (cl_option_hash_table, cl_optimization_node, INSERT);
10789   t = (tree) *slot;
10790   if (!t)
10791     {
10792       /* Insert this one into the hash table.  */
10793       t = cl_optimization_node;
10794       *slot = t;
10795
10796       /* Make a new node for next time round.  */
10797       cl_optimization_node = make_node (OPTIMIZATION_NODE);
10798     }
10799
10800   return t;
10801 }
10802
10803 /* Build a TARGET_OPTION_NODE based on the current options.  */
10804
10805 tree
10806 build_target_option_node (void)
10807 {
10808   tree t;
10809   void **slot;
10810
10811   /* Use the cache of optimization nodes.  */
10812
10813   cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node),
10814                          &global_options);
10815
10816   slot = htab_find_slot (cl_option_hash_table, cl_target_option_node, INSERT);
10817   t = (tree) *slot;
10818   if (!t)
10819     {
10820       /* Insert this one into the hash table.  */
10821       t = cl_target_option_node;
10822       *slot = t;
10823
10824       /* Make a new node for next time round.  */
10825       cl_target_option_node = make_node (TARGET_OPTION_NODE);
10826     }
10827
10828   return t;
10829 }
10830
10831 /* Determine the "ultimate origin" of a block.  The block may be an inlined
10832    instance of an inlined instance of a block which is local to an inline
10833    function, so we have to trace all of the way back through the origin chain
10834    to find out what sort of node actually served as the original seed for the
10835    given block.  */
10836
10837 tree
10838 block_ultimate_origin (const_tree block)
10839 {
10840   tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
10841
10842   /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
10843      nodes in the function to point to themselves; ignore that if
10844      we're trying to output the abstract instance of this function.  */
10845   if (BLOCK_ABSTRACT (block) && immediate_origin == block)
10846     return NULL_TREE;
10847
10848   if (immediate_origin == NULL_TREE)
10849     return NULL_TREE;
10850   else
10851     {
10852       tree ret_val;
10853       tree lookahead = immediate_origin;
10854
10855       do
10856         {
10857           ret_val = lookahead;
10858           lookahead = (TREE_CODE (ret_val) == BLOCK
10859                        ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
10860         }
10861       while (lookahead != NULL && lookahead != ret_val);
10862
10863       /* The block's abstract origin chain may not be the *ultimate* origin of
10864          the block. It could lead to a DECL that has an abstract origin set.
10865          If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
10866          will give us if it has one).  Note that DECL's abstract origins are
10867          supposed to be the most distant ancestor (or so decl_ultimate_origin
10868          claims), so we don't need to loop following the DECL origins.  */
10869       if (DECL_P (ret_val))
10870         return DECL_ORIGIN (ret_val);
10871
10872       return ret_val;
10873     }
10874 }
10875
10876 /* Return true if T1 and T2 are equivalent lists.  */
10877
10878 bool
10879 list_equal_p (const_tree t1, const_tree t2)
10880 {
10881   for (; t1 && t2; t1 = TREE_CHAIN (t1) , t2 = TREE_CHAIN (t2))
10882     if (TREE_VALUE (t1) != TREE_VALUE (t2))
10883       return false;
10884   return !t1 && !t2;
10885 }
10886
10887 /* Return true iff conversion in EXP generates no instruction.  Mark
10888    it inline so that we fully inline into the stripping functions even
10889    though we have two uses of this function.  */
10890
10891 static inline bool
10892 tree_nop_conversion (const_tree exp)
10893 {
10894   tree outer_type, inner_type;
10895
10896   if (!CONVERT_EXPR_P (exp)
10897       && TREE_CODE (exp) != NON_LVALUE_EXPR)
10898     return false;
10899   if (TREE_OPERAND (exp, 0) == error_mark_node)
10900     return false;
10901
10902   outer_type = TREE_TYPE (exp);
10903   inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
10904
10905   if (!inner_type)
10906     return false;
10907
10908   /* Use precision rather then machine mode when we can, which gives
10909      the correct answer even for submode (bit-field) types.  */
10910   if ((INTEGRAL_TYPE_P (outer_type)
10911        || POINTER_TYPE_P (outer_type)
10912        || TREE_CODE (outer_type) == OFFSET_TYPE)
10913       && (INTEGRAL_TYPE_P (inner_type)
10914           || POINTER_TYPE_P (inner_type)
10915           || TREE_CODE (inner_type) == OFFSET_TYPE))
10916     return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
10917
10918   /* Otherwise fall back on comparing machine modes (e.g. for
10919      aggregate types, floats).  */
10920   return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
10921 }
10922
10923 /* Return true iff conversion in EXP generates no instruction.  Don't
10924    consider conversions changing the signedness.  */
10925
10926 static bool
10927 tree_sign_nop_conversion (const_tree exp)
10928 {
10929   tree outer_type, inner_type;
10930
10931   if (!tree_nop_conversion (exp))
10932     return false;
10933
10934   outer_type = TREE_TYPE (exp);
10935   inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
10936
10937   return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
10938           && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
10939 }
10940
10941 /* Strip conversions from EXP according to tree_nop_conversion and
10942    return the resulting expression.  */
10943
10944 tree
10945 tree_strip_nop_conversions (tree exp)
10946 {
10947   while (tree_nop_conversion (exp))
10948     exp = TREE_OPERAND (exp, 0);
10949   return exp;
10950 }
10951
10952 /* Strip conversions from EXP according to tree_sign_nop_conversion
10953    and return the resulting expression.  */
10954
10955 tree
10956 tree_strip_sign_nop_conversions (tree exp)
10957 {
10958   while (tree_sign_nop_conversion (exp))
10959     exp = TREE_OPERAND (exp, 0);
10960   return exp;
10961 }
10962
10963 static GTY(()) tree gcc_eh_personality_decl;
10964
10965 /* Return the GCC personality function decl.  */
10966
10967 tree
10968 lhd_gcc_personality (void)
10969 {
10970   if (!gcc_eh_personality_decl)
10971     gcc_eh_personality_decl = build_personality_function ("gcc");
10972   return gcc_eh_personality_decl;
10973 }
10974
10975 /* Try to find a base info of BINFO that would have its field decl at offset
10976    OFFSET within the BINFO type and which is of EXPECTED_TYPE.  If it can be
10977    found, return, otherwise return NULL_TREE.  */
10978
10979 tree
10980 get_binfo_at_offset (tree binfo, HOST_WIDE_INT offset, tree expected_type)
10981 {
10982   tree type = BINFO_TYPE (binfo);
10983
10984   while (true)
10985     {
10986       HOST_WIDE_INT pos, size;
10987       tree fld;
10988       int i;
10989
10990       if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (expected_type))
10991           return binfo;
10992       if (offset < 0)
10993         return NULL_TREE;
10994
10995       for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
10996         {
10997           if (TREE_CODE (fld) != FIELD_DECL)
10998             continue;
10999
11000           pos = int_bit_position (fld);
11001           size = tree_low_cst (DECL_SIZE (fld), 1);
11002           if (pos <= offset && (pos + size) > offset)
11003             break;
11004         }
11005       if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE)
11006         return NULL_TREE;
11007
11008       if (!DECL_ARTIFICIAL (fld))
11009         {
11010           binfo = TYPE_BINFO (TREE_TYPE (fld));
11011           if (!binfo)
11012             return NULL_TREE;
11013         }
11014       /* Offset 0 indicates the primary base, whose vtable contents are
11015          represented in the binfo for the derived class.  */
11016       else if (offset != 0)
11017         {
11018           tree base_binfo, found_binfo = NULL_TREE;
11019           for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
11020             if (TREE_TYPE (base_binfo) == TREE_TYPE (fld))
11021               {
11022                 found_binfo = base_binfo;
11023                 break;
11024               }
11025           if (!found_binfo)
11026             return NULL_TREE;
11027           binfo = found_binfo;
11028         }
11029
11030       type = TREE_TYPE (fld);
11031       offset -= pos;
11032     }
11033 }
11034
11035 /* Returns true if X is a typedef decl.  */
11036
11037 bool
11038 is_typedef_decl (tree x)
11039 {
11040   return (x && TREE_CODE (x) == TYPE_DECL
11041           && DECL_ORIGINAL_TYPE (x) != NULL_TREE);
11042 }
11043
11044 /* Returns true iff TYPE is a type variant created for a typedef. */
11045
11046 bool
11047 typedef_variant_p (tree type)
11048 {
11049   return is_typedef_decl (TYPE_NAME (type));
11050 }
11051
11052 /* Warn about a use of an identifier which was marked deprecated.  */
11053 void
11054 warn_deprecated_use (tree node, tree attr)
11055 {
11056   const char *msg;
11057
11058   if (node == 0 || !warn_deprecated_decl)
11059     return;
11060
11061   if (!attr)
11062     {
11063       if (DECL_P (node))
11064         attr = DECL_ATTRIBUTES (node);
11065       else if (TYPE_P (node))
11066         {
11067           tree decl = TYPE_STUB_DECL (node);
11068           if (decl)
11069             attr = lookup_attribute ("deprecated",
11070                                      TYPE_ATTRIBUTES (TREE_TYPE (decl)));
11071         }
11072     }
11073
11074   if (attr)
11075     attr = lookup_attribute ("deprecated", attr);
11076
11077   if (attr)
11078     msg = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr)));
11079   else
11080     msg = NULL;
11081
11082   if (DECL_P (node))
11083     {
11084       expanded_location xloc = expand_location (DECL_SOURCE_LOCATION (node));
11085       if (msg)
11086         warning (OPT_Wdeprecated_declarations,
11087                  "%qD is deprecated (declared at %s:%d): %s",
11088                  node, xloc.file, xloc.line, msg);
11089       else
11090         warning (OPT_Wdeprecated_declarations,
11091                  "%qD is deprecated (declared at %s:%d)",
11092                  node, xloc.file, xloc.line);
11093     }
11094   else if (TYPE_P (node))
11095     {
11096       tree what = NULL_TREE;
11097       tree decl = TYPE_STUB_DECL (node);
11098
11099       if (TYPE_NAME (node))
11100         {
11101           if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
11102             what = TYPE_NAME (node);
11103           else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
11104                    && DECL_NAME (TYPE_NAME (node)))
11105             what = DECL_NAME (TYPE_NAME (node));
11106         }
11107
11108       if (decl)
11109         {
11110           expanded_location xloc
11111             = expand_location (DECL_SOURCE_LOCATION (decl));
11112           if (what)
11113             {
11114               if (msg)
11115                 warning (OPT_Wdeprecated_declarations,
11116                          "%qE is deprecated (declared at %s:%d): %s",
11117                          what, xloc.file, xloc.line, msg);
11118               else
11119                 warning (OPT_Wdeprecated_declarations,
11120                          "%qE is deprecated (declared at %s:%d)", what,
11121                          xloc.file, xloc.line);
11122             }
11123           else
11124             {
11125               if (msg)
11126                 warning (OPT_Wdeprecated_declarations,
11127                          "type is deprecated (declared at %s:%d): %s",
11128                          xloc.file, xloc.line, msg);
11129               else
11130                 warning (OPT_Wdeprecated_declarations,
11131                          "type is deprecated (declared at %s:%d)",
11132                          xloc.file, xloc.line);
11133             }
11134         }
11135       else
11136         {
11137           if (what)
11138             {
11139               if (msg)
11140                 warning (OPT_Wdeprecated_declarations, "%qE is deprecated: %s",
11141                          what, msg);
11142               else
11143                 warning (OPT_Wdeprecated_declarations, "%qE is deprecated", what);
11144             }
11145           else
11146             {
11147               if (msg)
11148                 warning (OPT_Wdeprecated_declarations, "type is deprecated: %s",
11149                          msg);
11150               else
11151                 warning (OPT_Wdeprecated_declarations, "type is deprecated");
11152             }
11153         }
11154     }
11155 }
11156
11157 #include "gt-tree.h"