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