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