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