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