OSDN Git Service

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