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