OSDN Git Service

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