OSDN Git Service

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