re PR tree-optimization/54570 (FAIL: gcc.dg/builtin-object-size-8.c execution test)
[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 tree old_t = t;
7509 t = type_hash_canon (hashcode, t);
7510 if (t != old_t)
7511 /* Lay it out again in case the element type has been completed since
7512 the array was added to the hash table. */
7513 layout_type (t);
7514 }
7515
7516 if (TYPE_CANONICAL (t) == t)
7517 {
7518 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
7519 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
7520 SET_TYPE_STRUCTURAL_EQUALITY (t);
7521 else if (TYPE_CANONICAL (elt_type) != elt_type
7522 || (index_type && TYPE_CANONICAL (index_type) != index_type))
7523 TYPE_CANONICAL (t)
7524 = build_array_type_1 (TYPE_CANONICAL (elt_type),
7525 index_type
7526 ? TYPE_CANONICAL (index_type) : NULL_TREE,
7527 shared);
7528 }
7529
7530 return t;
7531 }
7532
7533 /* Wrapper around build_array_type_1 with SHARED set to true. */
7534
7535 tree
7536 build_array_type (tree elt_type, tree index_type)
7537 {
7538 return build_array_type_1 (elt_type, index_type, true);
7539 }
7540
7541 /* Wrapper around build_array_type_1 with SHARED set to false. */
7542
7543 tree
7544 build_nonshared_array_type (tree elt_type, tree index_type)
7545 {
7546 return build_array_type_1 (elt_type, index_type, false);
7547 }
7548
7549 /* Return a representation of ELT_TYPE[NELTS], using indices of type
7550 sizetype. */
7551
7552 tree
7553 build_array_type_nelts (tree elt_type, unsigned HOST_WIDE_INT nelts)
7554 {
7555 return build_array_type (elt_type, build_index_type (size_int (nelts - 1)));
7556 }
7557
7558 /* Recursively examines the array elements of TYPE, until a non-array
7559 element type is found. */
7560
7561 tree
7562 strip_array_types (tree type)
7563 {
7564 while (TREE_CODE (type) == ARRAY_TYPE)
7565 type = TREE_TYPE (type);
7566
7567 return type;
7568 }
7569
7570 /* Computes the canonical argument types from the argument type list
7571 ARGTYPES.
7572
7573 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
7574 on entry to this function, or if any of the ARGTYPES are
7575 structural.
7576
7577 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
7578 true on entry to this function, or if any of the ARGTYPES are
7579 non-canonical.
7580
7581 Returns a canonical argument list, which may be ARGTYPES when the
7582 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
7583 true) or would not differ from ARGTYPES. */
7584
7585 static tree
7586 maybe_canonicalize_argtypes(tree argtypes,
7587 bool *any_structural_p,
7588 bool *any_noncanonical_p)
7589 {
7590 tree arg;
7591 bool any_noncanonical_argtypes_p = false;
7592
7593 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
7594 {
7595 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
7596 /* Fail gracefully by stating that the type is structural. */
7597 *any_structural_p = true;
7598 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
7599 *any_structural_p = true;
7600 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
7601 || TREE_PURPOSE (arg))
7602 /* If the argument has a default argument, we consider it
7603 non-canonical even though the type itself is canonical.
7604 That way, different variants of function and method types
7605 with default arguments will all point to the variant with
7606 no defaults as their canonical type. */
7607 any_noncanonical_argtypes_p = true;
7608 }
7609
7610 if (*any_structural_p)
7611 return argtypes;
7612
7613 if (any_noncanonical_argtypes_p)
7614 {
7615 /* Build the canonical list of argument types. */
7616 tree canon_argtypes = NULL_TREE;
7617 bool is_void = false;
7618
7619 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
7620 {
7621 if (arg == void_list_node)
7622 is_void = true;
7623 else
7624 canon_argtypes = tree_cons (NULL_TREE,
7625 TYPE_CANONICAL (TREE_VALUE (arg)),
7626 canon_argtypes);
7627 }
7628
7629 canon_argtypes = nreverse (canon_argtypes);
7630 if (is_void)
7631 canon_argtypes = chainon (canon_argtypes, void_list_node);
7632
7633 /* There is a non-canonical type. */
7634 *any_noncanonical_p = true;
7635 return canon_argtypes;
7636 }
7637
7638 /* The canonical argument types are the same as ARGTYPES. */
7639 return argtypes;
7640 }
7641
7642 /* Construct, lay out and return
7643 the type of functions returning type VALUE_TYPE
7644 given arguments of types ARG_TYPES.
7645 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
7646 are data type nodes for the arguments of the function.
7647 If such a type has already been constructed, reuse it. */
7648
7649 tree
7650 build_function_type (tree value_type, tree arg_types)
7651 {
7652 tree t;
7653 hashval_t hashcode = 0;
7654 bool any_structural_p, any_noncanonical_p;
7655 tree canon_argtypes;
7656
7657 if (TREE_CODE (value_type) == FUNCTION_TYPE)
7658 {
7659 error ("function return type cannot be function");
7660 value_type = integer_type_node;
7661 }
7662
7663 /* Make a node of the sort we want. */
7664 t = make_node (FUNCTION_TYPE);
7665 TREE_TYPE (t) = value_type;
7666 TYPE_ARG_TYPES (t) = arg_types;
7667
7668 /* If we already have such a type, use the old one. */
7669 hashcode = iterative_hash_object (TYPE_HASH (value_type), hashcode);
7670 hashcode = type_hash_list (arg_types, hashcode);
7671 t = type_hash_canon (hashcode, t);
7672
7673 /* Set up the canonical type. */
7674 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
7675 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
7676 canon_argtypes = maybe_canonicalize_argtypes (arg_types,
7677 &any_structural_p,
7678 &any_noncanonical_p);
7679 if (any_structural_p)
7680 SET_TYPE_STRUCTURAL_EQUALITY (t);
7681 else if (any_noncanonical_p)
7682 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
7683 canon_argtypes);
7684
7685 if (!COMPLETE_TYPE_P (t))
7686 layout_type (t);
7687 return t;
7688 }
7689
7690 /* Build variant of function type ORIG_TYPE skipping ARGS_TO_SKIP and the
7691 return value if SKIP_RETURN is true. */
7692
7693 static tree
7694 build_function_type_skip_args (tree orig_type, bitmap args_to_skip,
7695 bool skip_return)
7696 {
7697 tree new_type = NULL;
7698 tree args, new_args = NULL, t;
7699 tree new_reversed;
7700 int i = 0;
7701
7702 for (args = TYPE_ARG_TYPES (orig_type); args && args != void_list_node;
7703 args = TREE_CHAIN (args), i++)
7704 if (!args_to_skip || !bitmap_bit_p (args_to_skip, i))
7705 new_args = tree_cons (NULL_TREE, TREE_VALUE (args), new_args);
7706
7707 new_reversed = nreverse (new_args);
7708 if (args)
7709 {
7710 if (new_reversed)
7711 TREE_CHAIN (new_args) = void_list_node;
7712 else
7713 new_reversed = void_list_node;
7714 }
7715
7716 /* Use copy_node to preserve as much as possible from original type
7717 (debug info, attribute lists etc.)
7718 Exception is METHOD_TYPEs must have THIS argument.
7719 When we are asked to remove it, we need to build new FUNCTION_TYPE
7720 instead. */
7721 if (TREE_CODE (orig_type) != METHOD_TYPE
7722 || !args_to_skip
7723 || !bitmap_bit_p (args_to_skip, 0))
7724 {
7725 new_type = build_distinct_type_copy (orig_type);
7726 TYPE_ARG_TYPES (new_type) = new_reversed;
7727 }
7728 else
7729 {
7730 new_type
7731 = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type),
7732 new_reversed));
7733 TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
7734 }
7735
7736 if (skip_return)
7737 TREE_TYPE (new_type) = void_type_node;
7738
7739 /* This is a new type, not a copy of an old type. Need to reassociate
7740 variants. We can handle everything except the main variant lazily. */
7741 t = TYPE_MAIN_VARIANT (orig_type);
7742 if (t != orig_type)
7743 {
7744 t = build_function_type_skip_args (t, args_to_skip, skip_return);
7745 TYPE_MAIN_VARIANT (new_type) = t;
7746 TYPE_NEXT_VARIANT (new_type) = TYPE_NEXT_VARIANT (t);
7747 TYPE_NEXT_VARIANT (t) = new_type;
7748 }
7749 else
7750 {
7751 TYPE_MAIN_VARIANT (new_type) = new_type;
7752 TYPE_NEXT_VARIANT (new_type) = NULL;
7753 }
7754
7755 return new_type;
7756 }
7757
7758 /* Build variant of function decl ORIG_DECL skipping ARGS_TO_SKIP and the
7759 return value if SKIP_RETURN is true.
7760
7761 Arguments from DECL_ARGUMENTS list can't be removed now, since they are
7762 linked by TREE_CHAIN directly. The caller is responsible for eliminating
7763 them when they are being duplicated (i.e. copy_arguments_for_versioning). */
7764
7765 tree
7766 build_function_decl_skip_args (tree orig_decl, bitmap args_to_skip,
7767 bool skip_return)
7768 {
7769 tree new_decl = copy_node (orig_decl);
7770 tree new_type;
7771
7772 new_type = TREE_TYPE (orig_decl);
7773 if (prototype_p (new_type)
7774 || (skip_return && !VOID_TYPE_P (TREE_TYPE (new_type))))
7775 new_type
7776 = build_function_type_skip_args (new_type, args_to_skip, skip_return);
7777 TREE_TYPE (new_decl) = new_type;
7778
7779 /* For declarations setting DECL_VINDEX (i.e. methods)
7780 we expect first argument to be THIS pointer. */
7781 if (args_to_skip && bitmap_bit_p (args_to_skip, 0))
7782 DECL_VINDEX (new_decl) = NULL_TREE;
7783
7784 /* When signature changes, we need to clear builtin info. */
7785 if (DECL_BUILT_IN (new_decl)
7786 && args_to_skip
7787 && !bitmap_empty_p (args_to_skip))
7788 {
7789 DECL_BUILT_IN_CLASS (new_decl) = NOT_BUILT_IN;
7790 DECL_FUNCTION_CODE (new_decl) = (enum built_in_function) 0;
7791 }
7792 return new_decl;
7793 }
7794
7795 /* Build a function type. The RETURN_TYPE is the type returned by the
7796 function. If VAARGS is set, no void_type_node is appended to the
7797 the list. ARGP must be always be terminated be a NULL_TREE. */
7798
7799 static tree
7800 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
7801 {
7802 tree t, args, last;
7803
7804 t = va_arg (argp, tree);
7805 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
7806 args = tree_cons (NULL_TREE, t, args);
7807
7808 if (vaargs)
7809 {
7810 last = args;
7811 if (args != NULL_TREE)
7812 args = nreverse (args);
7813 gcc_assert (last != void_list_node);
7814 }
7815 else if (args == NULL_TREE)
7816 args = void_list_node;
7817 else
7818 {
7819 last = args;
7820 args = nreverse (args);
7821 TREE_CHAIN (last) = void_list_node;
7822 }
7823 args = build_function_type (return_type, args);
7824
7825 return args;
7826 }
7827
7828 /* Build a function type. The RETURN_TYPE is the type returned by the
7829 function. If additional arguments are provided, they are
7830 additional argument types. The list of argument types must always
7831 be terminated by NULL_TREE. */
7832
7833 tree
7834 build_function_type_list (tree return_type, ...)
7835 {
7836 tree args;
7837 va_list p;
7838
7839 va_start (p, return_type);
7840 args = build_function_type_list_1 (false, return_type, p);
7841 va_end (p);
7842 return args;
7843 }
7844
7845 /* Build a variable argument function type. The RETURN_TYPE is the
7846 type returned by the function. If additional arguments are provided,
7847 they are additional argument types. The list of argument types must
7848 always be terminated by NULL_TREE. */
7849
7850 tree
7851 build_varargs_function_type_list (tree return_type, ...)
7852 {
7853 tree args;
7854 va_list p;
7855
7856 va_start (p, return_type);
7857 args = build_function_type_list_1 (true, return_type, p);
7858 va_end (p);
7859
7860 return args;
7861 }
7862
7863 /* Build a function type. RETURN_TYPE is the type returned by the
7864 function; VAARGS indicates whether the function takes varargs. The
7865 function takes N named arguments, the types of which are provided in
7866 ARG_TYPES. */
7867
7868 static tree
7869 build_function_type_array_1 (bool vaargs, tree return_type, int n,
7870 tree *arg_types)
7871 {
7872 int i;
7873 tree t = vaargs ? NULL_TREE : void_list_node;
7874
7875 for (i = n - 1; i >= 0; i--)
7876 t = tree_cons (NULL_TREE, arg_types[i], t);
7877
7878 return build_function_type (return_type, t);
7879 }
7880
7881 /* Build a function type. RETURN_TYPE is the type returned by the
7882 function. The function takes N named arguments, the types of which
7883 are provided in ARG_TYPES. */
7884
7885 tree
7886 build_function_type_array (tree return_type, int n, tree *arg_types)
7887 {
7888 return build_function_type_array_1 (false, return_type, n, arg_types);
7889 }
7890
7891 /* Build a variable argument function type. RETURN_TYPE is the type
7892 returned by the function. The function takes N named arguments, the
7893 types of which are provided in ARG_TYPES. */
7894
7895 tree
7896 build_varargs_function_type_array (tree return_type, int n, tree *arg_types)
7897 {
7898 return build_function_type_array_1 (true, return_type, n, arg_types);
7899 }
7900
7901 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
7902 and ARGTYPES (a TREE_LIST) are the return type and arguments types
7903 for the method. An implicit additional parameter (of type
7904 pointer-to-BASETYPE) is added to the ARGTYPES. */
7905
7906 tree
7907 build_method_type_directly (tree basetype,
7908 tree rettype,
7909 tree argtypes)
7910 {
7911 tree t;
7912 tree ptype;
7913 int hashcode = 0;
7914 bool any_structural_p, any_noncanonical_p;
7915 tree canon_argtypes;
7916
7917 /* Make a node of the sort we want. */
7918 t = make_node (METHOD_TYPE);
7919
7920 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
7921 TREE_TYPE (t) = rettype;
7922 ptype = build_pointer_type (basetype);
7923
7924 /* The actual arglist for this function includes a "hidden" argument
7925 which is "this". Put it into the list of argument types. */
7926 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
7927 TYPE_ARG_TYPES (t) = argtypes;
7928
7929 /* If we already have such a type, use the old one. */
7930 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
7931 hashcode = iterative_hash_object (TYPE_HASH (rettype), hashcode);
7932 hashcode = type_hash_list (argtypes, hashcode);
7933 t = type_hash_canon (hashcode, t);
7934
7935 /* Set up the canonical type. */
7936 any_structural_p
7937 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
7938 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
7939 any_noncanonical_p
7940 = (TYPE_CANONICAL (basetype) != basetype
7941 || TYPE_CANONICAL (rettype) != rettype);
7942 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
7943 &any_structural_p,
7944 &any_noncanonical_p);
7945 if (any_structural_p)
7946 SET_TYPE_STRUCTURAL_EQUALITY (t);
7947 else if (any_noncanonical_p)
7948 TYPE_CANONICAL (t)
7949 = build_method_type_directly (TYPE_CANONICAL (basetype),
7950 TYPE_CANONICAL (rettype),
7951 canon_argtypes);
7952 if (!COMPLETE_TYPE_P (t))
7953 layout_type (t);
7954
7955 return t;
7956 }
7957
7958 /* Construct, lay out and return the type of methods belonging to class
7959 BASETYPE and whose arguments and values are described by TYPE.
7960 If that type exists already, reuse it.
7961 TYPE must be a FUNCTION_TYPE node. */
7962
7963 tree
7964 build_method_type (tree basetype, tree type)
7965 {
7966 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
7967
7968 return build_method_type_directly (basetype,
7969 TREE_TYPE (type),
7970 TYPE_ARG_TYPES (type));
7971 }
7972
7973 /* Construct, lay out and return the type of offsets to a value
7974 of type TYPE, within an object of type BASETYPE.
7975 If a suitable offset type exists already, reuse it. */
7976
7977 tree
7978 build_offset_type (tree basetype, tree type)
7979 {
7980 tree t;
7981 hashval_t hashcode = 0;
7982
7983 /* Make a node of the sort we want. */
7984 t = make_node (OFFSET_TYPE);
7985
7986 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
7987 TREE_TYPE (t) = type;
7988
7989 /* If we already have such a type, use the old one. */
7990 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
7991 hashcode = iterative_hash_object (TYPE_HASH (type), hashcode);
7992 t = type_hash_canon (hashcode, t);
7993
7994 if (!COMPLETE_TYPE_P (t))
7995 layout_type (t);
7996
7997 if (TYPE_CANONICAL (t) == t)
7998 {
7999 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8000 || TYPE_STRUCTURAL_EQUALITY_P (type))
8001 SET_TYPE_STRUCTURAL_EQUALITY (t);
8002 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
8003 || TYPE_CANONICAL (type) != type)
8004 TYPE_CANONICAL (t)
8005 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
8006 TYPE_CANONICAL (type));
8007 }
8008
8009 return t;
8010 }
8011
8012 /* Create a complex type whose components are COMPONENT_TYPE. */
8013
8014 tree
8015 build_complex_type (tree component_type)
8016 {
8017 tree t;
8018 hashval_t hashcode;
8019
8020 gcc_assert (INTEGRAL_TYPE_P (component_type)
8021 || SCALAR_FLOAT_TYPE_P (component_type)
8022 || FIXED_POINT_TYPE_P (component_type));
8023
8024 /* Make a node of the sort we want. */
8025 t = make_node (COMPLEX_TYPE);
8026
8027 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
8028
8029 /* If we already have such a type, use the old one. */
8030 hashcode = iterative_hash_object (TYPE_HASH (component_type), 0);
8031 t = type_hash_canon (hashcode, t);
8032
8033 if (!COMPLETE_TYPE_P (t))
8034 layout_type (t);
8035
8036 if (TYPE_CANONICAL (t) == t)
8037 {
8038 if (TYPE_STRUCTURAL_EQUALITY_P (component_type))
8039 SET_TYPE_STRUCTURAL_EQUALITY (t);
8040 else if (TYPE_CANONICAL (component_type) != component_type)
8041 TYPE_CANONICAL (t)
8042 = build_complex_type (TYPE_CANONICAL (component_type));
8043 }
8044
8045 /* We need to create a name, since complex is a fundamental type. */
8046 if (! TYPE_NAME (t))
8047 {
8048 const char *name;
8049 if (component_type == char_type_node)
8050 name = "complex char";
8051 else if (component_type == signed_char_type_node)
8052 name = "complex signed char";
8053 else if (component_type == unsigned_char_type_node)
8054 name = "complex unsigned char";
8055 else if (component_type == short_integer_type_node)
8056 name = "complex short int";
8057 else if (component_type == short_unsigned_type_node)
8058 name = "complex short unsigned int";
8059 else if (component_type == integer_type_node)
8060 name = "complex int";
8061 else if (component_type == unsigned_type_node)
8062 name = "complex unsigned int";
8063 else if (component_type == long_integer_type_node)
8064 name = "complex long int";
8065 else if (component_type == long_unsigned_type_node)
8066 name = "complex long unsigned int";
8067 else if (component_type == long_long_integer_type_node)
8068 name = "complex long long int";
8069 else if (component_type == long_long_unsigned_type_node)
8070 name = "complex long long unsigned int";
8071 else
8072 name = 0;
8073
8074 if (name != 0)
8075 TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
8076 get_identifier (name), t);
8077 }
8078
8079 return build_qualified_type (t, TYPE_QUALS (component_type));
8080 }
8081
8082 /* If TYPE is a real or complex floating-point type and the target
8083 does not directly support arithmetic on TYPE then return the wider
8084 type to be used for arithmetic on TYPE. Otherwise, return
8085 NULL_TREE. */
8086
8087 tree
8088 excess_precision_type (tree type)
8089 {
8090 if (flag_excess_precision != EXCESS_PRECISION_FAST)
8091 {
8092 int flt_eval_method = TARGET_FLT_EVAL_METHOD;
8093 switch (TREE_CODE (type))
8094 {
8095 case REAL_TYPE:
8096 switch (flt_eval_method)
8097 {
8098 case 1:
8099 if (TYPE_MODE (type) == TYPE_MODE (float_type_node))
8100 return double_type_node;
8101 break;
8102 case 2:
8103 if (TYPE_MODE (type) == TYPE_MODE (float_type_node)
8104 || TYPE_MODE (type) == TYPE_MODE (double_type_node))
8105 return long_double_type_node;
8106 break;
8107 default:
8108 gcc_unreachable ();
8109 }
8110 break;
8111 case COMPLEX_TYPE:
8112 if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
8113 return NULL_TREE;
8114 switch (flt_eval_method)
8115 {
8116 case 1:
8117 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node))
8118 return complex_double_type_node;
8119 break;
8120 case 2:
8121 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node)
8122 || (TYPE_MODE (TREE_TYPE (type))
8123 == TYPE_MODE (double_type_node)))
8124 return complex_long_double_type_node;
8125 break;
8126 default:
8127 gcc_unreachable ();
8128 }
8129 break;
8130 default:
8131 break;
8132 }
8133 }
8134 return NULL_TREE;
8135 }
8136 \f
8137 /* Return OP, stripped of any conversions to wider types as much as is safe.
8138 Converting the value back to OP's type makes a value equivalent to OP.
8139
8140 If FOR_TYPE is nonzero, we return a value which, if converted to
8141 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
8142
8143 OP must have integer, real or enumeral type. Pointers are not allowed!
8144
8145 There are some cases where the obvious value we could return
8146 would regenerate to OP if converted to OP's type,
8147 but would not extend like OP to wider types.
8148 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
8149 For example, if OP is (unsigned short)(signed char)-1,
8150 we avoid returning (signed char)-1 if FOR_TYPE is int,
8151 even though extending that to an unsigned short would regenerate OP,
8152 since the result of extending (signed char)-1 to (int)
8153 is different from (int) OP. */
8154
8155 tree
8156 get_unwidened (tree op, tree for_type)
8157 {
8158 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
8159 tree type = TREE_TYPE (op);
8160 unsigned final_prec
8161 = TYPE_PRECISION (for_type != 0 ? for_type : type);
8162 int uns
8163 = (for_type != 0 && for_type != type
8164 && final_prec > TYPE_PRECISION (type)
8165 && TYPE_UNSIGNED (type));
8166 tree win = op;
8167
8168 while (CONVERT_EXPR_P (op))
8169 {
8170 int bitschange;
8171
8172 /* TYPE_PRECISION on vector types has different meaning
8173 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
8174 so avoid them here. */
8175 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
8176 break;
8177
8178 bitschange = TYPE_PRECISION (TREE_TYPE (op))
8179 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
8180
8181 /* Truncations are many-one so cannot be removed.
8182 Unless we are later going to truncate down even farther. */
8183 if (bitschange < 0
8184 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
8185 break;
8186
8187 /* See what's inside this conversion. If we decide to strip it,
8188 we will set WIN. */
8189 op = TREE_OPERAND (op, 0);
8190
8191 /* If we have not stripped any zero-extensions (uns is 0),
8192 we can strip any kind of extension.
8193 If we have previously stripped a zero-extension,
8194 only zero-extensions can safely be stripped.
8195 Any extension can be stripped if the bits it would produce
8196 are all going to be discarded later by truncating to FOR_TYPE. */
8197
8198 if (bitschange > 0)
8199 {
8200 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
8201 win = op;
8202 /* TYPE_UNSIGNED says whether this is a zero-extension.
8203 Let's avoid computing it if it does not affect WIN
8204 and if UNS will not be needed again. */
8205 if ((uns
8206 || CONVERT_EXPR_P (op))
8207 && TYPE_UNSIGNED (TREE_TYPE (op)))
8208 {
8209 uns = 1;
8210 win = op;
8211 }
8212 }
8213 }
8214
8215 /* If we finally reach a constant see if it fits in for_type and
8216 in that case convert it. */
8217 if (for_type
8218 && TREE_CODE (win) == INTEGER_CST
8219 && TREE_TYPE (win) != for_type
8220 && int_fits_type_p (win, for_type))
8221 win = fold_convert (for_type, win);
8222
8223 return win;
8224 }
8225 \f
8226 /* Return OP or a simpler expression for a narrower value
8227 which can be sign-extended or zero-extended to give back OP.
8228 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
8229 or 0 if the value should be sign-extended. */
8230
8231 tree
8232 get_narrower (tree op, int *unsignedp_ptr)
8233 {
8234 int uns = 0;
8235 int first = 1;
8236 tree win = op;
8237 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
8238
8239 while (TREE_CODE (op) == NOP_EXPR)
8240 {
8241 int bitschange
8242 = (TYPE_PRECISION (TREE_TYPE (op))
8243 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
8244
8245 /* Truncations are many-one so cannot be removed. */
8246 if (bitschange < 0)
8247 break;
8248
8249 /* See what's inside this conversion. If we decide to strip it,
8250 we will set WIN. */
8251
8252 if (bitschange > 0)
8253 {
8254 op = TREE_OPERAND (op, 0);
8255 /* An extension: the outermost one can be stripped,
8256 but remember whether it is zero or sign extension. */
8257 if (first)
8258 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8259 /* Otherwise, if a sign extension has been stripped,
8260 only sign extensions can now be stripped;
8261 if a zero extension has been stripped, only zero-extensions. */
8262 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
8263 break;
8264 first = 0;
8265 }
8266 else /* bitschange == 0 */
8267 {
8268 /* A change in nominal type can always be stripped, but we must
8269 preserve the unsignedness. */
8270 if (first)
8271 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8272 first = 0;
8273 op = TREE_OPERAND (op, 0);
8274 /* Keep trying to narrow, but don't assign op to win if it
8275 would turn an integral type into something else. */
8276 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
8277 continue;
8278 }
8279
8280 win = op;
8281 }
8282
8283 if (TREE_CODE (op) == COMPONENT_REF
8284 /* Since type_for_size always gives an integer type. */
8285 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
8286 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
8287 /* Ensure field is laid out already. */
8288 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
8289 && host_integerp (DECL_SIZE (TREE_OPERAND (op, 1)), 1))
8290 {
8291 unsigned HOST_WIDE_INT innerprec
8292 = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
8293 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
8294 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
8295 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
8296
8297 /* We can get this structure field in a narrower type that fits it,
8298 but the resulting extension to its nominal type (a fullword type)
8299 must satisfy the same conditions as for other extensions.
8300
8301 Do this only for fields that are aligned (not bit-fields),
8302 because when bit-field insns will be used there is no
8303 advantage in doing this. */
8304
8305 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
8306 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
8307 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
8308 && type != 0)
8309 {
8310 if (first)
8311 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
8312 win = fold_convert (type, op);
8313 }
8314 }
8315
8316 *unsignedp_ptr = uns;
8317 return win;
8318 }
8319 \f
8320 /* Returns true if integer constant C has a value that is permissible
8321 for type TYPE (an INTEGER_TYPE). */
8322
8323 bool
8324 int_fits_type_p (const_tree c, const_tree type)
8325 {
8326 tree type_low_bound, type_high_bound;
8327 bool ok_for_low_bound, ok_for_high_bound, unsc;
8328 double_int dc, dd;
8329
8330 dc = tree_to_double_int (c);
8331 unsc = TYPE_UNSIGNED (TREE_TYPE (c));
8332
8333 retry:
8334 type_low_bound = TYPE_MIN_VALUE (type);
8335 type_high_bound = TYPE_MAX_VALUE (type);
8336
8337 /* If at least one bound of the type is a constant integer, we can check
8338 ourselves and maybe make a decision. If no such decision is possible, but
8339 this type is a subtype, try checking against that. Otherwise, use
8340 double_int_fits_to_tree_p, which checks against the precision.
8341
8342 Compute the status for each possibly constant bound, and return if we see
8343 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
8344 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
8345 for "constant known to fit". */
8346
8347 /* Check if c >= type_low_bound. */
8348 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
8349 {
8350 dd = tree_to_double_int (type_low_bound);
8351 if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_low_bound)))
8352 {
8353 int c_neg = (!unsc && dc.is_negative ());
8354 int t_neg = (unsc && dd.is_negative ());
8355
8356 if (c_neg && !t_neg)
8357 return false;
8358 if ((c_neg || !t_neg) && dc.ult (dd))
8359 return false;
8360 }
8361 else if (dc.cmp (dd, unsc) < 0)
8362 return false;
8363 ok_for_low_bound = true;
8364 }
8365 else
8366 ok_for_low_bound = false;
8367
8368 /* Check if c <= type_high_bound. */
8369 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
8370 {
8371 dd = tree_to_double_int (type_high_bound);
8372 if (unsc != TYPE_UNSIGNED (TREE_TYPE (type_high_bound)))
8373 {
8374 int c_neg = (!unsc && dc.is_negative ());
8375 int t_neg = (unsc && dd.is_negative ());
8376
8377 if (t_neg && !c_neg)
8378 return false;
8379 if ((t_neg || !c_neg) && dc.ugt (dd))
8380 return false;
8381 }
8382 else if (dc.cmp (dd, unsc) > 0)
8383 return false;
8384 ok_for_high_bound = true;
8385 }
8386 else
8387 ok_for_high_bound = false;
8388
8389 /* If the constant fits both bounds, the result is known. */
8390 if (ok_for_low_bound && ok_for_high_bound)
8391 return true;
8392
8393 /* Perform some generic filtering which may allow making a decision
8394 even if the bounds are not constant. First, negative integers
8395 never fit in unsigned types, */
8396 if (TYPE_UNSIGNED (type) && !unsc && dc.is_negative ())
8397 return false;
8398
8399 /* Second, narrower types always fit in wider ones. */
8400 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
8401 return true;
8402
8403 /* Third, unsigned integers with top bit set never fit signed types. */
8404 if (! TYPE_UNSIGNED (type) && unsc)
8405 {
8406 int prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (c))) - 1;
8407 if (prec < HOST_BITS_PER_WIDE_INT)
8408 {
8409 if (((((unsigned HOST_WIDE_INT) 1) << prec) & dc.low) != 0)
8410 return false;
8411 }
8412 else if (((((unsigned HOST_WIDE_INT) 1)
8413 << (prec - HOST_BITS_PER_WIDE_INT)) & dc.high) != 0)
8414 return false;
8415 }
8416
8417 /* If we haven't been able to decide at this point, there nothing more we
8418 can check ourselves here. Look at the base type if we have one and it
8419 has the same precision. */
8420 if (TREE_CODE (type) == INTEGER_TYPE
8421 && TREE_TYPE (type) != 0
8422 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
8423 {
8424 type = TREE_TYPE (type);
8425 goto retry;
8426 }
8427
8428 /* Or to double_int_fits_to_tree_p, if nothing else. */
8429 return double_int_fits_to_tree_p (type, dc);
8430 }
8431
8432 /* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
8433 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
8434 represented (assuming two's-complement arithmetic) within the bit
8435 precision of the type are returned instead. */
8436
8437 void
8438 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
8439 {
8440 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
8441 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
8442 mpz_set_double_int (min, tree_to_double_int (TYPE_MIN_VALUE (type)),
8443 TYPE_UNSIGNED (type));
8444 else
8445 {
8446 if (TYPE_UNSIGNED (type))
8447 mpz_set_ui (min, 0);
8448 else
8449 {
8450 double_int mn;
8451 mn = double_int::mask (TYPE_PRECISION (type) - 1);
8452 mn = (mn + double_int_one).sext (TYPE_PRECISION (type));
8453 mpz_set_double_int (min, mn, false);
8454 }
8455 }
8456
8457 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
8458 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
8459 mpz_set_double_int (max, tree_to_double_int (TYPE_MAX_VALUE (type)),
8460 TYPE_UNSIGNED (type));
8461 else
8462 {
8463 if (TYPE_UNSIGNED (type))
8464 mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type)),
8465 true);
8466 else
8467 mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type) - 1),
8468 true);
8469 }
8470 }
8471
8472 /* Return true if VAR is an automatic variable defined in function FN. */
8473
8474 bool
8475 auto_var_in_fn_p (const_tree var, const_tree fn)
8476 {
8477 return (DECL_P (var) && DECL_CONTEXT (var) == fn
8478 && ((((TREE_CODE (var) == VAR_DECL && ! DECL_EXTERNAL (var))
8479 || TREE_CODE (var) == PARM_DECL)
8480 && ! TREE_STATIC (var))
8481 || TREE_CODE (var) == LABEL_DECL
8482 || TREE_CODE (var) == RESULT_DECL));
8483 }
8484
8485 /* Subprogram of following function. Called by walk_tree.
8486
8487 Return *TP if it is an automatic variable or parameter of the
8488 function passed in as DATA. */
8489
8490 static tree
8491 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
8492 {
8493 tree fn = (tree) data;
8494
8495 if (TYPE_P (*tp))
8496 *walk_subtrees = 0;
8497
8498 else if (DECL_P (*tp)
8499 && auto_var_in_fn_p (*tp, fn))
8500 return *tp;
8501
8502 return NULL_TREE;
8503 }
8504
8505 /* Returns true if T is, contains, or refers to a type with variable
8506 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
8507 arguments, but not the return type. If FN is nonzero, only return
8508 true if a modifier of the type or position of FN is a variable or
8509 parameter inside FN.
8510
8511 This concept is more general than that of C99 'variably modified types':
8512 in C99, a struct type is never variably modified because a VLA may not
8513 appear as a structure member. However, in GNU C code like:
8514
8515 struct S { int i[f()]; };
8516
8517 is valid, and other languages may define similar constructs. */
8518
8519 bool
8520 variably_modified_type_p (tree type, tree fn)
8521 {
8522 tree t;
8523
8524 /* Test if T is either variable (if FN is zero) or an expression containing
8525 a variable in FN. If TYPE isn't gimplified, return true also if
8526 gimplify_one_sizepos would gimplify the expression into a local
8527 variable. */
8528 #define RETURN_TRUE_IF_VAR(T) \
8529 do { tree _t = (T); \
8530 if (_t != NULL_TREE \
8531 && _t != error_mark_node \
8532 && TREE_CODE (_t) != INTEGER_CST \
8533 && TREE_CODE (_t) != PLACEHOLDER_EXPR \
8534 && (!fn \
8535 || (!TYPE_SIZES_GIMPLIFIED (type) \
8536 && !is_gimple_sizepos (_t)) \
8537 || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
8538 return true; } while (0)
8539
8540 if (type == error_mark_node)
8541 return false;
8542
8543 /* If TYPE itself has variable size, it is variably modified. */
8544 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
8545 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
8546
8547 switch (TREE_CODE (type))
8548 {
8549 case POINTER_TYPE:
8550 case REFERENCE_TYPE:
8551 case VECTOR_TYPE:
8552 if (variably_modified_type_p (TREE_TYPE (type), fn))
8553 return true;
8554 break;
8555
8556 case FUNCTION_TYPE:
8557 case METHOD_TYPE:
8558 /* If TYPE is a function type, it is variably modified if the
8559 return type is variably modified. */
8560 if (variably_modified_type_p (TREE_TYPE (type), fn))
8561 return true;
8562 break;
8563
8564 case INTEGER_TYPE:
8565 case REAL_TYPE:
8566 case FIXED_POINT_TYPE:
8567 case ENUMERAL_TYPE:
8568 case BOOLEAN_TYPE:
8569 /* Scalar types are variably modified if their end points
8570 aren't constant. */
8571 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
8572 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
8573 break;
8574
8575 case RECORD_TYPE:
8576 case UNION_TYPE:
8577 case QUAL_UNION_TYPE:
8578 /* We can't see if any of the fields are variably-modified by the
8579 definition we normally use, since that would produce infinite
8580 recursion via pointers. */
8581 /* This is variably modified if some field's type is. */
8582 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
8583 if (TREE_CODE (t) == FIELD_DECL)
8584 {
8585 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
8586 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
8587 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
8588
8589 if (TREE_CODE (type) == QUAL_UNION_TYPE)
8590 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
8591 }
8592 break;
8593
8594 case ARRAY_TYPE:
8595 /* Do not call ourselves to avoid infinite recursion. This is
8596 variably modified if the element type is. */
8597 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
8598 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
8599 break;
8600
8601 default:
8602 break;
8603 }
8604
8605 /* The current language may have other cases to check, but in general,
8606 all other types are not variably modified. */
8607 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
8608
8609 #undef RETURN_TRUE_IF_VAR
8610 }
8611
8612 /* Given a DECL or TYPE, return the scope in which it was declared, or
8613 NULL_TREE if there is no containing scope. */
8614
8615 tree
8616 get_containing_scope (const_tree t)
8617 {
8618 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
8619 }
8620
8621 /* Return the innermost context enclosing DECL that is
8622 a FUNCTION_DECL, or zero if none. */
8623
8624 tree
8625 decl_function_context (const_tree decl)
8626 {
8627 tree context;
8628
8629 if (TREE_CODE (decl) == ERROR_MARK)
8630 return 0;
8631
8632 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
8633 where we look up the function at runtime. Such functions always take
8634 a first argument of type 'pointer to real context'.
8635
8636 C++ should really be fixed to use DECL_CONTEXT for the real context,
8637 and use something else for the "virtual context". */
8638 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
8639 context
8640 = TYPE_MAIN_VARIANT
8641 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
8642 else
8643 context = DECL_CONTEXT (decl);
8644
8645 while (context && TREE_CODE (context) != FUNCTION_DECL)
8646 {
8647 if (TREE_CODE (context) == BLOCK)
8648 context = BLOCK_SUPERCONTEXT (context);
8649 else
8650 context = get_containing_scope (context);
8651 }
8652
8653 return context;
8654 }
8655
8656 /* Return the innermost context enclosing DECL that is
8657 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
8658 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
8659
8660 tree
8661 decl_type_context (const_tree decl)
8662 {
8663 tree context = DECL_CONTEXT (decl);
8664
8665 while (context)
8666 switch (TREE_CODE (context))
8667 {
8668 case NAMESPACE_DECL:
8669 case TRANSLATION_UNIT_DECL:
8670 return NULL_TREE;
8671
8672 case RECORD_TYPE:
8673 case UNION_TYPE:
8674 case QUAL_UNION_TYPE:
8675 return context;
8676
8677 case TYPE_DECL:
8678 case FUNCTION_DECL:
8679 context = DECL_CONTEXT (context);
8680 break;
8681
8682 case BLOCK:
8683 context = BLOCK_SUPERCONTEXT (context);
8684 break;
8685
8686 default:
8687 gcc_unreachable ();
8688 }
8689
8690 return NULL_TREE;
8691 }
8692
8693 /* CALL is a CALL_EXPR. Return the declaration for the function
8694 called, or NULL_TREE if the called function cannot be
8695 determined. */
8696
8697 tree
8698 get_callee_fndecl (const_tree call)
8699 {
8700 tree addr;
8701
8702 if (call == error_mark_node)
8703 return error_mark_node;
8704
8705 /* It's invalid to call this function with anything but a
8706 CALL_EXPR. */
8707 gcc_assert (TREE_CODE (call) == CALL_EXPR);
8708
8709 /* The first operand to the CALL is the address of the function
8710 called. */
8711 addr = CALL_EXPR_FN (call);
8712
8713 STRIP_NOPS (addr);
8714
8715 /* If this is a readonly function pointer, extract its initial value. */
8716 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
8717 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
8718 && DECL_INITIAL (addr))
8719 addr = DECL_INITIAL (addr);
8720
8721 /* If the address is just `&f' for some function `f', then we know
8722 that `f' is being called. */
8723 if (TREE_CODE (addr) == ADDR_EXPR
8724 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
8725 return TREE_OPERAND (addr, 0);
8726
8727 /* We couldn't figure out what was being called. */
8728 return NULL_TREE;
8729 }
8730
8731 /* Print debugging information about tree nodes generated during the compile,
8732 and any language-specific information. */
8733
8734 void
8735 dump_tree_statistics (void)
8736 {
8737 if (GATHER_STATISTICS)
8738 {
8739 int i;
8740 int total_nodes, total_bytes;
8741 fprintf (stderr, "Kind Nodes Bytes\n");
8742 fprintf (stderr, "---------------------------------------\n");
8743 total_nodes = total_bytes = 0;
8744 for (i = 0; i < (int) all_kinds; i++)
8745 {
8746 fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
8747 tree_node_counts[i], tree_node_sizes[i]);
8748 total_nodes += tree_node_counts[i];
8749 total_bytes += tree_node_sizes[i];
8750 }
8751 fprintf (stderr, "---------------------------------------\n");
8752 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
8753 fprintf (stderr, "---------------------------------------\n");
8754 fprintf (stderr, "Code Nodes\n");
8755 fprintf (stderr, "----------------------------\n");
8756 for (i = 0; i < (int) MAX_TREE_CODES; i++)
8757 fprintf (stderr, "%-20s %7d\n", tree_code_name[i], tree_code_counts[i]);
8758 fprintf (stderr, "----------------------------\n");
8759 ssanames_print_statistics ();
8760 phinodes_print_statistics ();
8761 }
8762 else
8763 fprintf (stderr, "(No per-node statistics)\n");
8764
8765 print_type_hash_statistics ();
8766 print_debug_expr_statistics ();
8767 print_value_expr_statistics ();
8768 lang_hooks.print_statistics ();
8769 }
8770 \f
8771 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
8772
8773 /* Generate a crc32 of a byte. */
8774
8775 static unsigned
8776 crc32_unsigned_bits (unsigned chksum, unsigned value, unsigned bits)
8777 {
8778 unsigned ix;
8779
8780 for (ix = bits; ix--; value <<= 1)
8781 {
8782 unsigned feedback;
8783
8784 feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
8785 chksum <<= 1;
8786 chksum ^= feedback;
8787 }
8788 return chksum;
8789 }
8790
8791 /* Generate a crc32 of a 32-bit unsigned. */
8792
8793 unsigned
8794 crc32_unsigned (unsigned chksum, unsigned value)
8795 {
8796 return crc32_unsigned_bits (chksum, value, 32);
8797 }
8798
8799 /* Generate a crc32 of a byte. */
8800
8801 unsigned
8802 crc32_byte (unsigned chksum, char byte)
8803 {
8804 return crc32_unsigned_bits (chksum, (unsigned) byte << 24, 8);
8805 }
8806
8807 /* Generate a crc32 of a string. */
8808
8809 unsigned
8810 crc32_string (unsigned chksum, const char *string)
8811 {
8812 do
8813 {
8814 chksum = crc32_byte (chksum, *string);
8815 }
8816 while (*string++);
8817 return chksum;
8818 }
8819
8820 /* P is a string that will be used in a symbol. Mask out any characters
8821 that are not valid in that context. */
8822
8823 void
8824 clean_symbol_name (char *p)
8825 {
8826 for (; *p; p++)
8827 if (! (ISALNUM (*p)
8828 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
8829 || *p == '$'
8830 #endif
8831 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
8832 || *p == '.'
8833 #endif
8834 ))
8835 *p = '_';
8836 }
8837
8838 /* Generate a name for a special-purpose function.
8839 The generated name may need to be unique across the whole link.
8840 Changes to this function may also require corresponding changes to
8841 xstrdup_mask_random.
8842 TYPE is some string to identify the purpose of this function to the
8843 linker or collect2; it must start with an uppercase letter,
8844 one of:
8845 I - for constructors
8846 D - for destructors
8847 N - for C++ anonymous namespaces
8848 F - for DWARF unwind frame information. */
8849
8850 tree
8851 get_file_function_name (const char *type)
8852 {
8853 char *buf;
8854 const char *p;
8855 char *q;
8856
8857 /* If we already have a name we know to be unique, just use that. */
8858 if (first_global_object_name)
8859 p = q = ASTRDUP (first_global_object_name);
8860 /* If the target is handling the constructors/destructors, they
8861 will be local to this file and the name is only necessary for
8862 debugging purposes.
8863 We also assign sub_I and sub_D sufixes to constructors called from
8864 the global static constructors. These are always local. */
8865 else if (((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
8866 || (strncmp (type, "sub_", 4) == 0
8867 && (type[4] == 'I' || type[4] == 'D')))
8868 {
8869 const char *file = main_input_filename;
8870 if (! file)
8871 file = input_filename;
8872 /* Just use the file's basename, because the full pathname
8873 might be quite long. */
8874 p = q = ASTRDUP (lbasename (file));
8875 }
8876 else
8877 {
8878 /* Otherwise, the name must be unique across the entire link.
8879 We don't have anything that we know to be unique to this translation
8880 unit, so use what we do have and throw in some randomness. */
8881 unsigned len;
8882 const char *name = weak_global_object_name;
8883 const char *file = main_input_filename;
8884
8885 if (! name)
8886 name = "";
8887 if (! file)
8888 file = input_filename;
8889
8890 len = strlen (file);
8891 q = (char *) alloca (9 + 17 + len + 1);
8892 memcpy (q, file, len + 1);
8893
8894 snprintf (q + len, 9 + 17 + 1, "_%08X_" HOST_WIDE_INT_PRINT_HEX,
8895 crc32_string (0, name), get_random_seed (false));
8896
8897 p = q;
8898 }
8899
8900 clean_symbol_name (q);
8901 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
8902 + strlen (type));
8903
8904 /* Set up the name of the file-level functions we may need.
8905 Use a global object (which is already required to be unique over
8906 the program) rather than the file name (which imposes extra
8907 constraints). */
8908 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
8909
8910 return get_identifier (buf);
8911 }
8912 \f
8913 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
8914
8915 /* Complain that the tree code of NODE does not match the expected 0
8916 terminated list of trailing codes. The trailing code list can be
8917 empty, for a more vague error message. FILE, LINE, and FUNCTION
8918 are of the caller. */
8919
8920 void
8921 tree_check_failed (const_tree node, const char *file,
8922 int line, const char *function, ...)
8923 {
8924 va_list args;
8925 const char *buffer;
8926 unsigned length = 0;
8927 int code;
8928
8929 va_start (args, function);
8930 while ((code = va_arg (args, int)))
8931 length += 4 + strlen (tree_code_name[code]);
8932 va_end (args);
8933 if (length)
8934 {
8935 char *tmp;
8936 va_start (args, function);
8937 length += strlen ("expected ");
8938 buffer = tmp = (char *) alloca (length);
8939 length = 0;
8940 while ((code = va_arg (args, int)))
8941 {
8942 const char *prefix = length ? " or " : "expected ";
8943
8944 strcpy (tmp + length, prefix);
8945 length += strlen (prefix);
8946 strcpy (tmp + length, tree_code_name[code]);
8947 length += strlen (tree_code_name[code]);
8948 }
8949 va_end (args);
8950 }
8951 else
8952 buffer = "unexpected node";
8953
8954 internal_error ("tree check: %s, have %s in %s, at %s:%d",
8955 buffer, tree_code_name[TREE_CODE (node)],
8956 function, trim_filename (file), line);
8957 }
8958
8959 /* Complain that the tree code of NODE does match the expected 0
8960 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
8961 the caller. */
8962
8963 void
8964 tree_not_check_failed (const_tree node, const char *file,
8965 int line, const char *function, ...)
8966 {
8967 va_list args;
8968 char *buffer;
8969 unsigned length = 0;
8970 int code;
8971
8972 va_start (args, function);
8973 while ((code = va_arg (args, int)))
8974 length += 4 + strlen (tree_code_name[code]);
8975 va_end (args);
8976 va_start (args, function);
8977 buffer = (char *) alloca (length);
8978 length = 0;
8979 while ((code = va_arg (args, int)))
8980 {
8981 if (length)
8982 {
8983 strcpy (buffer + length, " or ");
8984 length += 4;
8985 }
8986 strcpy (buffer + length, tree_code_name[code]);
8987 length += strlen (tree_code_name[code]);
8988 }
8989 va_end (args);
8990
8991 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
8992 buffer, tree_code_name[TREE_CODE (node)],
8993 function, trim_filename (file), line);
8994 }
8995
8996 /* Similar to tree_check_failed, except that we check for a class of tree
8997 code, given in CL. */
8998
8999 void
9000 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
9001 const char *file, int line, const char *function)
9002 {
9003 internal_error
9004 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
9005 TREE_CODE_CLASS_STRING (cl),
9006 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9007 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
9008 }
9009
9010 /* Similar to tree_check_failed, except that instead of specifying a
9011 dozen codes, use the knowledge that they're all sequential. */
9012
9013 void
9014 tree_range_check_failed (const_tree node, const char *file, int line,
9015 const char *function, enum tree_code c1,
9016 enum tree_code c2)
9017 {
9018 char *buffer;
9019 unsigned length = 0;
9020 unsigned int c;
9021
9022 for (c = c1; c <= c2; ++c)
9023 length += 4 + strlen (tree_code_name[c]);
9024
9025 length += strlen ("expected ");
9026 buffer = (char *) alloca (length);
9027 length = 0;
9028
9029 for (c = c1; c <= c2; ++c)
9030 {
9031 const char *prefix = length ? " or " : "expected ";
9032
9033 strcpy (buffer + length, prefix);
9034 length += strlen (prefix);
9035 strcpy (buffer + length, tree_code_name[c]);
9036 length += strlen (tree_code_name[c]);
9037 }
9038
9039 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9040 buffer, tree_code_name[TREE_CODE (node)],
9041 function, trim_filename (file), line);
9042 }
9043
9044
9045 /* Similar to tree_check_failed, except that we check that a tree does
9046 not have the specified code, given in CL. */
9047
9048 void
9049 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
9050 const char *file, int line, const char *function)
9051 {
9052 internal_error
9053 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
9054 TREE_CODE_CLASS_STRING (cl),
9055 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9056 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
9057 }
9058
9059
9060 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
9061
9062 void
9063 omp_clause_check_failed (const_tree node, const char *file, int line,
9064 const char *function, enum omp_clause_code code)
9065 {
9066 internal_error ("tree check: expected omp_clause %s, have %s in %s, at %s:%d",
9067 omp_clause_code_name[code], tree_code_name[TREE_CODE (node)],
9068 function, trim_filename (file), line);
9069 }
9070
9071
9072 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
9073
9074 void
9075 omp_clause_range_check_failed (const_tree node, const char *file, int line,
9076 const char *function, enum omp_clause_code c1,
9077 enum omp_clause_code c2)
9078 {
9079 char *buffer;
9080 unsigned length = 0;
9081 unsigned int c;
9082
9083 for (c = c1; c <= c2; ++c)
9084 length += 4 + strlen (omp_clause_code_name[c]);
9085
9086 length += strlen ("expected ");
9087 buffer = (char *) alloca (length);
9088 length = 0;
9089
9090 for (c = c1; c <= c2; ++c)
9091 {
9092 const char *prefix = length ? " or " : "expected ";
9093
9094 strcpy (buffer + length, prefix);
9095 length += strlen (prefix);
9096 strcpy (buffer + length, omp_clause_code_name[c]);
9097 length += strlen (omp_clause_code_name[c]);
9098 }
9099
9100 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9101 buffer, omp_clause_code_name[TREE_CODE (node)],
9102 function, trim_filename (file), line);
9103 }
9104
9105
9106 #undef DEFTREESTRUCT
9107 #define DEFTREESTRUCT(VAL, NAME) NAME,
9108
9109 static const char *ts_enum_names[] = {
9110 #include "treestruct.def"
9111 };
9112 #undef DEFTREESTRUCT
9113
9114 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
9115
9116 /* Similar to tree_class_check_failed, except that we check for
9117 whether CODE contains the tree structure identified by EN. */
9118
9119 void
9120 tree_contains_struct_check_failed (const_tree node,
9121 const enum tree_node_structure_enum en,
9122 const char *file, int line,
9123 const char *function)
9124 {
9125 internal_error
9126 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
9127 TS_ENUM_NAME(en),
9128 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
9129 }
9130
9131
9132 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9133 (dynamically sized) vector. */
9134
9135 void
9136 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
9137 const char *function)
9138 {
9139 internal_error
9140 ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
9141 idx + 1, len, function, trim_filename (file), line);
9142 }
9143
9144 /* Similar to above, except that the check is for the bounds of the operand
9145 vector of an expression node EXP. */
9146
9147 void
9148 tree_operand_check_failed (int idx, const_tree exp, const char *file,
9149 int line, const char *function)
9150 {
9151 int code = TREE_CODE (exp);
9152 internal_error
9153 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
9154 idx + 1, tree_code_name[code], TREE_OPERAND_LENGTH (exp),
9155 function, trim_filename (file), line);
9156 }
9157
9158 /* Similar to above, except that the check is for the number of
9159 operands of an OMP_CLAUSE node. */
9160
9161 void
9162 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
9163 int line, const char *function)
9164 {
9165 internal_error
9166 ("tree check: accessed operand %d of omp_clause %s with %d operands "
9167 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
9168 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
9169 trim_filename (file), line);
9170 }
9171 #endif /* ENABLE_TREE_CHECKING */
9172 \f
9173 /* Create a new vector type node holding SUBPARTS units of type INNERTYPE,
9174 and mapped to the machine mode MODE. Initialize its fields and build
9175 the information necessary for debugging output. */
9176
9177 static tree
9178 make_vector_type (tree innertype, int nunits, enum machine_mode mode)
9179 {
9180 tree t;
9181 hashval_t hashcode = 0;
9182
9183 t = make_node (VECTOR_TYPE);
9184 TREE_TYPE (t) = TYPE_MAIN_VARIANT (innertype);
9185 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
9186 SET_TYPE_MODE (t, mode);
9187
9188 if (TYPE_STRUCTURAL_EQUALITY_P (innertype))
9189 SET_TYPE_STRUCTURAL_EQUALITY (t);
9190 else if (TYPE_CANONICAL (innertype) != innertype
9191 || mode != VOIDmode)
9192 TYPE_CANONICAL (t)
9193 = make_vector_type (TYPE_CANONICAL (innertype), nunits, VOIDmode);
9194
9195 layout_type (t);
9196
9197 hashcode = iterative_hash_host_wide_int (VECTOR_TYPE, hashcode);
9198 hashcode = iterative_hash_host_wide_int (nunits, hashcode);
9199 hashcode = iterative_hash_host_wide_int (mode, hashcode);
9200 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (t)), hashcode);
9201 t = type_hash_canon (hashcode, t);
9202
9203 /* We have built a main variant, based on the main variant of the
9204 inner type. Use it to build the variant we return. */
9205 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
9206 && TREE_TYPE (t) != innertype)
9207 return build_type_attribute_qual_variant (t,
9208 TYPE_ATTRIBUTES (innertype),
9209 TYPE_QUALS (innertype));
9210
9211 return t;
9212 }
9213
9214 static tree
9215 make_or_reuse_type (unsigned size, int unsignedp)
9216 {
9217 if (size == INT_TYPE_SIZE)
9218 return unsignedp ? unsigned_type_node : integer_type_node;
9219 if (size == CHAR_TYPE_SIZE)
9220 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
9221 if (size == SHORT_TYPE_SIZE)
9222 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
9223 if (size == LONG_TYPE_SIZE)
9224 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
9225 if (size == LONG_LONG_TYPE_SIZE)
9226 return (unsignedp ? long_long_unsigned_type_node
9227 : long_long_integer_type_node);
9228 if (size == 128 && int128_integer_type_node)
9229 return (unsignedp ? int128_unsigned_type_node
9230 : int128_integer_type_node);
9231
9232 if (unsignedp)
9233 return make_unsigned_type (size);
9234 else
9235 return make_signed_type (size);
9236 }
9237
9238 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
9239
9240 static tree
9241 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
9242 {
9243 if (satp)
9244 {
9245 if (size == SHORT_FRACT_TYPE_SIZE)
9246 return unsignedp ? sat_unsigned_short_fract_type_node
9247 : sat_short_fract_type_node;
9248 if (size == FRACT_TYPE_SIZE)
9249 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
9250 if (size == LONG_FRACT_TYPE_SIZE)
9251 return unsignedp ? sat_unsigned_long_fract_type_node
9252 : sat_long_fract_type_node;
9253 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9254 return unsignedp ? sat_unsigned_long_long_fract_type_node
9255 : sat_long_long_fract_type_node;
9256 }
9257 else
9258 {
9259 if (size == SHORT_FRACT_TYPE_SIZE)
9260 return unsignedp ? unsigned_short_fract_type_node
9261 : short_fract_type_node;
9262 if (size == FRACT_TYPE_SIZE)
9263 return unsignedp ? unsigned_fract_type_node : fract_type_node;
9264 if (size == LONG_FRACT_TYPE_SIZE)
9265 return unsignedp ? unsigned_long_fract_type_node
9266 : long_fract_type_node;
9267 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9268 return unsignedp ? unsigned_long_long_fract_type_node
9269 : long_long_fract_type_node;
9270 }
9271
9272 return make_fract_type (size, unsignedp, satp);
9273 }
9274
9275 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
9276
9277 static tree
9278 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
9279 {
9280 if (satp)
9281 {
9282 if (size == SHORT_ACCUM_TYPE_SIZE)
9283 return unsignedp ? sat_unsigned_short_accum_type_node
9284 : sat_short_accum_type_node;
9285 if (size == ACCUM_TYPE_SIZE)
9286 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
9287 if (size == LONG_ACCUM_TYPE_SIZE)
9288 return unsignedp ? sat_unsigned_long_accum_type_node
9289 : sat_long_accum_type_node;
9290 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9291 return unsignedp ? sat_unsigned_long_long_accum_type_node
9292 : sat_long_long_accum_type_node;
9293 }
9294 else
9295 {
9296 if (size == SHORT_ACCUM_TYPE_SIZE)
9297 return unsignedp ? unsigned_short_accum_type_node
9298 : short_accum_type_node;
9299 if (size == ACCUM_TYPE_SIZE)
9300 return unsignedp ? unsigned_accum_type_node : accum_type_node;
9301 if (size == LONG_ACCUM_TYPE_SIZE)
9302 return unsignedp ? unsigned_long_accum_type_node
9303 : long_accum_type_node;
9304 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9305 return unsignedp ? unsigned_long_long_accum_type_node
9306 : long_long_accum_type_node;
9307 }
9308
9309 return make_accum_type (size, unsignedp, satp);
9310 }
9311
9312 /* Create nodes for all integer types (and error_mark_node) using the sizes
9313 of C datatypes. SIGNED_CHAR specifies whether char is signed,
9314 SHORT_DOUBLE specifies whether double should be of the same precision
9315 as float. */
9316
9317 void
9318 build_common_tree_nodes (bool signed_char, bool short_double)
9319 {
9320 error_mark_node = make_node (ERROR_MARK);
9321 TREE_TYPE (error_mark_node) = error_mark_node;
9322
9323 initialize_sizetypes ();
9324
9325 /* Define both `signed char' and `unsigned char'. */
9326 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
9327 TYPE_STRING_FLAG (signed_char_type_node) = 1;
9328 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
9329 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
9330
9331 /* Define `char', which is like either `signed char' or `unsigned char'
9332 but not the same as either. */
9333 char_type_node
9334 = (signed_char
9335 ? make_signed_type (CHAR_TYPE_SIZE)
9336 : make_unsigned_type (CHAR_TYPE_SIZE));
9337 TYPE_STRING_FLAG (char_type_node) = 1;
9338
9339 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
9340 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
9341 integer_type_node = make_signed_type (INT_TYPE_SIZE);
9342 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
9343 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
9344 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
9345 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
9346 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
9347 #if HOST_BITS_PER_WIDE_INT >= 64
9348 /* TODO: This isn't correct, but as logic depends at the moment on
9349 host's instead of target's wide-integer.
9350 If there is a target not supporting TImode, but has an 128-bit
9351 integer-scalar register, this target check needs to be adjusted. */
9352 if (targetm.scalar_mode_supported_p (TImode))
9353 {
9354 int128_integer_type_node = make_signed_type (128);
9355 int128_unsigned_type_node = make_unsigned_type (128);
9356 }
9357 #endif
9358
9359 /* Define a boolean type. This type only represents boolean values but
9360 may be larger than char depending on the value of BOOL_TYPE_SIZE.
9361 Front ends which want to override this size (i.e. Java) can redefine
9362 boolean_type_node before calling build_common_tree_nodes_2. */
9363 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
9364 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
9365 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
9366 TYPE_PRECISION (boolean_type_node) = 1;
9367
9368 /* Define what type to use for size_t. */
9369 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
9370 size_type_node = unsigned_type_node;
9371 else if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
9372 size_type_node = long_unsigned_type_node;
9373 else if (strcmp (SIZE_TYPE, "long long unsigned int") == 0)
9374 size_type_node = long_long_unsigned_type_node;
9375 else if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
9376 size_type_node = short_unsigned_type_node;
9377 else
9378 gcc_unreachable ();
9379
9380 /* Fill in the rest of the sized types. Reuse existing type nodes
9381 when possible. */
9382 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
9383 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
9384 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
9385 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
9386 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
9387
9388 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
9389 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
9390 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
9391 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
9392 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
9393
9394 access_public_node = get_identifier ("public");
9395 access_protected_node = get_identifier ("protected");
9396 access_private_node = get_identifier ("private");
9397
9398 /* Define these next since types below may used them. */
9399 integer_zero_node = build_int_cst (integer_type_node, 0);
9400 integer_one_node = build_int_cst (integer_type_node, 1);
9401 integer_three_node = build_int_cst (integer_type_node, 3);
9402 integer_minus_one_node = build_int_cst (integer_type_node, -1);
9403
9404 size_zero_node = size_int (0);
9405 size_one_node = size_int (1);
9406 bitsize_zero_node = bitsize_int (0);
9407 bitsize_one_node = bitsize_int (1);
9408 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
9409
9410 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
9411 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
9412
9413 void_type_node = make_node (VOID_TYPE);
9414 layout_type (void_type_node);
9415
9416 /* We are not going to have real types in C with less than byte alignment,
9417 so we might as well not have any types that claim to have it. */
9418 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
9419 TYPE_USER_ALIGN (void_type_node) = 0;
9420
9421 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
9422 layout_type (TREE_TYPE (null_pointer_node));
9423
9424 ptr_type_node = build_pointer_type (void_type_node);
9425 const_ptr_type_node
9426 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
9427 fileptr_type_node = ptr_type_node;
9428
9429 float_type_node = make_node (REAL_TYPE);
9430 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
9431 layout_type (float_type_node);
9432
9433 double_type_node = make_node (REAL_TYPE);
9434 if (short_double)
9435 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
9436 else
9437 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
9438 layout_type (double_type_node);
9439
9440 long_double_type_node = make_node (REAL_TYPE);
9441 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
9442 layout_type (long_double_type_node);
9443
9444 float_ptr_type_node = build_pointer_type (float_type_node);
9445 double_ptr_type_node = build_pointer_type (double_type_node);
9446 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
9447 integer_ptr_type_node = build_pointer_type (integer_type_node);
9448
9449 /* Fixed size integer types. */
9450 uint16_type_node = build_nonstandard_integer_type (16, true);
9451 uint32_type_node = build_nonstandard_integer_type (32, true);
9452 uint64_type_node = build_nonstandard_integer_type (64, true);
9453
9454 /* Decimal float types. */
9455 dfloat32_type_node = make_node (REAL_TYPE);
9456 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
9457 layout_type (dfloat32_type_node);
9458 SET_TYPE_MODE (dfloat32_type_node, SDmode);
9459 dfloat32_ptr_type_node = build_pointer_type (dfloat32_type_node);
9460
9461 dfloat64_type_node = make_node (REAL_TYPE);
9462 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
9463 layout_type (dfloat64_type_node);
9464 SET_TYPE_MODE (dfloat64_type_node, DDmode);
9465 dfloat64_ptr_type_node = build_pointer_type (dfloat64_type_node);
9466
9467 dfloat128_type_node = make_node (REAL_TYPE);
9468 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
9469 layout_type (dfloat128_type_node);
9470 SET_TYPE_MODE (dfloat128_type_node, TDmode);
9471 dfloat128_ptr_type_node = build_pointer_type (dfloat128_type_node);
9472
9473 complex_integer_type_node = build_complex_type (integer_type_node);
9474 complex_float_type_node = build_complex_type (float_type_node);
9475 complex_double_type_node = build_complex_type (double_type_node);
9476 complex_long_double_type_node = build_complex_type (long_double_type_node);
9477
9478 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
9479 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
9480 sat_ ## KIND ## _type_node = \
9481 make_sat_signed_ ## KIND ## _type (SIZE); \
9482 sat_unsigned_ ## KIND ## _type_node = \
9483 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9484 KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9485 unsigned_ ## KIND ## _type_node = \
9486 make_unsigned_ ## KIND ## _type (SIZE);
9487
9488 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
9489 sat_ ## WIDTH ## KIND ## _type_node = \
9490 make_sat_signed_ ## KIND ## _type (SIZE); \
9491 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
9492 make_sat_unsigned_ ## KIND ## _type (SIZE); \
9493 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
9494 unsigned_ ## WIDTH ## KIND ## _type_node = \
9495 make_unsigned_ ## KIND ## _type (SIZE);
9496
9497 /* Make fixed-point type nodes based on four different widths. */
9498 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
9499 MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
9500 MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
9501 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
9502 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
9503
9504 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
9505 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
9506 NAME ## _type_node = \
9507 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
9508 u ## NAME ## _type_node = \
9509 make_or_reuse_unsigned_ ## KIND ## _type \
9510 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
9511 sat_ ## NAME ## _type_node = \
9512 make_or_reuse_sat_signed_ ## KIND ## _type \
9513 (GET_MODE_BITSIZE (MODE ## mode)); \
9514 sat_u ## NAME ## _type_node = \
9515 make_or_reuse_sat_unsigned_ ## KIND ## _type \
9516 (GET_MODE_BITSIZE (U ## MODE ## mode));
9517
9518 /* Fixed-point type and mode nodes. */
9519 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
9520 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
9521 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
9522 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
9523 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
9524 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
9525 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
9526 MAKE_FIXED_MODE_NODE (accum, ha, HA)
9527 MAKE_FIXED_MODE_NODE (accum, sa, SA)
9528 MAKE_FIXED_MODE_NODE (accum, da, DA)
9529 MAKE_FIXED_MODE_NODE (accum, ta, TA)
9530
9531 {
9532 tree t = targetm.build_builtin_va_list ();
9533
9534 /* Many back-ends define record types without setting TYPE_NAME.
9535 If we copied the record type here, we'd keep the original
9536 record type without a name. This breaks name mangling. So,
9537 don't copy record types and let c_common_nodes_and_builtins()
9538 declare the type to be __builtin_va_list. */
9539 if (TREE_CODE (t) != RECORD_TYPE)
9540 t = build_variant_type_copy (t);
9541
9542 va_list_type_node = t;
9543 }
9544 }
9545
9546 /* Modify DECL for given flags. */
9547 void
9548 set_call_expr_flags (tree decl, int flags)
9549 {
9550 if (flags & ECF_NOTHROW)
9551 TREE_NOTHROW (decl) = 1;
9552 if (flags & ECF_CONST)
9553 TREE_READONLY (decl) = 1;
9554 if (flags & ECF_PURE)
9555 DECL_PURE_P (decl) = 1;
9556 if (flags & ECF_LOOPING_CONST_OR_PURE)
9557 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
9558 if (flags & ECF_NOVOPS)
9559 DECL_IS_NOVOPS (decl) = 1;
9560 if (flags & ECF_NORETURN)
9561 TREE_THIS_VOLATILE (decl) = 1;
9562 if (flags & ECF_MALLOC)
9563 DECL_IS_MALLOC (decl) = 1;
9564 if (flags & ECF_RETURNS_TWICE)
9565 DECL_IS_RETURNS_TWICE (decl) = 1;
9566 if (flags & ECF_LEAF)
9567 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("leaf"),
9568 NULL, DECL_ATTRIBUTES (decl));
9569 if ((flags & ECF_TM_PURE) && flag_tm)
9570 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("transaction_pure"),
9571 NULL, DECL_ATTRIBUTES (decl));
9572 /* Looping const or pure is implied by noreturn.
9573 There is currently no way to declare looping const or looping pure alone. */
9574 gcc_assert (!(flags & ECF_LOOPING_CONST_OR_PURE)
9575 || ((flags & ECF_NORETURN) && (flags & (ECF_CONST | ECF_PURE))));
9576 }
9577
9578
9579 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
9580
9581 static void
9582 local_define_builtin (const char *name, tree type, enum built_in_function code,
9583 const char *library_name, int ecf_flags)
9584 {
9585 tree decl;
9586
9587 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
9588 library_name, NULL_TREE);
9589 set_call_expr_flags (decl, ecf_flags);
9590
9591 set_builtin_decl (code, decl, true);
9592 }
9593
9594 /* Call this function after instantiating all builtins that the language
9595 front end cares about. This will build the rest of the builtins that
9596 are relied upon by the tree optimizers and the middle-end. */
9597
9598 void
9599 build_common_builtin_nodes (void)
9600 {
9601 tree tmp, ftype;
9602 int ecf_flags;
9603
9604 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE))
9605 {
9606 ftype = build_function_type (void_type_node, void_list_node);
9607 local_define_builtin ("__builtin_unreachable", ftype, BUILT_IN_UNREACHABLE,
9608 "__builtin_unreachable",
9609 ECF_NOTHROW | ECF_LEAF | ECF_NORETURN
9610 | ECF_CONST | ECF_LEAF);
9611 }
9612
9613 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY)
9614 || !builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9615 {
9616 ftype = build_function_type_list (ptr_type_node,
9617 ptr_type_node, const_ptr_type_node,
9618 size_type_node, NULL_TREE);
9619
9620 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY))
9621 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
9622 "memcpy", ECF_NOTHROW | ECF_LEAF);
9623 if (!builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
9624 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
9625 "memmove", ECF_NOTHROW | ECF_LEAF);
9626 }
9627
9628 if (!builtin_decl_explicit_p (BUILT_IN_MEMCMP))
9629 {
9630 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
9631 const_ptr_type_node, size_type_node,
9632 NULL_TREE);
9633 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
9634 "memcmp", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9635 }
9636
9637 if (!builtin_decl_explicit_p (BUILT_IN_MEMSET))
9638 {
9639 ftype = build_function_type_list (ptr_type_node,
9640 ptr_type_node, integer_type_node,
9641 size_type_node, NULL_TREE);
9642 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
9643 "memset", ECF_NOTHROW | ECF_LEAF);
9644 }
9645
9646 if (!builtin_decl_explicit_p (BUILT_IN_ALLOCA))
9647 {
9648 ftype = build_function_type_list (ptr_type_node,
9649 size_type_node, NULL_TREE);
9650 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
9651 "alloca", ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
9652 }
9653
9654 ftype = build_function_type_list (ptr_type_node, size_type_node,
9655 size_type_node, NULL_TREE);
9656 local_define_builtin ("__builtin_alloca_with_align", ftype,
9657 BUILT_IN_ALLOCA_WITH_ALIGN, "alloca",
9658 ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
9659
9660 /* If we're checking the stack, `alloca' can throw. */
9661 if (flag_stack_check)
9662 {
9663 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA)) = 0;
9664 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN)) = 0;
9665 }
9666
9667 ftype = build_function_type_list (void_type_node,
9668 ptr_type_node, ptr_type_node,
9669 ptr_type_node, NULL_TREE);
9670 local_define_builtin ("__builtin_init_trampoline", ftype,
9671 BUILT_IN_INIT_TRAMPOLINE,
9672 "__builtin_init_trampoline", ECF_NOTHROW | ECF_LEAF);
9673 local_define_builtin ("__builtin_init_heap_trampoline", ftype,
9674 BUILT_IN_INIT_HEAP_TRAMPOLINE,
9675 "__builtin_init_heap_trampoline",
9676 ECF_NOTHROW | ECF_LEAF);
9677
9678 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
9679 local_define_builtin ("__builtin_adjust_trampoline", ftype,
9680 BUILT_IN_ADJUST_TRAMPOLINE,
9681 "__builtin_adjust_trampoline",
9682 ECF_CONST | ECF_NOTHROW);
9683
9684 ftype = build_function_type_list (void_type_node,
9685 ptr_type_node, ptr_type_node, NULL_TREE);
9686 local_define_builtin ("__builtin_nonlocal_goto", ftype,
9687 BUILT_IN_NONLOCAL_GOTO,
9688 "__builtin_nonlocal_goto",
9689 ECF_NORETURN | ECF_NOTHROW);
9690
9691 ftype = build_function_type_list (void_type_node,
9692 ptr_type_node, ptr_type_node, NULL_TREE);
9693 local_define_builtin ("__builtin_setjmp_setup", ftype,
9694 BUILT_IN_SETJMP_SETUP,
9695 "__builtin_setjmp_setup", ECF_NOTHROW);
9696
9697 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
9698 local_define_builtin ("__builtin_setjmp_dispatcher", ftype,
9699 BUILT_IN_SETJMP_DISPATCHER,
9700 "__builtin_setjmp_dispatcher",
9701 ECF_PURE | ECF_NOTHROW);
9702
9703 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9704 local_define_builtin ("__builtin_setjmp_receiver", ftype,
9705 BUILT_IN_SETJMP_RECEIVER,
9706 "__builtin_setjmp_receiver", ECF_NOTHROW);
9707
9708 ftype = build_function_type_list (ptr_type_node, NULL_TREE);
9709 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
9710 "__builtin_stack_save", ECF_NOTHROW | ECF_LEAF);
9711
9712 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9713 local_define_builtin ("__builtin_stack_restore", ftype,
9714 BUILT_IN_STACK_RESTORE,
9715 "__builtin_stack_restore", ECF_NOTHROW | ECF_LEAF);
9716
9717 /* If there's a possibility that we might use the ARM EABI, build the
9718 alternate __cxa_end_cleanup node used to resume from C++ and Java. */
9719 if (targetm.arm_eabi_unwinder)
9720 {
9721 ftype = build_function_type_list (void_type_node, NULL_TREE);
9722 local_define_builtin ("__builtin_cxa_end_cleanup", ftype,
9723 BUILT_IN_CXA_END_CLEANUP,
9724 "__cxa_end_cleanup", ECF_NORETURN | ECF_LEAF);
9725 }
9726
9727 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
9728 local_define_builtin ("__builtin_unwind_resume", ftype,
9729 BUILT_IN_UNWIND_RESUME,
9730 ((targetm_common.except_unwind_info (&global_options)
9731 == UI_SJLJ)
9732 ? "_Unwind_SjLj_Resume" : "_Unwind_Resume"),
9733 ECF_NORETURN);
9734
9735 if (builtin_decl_explicit (BUILT_IN_RETURN_ADDRESS) == NULL_TREE)
9736 {
9737 ftype = build_function_type_list (ptr_type_node, integer_type_node,
9738 NULL_TREE);
9739 local_define_builtin ("__builtin_return_address", ftype,
9740 BUILT_IN_RETURN_ADDRESS,
9741 "__builtin_return_address",
9742 ECF_NOTHROW);
9743 }
9744
9745 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER)
9746 || !builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
9747 {
9748 ftype = build_function_type_list (void_type_node, ptr_type_node,
9749 ptr_type_node, NULL_TREE);
9750 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER))
9751 local_define_builtin ("__cyg_profile_func_enter", ftype,
9752 BUILT_IN_PROFILE_FUNC_ENTER,
9753 "__cyg_profile_func_enter", 0);
9754 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
9755 local_define_builtin ("__cyg_profile_func_exit", ftype,
9756 BUILT_IN_PROFILE_FUNC_EXIT,
9757 "__cyg_profile_func_exit", 0);
9758 }
9759
9760 /* The exception object and filter values from the runtime. The argument
9761 must be zero before exception lowering, i.e. from the front end. After
9762 exception lowering, it will be the region number for the exception
9763 landing pad. These functions are PURE instead of CONST to prevent
9764 them from being hoisted past the exception edge that will initialize
9765 its value in the landing pad. */
9766 ftype = build_function_type_list (ptr_type_node,
9767 integer_type_node, NULL_TREE);
9768 ecf_flags = ECF_PURE | ECF_NOTHROW | ECF_LEAF;
9769 /* Only use TM_PURE if we we have TM language support. */
9770 if (builtin_decl_explicit_p (BUILT_IN_TM_LOAD_1))
9771 ecf_flags |= ECF_TM_PURE;
9772 local_define_builtin ("__builtin_eh_pointer", ftype, BUILT_IN_EH_POINTER,
9773 "__builtin_eh_pointer", ecf_flags);
9774
9775 tmp = lang_hooks.types.type_for_mode (targetm.eh_return_filter_mode (), 0);
9776 ftype = build_function_type_list (tmp, integer_type_node, NULL_TREE);
9777 local_define_builtin ("__builtin_eh_filter", ftype, BUILT_IN_EH_FILTER,
9778 "__builtin_eh_filter", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
9779
9780 ftype = build_function_type_list (void_type_node,
9781 integer_type_node, integer_type_node,
9782 NULL_TREE);
9783 local_define_builtin ("__builtin_eh_copy_values", ftype,
9784 BUILT_IN_EH_COPY_VALUES,
9785 "__builtin_eh_copy_values", ECF_NOTHROW);
9786
9787 /* Complex multiplication and division. These are handled as builtins
9788 rather than optabs because emit_library_call_value doesn't support
9789 complex. Further, we can do slightly better with folding these
9790 beasties if the real and complex parts of the arguments are separate. */
9791 {
9792 int mode;
9793
9794 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
9795 {
9796 char mode_name_buf[4], *q;
9797 const char *p;
9798 enum built_in_function mcode, dcode;
9799 tree type, inner_type;
9800 const char *prefix = "__";
9801
9802 if (targetm.libfunc_gnu_prefix)
9803 prefix = "__gnu_";
9804
9805 type = lang_hooks.types.type_for_mode ((enum machine_mode) mode, 0);
9806 if (type == NULL)
9807 continue;
9808 inner_type = TREE_TYPE (type);
9809
9810 ftype = build_function_type_list (type, inner_type, inner_type,
9811 inner_type, inner_type, NULL_TREE);
9812
9813 mcode = ((enum built_in_function)
9814 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
9815 dcode = ((enum built_in_function)
9816 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
9817
9818 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
9819 *q = TOLOWER (*p);
9820 *q = '\0';
9821
9822 built_in_names[mcode] = concat (prefix, "mul", mode_name_buf, "3",
9823 NULL);
9824 local_define_builtin (built_in_names[mcode], ftype, mcode,
9825 built_in_names[mcode],
9826 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
9827
9828 built_in_names[dcode] = concat (prefix, "div", mode_name_buf, "3",
9829 NULL);
9830 local_define_builtin (built_in_names[dcode], ftype, dcode,
9831 built_in_names[dcode],
9832 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
9833 }
9834 }
9835 }
9836
9837 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
9838 better way.
9839
9840 If we requested a pointer to a vector, build up the pointers that
9841 we stripped off while looking for the inner type. Similarly for
9842 return values from functions.
9843
9844 The argument TYPE is the top of the chain, and BOTTOM is the
9845 new type which we will point to. */
9846
9847 tree
9848 reconstruct_complex_type (tree type, tree bottom)
9849 {
9850 tree inner, outer;
9851
9852 if (TREE_CODE (type) == POINTER_TYPE)
9853 {
9854 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9855 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
9856 TYPE_REF_CAN_ALIAS_ALL (type));
9857 }
9858 else if (TREE_CODE (type) == REFERENCE_TYPE)
9859 {
9860 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9861 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
9862 TYPE_REF_CAN_ALIAS_ALL (type));
9863 }
9864 else if (TREE_CODE (type) == ARRAY_TYPE)
9865 {
9866 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9867 outer = build_array_type (inner, TYPE_DOMAIN (type));
9868 }
9869 else if (TREE_CODE (type) == FUNCTION_TYPE)
9870 {
9871 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9872 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
9873 }
9874 else if (TREE_CODE (type) == METHOD_TYPE)
9875 {
9876 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9877 /* The build_method_type_directly() routine prepends 'this' to argument list,
9878 so we must compensate by getting rid of it. */
9879 outer
9880 = build_method_type_directly
9881 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
9882 inner,
9883 TREE_CHAIN (TYPE_ARG_TYPES (type)));
9884 }
9885 else if (TREE_CODE (type) == OFFSET_TYPE)
9886 {
9887 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
9888 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
9889 }
9890 else
9891 return bottom;
9892
9893 return build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
9894 TYPE_QUALS (type));
9895 }
9896
9897 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
9898 the inner type. */
9899 tree
9900 build_vector_type_for_mode (tree innertype, enum machine_mode mode)
9901 {
9902 int nunits;
9903
9904 switch (GET_MODE_CLASS (mode))
9905 {
9906 case MODE_VECTOR_INT:
9907 case MODE_VECTOR_FLOAT:
9908 case MODE_VECTOR_FRACT:
9909 case MODE_VECTOR_UFRACT:
9910 case MODE_VECTOR_ACCUM:
9911 case MODE_VECTOR_UACCUM:
9912 nunits = GET_MODE_NUNITS (mode);
9913 break;
9914
9915 case MODE_INT:
9916 /* Check that there are no leftover bits. */
9917 gcc_assert (GET_MODE_BITSIZE (mode)
9918 % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
9919
9920 nunits = GET_MODE_BITSIZE (mode)
9921 / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
9922 break;
9923
9924 default:
9925 gcc_unreachable ();
9926 }
9927
9928 return make_vector_type (innertype, nunits, mode);
9929 }
9930
9931 /* Similarly, but takes the inner type and number of units, which must be
9932 a power of two. */
9933
9934 tree
9935 build_vector_type (tree innertype, int nunits)
9936 {
9937 return make_vector_type (innertype, nunits, VOIDmode);
9938 }
9939
9940 /* Similarly, but builds a variant type with TYPE_VECTOR_OPAQUE set. */
9941
9942 tree
9943 build_opaque_vector_type (tree innertype, int nunits)
9944 {
9945 tree t = make_vector_type (innertype, nunits, VOIDmode);
9946 tree cand;
9947 /* We always build the non-opaque variant before the opaque one,
9948 so if it already exists, it is TYPE_NEXT_VARIANT of this one. */
9949 cand = TYPE_NEXT_VARIANT (t);
9950 if (cand
9951 && TYPE_VECTOR_OPAQUE (cand)
9952 && check_qualified_type (cand, t, TYPE_QUALS (t)))
9953 return cand;
9954 /* Othewise build a variant type and make sure to queue it after
9955 the non-opaque type. */
9956 cand = build_distinct_type_copy (t);
9957 TYPE_VECTOR_OPAQUE (cand) = true;
9958 TYPE_CANONICAL (cand) = TYPE_CANONICAL (t);
9959 TYPE_NEXT_VARIANT (cand) = TYPE_NEXT_VARIANT (t);
9960 TYPE_NEXT_VARIANT (t) = cand;
9961 TYPE_MAIN_VARIANT (cand) = TYPE_MAIN_VARIANT (t);
9962 return cand;
9963 }
9964
9965
9966 /* Given an initializer INIT, return TRUE if INIT is zero or some
9967 aggregate of zeros. Otherwise return FALSE. */
9968 bool
9969 initializer_zerop (const_tree init)
9970 {
9971 tree elt;
9972
9973 STRIP_NOPS (init);
9974
9975 switch (TREE_CODE (init))
9976 {
9977 case INTEGER_CST:
9978 return integer_zerop (init);
9979
9980 case REAL_CST:
9981 /* ??? Note that this is not correct for C4X float formats. There,
9982 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
9983 negative exponent. */
9984 return real_zerop (init)
9985 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
9986
9987 case FIXED_CST:
9988 return fixed_zerop (init);
9989
9990 case COMPLEX_CST:
9991 return integer_zerop (init)
9992 || (real_zerop (init)
9993 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
9994 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
9995
9996 case VECTOR_CST:
9997 {
9998 unsigned i;
9999 for (i = 0; i < VECTOR_CST_NELTS (init); ++i)
10000 if (!initializer_zerop (VECTOR_CST_ELT (init, i)))
10001 return false;
10002 return true;
10003 }
10004
10005 case CONSTRUCTOR:
10006 {
10007 unsigned HOST_WIDE_INT idx;
10008
10009 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
10010 if (!initializer_zerop (elt))
10011 return false;
10012 return true;
10013 }
10014
10015 case STRING_CST:
10016 {
10017 int i;
10018
10019 /* We need to loop through all elements to handle cases like
10020 "\0" and "\0foobar". */
10021 for (i = 0; i < TREE_STRING_LENGTH (init); ++i)
10022 if (TREE_STRING_POINTER (init)[i] != '\0')
10023 return false;
10024
10025 return true;
10026 }
10027
10028 default:
10029 return false;
10030 }
10031 }
10032
10033 /* Build an empty statement at location LOC. */
10034
10035 tree
10036 build_empty_stmt (location_t loc)
10037 {
10038 tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
10039 SET_EXPR_LOCATION (t, loc);
10040 return t;
10041 }
10042
10043
10044 /* Build an OpenMP clause with code CODE. LOC is the location of the
10045 clause. */
10046
10047 tree
10048 build_omp_clause (location_t loc, enum omp_clause_code code)
10049 {
10050 tree t;
10051 int size, length;
10052
10053 length = omp_clause_num_ops[code];
10054 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
10055
10056 record_node_allocation_statistics (OMP_CLAUSE, size);
10057
10058 t = ggc_alloc_tree_node (size);
10059 memset (t, 0, size);
10060 TREE_SET_CODE (t, OMP_CLAUSE);
10061 OMP_CLAUSE_SET_CODE (t, code);
10062 OMP_CLAUSE_LOCATION (t) = loc;
10063
10064 return t;
10065 }
10066
10067 /* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
10068 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
10069 Except for the CODE and operand count field, other storage for the
10070 object is initialized to zeros. */
10071
10072 tree
10073 build_vl_exp_stat (enum tree_code code, int len MEM_STAT_DECL)
10074 {
10075 tree t;
10076 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
10077
10078 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
10079 gcc_assert (len >= 1);
10080
10081 record_node_allocation_statistics (code, length);
10082
10083 t = ggc_alloc_zone_cleared_tree_node_stat (&tree_zone, length PASS_MEM_STAT);
10084
10085 TREE_SET_CODE (t, code);
10086
10087 /* Can't use TREE_OPERAND to store the length because if checking is
10088 enabled, it will try to check the length before we store it. :-P */
10089 t->exp.operands[0] = build_int_cst (sizetype, len);
10090
10091 return t;
10092 }
10093
10094 /* Helper function for build_call_* functions; build a CALL_EXPR with
10095 indicated RETURN_TYPE, FN, and NARGS, but do not initialize any of
10096 the argument slots. */
10097
10098 static tree
10099 build_call_1 (tree return_type, tree fn, int nargs)
10100 {
10101 tree t;
10102
10103 t = build_vl_exp (CALL_EXPR, nargs + 3);
10104 TREE_TYPE (t) = return_type;
10105 CALL_EXPR_FN (t) = fn;
10106 CALL_EXPR_STATIC_CHAIN (t) = NULL;
10107
10108 return t;
10109 }
10110
10111 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10112 FN and a null static chain slot. NARGS is the number of call arguments
10113 which are specified as "..." arguments. */
10114
10115 tree
10116 build_call_nary (tree return_type, tree fn, int nargs, ...)
10117 {
10118 tree ret;
10119 va_list args;
10120 va_start (args, nargs);
10121 ret = build_call_valist (return_type, fn, nargs, args);
10122 va_end (args);
10123 return ret;
10124 }
10125
10126 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10127 FN and a null static chain slot. NARGS is the number of call arguments
10128 which are specified as a va_list ARGS. */
10129
10130 tree
10131 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
10132 {
10133 tree t;
10134 int i;
10135
10136 t = build_call_1 (return_type, fn, nargs);
10137 for (i = 0; i < nargs; i++)
10138 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
10139 process_call_operands (t);
10140 return t;
10141 }
10142
10143 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10144 FN and a null static chain slot. NARGS is the number of call arguments
10145 which are specified as a tree array ARGS. */
10146
10147 tree
10148 build_call_array_loc (location_t loc, tree return_type, tree fn,
10149 int nargs, const tree *args)
10150 {
10151 tree t;
10152 int i;
10153
10154 t = build_call_1 (return_type, fn, nargs);
10155 for (i = 0; i < nargs; i++)
10156 CALL_EXPR_ARG (t, i) = args[i];
10157 process_call_operands (t);
10158 SET_EXPR_LOCATION (t, loc);
10159 return t;
10160 }
10161
10162 /* Like build_call_array, but takes a vec. */
10163
10164 tree
10165 build_call_vec (tree return_type, tree fn, vec<tree, va_gc> *args)
10166 {
10167 tree ret, t;
10168 unsigned int ix;
10169
10170 ret = build_call_1 (return_type, fn, vec_safe_length (args));
10171 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
10172 CALL_EXPR_ARG (ret, ix) = t;
10173 process_call_operands (ret);
10174 return ret;
10175 }
10176
10177
10178 /* Returns true if it is possible to prove that the index of
10179 an array access REF (an ARRAY_REF expression) falls into the
10180 array bounds. */
10181
10182 bool
10183 in_array_bounds_p (tree ref)
10184 {
10185 tree idx = TREE_OPERAND (ref, 1);
10186 tree min, max;
10187
10188 if (TREE_CODE (idx) != INTEGER_CST)
10189 return false;
10190
10191 min = array_ref_low_bound (ref);
10192 max = array_ref_up_bound (ref);
10193 if (!min
10194 || !max
10195 || TREE_CODE (min) != INTEGER_CST
10196 || TREE_CODE (max) != INTEGER_CST)
10197 return false;
10198
10199 if (tree_int_cst_lt (idx, min)
10200 || tree_int_cst_lt (max, idx))
10201 return false;
10202
10203 return true;
10204 }
10205
10206 /* Returns true if it is possible to prove that the range of
10207 an array access REF (an ARRAY_RANGE_REF expression) falls
10208 into the array bounds. */
10209
10210 bool
10211 range_in_array_bounds_p (tree ref)
10212 {
10213 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ref));
10214 tree range_min, range_max, min, max;
10215
10216 range_min = TYPE_MIN_VALUE (domain_type);
10217 range_max = TYPE_MAX_VALUE (domain_type);
10218 if (!range_min
10219 || !range_max
10220 || TREE_CODE (range_min) != INTEGER_CST
10221 || TREE_CODE (range_max) != INTEGER_CST)
10222 return false;
10223
10224 min = array_ref_low_bound (ref);
10225 max = array_ref_up_bound (ref);
10226 if (!min
10227 || !max
10228 || TREE_CODE (min) != INTEGER_CST
10229 || TREE_CODE (max) != INTEGER_CST)
10230 return false;
10231
10232 if (tree_int_cst_lt (range_min, min)
10233 || tree_int_cst_lt (max, range_max))
10234 return false;
10235
10236 return true;
10237 }
10238
10239 /* Return true if T (assumed to be a DECL) must be assigned a memory
10240 location. */
10241
10242 bool
10243 needs_to_live_in_memory (const_tree t)
10244 {
10245 return (TREE_ADDRESSABLE (t)
10246 || is_global_var (t)
10247 || (TREE_CODE (t) == RESULT_DECL
10248 && !DECL_BY_REFERENCE (t)
10249 && aggregate_value_p (t, current_function_decl)));
10250 }
10251
10252 /* Return value of a constant X and sign-extend it. */
10253
10254 HOST_WIDE_INT
10255 int_cst_value (const_tree x)
10256 {
10257 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10258 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
10259
10260 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10261 gcc_assert (TREE_INT_CST_HIGH (x) == 0
10262 || TREE_INT_CST_HIGH (x) == -1);
10263
10264 if (bits < HOST_BITS_PER_WIDE_INT)
10265 {
10266 bool negative = ((val >> (bits - 1)) & 1) != 0;
10267 if (negative)
10268 val |= (~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1;
10269 else
10270 val &= ~((~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1);
10271 }
10272
10273 return val;
10274 }
10275
10276 /* Return value of a constant X and sign-extend it. */
10277
10278 HOST_WIDEST_INT
10279 widest_int_cst_value (const_tree x)
10280 {
10281 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
10282 unsigned HOST_WIDEST_INT val = TREE_INT_CST_LOW (x);
10283
10284 #if HOST_BITS_PER_WIDEST_INT > HOST_BITS_PER_WIDE_INT
10285 gcc_assert (HOST_BITS_PER_WIDEST_INT >= HOST_BITS_PER_DOUBLE_INT);
10286 val |= (((unsigned HOST_WIDEST_INT) TREE_INT_CST_HIGH (x))
10287 << HOST_BITS_PER_WIDE_INT);
10288 #else
10289 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
10290 gcc_assert (TREE_INT_CST_HIGH (x) == 0
10291 || TREE_INT_CST_HIGH (x) == -1);
10292 #endif
10293
10294 if (bits < HOST_BITS_PER_WIDEST_INT)
10295 {
10296 bool negative = ((val >> (bits - 1)) & 1) != 0;
10297 if (negative)
10298 val |= (~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1;
10299 else
10300 val &= ~((~(unsigned HOST_WIDEST_INT) 0) << (bits - 1) << 1);
10301 }
10302
10303 return val;
10304 }
10305
10306 /* If TYPE is an integral or pointer type, return an integer type with
10307 the same precision which is unsigned iff UNSIGNEDP is true, or itself
10308 if TYPE is already an integer type of signedness UNSIGNEDP. */
10309
10310 tree
10311 signed_or_unsigned_type_for (int unsignedp, tree type)
10312 {
10313 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type) == unsignedp)
10314 return type;
10315
10316 if (TREE_CODE (type) == VECTOR_TYPE)
10317 {
10318 tree inner = TREE_TYPE (type);
10319 tree inner2 = signed_or_unsigned_type_for (unsignedp, inner);
10320 if (!inner2)
10321 return NULL_TREE;
10322 if (inner == inner2)
10323 return type;
10324 return build_vector_type (inner2, TYPE_VECTOR_SUBPARTS (type));
10325 }
10326
10327 if (!INTEGRAL_TYPE_P (type)
10328 && !POINTER_TYPE_P (type))
10329 return NULL_TREE;
10330
10331 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
10332 }
10333
10334 /* If TYPE is an integral or pointer type, return an integer type with
10335 the same precision which is unsigned, or itself if TYPE is already an
10336 unsigned integer type. */
10337
10338 tree
10339 unsigned_type_for (tree type)
10340 {
10341 return signed_or_unsigned_type_for (1, type);
10342 }
10343
10344 /* If TYPE is an integral or pointer type, return an integer type with
10345 the same precision which is signed, or itself if TYPE is already a
10346 signed integer type. */
10347
10348 tree
10349 signed_type_for (tree type)
10350 {
10351 return signed_or_unsigned_type_for (0, type);
10352 }
10353
10354 /* If TYPE is a vector type, return a signed integer vector type with the
10355 same width and number of subparts. Otherwise return boolean_type_node. */
10356
10357 tree
10358 truth_type_for (tree type)
10359 {
10360 if (TREE_CODE (type) == VECTOR_TYPE)
10361 {
10362 tree elem = lang_hooks.types.type_for_size
10363 (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))), 0);
10364 return build_opaque_vector_type (elem, TYPE_VECTOR_SUBPARTS (type));
10365 }
10366 else
10367 return boolean_type_node;
10368 }
10369
10370 /* Returns the largest value obtainable by casting something in INNER type to
10371 OUTER type. */
10372
10373 tree
10374 upper_bound_in_type (tree outer, tree inner)
10375 {
10376 double_int high;
10377 unsigned int det = 0;
10378 unsigned oprec = TYPE_PRECISION (outer);
10379 unsigned iprec = TYPE_PRECISION (inner);
10380 unsigned prec;
10381
10382 /* Compute a unique number for every combination. */
10383 det |= (oprec > iprec) ? 4 : 0;
10384 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
10385 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
10386
10387 /* Determine the exponent to use. */
10388 switch (det)
10389 {
10390 case 0:
10391 case 1:
10392 /* oprec <= iprec, outer: signed, inner: don't care. */
10393 prec = oprec - 1;
10394 break;
10395 case 2:
10396 case 3:
10397 /* oprec <= iprec, outer: unsigned, inner: don't care. */
10398 prec = oprec;
10399 break;
10400 case 4:
10401 /* oprec > iprec, outer: signed, inner: signed. */
10402 prec = iprec - 1;
10403 break;
10404 case 5:
10405 /* oprec > iprec, outer: signed, inner: unsigned. */
10406 prec = iprec;
10407 break;
10408 case 6:
10409 /* oprec > iprec, outer: unsigned, inner: signed. */
10410 prec = oprec;
10411 break;
10412 case 7:
10413 /* oprec > iprec, outer: unsigned, inner: unsigned. */
10414 prec = iprec;
10415 break;
10416 default:
10417 gcc_unreachable ();
10418 }
10419
10420 /* Compute 2^^prec - 1. */
10421 if (prec <= HOST_BITS_PER_WIDE_INT)
10422 {
10423 high.high = 0;
10424 high.low = ((~(unsigned HOST_WIDE_INT) 0)
10425 >> (HOST_BITS_PER_WIDE_INT - prec));
10426 }
10427 else
10428 {
10429 high.high = ((~(unsigned HOST_WIDE_INT) 0)
10430 >> (HOST_BITS_PER_DOUBLE_INT - prec));
10431 high.low = ~(unsigned HOST_WIDE_INT) 0;
10432 }
10433
10434 return double_int_to_tree (outer, high);
10435 }
10436
10437 /* Returns the smallest value obtainable by casting something in INNER type to
10438 OUTER type. */
10439
10440 tree
10441 lower_bound_in_type (tree outer, tree inner)
10442 {
10443 double_int low;
10444 unsigned oprec = TYPE_PRECISION (outer);
10445 unsigned iprec = TYPE_PRECISION (inner);
10446
10447 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
10448 and obtain 0. */
10449 if (TYPE_UNSIGNED (outer)
10450 /* If we are widening something of an unsigned type, OUTER type
10451 contains all values of INNER type. In particular, both INNER
10452 and OUTER types have zero in common. */
10453 || (oprec > iprec && TYPE_UNSIGNED (inner)))
10454 low.low = low.high = 0;
10455 else
10456 {
10457 /* If we are widening a signed type to another signed type, we
10458 want to obtain -2^^(iprec-1). If we are keeping the
10459 precision or narrowing to a signed type, we want to obtain
10460 -2^(oprec-1). */
10461 unsigned prec = oprec > iprec ? iprec : oprec;
10462
10463 if (prec <= HOST_BITS_PER_WIDE_INT)
10464 {
10465 low.high = ~(unsigned HOST_WIDE_INT) 0;
10466 low.low = (~(unsigned HOST_WIDE_INT) 0) << (prec - 1);
10467 }
10468 else
10469 {
10470 low.high = ((~(unsigned HOST_WIDE_INT) 0)
10471 << (prec - HOST_BITS_PER_WIDE_INT - 1));
10472 low.low = 0;
10473 }
10474 }
10475
10476 return double_int_to_tree (outer, low);
10477 }
10478
10479 /* Return nonzero if two operands that are suitable for PHI nodes are
10480 necessarily equal. Specifically, both ARG0 and ARG1 must be either
10481 SSA_NAME or invariant. Note that this is strictly an optimization.
10482 That is, callers of this function can directly call operand_equal_p
10483 and get the same result, only slower. */
10484
10485 int
10486 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
10487 {
10488 if (arg0 == arg1)
10489 return 1;
10490 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
10491 return 0;
10492 return operand_equal_p (arg0, arg1, 0);
10493 }
10494
10495 /* Returns number of zeros at the end of binary representation of X.
10496
10497 ??? Use ffs if available? */
10498
10499 tree
10500 num_ending_zeros (const_tree x)
10501 {
10502 unsigned HOST_WIDE_INT fr, nfr;
10503 unsigned num, abits;
10504 tree type = TREE_TYPE (x);
10505
10506 if (TREE_INT_CST_LOW (x) == 0)
10507 {
10508 num = HOST_BITS_PER_WIDE_INT;
10509 fr = TREE_INT_CST_HIGH (x);
10510 }
10511 else
10512 {
10513 num = 0;
10514 fr = TREE_INT_CST_LOW (x);
10515 }
10516
10517 for (abits = HOST_BITS_PER_WIDE_INT / 2; abits; abits /= 2)
10518 {
10519 nfr = fr >> abits;
10520 if (nfr << abits == fr)
10521 {
10522 num += abits;
10523 fr = nfr;
10524 }
10525 }
10526
10527 if (num > TYPE_PRECISION (type))
10528 num = TYPE_PRECISION (type);
10529
10530 return build_int_cst_type (type, num);
10531 }
10532
10533
10534 #define WALK_SUBTREE(NODE) \
10535 do \
10536 { \
10537 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
10538 if (result) \
10539 return result; \
10540 } \
10541 while (0)
10542
10543 /* This is a subroutine of walk_tree that walks field of TYPE that are to
10544 be walked whenever a type is seen in the tree. Rest of operands and return
10545 value are as for walk_tree. */
10546
10547 static tree
10548 walk_type_fields (tree type, walk_tree_fn func, void *data,
10549 struct pointer_set_t *pset, walk_tree_lh lh)
10550 {
10551 tree result = NULL_TREE;
10552
10553 switch (TREE_CODE (type))
10554 {
10555 case POINTER_TYPE:
10556 case REFERENCE_TYPE:
10557 /* We have to worry about mutually recursive pointers. These can't
10558 be written in C. They can in Ada. It's pathological, but
10559 there's an ACATS test (c38102a) that checks it. Deal with this
10560 by checking if we're pointing to another pointer, that one
10561 points to another pointer, that one does too, and we have no htab.
10562 If so, get a hash table. We check three levels deep to avoid
10563 the cost of the hash table if we don't need one. */
10564 if (POINTER_TYPE_P (TREE_TYPE (type))
10565 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
10566 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
10567 && !pset)
10568 {
10569 result = walk_tree_without_duplicates (&TREE_TYPE (type),
10570 func, data);
10571 if (result)
10572 return result;
10573
10574 break;
10575 }
10576
10577 /* ... fall through ... */
10578
10579 case COMPLEX_TYPE:
10580 WALK_SUBTREE (TREE_TYPE (type));
10581 break;
10582
10583 case METHOD_TYPE:
10584 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
10585
10586 /* Fall through. */
10587
10588 case FUNCTION_TYPE:
10589 WALK_SUBTREE (TREE_TYPE (type));
10590 {
10591 tree arg;
10592
10593 /* We never want to walk into default arguments. */
10594 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
10595 WALK_SUBTREE (TREE_VALUE (arg));
10596 }
10597 break;
10598
10599 case ARRAY_TYPE:
10600 /* Don't follow this nodes's type if a pointer for fear that
10601 we'll have infinite recursion. If we have a PSET, then we
10602 need not fear. */
10603 if (pset
10604 || (!POINTER_TYPE_P (TREE_TYPE (type))
10605 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
10606 WALK_SUBTREE (TREE_TYPE (type));
10607 WALK_SUBTREE (TYPE_DOMAIN (type));
10608 break;
10609
10610 case OFFSET_TYPE:
10611 WALK_SUBTREE (TREE_TYPE (type));
10612 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
10613 break;
10614
10615 default:
10616 break;
10617 }
10618
10619 return NULL_TREE;
10620 }
10621
10622 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
10623 called with the DATA and the address of each sub-tree. If FUNC returns a
10624 non-NULL value, the traversal is stopped, and the value returned by FUNC
10625 is returned. If PSET is non-NULL it is used to record the nodes visited,
10626 and to avoid visiting a node more than once. */
10627
10628 tree
10629 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
10630 struct pointer_set_t *pset, walk_tree_lh lh)
10631 {
10632 enum tree_code code;
10633 int walk_subtrees;
10634 tree result;
10635
10636 #define WALK_SUBTREE_TAIL(NODE) \
10637 do \
10638 { \
10639 tp = & (NODE); \
10640 goto tail_recurse; \
10641 } \
10642 while (0)
10643
10644 tail_recurse:
10645 /* Skip empty subtrees. */
10646 if (!*tp)
10647 return NULL_TREE;
10648
10649 /* Don't walk the same tree twice, if the user has requested
10650 that we avoid doing so. */
10651 if (pset && pointer_set_insert (pset, *tp))
10652 return NULL_TREE;
10653
10654 /* Call the function. */
10655 walk_subtrees = 1;
10656 result = (*func) (tp, &walk_subtrees, data);
10657
10658 /* If we found something, return it. */
10659 if (result)
10660 return result;
10661
10662 code = TREE_CODE (*tp);
10663
10664 /* Even if we didn't, FUNC may have decided that there was nothing
10665 interesting below this point in the tree. */
10666 if (!walk_subtrees)
10667 {
10668 /* But we still need to check our siblings. */
10669 if (code == TREE_LIST)
10670 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
10671 else if (code == OMP_CLAUSE)
10672 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10673 else
10674 return NULL_TREE;
10675 }
10676
10677 if (lh)
10678 {
10679 result = (*lh) (tp, &walk_subtrees, func, data, pset);
10680 if (result || !walk_subtrees)
10681 return result;
10682 }
10683
10684 switch (code)
10685 {
10686 case ERROR_MARK:
10687 case IDENTIFIER_NODE:
10688 case INTEGER_CST:
10689 case REAL_CST:
10690 case FIXED_CST:
10691 case VECTOR_CST:
10692 case STRING_CST:
10693 case BLOCK:
10694 case PLACEHOLDER_EXPR:
10695 case SSA_NAME:
10696 case FIELD_DECL:
10697 case RESULT_DECL:
10698 /* None of these have subtrees other than those already walked
10699 above. */
10700 break;
10701
10702 case TREE_LIST:
10703 WALK_SUBTREE (TREE_VALUE (*tp));
10704 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
10705 break;
10706
10707 case TREE_VEC:
10708 {
10709 int len = TREE_VEC_LENGTH (*tp);
10710
10711 if (len == 0)
10712 break;
10713
10714 /* Walk all elements but the first. */
10715 while (--len)
10716 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
10717
10718 /* Now walk the first one as a tail call. */
10719 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
10720 }
10721
10722 case COMPLEX_CST:
10723 WALK_SUBTREE (TREE_REALPART (*tp));
10724 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
10725
10726 case CONSTRUCTOR:
10727 {
10728 unsigned HOST_WIDE_INT idx;
10729 constructor_elt *ce;
10730
10731 for (idx = 0; vec_safe_iterate(CONSTRUCTOR_ELTS (*tp), idx, &ce); idx++)
10732 WALK_SUBTREE (ce->value);
10733 }
10734 break;
10735
10736 case SAVE_EXPR:
10737 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
10738
10739 case BIND_EXPR:
10740 {
10741 tree decl;
10742 for (decl = BIND_EXPR_VARS (*tp); decl; decl = DECL_CHAIN (decl))
10743 {
10744 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
10745 into declarations that are just mentioned, rather than
10746 declared; they don't really belong to this part of the tree.
10747 And, we can see cycles: the initializer for a declaration
10748 can refer to the declaration itself. */
10749 WALK_SUBTREE (DECL_INITIAL (decl));
10750 WALK_SUBTREE (DECL_SIZE (decl));
10751 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
10752 }
10753 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
10754 }
10755
10756 case STATEMENT_LIST:
10757 {
10758 tree_stmt_iterator i;
10759 for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
10760 WALK_SUBTREE (*tsi_stmt_ptr (i));
10761 }
10762 break;
10763
10764 case OMP_CLAUSE:
10765 switch (OMP_CLAUSE_CODE (*tp))
10766 {
10767 case OMP_CLAUSE_PRIVATE:
10768 case OMP_CLAUSE_SHARED:
10769 case OMP_CLAUSE_FIRSTPRIVATE:
10770 case OMP_CLAUSE_COPYIN:
10771 case OMP_CLAUSE_COPYPRIVATE:
10772 case OMP_CLAUSE_FINAL:
10773 case OMP_CLAUSE_IF:
10774 case OMP_CLAUSE_NUM_THREADS:
10775 case OMP_CLAUSE_SCHEDULE:
10776 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0));
10777 /* FALLTHRU */
10778
10779 case OMP_CLAUSE_NOWAIT:
10780 case OMP_CLAUSE_ORDERED:
10781 case OMP_CLAUSE_DEFAULT:
10782 case OMP_CLAUSE_UNTIED:
10783 case OMP_CLAUSE_MERGEABLE:
10784 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10785
10786 case OMP_CLAUSE_LASTPRIVATE:
10787 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
10788 WALK_SUBTREE (OMP_CLAUSE_LASTPRIVATE_STMT (*tp));
10789 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10790
10791 case OMP_CLAUSE_COLLAPSE:
10792 {
10793 int i;
10794 for (i = 0; i < 3; i++)
10795 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
10796 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10797 }
10798
10799 case OMP_CLAUSE_REDUCTION:
10800 {
10801 int i;
10802 for (i = 0; i < 4; i++)
10803 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
10804 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
10805 }
10806
10807 default:
10808 gcc_unreachable ();
10809 }
10810 break;
10811
10812 case TARGET_EXPR:
10813 {
10814 int i, len;
10815
10816 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
10817 But, we only want to walk once. */
10818 len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
10819 for (i = 0; i < len; ++i)
10820 WALK_SUBTREE (TREE_OPERAND (*tp, i));
10821 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
10822 }
10823
10824 case DECL_EXPR:
10825 /* If this is a TYPE_DECL, walk into the fields of the type that it's
10826 defining. We only want to walk into these fields of a type in this
10827 case and not in the general case of a mere reference to the type.
10828
10829 The criterion is as follows: if the field can be an expression, it
10830 must be walked only here. This should be in keeping with the fields
10831 that are directly gimplified in gimplify_type_sizes in order for the
10832 mark/copy-if-shared/unmark machinery of the gimplifier to work with
10833 variable-sized types.
10834
10835 Note that DECLs get walked as part of processing the BIND_EXPR. */
10836 if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
10837 {
10838 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
10839 if (TREE_CODE (*type_p) == ERROR_MARK)
10840 return NULL_TREE;
10841
10842 /* Call the function for the type. See if it returns anything or
10843 doesn't want us to continue. If we are to continue, walk both
10844 the normal fields and those for the declaration case. */
10845 result = (*func) (type_p, &walk_subtrees, data);
10846 if (result || !walk_subtrees)
10847 return result;
10848
10849 /* But do not walk a pointed-to type since it may itself need to
10850 be walked in the declaration case if it isn't anonymous. */
10851 if (!POINTER_TYPE_P (*type_p))
10852 {
10853 result = walk_type_fields (*type_p, func, data, pset, lh);
10854 if (result)
10855 return result;
10856 }
10857
10858 /* If this is a record type, also walk the fields. */
10859 if (RECORD_OR_UNION_TYPE_P (*type_p))
10860 {
10861 tree field;
10862
10863 for (field = TYPE_FIELDS (*type_p); field;
10864 field = DECL_CHAIN (field))
10865 {
10866 /* We'd like to look at the type of the field, but we can
10867 easily get infinite recursion. So assume it's pointed
10868 to elsewhere in the tree. Also, ignore things that
10869 aren't fields. */
10870 if (TREE_CODE (field) != FIELD_DECL)
10871 continue;
10872
10873 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
10874 WALK_SUBTREE (DECL_SIZE (field));
10875 WALK_SUBTREE (DECL_SIZE_UNIT (field));
10876 if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
10877 WALK_SUBTREE (DECL_QUALIFIER (field));
10878 }
10879 }
10880
10881 /* Same for scalar types. */
10882 else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
10883 || TREE_CODE (*type_p) == ENUMERAL_TYPE
10884 || TREE_CODE (*type_p) == INTEGER_TYPE
10885 || TREE_CODE (*type_p) == FIXED_POINT_TYPE
10886 || TREE_CODE (*type_p) == REAL_TYPE)
10887 {
10888 WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
10889 WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
10890 }
10891
10892 WALK_SUBTREE (TYPE_SIZE (*type_p));
10893 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
10894 }
10895 /* FALLTHRU */
10896
10897 default:
10898 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
10899 {
10900 int i, len;
10901
10902 /* Walk over all the sub-trees of this operand. */
10903 len = TREE_OPERAND_LENGTH (*tp);
10904
10905 /* Go through the subtrees. We need to do this in forward order so
10906 that the scope of a FOR_EXPR is handled properly. */
10907 if (len)
10908 {
10909 for (i = 0; i < len - 1; ++i)
10910 WALK_SUBTREE (TREE_OPERAND (*tp, i));
10911 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
10912 }
10913 }
10914 /* If this is a type, walk the needed fields in the type. */
10915 else if (TYPE_P (*tp))
10916 return walk_type_fields (*tp, func, data, pset, lh);
10917 break;
10918 }
10919
10920 /* We didn't find what we were looking for. */
10921 return NULL_TREE;
10922
10923 #undef WALK_SUBTREE_TAIL
10924 }
10925 #undef WALK_SUBTREE
10926
10927 /* Like walk_tree, but does not walk duplicate nodes more than once. */
10928
10929 tree
10930 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
10931 walk_tree_lh lh)
10932 {
10933 tree result;
10934 struct pointer_set_t *pset;
10935
10936 pset = pointer_set_create ();
10937 result = walk_tree_1 (tp, func, data, pset, lh);
10938 pointer_set_destroy (pset);
10939 return result;
10940 }
10941
10942
10943 tree
10944 tree_block (tree t)
10945 {
10946 char const c = TREE_CODE_CLASS (TREE_CODE (t));
10947
10948 if (IS_EXPR_CODE_CLASS (c))
10949 return LOCATION_BLOCK (t->exp.locus);
10950 gcc_unreachable ();
10951 return NULL;
10952 }
10953
10954 void
10955 tree_set_block (tree t, tree b)
10956 {
10957 char const c = TREE_CODE_CLASS (TREE_CODE (t));
10958
10959 if (IS_EXPR_CODE_CLASS (c))
10960 {
10961 if (b)
10962 t->exp.locus = COMBINE_LOCATION_DATA (line_table, t->exp.locus, b);
10963 else
10964 t->exp.locus = LOCATION_LOCUS (t->exp.locus);
10965 }
10966 else
10967 gcc_unreachable ();
10968 }
10969
10970 /* Create a nameless artificial label and put it in the current
10971 function context. The label has a location of LOC. Returns the
10972 newly created label. */
10973
10974 tree
10975 create_artificial_label (location_t loc)
10976 {
10977 tree lab = build_decl (loc,
10978 LABEL_DECL, NULL_TREE, void_type_node);
10979
10980 DECL_ARTIFICIAL (lab) = 1;
10981 DECL_IGNORED_P (lab) = 1;
10982 DECL_CONTEXT (lab) = current_function_decl;
10983 return lab;
10984 }
10985
10986 /* Given a tree, try to return a useful variable name that we can use
10987 to prefix a temporary that is being assigned the value of the tree.
10988 I.E. given <temp> = &A, return A. */
10989
10990 const char *
10991 get_name (tree t)
10992 {
10993 tree stripped_decl;
10994
10995 stripped_decl = t;
10996 STRIP_NOPS (stripped_decl);
10997 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
10998 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
10999 else if (TREE_CODE (stripped_decl) == SSA_NAME)
11000 {
11001 tree name = SSA_NAME_IDENTIFIER (stripped_decl);
11002 if (!name)
11003 return NULL;
11004 return IDENTIFIER_POINTER (name);
11005 }
11006 else
11007 {
11008 switch (TREE_CODE (stripped_decl))
11009 {
11010 case ADDR_EXPR:
11011 return get_name (TREE_OPERAND (stripped_decl, 0));
11012 default:
11013 return NULL;
11014 }
11015 }
11016 }
11017
11018 /* Return true if TYPE has a variable argument list. */
11019
11020 bool
11021 stdarg_p (const_tree fntype)
11022 {
11023 function_args_iterator args_iter;
11024 tree n = NULL_TREE, t;
11025
11026 if (!fntype)
11027 return false;
11028
11029 FOREACH_FUNCTION_ARGS(fntype, t, args_iter)
11030 {
11031 n = t;
11032 }
11033
11034 return n != NULL_TREE && n != void_type_node;
11035 }
11036
11037 /* Return true if TYPE has a prototype. */
11038
11039 bool
11040 prototype_p (tree fntype)
11041 {
11042 tree t;
11043
11044 gcc_assert (fntype != NULL_TREE);
11045
11046 t = TYPE_ARG_TYPES (fntype);
11047 return (t != NULL_TREE);
11048 }
11049
11050 /* If BLOCK is inlined from an __attribute__((__artificial__))
11051 routine, return pointer to location from where it has been
11052 called. */
11053 location_t *
11054 block_nonartificial_location (tree block)
11055 {
11056 location_t *ret = NULL;
11057
11058 while (block && TREE_CODE (block) == BLOCK
11059 && BLOCK_ABSTRACT_ORIGIN (block))
11060 {
11061 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
11062
11063 while (TREE_CODE (ao) == BLOCK
11064 && BLOCK_ABSTRACT_ORIGIN (ao)
11065 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
11066 ao = BLOCK_ABSTRACT_ORIGIN (ao);
11067
11068 if (TREE_CODE (ao) == FUNCTION_DECL)
11069 {
11070 /* If AO is an artificial inline, point RET to the
11071 call site locus at which it has been inlined and continue
11072 the loop, in case AO's caller is also an artificial
11073 inline. */
11074 if (DECL_DECLARED_INLINE_P (ao)
11075 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
11076 ret = &BLOCK_SOURCE_LOCATION (block);
11077 else
11078 break;
11079 }
11080 else if (TREE_CODE (ao) != BLOCK)
11081 break;
11082
11083 block = BLOCK_SUPERCONTEXT (block);
11084 }
11085 return ret;
11086 }
11087
11088
11089 /* If EXP is inlined from an __attribute__((__artificial__))
11090 function, return the location of the original call expression. */
11091
11092 location_t
11093 tree_nonartificial_location (tree exp)
11094 {
11095 location_t *loc = block_nonartificial_location (TREE_BLOCK (exp));
11096
11097 if (loc)
11098 return *loc;
11099 else
11100 return EXPR_LOCATION (exp);
11101 }
11102
11103
11104 /* These are the hash table functions for the hash table of OPTIMIZATION_NODEq
11105 nodes. */
11106
11107 /* Return the hash code code X, an OPTIMIZATION_NODE or TARGET_OPTION code. */
11108
11109 static hashval_t
11110 cl_option_hash_hash (const void *x)
11111 {
11112 const_tree const t = (const_tree) x;
11113 const char *p;
11114 size_t i;
11115 size_t len = 0;
11116 hashval_t hash = 0;
11117
11118 if (TREE_CODE (t) == OPTIMIZATION_NODE)
11119 {
11120 p = (const char *)TREE_OPTIMIZATION (t);
11121 len = sizeof (struct cl_optimization);
11122 }
11123
11124 else if (TREE_CODE (t) == TARGET_OPTION_NODE)
11125 {
11126 p = (const char *)TREE_TARGET_OPTION (t);
11127 len = sizeof (struct cl_target_option);
11128 }
11129
11130 else
11131 gcc_unreachable ();
11132
11133 /* assume most opt flags are just 0/1, some are 2-3, and a few might be
11134 something else. */
11135 for (i = 0; i < len; i++)
11136 if (p[i])
11137 hash = (hash << 4) ^ ((i << 2) | p[i]);
11138
11139 return hash;
11140 }
11141
11142 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
11143 TARGET_OPTION tree node) is the same as that given by *Y, which is the
11144 same. */
11145
11146 static int
11147 cl_option_hash_eq (const void *x, const void *y)
11148 {
11149 const_tree const xt = (const_tree) x;
11150 const_tree const yt = (const_tree) y;
11151 const char *xp;
11152 const char *yp;
11153 size_t len;
11154
11155 if (TREE_CODE (xt) != TREE_CODE (yt))
11156 return 0;
11157
11158 if (TREE_CODE (xt) == OPTIMIZATION_NODE)
11159 {
11160 xp = (const char *)TREE_OPTIMIZATION (xt);
11161 yp = (const char *)TREE_OPTIMIZATION (yt);
11162 len = sizeof (struct cl_optimization);
11163 }
11164
11165 else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
11166 {
11167 xp = (const char *)TREE_TARGET_OPTION (xt);
11168 yp = (const char *)TREE_TARGET_OPTION (yt);
11169 len = sizeof (struct cl_target_option);
11170 }
11171
11172 else
11173 gcc_unreachable ();
11174
11175 return (memcmp (xp, yp, len) == 0);
11176 }
11177
11178 /* Build an OPTIMIZATION_NODE based on the current options. */
11179
11180 tree
11181 build_optimization_node (void)
11182 {
11183 tree t;
11184 void **slot;
11185
11186 /* Use the cache of optimization nodes. */
11187
11188 cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node),
11189 &global_options);
11190
11191 slot = htab_find_slot (cl_option_hash_table, cl_optimization_node, INSERT);
11192 t = (tree) *slot;
11193 if (!t)
11194 {
11195 /* Insert this one into the hash table. */
11196 t = cl_optimization_node;
11197 *slot = t;
11198
11199 /* Make a new node for next time round. */
11200 cl_optimization_node = make_node (OPTIMIZATION_NODE);
11201 }
11202
11203 return t;
11204 }
11205
11206 /* Build a TARGET_OPTION_NODE based on the current options. */
11207
11208 tree
11209 build_target_option_node (void)
11210 {
11211 tree t;
11212 void **slot;
11213
11214 /* Use the cache of optimization nodes. */
11215
11216 cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node),
11217 &global_options);
11218
11219 slot = htab_find_slot (cl_option_hash_table, cl_target_option_node, INSERT);
11220 t = (tree) *slot;
11221 if (!t)
11222 {
11223 /* Insert this one into the hash table. */
11224 t = cl_target_option_node;
11225 *slot = t;
11226
11227 /* Make a new node for next time round. */
11228 cl_target_option_node = make_node (TARGET_OPTION_NODE);
11229 }
11230
11231 return t;
11232 }
11233
11234 /* Determine the "ultimate origin" of a block. The block may be an inlined
11235 instance of an inlined instance of a block which is local to an inline
11236 function, so we have to trace all of the way back through the origin chain
11237 to find out what sort of node actually served as the original seed for the
11238 given block. */
11239
11240 tree
11241 block_ultimate_origin (const_tree block)
11242 {
11243 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
11244
11245 /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
11246 nodes in the function to point to themselves; ignore that if
11247 we're trying to output the abstract instance of this function. */
11248 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
11249 return NULL_TREE;
11250
11251 if (immediate_origin == NULL_TREE)
11252 return NULL_TREE;
11253 else
11254 {
11255 tree ret_val;
11256 tree lookahead = immediate_origin;
11257
11258 do
11259 {
11260 ret_val = lookahead;
11261 lookahead = (TREE_CODE (ret_val) == BLOCK
11262 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
11263 }
11264 while (lookahead != NULL && lookahead != ret_val);
11265
11266 /* The block's abstract origin chain may not be the *ultimate* origin of
11267 the block. It could lead to a DECL that has an abstract origin set.
11268 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
11269 will give us if it has one). Note that DECL's abstract origins are
11270 supposed to be the most distant ancestor (or so decl_ultimate_origin
11271 claims), so we don't need to loop following the DECL origins. */
11272 if (DECL_P (ret_val))
11273 return DECL_ORIGIN (ret_val);
11274
11275 return ret_val;
11276 }
11277 }
11278
11279 /* Return true if T1 and T2 are equivalent lists. */
11280
11281 bool
11282 list_equal_p (const_tree t1, const_tree t2)
11283 {
11284 for (; t1 && t2; t1 = TREE_CHAIN (t1) , t2 = TREE_CHAIN (t2))
11285 if (TREE_VALUE (t1) != TREE_VALUE (t2))
11286 return false;
11287 return !t1 && !t2;
11288 }
11289
11290 /* Return true iff conversion in EXP generates no instruction. Mark
11291 it inline so that we fully inline into the stripping functions even
11292 though we have two uses of this function. */
11293
11294 static inline bool
11295 tree_nop_conversion (const_tree exp)
11296 {
11297 tree outer_type, inner_type;
11298
11299 if (!CONVERT_EXPR_P (exp)
11300 && TREE_CODE (exp) != NON_LVALUE_EXPR)
11301 return false;
11302 if (TREE_OPERAND (exp, 0) == error_mark_node)
11303 return false;
11304
11305 outer_type = TREE_TYPE (exp);
11306 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11307
11308 if (!inner_type)
11309 return false;
11310
11311 /* Use precision rather then machine mode when we can, which gives
11312 the correct answer even for submode (bit-field) types. */
11313 if ((INTEGRAL_TYPE_P (outer_type)
11314 || POINTER_TYPE_P (outer_type)
11315 || TREE_CODE (outer_type) == OFFSET_TYPE)
11316 && (INTEGRAL_TYPE_P (inner_type)
11317 || POINTER_TYPE_P (inner_type)
11318 || TREE_CODE (inner_type) == OFFSET_TYPE))
11319 return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
11320
11321 /* Otherwise fall back on comparing machine modes (e.g. for
11322 aggregate types, floats). */
11323 return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
11324 }
11325
11326 /* Return true iff conversion in EXP generates no instruction. Don't
11327 consider conversions changing the signedness. */
11328
11329 static bool
11330 tree_sign_nop_conversion (const_tree exp)
11331 {
11332 tree outer_type, inner_type;
11333
11334 if (!tree_nop_conversion (exp))
11335 return false;
11336
11337 outer_type = TREE_TYPE (exp);
11338 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
11339
11340 return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
11341 && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
11342 }
11343
11344 /* Strip conversions from EXP according to tree_nop_conversion and
11345 return the resulting expression. */
11346
11347 tree
11348 tree_strip_nop_conversions (tree exp)
11349 {
11350 while (tree_nop_conversion (exp))
11351 exp = TREE_OPERAND (exp, 0);
11352 return exp;
11353 }
11354
11355 /* Strip conversions from EXP according to tree_sign_nop_conversion
11356 and return the resulting expression. */
11357
11358 tree
11359 tree_strip_sign_nop_conversions (tree exp)
11360 {
11361 while (tree_sign_nop_conversion (exp))
11362 exp = TREE_OPERAND (exp, 0);
11363 return exp;
11364 }
11365
11366 /* Avoid any floating point extensions from EXP. */
11367 tree
11368 strip_float_extensions (tree exp)
11369 {
11370 tree sub, expt, subt;
11371
11372 /* For floating point constant look up the narrowest type that can hold
11373 it properly and handle it like (type)(narrowest_type)constant.
11374 This way we can optimize for instance a=a*2.0 where "a" is float
11375 but 2.0 is double constant. */
11376 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
11377 {
11378 REAL_VALUE_TYPE orig;
11379 tree type = NULL;
11380
11381 orig = TREE_REAL_CST (exp);
11382 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
11383 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
11384 type = float_type_node;
11385 else if (TYPE_PRECISION (TREE_TYPE (exp))
11386 > TYPE_PRECISION (double_type_node)
11387 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
11388 type = double_type_node;
11389 if (type)
11390 return build_real (type, real_value_truncate (TYPE_MODE (type), orig));
11391 }
11392
11393 if (!CONVERT_EXPR_P (exp))
11394 return exp;
11395
11396 sub = TREE_OPERAND (exp, 0);
11397 subt = TREE_TYPE (sub);
11398 expt = TREE_TYPE (exp);
11399
11400 if (!FLOAT_TYPE_P (subt))
11401 return exp;
11402
11403 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
11404 return exp;
11405
11406 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
11407 return exp;
11408
11409 return strip_float_extensions (sub);
11410 }
11411
11412 /* Strip out all handled components that produce invariant
11413 offsets. */
11414
11415 const_tree
11416 strip_invariant_refs (const_tree op)
11417 {
11418 while (handled_component_p (op))
11419 {
11420 switch (TREE_CODE (op))
11421 {
11422 case ARRAY_REF:
11423 case ARRAY_RANGE_REF:
11424 if (!is_gimple_constant (TREE_OPERAND (op, 1))
11425 || TREE_OPERAND (op, 2) != NULL_TREE
11426 || TREE_OPERAND (op, 3) != NULL_TREE)
11427 return NULL;
11428 break;
11429
11430 case COMPONENT_REF:
11431 if (TREE_OPERAND (op, 2) != NULL_TREE)
11432 return NULL;
11433 break;
11434
11435 default:;
11436 }
11437 op = TREE_OPERAND (op, 0);
11438 }
11439
11440 return op;
11441 }
11442
11443 static GTY(()) tree gcc_eh_personality_decl;
11444
11445 /* Return the GCC personality function decl. */
11446
11447 tree
11448 lhd_gcc_personality (void)
11449 {
11450 if (!gcc_eh_personality_decl)
11451 gcc_eh_personality_decl = build_personality_function ("gcc");
11452 return gcc_eh_personality_decl;
11453 }
11454
11455 /* Try to find a base info of BINFO that would have its field decl at offset
11456 OFFSET within the BINFO type and which is of EXPECTED_TYPE. If it can be
11457 found, return, otherwise return NULL_TREE. */
11458
11459 tree
11460 get_binfo_at_offset (tree binfo, HOST_WIDE_INT offset, tree expected_type)
11461 {
11462 tree type = BINFO_TYPE (binfo);
11463
11464 while (true)
11465 {
11466 HOST_WIDE_INT pos, size;
11467 tree fld;
11468 int i;
11469
11470 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (expected_type))
11471 return binfo;
11472 if (offset < 0)
11473 return NULL_TREE;
11474
11475 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
11476 {
11477 if (TREE_CODE (fld) != FIELD_DECL)
11478 continue;
11479
11480 pos = int_bit_position (fld);
11481 size = tree_low_cst (DECL_SIZE (fld), 1);
11482 if (pos <= offset && (pos + size) > offset)
11483 break;
11484 }
11485 if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE)
11486 return NULL_TREE;
11487
11488 if (!DECL_ARTIFICIAL (fld))
11489 {
11490 binfo = TYPE_BINFO (TREE_TYPE (fld));
11491 if (!binfo)
11492 return NULL_TREE;
11493 }
11494 /* Offset 0 indicates the primary base, whose vtable contents are
11495 represented in the binfo for the derived class. */
11496 else if (offset != 0)
11497 {
11498 tree base_binfo, found_binfo = NULL_TREE;
11499 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
11500 if (TREE_TYPE (base_binfo) == TREE_TYPE (fld))
11501 {
11502 found_binfo = base_binfo;
11503 break;
11504 }
11505 if (!found_binfo)
11506 return NULL_TREE;
11507 binfo = found_binfo;
11508 }
11509
11510 type = TREE_TYPE (fld);
11511 offset -= pos;
11512 }
11513 }
11514
11515 /* Returns true if X is a typedef decl. */
11516
11517 bool
11518 is_typedef_decl (tree x)
11519 {
11520 return (x && TREE_CODE (x) == TYPE_DECL
11521 && DECL_ORIGINAL_TYPE (x) != NULL_TREE);
11522 }
11523
11524 /* Returns true iff TYPE is a type variant created for a typedef. */
11525
11526 bool
11527 typedef_variant_p (tree type)
11528 {
11529 return is_typedef_decl (TYPE_NAME (type));
11530 }
11531
11532 /* Warn about a use of an identifier which was marked deprecated. */
11533 void
11534 warn_deprecated_use (tree node, tree attr)
11535 {
11536 const char *msg;
11537
11538 if (node == 0 || !warn_deprecated_decl)
11539 return;
11540
11541 if (!attr)
11542 {
11543 if (DECL_P (node))
11544 attr = DECL_ATTRIBUTES (node);
11545 else if (TYPE_P (node))
11546 {
11547 tree decl = TYPE_STUB_DECL (node);
11548 if (decl)
11549 attr = lookup_attribute ("deprecated",
11550 TYPE_ATTRIBUTES (TREE_TYPE (decl)));
11551 }
11552 }
11553
11554 if (attr)
11555 attr = lookup_attribute ("deprecated", attr);
11556
11557 if (attr)
11558 msg = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr)));
11559 else
11560 msg = NULL;
11561
11562 if (DECL_P (node))
11563 {
11564 expanded_location xloc = expand_location (DECL_SOURCE_LOCATION (node));
11565 if (msg)
11566 warning (OPT_Wdeprecated_declarations,
11567 "%qD is deprecated (declared at %s:%d): %s",
11568 node, xloc.file, xloc.line, msg);
11569 else
11570 warning (OPT_Wdeprecated_declarations,
11571 "%qD is deprecated (declared at %s:%d)",
11572 node, xloc.file, xloc.line);
11573 }
11574 else if (TYPE_P (node))
11575 {
11576 tree what = NULL_TREE;
11577 tree decl = TYPE_STUB_DECL (node);
11578
11579 if (TYPE_NAME (node))
11580 {
11581 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
11582 what = TYPE_NAME (node);
11583 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
11584 && DECL_NAME (TYPE_NAME (node)))
11585 what = DECL_NAME (TYPE_NAME (node));
11586 }
11587
11588 if (decl)
11589 {
11590 expanded_location xloc
11591 = expand_location (DECL_SOURCE_LOCATION (decl));
11592 if (what)
11593 {
11594 if (msg)
11595 warning (OPT_Wdeprecated_declarations,
11596 "%qE is deprecated (declared at %s:%d): %s",
11597 what, xloc.file, xloc.line, msg);
11598 else
11599 warning (OPT_Wdeprecated_declarations,
11600 "%qE is deprecated (declared at %s:%d)", what,
11601 xloc.file, xloc.line);
11602 }
11603 else
11604 {
11605 if (msg)
11606 warning (OPT_Wdeprecated_declarations,
11607 "type is deprecated (declared at %s:%d): %s",
11608 xloc.file, xloc.line, msg);
11609 else
11610 warning (OPT_Wdeprecated_declarations,
11611 "type is deprecated (declared at %s:%d)",
11612 xloc.file, xloc.line);
11613 }
11614 }
11615 else
11616 {
11617 if (what)
11618 {
11619 if (msg)
11620 warning (OPT_Wdeprecated_declarations, "%qE is deprecated: %s",
11621 what, msg);
11622 else
11623 warning (OPT_Wdeprecated_declarations, "%qE is deprecated", what);
11624 }
11625 else
11626 {
11627 if (msg)
11628 warning (OPT_Wdeprecated_declarations, "type is deprecated: %s",
11629 msg);
11630 else
11631 warning (OPT_Wdeprecated_declarations, "type is deprecated");
11632 }
11633 }
11634 }
11635 }
11636
11637 #include "gt-tree.h"