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