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