ipa-free-lang-data.cc revision 1.1 1 /* Pass to free or clear language-specific data structures from
2 the IL before they reach the middle end.
3
4 Copyright (C) 1987-2022 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 can occasionally
30 calls language-dependent routines. */
31
32 #include "config.h"
33 #include "system.h"
34 #include "coretypes.h"
35 #include "backend.h"
36 #include "target.h"
37 #include "tree.h"
38 #include "gimple.h"
39 #include "tree-pass.h"
40 #include "ssa.h"
41 #include "cgraph.h"
42 #include "diagnostic.h"
43 #include "alias.h"
44 #include "attribs.h"
45 #include "langhooks.h"
46 #include "gimple-iterator.h"
47 #include "langhooks-def.h"
48 #include "tree-diagnostic.h"
49 #include "except.h"
50 #include "ipa-utils.h"
51
52 namespace {
53
54 /* Data used when collecting DECLs and TYPEs for language data removal. */
55
56 class free_lang_data_d
57 {
58 public:
59 free_lang_data_d () : decls (100), types (100) {}
60
61 /* Worklist to avoid excessive recursion. */
62 auto_vec<tree> worklist;
63
64 /* Set of traversed objects. Used to avoid duplicate visits. */
65 hash_set<tree> pset;
66
67 /* Array of symbols to process with free_lang_data_in_decl. */
68 auto_vec<tree> decls;
69
70 /* Array of types to process with free_lang_data_in_type. */
71 auto_vec<tree> types;
72 };
73
74
75 /* Add type or decl T to one of the list of tree nodes that need their
76 language data removed. The lists are held inside FLD. */
77
78 static void
79 add_tree_to_fld_list (tree t, class free_lang_data_d *fld)
80 {
81 if (DECL_P (t))
82 fld->decls.safe_push (t);
83 else if (TYPE_P (t))
84 fld->types.safe_push (t);
85 else
86 gcc_unreachable ();
87 }
88
89 /* Push tree node T into FLD->WORKLIST. */
90
91 static inline void
92 fld_worklist_push (tree t, class free_lang_data_d *fld)
93 {
94 if (t && !is_lang_specific (t) && !fld->pset.contains (t))
95 fld->worklist.safe_push ((t));
96 }
97
98
99
100 /* Return simplified TYPE_NAME of TYPE. */
102
103 static tree
104 fld_simplified_type_name (tree type)
105 {
106 if (!TYPE_NAME (type) || TREE_CODE (TYPE_NAME (type)) != TYPE_DECL)
107 return TYPE_NAME (type);
108 /* Drop TYPE_DECLs in TYPE_NAME in favor of the identifier in the
109 TYPE_DECL if the type doesn't have linkage.
110 this must match fld_ */
111 if (type != TYPE_MAIN_VARIANT (type)
112 || (!DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (type))
113 && (TREE_CODE (type) != RECORD_TYPE
114 || !TYPE_BINFO (type)
115 || !BINFO_VTABLE (TYPE_BINFO (type)))))
116 return DECL_NAME (TYPE_NAME (type));
117 return TYPE_NAME (type);
118 }
119
120 /* Do same comparsion as check_qualified_type skipping lang part of type
121 and be more permissive about type names: we only care that names are
122 same (for diagnostics) and that ODR names are the same.
123 If INNER_TYPE is non-NULL, be sure that TREE_TYPE match it. */
124
125 static bool
126 fld_type_variant_equal_p (tree t, tree v, tree inner_type)
127 {
128 if (TYPE_QUALS (t) != TYPE_QUALS (v)
129 /* We want to match incomplete variants with complete types.
130 In this case we need to ignore alignment. */
131 || ((!RECORD_OR_UNION_TYPE_P (t) || COMPLETE_TYPE_P (v))
132 && (TYPE_ALIGN (t) != TYPE_ALIGN (v)
133 || TYPE_USER_ALIGN (t) != TYPE_USER_ALIGN (v)))
134 || fld_simplified_type_name (t) != fld_simplified_type_name (v)
135 || !attribute_list_equal (TYPE_ATTRIBUTES (t),
136 TYPE_ATTRIBUTES (v))
137 || (inner_type && TREE_TYPE (v) != inner_type))
138 return false;
139
140 return true;
141 }
142
143 /* Find variant of FIRST that match T and create new one if necessary.
144 Set TREE_TYPE to INNER_TYPE if non-NULL. */
145
146 static tree
147 fld_type_variant (tree first, tree t, class free_lang_data_d *fld,
148 tree inner_type = NULL)
149 {
150 if (first == TYPE_MAIN_VARIANT (t))
151 return t;
152 for (tree v = first; v; v = TYPE_NEXT_VARIANT (v))
153 if (fld_type_variant_equal_p (t, v, inner_type))
154 return v;
155 tree v = build_variant_type_copy (first);
156 TYPE_READONLY (v) = TYPE_READONLY (t);
157 TYPE_VOLATILE (v) = TYPE_VOLATILE (t);
158 TYPE_ATOMIC (v) = TYPE_ATOMIC (t);
159 TYPE_RESTRICT (v) = TYPE_RESTRICT (t);
160 TYPE_ADDR_SPACE (v) = TYPE_ADDR_SPACE (t);
161 TYPE_NAME (v) = TYPE_NAME (t);
162 TYPE_ATTRIBUTES (v) = TYPE_ATTRIBUTES (t);
163 TYPE_CANONICAL (v) = TYPE_CANONICAL (t);
164 /* Variants of incomplete types should have alignment
165 set to BITS_PER_UNIT. Do not copy the actual alignment. */
166 if (!RECORD_OR_UNION_TYPE_P (v) || COMPLETE_TYPE_P (v))
167 {
168 SET_TYPE_ALIGN (v, TYPE_ALIGN (t));
169 TYPE_USER_ALIGN (v) = TYPE_USER_ALIGN (t);
170 }
171 if (inner_type)
172 TREE_TYPE (v) = inner_type;
173 gcc_checking_assert (fld_type_variant_equal_p (t,v, inner_type));
174 if (!fld->pset.add (v))
175 add_tree_to_fld_list (v, fld);
176 return v;
177 }
178
179 /* Map complete types to incomplete types. */
180
181 static hash_map<tree, tree> *fld_incomplete_types;
182
183 /* Map types to simplified types. */
184
185 static hash_map<tree, tree> *fld_simplified_types;
186
187 /* Produce variant of T whose TREE_TYPE is T2. If it is main variant,
188 use MAP to prevent duplicates. */
189
190 static tree
191 fld_process_array_type (tree t, tree t2, hash_map<tree, tree> *map,
192 class free_lang_data_d *fld)
193 {
194 if (TREE_TYPE (t) == t2)
195 return t;
196
197 if (TYPE_MAIN_VARIANT (t) != t)
198 {
199 return fld_type_variant
200 (fld_process_array_type (TYPE_MAIN_VARIANT (t),
201 TYPE_MAIN_VARIANT (t2), map, fld),
202 t, fld, t2);
203 }
204
205 bool existed;
206 tree &array
207 = map->get_or_insert (t, &existed);
208 if (!existed)
209 {
210 array
211 = build_array_type_1 (t2, TYPE_DOMAIN (t), TYPE_TYPELESS_STORAGE (t),
212 false, false);
213 TYPE_CANONICAL (array) = TYPE_CANONICAL (t);
214 if (!fld->pset.add (array))
215 add_tree_to_fld_list (array, fld);
216 }
217 return array;
218 }
219
220 /* Return CTX after removal of contexts that are not relevant */
221
222 static tree
223 fld_decl_context (tree ctx)
224 {
225 /* Variably modified types are needed for tree_is_indexable to decide
226 whether the type needs to go to local or global section.
227 This code is semi-broken but for now it is easiest to keep contexts
228 as expected. */
229 if (ctx && TYPE_P (ctx)
230 && !variably_modified_type_p (ctx, NULL_TREE))
231 {
232 while (ctx && TYPE_P (ctx))
233 ctx = TYPE_CONTEXT (ctx);
234 }
235 return ctx;
236 }
237
238 /* For T being aggregate type try to turn it into a incomplete variant.
239 Return T if no simplification is possible. */
240
241 static tree
242 fld_incomplete_type_of (tree t, class free_lang_data_d *fld)
243 {
244 if (!t)
245 return NULL;
246 if (POINTER_TYPE_P (t))
247 {
248 tree t2 = fld_incomplete_type_of (TREE_TYPE (t), fld);
249 if (t2 != TREE_TYPE (t))
250 {
251 tree first;
252 if (TREE_CODE (t) == POINTER_TYPE)
253 first = build_pointer_type_for_mode (t2, TYPE_MODE (t),
254 TYPE_REF_CAN_ALIAS_ALL (t));
255 else
256 first = build_reference_type_for_mode (t2, TYPE_MODE (t),
257 TYPE_REF_CAN_ALIAS_ALL (t));
258 gcc_assert (TYPE_CANONICAL (t2) != t2
259 && TYPE_CANONICAL (t2) == TYPE_CANONICAL (TREE_TYPE (t)));
260 if (!fld->pset.add (first))
261 add_tree_to_fld_list (first, fld);
262 return fld_type_variant (first, t, fld);
263 }
264 return t;
265 }
266 if (TREE_CODE (t) == ARRAY_TYPE)
267 return fld_process_array_type (t,
268 fld_incomplete_type_of (TREE_TYPE (t), fld),
269 fld_incomplete_types, fld);
270 if ((!RECORD_OR_UNION_TYPE_P (t) && TREE_CODE (t) != ENUMERAL_TYPE)
271 || !COMPLETE_TYPE_P (t))
272 return t;
273 if (TYPE_MAIN_VARIANT (t) == t)
274 {
275 bool existed;
276 tree ©
277 = fld_incomplete_types->get_or_insert (t, &existed);
278
279 if (!existed)
280 {
281 copy = build_distinct_type_copy (t);
282
283 /* It is possible that type was not seen by free_lang_data yet. */
284 if (!fld->pset.add (copy))
285 add_tree_to_fld_list (copy, fld);
286 TYPE_SIZE (copy) = NULL;
287 TYPE_USER_ALIGN (copy) = 0;
288 TYPE_SIZE_UNIT (copy) = NULL;
289 TYPE_CANONICAL (copy) = TYPE_CANONICAL (t);
290 TREE_ADDRESSABLE (copy) = 0;
291 if (AGGREGATE_TYPE_P (t))
292 {
293 SET_TYPE_MODE (copy, VOIDmode);
294 SET_TYPE_ALIGN (copy, BITS_PER_UNIT);
295 TYPE_TYPELESS_STORAGE (copy) = 0;
296 TYPE_FIELDS (copy) = NULL;
297 TYPE_BINFO (copy) = NULL;
298 TYPE_FINAL_P (copy) = 0;
299 TYPE_EMPTY_P (copy) = 0;
300 }
301 else
302 {
303 TYPE_VALUES (copy) = NULL;
304 ENUM_IS_OPAQUE (copy) = 0;
305 ENUM_IS_SCOPED (copy) = 0;
306 }
307
308 /* Build copy of TYPE_DECL in TYPE_NAME if necessary.
309 This is needed for ODR violation warnings to come out right (we
310 want duplicate TYPE_DECLs whenever the type is duplicated because
311 of ODR violation. Because lang data in the TYPE_DECL may not
312 have been freed yet, rebuild it from scratch and copy relevant
313 fields. */
314 TYPE_NAME (copy) = fld_simplified_type_name (copy);
315 tree name = TYPE_NAME (copy);
316
317 if (name && TREE_CODE (name) == TYPE_DECL)
318 {
319 gcc_checking_assert (TREE_TYPE (name) == t);
320 tree name2 = build_decl (DECL_SOURCE_LOCATION (name), TYPE_DECL,
321 DECL_NAME (name), copy);
322 if (DECL_ASSEMBLER_NAME_SET_P (name))
323 SET_DECL_ASSEMBLER_NAME (name2, DECL_ASSEMBLER_NAME (name));
324 SET_DECL_ALIGN (name2, 0);
325 DECL_CONTEXT (name2) = fld_decl_context
326 (DECL_CONTEXT (name));
327 TYPE_NAME (copy) = name2;
328 }
329 }
330 return copy;
331 }
332 return (fld_type_variant
333 (fld_incomplete_type_of (TYPE_MAIN_VARIANT (t), fld), t, fld));
334 }
335
336 /* Simplify type T for scenarios where we do not need complete pointer
337 types. */
338
339 static tree
340 fld_simplified_type (tree t, class free_lang_data_d *fld)
341 {
342 if (!t)
343 return t;
344 if (POINTER_TYPE_P (t))
345 return fld_incomplete_type_of (t, fld);
346 /* FIXME: This triggers verification error, see PR88140. */
347 #if 0
348 if (TREE_CODE (t) == ARRAY_TYPE)
349 return fld_process_array_type (t, fld_simplified_type (TREE_TYPE (t), fld),
350 fld_simplified_types, fld);
351 #endif
352 return t;
353 }
354
355 /* Reset the expression *EXPR_P, a size or position.
356
357 ??? We could reset all non-constant sizes or positions. But it's cheap
358 enough to not do so and refrain from adding workarounds to dwarf2out.cc.
359
360 We need to reset self-referential sizes or positions because they cannot
361 be gimplified and thus can contain a CALL_EXPR after the gimplification
362 is finished, which will run afoul of LTO streaming. And they need to be
363 reset to something essentially dummy but not constant, so as to preserve
364 the properties of the object they are attached to. */
365
366 static inline void
367 free_lang_data_in_one_sizepos (tree *expr_p)
368 {
369 tree expr = *expr_p;
370 if (CONTAINS_PLACEHOLDER_P (expr))
371 *expr_p = build0 (PLACEHOLDER_EXPR, TREE_TYPE (expr));
372 }
373
374
375 /* Reset all the fields in a binfo node BINFO. We only keep
376 BINFO_VTABLE, which is used by gimple_fold_obj_type_ref. */
377
378 static void
379 free_lang_data_in_binfo (tree binfo)
380 {
381 unsigned i;
382 tree t;
383
384 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
385
386 BINFO_VIRTUALS (binfo) = NULL_TREE;
387 BINFO_BASE_ACCESSES (binfo) = NULL;
388 BINFO_INHERITANCE_CHAIN (binfo) = NULL_TREE;
389 BINFO_SUBVTT_INDEX (binfo) = NULL_TREE;
390 BINFO_VPTR_FIELD (binfo) = NULL_TREE;
391 TREE_PUBLIC (binfo) = 0;
392
393 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (binfo), i, t)
394 free_lang_data_in_binfo (t);
395 }
396
397
398 /* Reset all language specific information still present in TYPE. */
399
400 static void
401 free_lang_data_in_type (tree type, class free_lang_data_d *fld)
402 {
403 gcc_assert (TYPE_P (type));
404
405 /* Give the FE a chance to remove its own data first. */
406 lang_hooks.free_lang_data (type);
407
408 TREE_LANG_FLAG_0 (type) = 0;
409 TREE_LANG_FLAG_1 (type) = 0;
410 TREE_LANG_FLAG_2 (type) = 0;
411 TREE_LANG_FLAG_3 (type) = 0;
412 TREE_LANG_FLAG_4 (type) = 0;
413 TREE_LANG_FLAG_5 (type) = 0;
414 TREE_LANG_FLAG_6 (type) = 0;
415
416 TYPE_NEEDS_CONSTRUCTING (type) = 0;
417
418 /* Purge non-marked variants from the variants chain, so that they
419 don't reappear in the IL after free_lang_data. */
420 while (TYPE_NEXT_VARIANT (type)
421 && !fld->pset.contains (TYPE_NEXT_VARIANT (type)))
422 {
423 tree t = TYPE_NEXT_VARIANT (type);
424 TYPE_NEXT_VARIANT (type) = TYPE_NEXT_VARIANT (t);
425 /* Turn the removed types into distinct types. */
426 TYPE_MAIN_VARIANT (t) = t;
427 TYPE_NEXT_VARIANT (t) = NULL_TREE;
428 }
429
430 if (TREE_CODE (type) == FUNCTION_TYPE)
431 {
432 TREE_TYPE (type) = fld_simplified_type (TREE_TYPE (type), fld);
433 /* Remove the const and volatile qualifiers from arguments. The
434 C++ front end removes them, but the C front end does not,
435 leading to false ODR violation errors when merging two
436 instances of the same function signature compiled by
437 different front ends. */
438 for (tree p = TYPE_ARG_TYPES (type); p; p = TREE_CHAIN (p))
439 {
440 tree arg_type = TREE_VALUE (p);
441 if (TYPE_READONLY (arg_type) || TYPE_VOLATILE (arg_type))
442 {
443 int quals = TYPE_QUALS (arg_type)
444 & ~TYPE_QUAL_CONST
445 & ~TYPE_QUAL_VOLATILE;
446 TREE_VALUE (p) = build_qualified_type (arg_type, quals);
447 if (!fld->pset.add (TREE_VALUE (p)))
448 free_lang_data_in_type (TREE_VALUE (p), fld);
449 }
450 TREE_VALUE (p) = fld_simplified_type (TREE_VALUE (p), fld);
451 /* C++ FE uses TREE_PURPOSE to store initial values. */
452 TREE_PURPOSE (p) = NULL;
453 }
454 }
455 else if (TREE_CODE (type) == METHOD_TYPE)
456 {
457 TREE_TYPE (type) = fld_simplified_type (TREE_TYPE (type), fld);
458 for (tree p = TYPE_ARG_TYPES (type); p; p = TREE_CHAIN (p))
459 {
460 /* C++ FE uses TREE_PURPOSE to store initial values. */
461 TREE_VALUE (p) = fld_simplified_type (TREE_VALUE (p), fld);
462 TREE_PURPOSE (p) = NULL;
463 }
464 }
465 else if (RECORD_OR_UNION_TYPE_P (type))
466 {
467 /* Remove members that are not FIELD_DECLs from the field list
468 of an aggregate. These occur in C++. */
469 for (tree *prev = &TYPE_FIELDS (type), member; (member = *prev);)
470 if (TREE_CODE (member) == FIELD_DECL)
471 prev = &DECL_CHAIN (member);
472 else
473 *prev = DECL_CHAIN (member);
474
475 TYPE_VFIELD (type) = NULL_TREE;
476
477 if (TYPE_BINFO (type))
478 {
479 free_lang_data_in_binfo (TYPE_BINFO (type));
480 /* We need to preserve link to bases and virtual table for all
481 polymorphic types to make devirtualization machinery working. */
482 if (!BINFO_VTABLE (TYPE_BINFO (type)))
483 TYPE_BINFO (type) = NULL;
484 }
485 }
486 else if (INTEGRAL_TYPE_P (type)
487 || SCALAR_FLOAT_TYPE_P (type)
488 || FIXED_POINT_TYPE_P (type))
489 {
490 if (TREE_CODE (type) == ENUMERAL_TYPE)
491 {
492 ENUM_IS_OPAQUE (type) = 0;
493 ENUM_IS_SCOPED (type) = 0;
494 /* Type values are used only for C++ ODR checking. Drop them
495 for all type variants and non-ODR types.
496 For ODR types the data is freed in free_odr_warning_data. */
497 if (!TYPE_VALUES (type))
498 ;
499 else if (TYPE_MAIN_VARIANT (type) != type
500 || !type_with_linkage_p (type)
501 || type_in_anonymous_namespace_p (type))
502 TYPE_VALUES (type) = NULL;
503 else
504 register_odr_enum (type);
505 }
506 free_lang_data_in_one_sizepos (&TYPE_MIN_VALUE (type));
507 free_lang_data_in_one_sizepos (&TYPE_MAX_VALUE (type));
508 }
509
510 TYPE_LANG_SLOT_1 (type) = NULL_TREE;
511
512 free_lang_data_in_one_sizepos (&TYPE_SIZE (type));
513 free_lang_data_in_one_sizepos (&TYPE_SIZE_UNIT (type));
514
515 if (TYPE_CONTEXT (type)
516 && TREE_CODE (TYPE_CONTEXT (type)) == BLOCK)
517 {
518 tree ctx = TYPE_CONTEXT (type);
519 do
520 {
521 ctx = BLOCK_SUPERCONTEXT (ctx);
522 }
523 while (ctx && TREE_CODE (ctx) == BLOCK);
524 TYPE_CONTEXT (type) = ctx;
525 }
526
527 TYPE_STUB_DECL (type) = NULL;
528 TYPE_NAME (type) = fld_simplified_type_name (type);
529 }
530
531 /* Reset all language specific information still present in symbol
532 DECL. */
533
534 static void
535 free_lang_data_in_decl (tree decl, class free_lang_data_d *fld)
536 {
537 gcc_assert (DECL_P (decl));
538
539 /* Give the FE a chance to remove its own data first. */
540 lang_hooks.free_lang_data (decl);
541
542 TREE_LANG_FLAG_0 (decl) = 0;
543 TREE_LANG_FLAG_1 (decl) = 0;
544 TREE_LANG_FLAG_2 (decl) = 0;
545 TREE_LANG_FLAG_3 (decl) = 0;
546 TREE_LANG_FLAG_4 (decl) = 0;
547 TREE_LANG_FLAG_5 (decl) = 0;
548 TREE_LANG_FLAG_6 (decl) = 0;
549
550 free_lang_data_in_one_sizepos (&DECL_SIZE (decl));
551 free_lang_data_in_one_sizepos (&DECL_SIZE_UNIT (decl));
552 if (TREE_CODE (decl) == FIELD_DECL)
553 {
554 DECL_FCONTEXT (decl) = NULL;
555 free_lang_data_in_one_sizepos (&DECL_FIELD_OFFSET (decl));
556 if (TREE_CODE (DECL_CONTEXT (decl)) == QUAL_UNION_TYPE)
557 DECL_QUALIFIER (decl) = NULL_TREE;
558 }
559
560 if (TREE_CODE (decl) == FUNCTION_DECL)
561 {
562 struct cgraph_node *node;
563 /* Frontends do not set TREE_ADDRESSABLE on public variables even though
564 the address may be taken in other unit, so this flag has no practical
565 use for middle-end.
566
567 It would make more sense if frontends set TREE_ADDRESSABLE to 0 only
568 for public objects that indeed cannot be adressed, but it is not
569 the case. Set the flag to true so we do not get merge failures for
570 i.e. virtual tables between units that take address of it and
571 units that don't. */
572 if (TREE_PUBLIC (decl))
573 TREE_ADDRESSABLE (decl) = true;
574 TREE_TYPE (decl) = fld_simplified_type (TREE_TYPE (decl), fld);
575 if (!(node = cgraph_node::get (decl))
576 || (!node->definition && !node->clones))
577 {
578 if (node && !node->declare_variant_alt)
579 node->release_body ();
580 else
581 {
582 release_function_body (decl);
583 DECL_ARGUMENTS (decl) = NULL;
584 DECL_RESULT (decl) = NULL;
585 DECL_INITIAL (decl) = error_mark_node;
586 }
587 }
588 if (gimple_has_body_p (decl) || (node && node->thunk))
589 {
590 tree t;
591
592 /* If DECL has a gimple body, then the context for its
593 arguments must be DECL. Otherwise, it doesn't really
594 matter, as we will not be emitting any code for DECL. In
595 general, there may be other instances of DECL created by
596 the front end and since PARM_DECLs are generally shared,
597 their DECL_CONTEXT changes as the replicas of DECL are
598 created. The only time where DECL_CONTEXT is important
599 is for the FUNCTION_DECLs that have a gimple body (since
600 the PARM_DECL will be used in the function's body). */
601 for (t = DECL_ARGUMENTS (decl); t; t = TREE_CHAIN (t))
602 DECL_CONTEXT (t) = decl;
603 if (!DECL_FUNCTION_SPECIFIC_TARGET (decl))
604 DECL_FUNCTION_SPECIFIC_TARGET (decl)
605 = target_option_default_node;
606 if (!DECL_FUNCTION_SPECIFIC_OPTIMIZATION (decl))
607 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (decl)
608 = optimization_default_node;
609 }
610
611 /* DECL_SAVED_TREE holds the GENERIC representation for DECL.
612 At this point, it is not needed anymore. */
613 DECL_SAVED_TREE (decl) = NULL_TREE;
614
615 /* Clear the abstract origin if it refers to a method.
616 Otherwise dwarf2out.cc will ICE as we splice functions out of
617 TYPE_FIELDS and thus the origin will not be output
618 correctly. */
619 if (DECL_ABSTRACT_ORIGIN (decl)
620 && DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))
621 && RECORD_OR_UNION_TYPE_P
622 (DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))))
623 DECL_ABSTRACT_ORIGIN (decl) = NULL_TREE;
624
625 DECL_VINDEX (decl) = NULL_TREE;
626 }
627 else if (VAR_P (decl))
628 {
629 /* See comment above why we set the flag for functions. */
630 if (TREE_PUBLIC (decl))
631 TREE_ADDRESSABLE (decl) = true;
632 if ((DECL_EXTERNAL (decl)
633 && (!TREE_STATIC (decl) || !TREE_READONLY (decl)))
634 || (decl_function_context (decl) && !TREE_STATIC (decl)))
635 DECL_INITIAL (decl) = NULL_TREE;
636 }
637 else if (TREE_CODE (decl) == TYPE_DECL)
638 {
639 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
640 DECL_VISIBILITY_SPECIFIED (decl) = 0;
641 TREE_PUBLIC (decl) = 0;
642 TREE_PRIVATE (decl) = 0;
643 DECL_ARTIFICIAL (decl) = 0;
644 TYPE_DECL_SUPPRESS_DEBUG (decl) = 0;
645 DECL_INITIAL (decl) = NULL_TREE;
646 DECL_ORIGINAL_TYPE (decl) = NULL_TREE;
647 DECL_MODE (decl) = VOIDmode;
648 SET_DECL_ALIGN (decl, 0);
649 /* TREE_TYPE is cleared at WPA time in free_odr_warning_data. */
650 }
651 else if (TREE_CODE (decl) == FIELD_DECL)
652 {
653 TREE_TYPE (decl) = fld_simplified_type (TREE_TYPE (decl), fld);
654 DECL_INITIAL (decl) = NULL_TREE;
655 }
656 else if (TREE_CODE (decl) == TRANSLATION_UNIT_DECL
657 && DECL_INITIAL (decl)
658 && TREE_CODE (DECL_INITIAL (decl)) == BLOCK)
659 {
660 /* Strip builtins from the translation-unit BLOCK. We still have targets
661 without builtin_decl_explicit support and also builtins are shared
662 nodes and thus we can't use TREE_CHAIN in multiple lists. */
663 tree *nextp = &BLOCK_VARS (DECL_INITIAL (decl));
664 while (*nextp)
665 {
666 tree var = *nextp;
667 if (TREE_CODE (var) == FUNCTION_DECL
668 && fndecl_built_in_p (var))
669 *nextp = TREE_CHAIN (var);
670 else
671 nextp = &TREE_CHAIN (var);
672 }
673 }
674 /* We need to keep field decls associated with their trees. Otherwise tree
675 merging may merge some fields and keep others disjoint which in turn will
676 not do well with TREE_CHAIN pointers linking them.
677
678 Also do not drop containing types for virtual methods and tables because
679 these are needed by devirtualization.
680 C++ destructors are special because C++ frontends sometimes produces
681 virtual destructor as an alias of non-virtual destructor. In
682 devirutalization code we always walk through aliases and we need
683 context to be preserved too. See PR89335 */
684 if (TREE_CODE (decl) != FIELD_DECL
685 && ((TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FUNCTION_DECL)
686 || (!DECL_VIRTUAL_P (decl)
687 && (TREE_CODE (decl) != FUNCTION_DECL
688 || !DECL_CXX_DESTRUCTOR_P (decl)))))
689 DECL_CONTEXT (decl) = fld_decl_context (DECL_CONTEXT (decl));
690 }
691
692
693 /* Operand callback helper for free_lang_data_in_node. *TP is the
694 subtree operand being considered. */
695
696 static tree
697 find_decls_types_r (tree *tp, int *ws, void *data)
698 {
699 tree t = *tp;
700 class free_lang_data_d *fld = (class free_lang_data_d *) data;
701
702 if (TREE_CODE (t) == TREE_LIST)
703 return NULL_TREE;
704
705 /* Language specific nodes will be removed, so there is no need
706 to gather anything under them. */
707 if (is_lang_specific (t))
708 {
709 *ws = 0;
710 return NULL_TREE;
711 }
712
713 if (DECL_P (t))
714 {
715 /* Note that walk_tree does not traverse every possible field in
716 decls, so we have to do our own traversals here. */
717 add_tree_to_fld_list (t, fld);
718
719 fld_worklist_push (DECL_NAME (t), fld);
720 fld_worklist_push (DECL_CONTEXT (t), fld);
721 fld_worklist_push (DECL_SIZE (t), fld);
722 fld_worklist_push (DECL_SIZE_UNIT (t), fld);
723
724 /* We are going to remove everything under DECL_INITIAL for
725 TYPE_DECLs. No point walking them. */
726 if (TREE_CODE (t) != TYPE_DECL)
727 fld_worklist_push (DECL_INITIAL (t), fld);
728
729 fld_worklist_push (DECL_ATTRIBUTES (t), fld);
730 fld_worklist_push (DECL_ABSTRACT_ORIGIN (t), fld);
731
732 if (TREE_CODE (t) == FUNCTION_DECL)
733 {
734 fld_worklist_push (DECL_ARGUMENTS (t), fld);
735 fld_worklist_push (DECL_RESULT (t), fld);
736 }
737 else if (TREE_CODE (t) == FIELD_DECL)
738 {
739 fld_worklist_push (DECL_FIELD_OFFSET (t), fld);
740 fld_worklist_push (DECL_BIT_FIELD_TYPE (t), fld);
741 fld_worklist_push (DECL_FIELD_BIT_OFFSET (t), fld);
742 fld_worklist_push (DECL_FCONTEXT (t), fld);
743 }
744
745 if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL)
746 && DECL_HAS_VALUE_EXPR_P (t))
747 fld_worklist_push (DECL_VALUE_EXPR (t), fld);
748
749 if (TREE_CODE (t) != FIELD_DECL
750 && TREE_CODE (t) != TYPE_DECL)
751 fld_worklist_push (TREE_CHAIN (t), fld);
752 *ws = 0;
753 }
754 else if (TYPE_P (t))
755 {
756 /* Note that walk_tree does not traverse every possible field in
757 types, so we have to do our own traversals here. */
758 add_tree_to_fld_list (t, fld);
759
760 if (!RECORD_OR_UNION_TYPE_P (t))
761 fld_worklist_push (TYPE_CACHED_VALUES (t), fld);
762 fld_worklist_push (TYPE_SIZE (t), fld);
763 fld_worklist_push (TYPE_SIZE_UNIT (t), fld);
764 fld_worklist_push (TYPE_ATTRIBUTES (t), fld);
765 fld_worklist_push (TYPE_POINTER_TO (t), fld);
766 fld_worklist_push (TYPE_REFERENCE_TO (t), fld);
767 fld_worklist_push (TYPE_NAME (t), fld);
768 /* While we do not stream TYPE_POINTER_TO and TYPE_REFERENCE_TO
769 lists, we may look types up in these lists and use them while
770 optimizing the function body. Thus we need to free lang data
771 in them. */
772 if (TREE_CODE (t) == POINTER_TYPE)
773 fld_worklist_push (TYPE_NEXT_PTR_TO (t), fld);
774 if (TREE_CODE (t) == REFERENCE_TYPE)
775 fld_worklist_push (TYPE_NEXT_REF_TO (t), fld);
776 if (!POINTER_TYPE_P (t))
777 fld_worklist_push (TYPE_MIN_VALUE_RAW (t), fld);
778 /* TYPE_MAX_VALUE_RAW is TYPE_BINFO for record types. */
779 if (!RECORD_OR_UNION_TYPE_P (t))
780 fld_worklist_push (TYPE_MAX_VALUE_RAW (t), fld);
781 fld_worklist_push (TYPE_MAIN_VARIANT (t), fld);
782 /* Do not walk TYPE_NEXT_VARIANT. We do not stream it and thus
783 do not and want not to reach unused variants this way. */
784 if (TYPE_CONTEXT (t))
785 {
786 tree ctx = TYPE_CONTEXT (t);
787 /* We adjust BLOCK TYPE_CONTEXTs to the innermost non-BLOCK one.
788 So push that instead. */
789 while (ctx && TREE_CODE (ctx) == BLOCK)
790 ctx = BLOCK_SUPERCONTEXT (ctx);
791 fld_worklist_push (ctx, fld);
792 }
793 fld_worklist_push (TYPE_CANONICAL (t), fld);
794
795 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t))
796 {
797 unsigned i;
798 tree tem;
799 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (TYPE_BINFO (t)), i, tem)
800 fld_worklist_push (TREE_TYPE (tem), fld);
801 fld_worklist_push (BINFO_TYPE (TYPE_BINFO (t)), fld);
802 fld_worklist_push (BINFO_VTABLE (TYPE_BINFO (t)), fld);
803 }
804 if (RECORD_OR_UNION_TYPE_P (t))
805 {
806 tree tem;
807 /* Push all TYPE_FIELDS - there can be interleaving interesting
808 and non-interesting things. */
809 tem = TYPE_FIELDS (t);
810 while (tem)
811 {
812 if (TREE_CODE (tem) == FIELD_DECL)
813 fld_worklist_push (tem, fld);
814 tem = TREE_CHAIN (tem);
815 }
816 }
817 if (FUNC_OR_METHOD_TYPE_P (t))
818 fld_worklist_push (TYPE_METHOD_BASETYPE (t), fld);
819
820 fld_worklist_push (TYPE_STUB_DECL (t), fld);
821 *ws = 0;
822 }
823 else if (TREE_CODE (t) == BLOCK)
824 {
825 for (tree *tem = &BLOCK_VARS (t); *tem; )
826 {
827 if (TREE_CODE (*tem) != LABEL_DECL
828 && (TREE_CODE (*tem) != VAR_DECL
829 || !auto_var_in_fn_p (*tem, DECL_CONTEXT (*tem))))
830 {
831 gcc_assert (TREE_CODE (*tem) != RESULT_DECL
832 && TREE_CODE (*tem) != PARM_DECL);
833 *tem = TREE_CHAIN (*tem);
834 }
835 else
836 {
837 fld_worklist_push (*tem, fld);
838 tem = &TREE_CHAIN (*tem);
839 }
840 }
841 for (tree tem = BLOCK_SUBBLOCKS (t); tem; tem = BLOCK_CHAIN (tem))
842 fld_worklist_push (tem, fld);
843 fld_worklist_push (BLOCK_ABSTRACT_ORIGIN (t), fld);
844 }
845 /* walk_tree does not visit ce->index which can be a FIELD_DECL, pulling
846 in otherwise unused structure fields so handle CTORs explicitly. */
847 else if (TREE_CODE (t) == CONSTRUCTOR)
848 {
849 unsigned HOST_WIDE_INT idx;
850 constructor_elt *ce;
851 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t), idx, &ce); idx++)
852 {
853 if (ce->index)
854 fld_worklist_push (ce->index, fld);
855 fld_worklist_push (ce->value, fld);
856 }
857 *ws = 0;
858 }
859
860 if (TREE_CODE (t) != IDENTIFIER_NODE
861 && CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED))
862 fld_worklist_push (TREE_TYPE (t), fld);
863
864 return NULL_TREE;
865 }
866
867
868 /* Find decls and types in T. */
869
870 static void
871 find_decls_types (tree t, class free_lang_data_d *fld)
872 {
873 while (1)
874 {
875 if (!fld->pset.contains (t))
876 walk_tree (&t, find_decls_types_r, fld, &fld->pset);
877 if (fld->worklist.is_empty ())
878 break;
879 t = fld->worklist.pop ();
880 }
881 }
882
883 /* Translate all the types in LIST with the corresponding runtime
884 types. */
885
886 static tree
887 get_eh_types_for_runtime (tree list)
888 {
889 tree head, prev;
890
891 if (list == NULL_TREE)
892 return NULL_TREE;
893
894 head = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
895 prev = head;
896 list = TREE_CHAIN (list);
897 while (list)
898 {
899 tree n = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
900 TREE_CHAIN (prev) = n;
901 prev = TREE_CHAIN (prev);
902 list = TREE_CHAIN (list);
903 }
904
905 return head;
906 }
907
908
909 /* Find decls and types referenced in EH region R and store them in
910 FLD->DECLS and FLD->TYPES. */
911
912 static void
913 find_decls_types_in_eh_region (eh_region r, class free_lang_data_d *fld)
914 {
915 switch (r->type)
916 {
917 case ERT_CLEANUP:
918 break;
919
920 case ERT_TRY:
921 {
922 eh_catch c;
923
924 /* The types referenced in each catch must first be changed to the
925 EH types used at runtime. This removes references to FE types
926 in the region. */
927 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
928 {
929 c->type_list = get_eh_types_for_runtime (c->type_list);
930 walk_tree (&c->type_list, find_decls_types_r, fld, &fld->pset);
931 }
932 }
933 break;
934
935 case ERT_ALLOWED_EXCEPTIONS:
936 r->u.allowed.type_list
937 = get_eh_types_for_runtime (r->u.allowed.type_list);
938 walk_tree (&r->u.allowed.type_list, find_decls_types_r, fld, &fld->pset);
939 break;
940
941 case ERT_MUST_NOT_THROW:
942 walk_tree (&r->u.must_not_throw.failure_decl,
943 find_decls_types_r, fld, &fld->pset);
944 break;
945 }
946 }
947
948
949 /* Find decls and types referenced in cgraph node N and store them in
950 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
951 look for *every* kind of DECL and TYPE node reachable from N,
952 including those embedded inside types and decls (i.e,, TYPE_DECLs,
953 NAMESPACE_DECLs, etc). */
954
955 static void
956 find_decls_types_in_node (struct cgraph_node *n, class free_lang_data_d *fld)
957 {
958 basic_block bb;
959 struct function *fn;
960 unsigned ix;
961 tree t;
962
963 find_decls_types (n->decl, fld);
964
965 if (!gimple_has_body_p (n->decl))
966 return;
967
968 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
969
970 fn = DECL_STRUCT_FUNCTION (n->decl);
971
972 /* Traverse locals. */
973 FOR_EACH_LOCAL_DECL (fn, ix, t)
974 find_decls_types (t, fld);
975
976 /* Traverse EH regions in FN. */
977 {
978 eh_region r;
979 FOR_ALL_EH_REGION_FN (r, fn)
980 find_decls_types_in_eh_region (r, fld);
981 }
982
983 /* Traverse every statement in FN. */
984 FOR_EACH_BB_FN (bb, fn)
985 {
986 gphi_iterator psi;
987 gimple_stmt_iterator si;
988 unsigned i;
989
990 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
991 {
992 gphi *phi = psi.phi ();
993
994 for (i = 0; i < gimple_phi_num_args (phi); i++)
995 {
996 tree *arg_p = gimple_phi_arg_def_ptr (phi, i);
997 find_decls_types (*arg_p, fld);
998 }
999 }
1000
1001 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
1002 {
1003 gimple *stmt = gsi_stmt (si);
1004
1005 if (is_gimple_call (stmt))
1006 find_decls_types (gimple_call_fntype (stmt), fld);
1007
1008 for (i = 0; i < gimple_num_ops (stmt); i++)
1009 {
1010 tree arg = gimple_op (stmt, i);
1011 find_decls_types (arg, fld);
1012 /* find_decls_types doesn't walk TREE_PURPOSE of TREE_LISTs,
1013 which we need for asm stmts. */
1014 if (arg
1015 && TREE_CODE (arg) == TREE_LIST
1016 && TREE_PURPOSE (arg)
1017 && gimple_code (stmt) == GIMPLE_ASM)
1018 find_decls_types (TREE_PURPOSE (arg), fld);
1019 }
1020 }
1021 }
1022 }
1023
1024
1025 /* Find decls and types referenced in varpool node N and store them in
1026 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
1027 look for *every* kind of DECL and TYPE node reachable from N,
1028 including those embedded inside types and decls (i.e,, TYPE_DECLs,
1029 NAMESPACE_DECLs, etc). */
1030
1031 static void
1032 find_decls_types_in_var (varpool_node *v, class free_lang_data_d *fld)
1033 {
1034 find_decls_types (v->decl, fld);
1035 }
1036
1037 /* Free language specific information for every operand and expression
1038 in every node of the call graph. This process operates in three stages:
1039
1040 1- Every callgraph node and varpool node is traversed looking for
1041 decls and types embedded in them. This is a more exhaustive
1042 search than that done by find_referenced_vars, because it will
1043 also collect individual fields, decls embedded in types, etc.
1044
1045 2- All the decls found are sent to free_lang_data_in_decl.
1046
1047 3- All the types found are sent to free_lang_data_in_type.
1048
1049 The ordering between decls and types is important because
1050 free_lang_data_in_decl sets assembler names, which includes
1051 mangling. So types cannot be freed up until assembler names have
1052 been set up. */
1053
1054 static void
1055 free_lang_data_in_cgraph (class free_lang_data_d *fld)
1056 {
1057 struct cgraph_node *n;
1058 varpool_node *v;
1059 tree t;
1060 unsigned i;
1061 alias_pair *p;
1062
1063 /* Find decls and types in the body of every function in the callgraph. */
1064 FOR_EACH_FUNCTION (n)
1065 find_decls_types_in_node (n, fld);
1066
1067 FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
1068 find_decls_types (p->decl, fld);
1069
1070 /* Find decls and types in every varpool symbol. */
1071 FOR_EACH_VARIABLE (v)
1072 find_decls_types_in_var (v, fld);
1073
1074 /* Set the assembler name on every decl found. We need to do this
1075 now because free_lang_data_in_decl will invalidate data needed
1076 for mangling. This breaks mangling on interdependent decls. */
1077 FOR_EACH_VEC_ELT (fld->decls, i, t)
1078 assign_assembler_name_if_needed (t);
1079
1080 /* Traverse every decl found freeing its language data. */
1081 FOR_EACH_VEC_ELT (fld->decls, i, t)
1082 free_lang_data_in_decl (t, fld);
1083
1084 /* Traverse every type found freeing its language data. */
1085 FOR_EACH_VEC_ELT (fld->types, i, t)
1086 free_lang_data_in_type (t, fld);
1087 }
1088
1089
1090 /* Free resources that are used by FE but are not needed once they are done. */
1091
1092 static unsigned
1093 free_lang_data (void)
1094 {
1095 unsigned i;
1096 class free_lang_data_d fld;
1097
1098 /* If we are the LTO frontend we have freed lang-specific data already. */
1099 if (in_lto_p
1100 || (!flag_generate_lto && !flag_generate_offload))
1101 {
1102 /* Rebuild type inheritance graph even when not doing LTO to get
1103 consistent profile data. */
1104 rebuild_type_inheritance_graph ();
1105 return 0;
1106 }
1107
1108 fld_incomplete_types = new hash_map<tree, tree>;
1109 fld_simplified_types = new hash_map<tree, tree>;
1110
1111 /* Provide a dummy TRANSLATION_UNIT_DECL if the FE failed to provide one. */
1112 if (vec_safe_is_empty (all_translation_units))
1113 build_translation_unit_decl (NULL_TREE);
1114
1115 /* Allocate and assign alias sets to the standard integer types
1116 while the slots are still in the way the frontends generated them. */
1117 for (i = 0; i < itk_none; ++i)
1118 if (integer_types[i])
1119 TYPE_ALIAS_SET (integer_types[i]) = get_alias_set (integer_types[i]);
1120
1121 /* Traverse the IL resetting language specific information for
1122 operands, expressions, etc. */
1123 free_lang_data_in_cgraph (&fld);
1124
1125 /* Create gimple variants for common types. */
1126 for (unsigned i = 0;
1127 i < sizeof (builtin_structptr_types) / sizeof (builtin_structptr_type);
1128 ++i)
1129 builtin_structptr_types[i].node = builtin_structptr_types[i].base;
1130
1131 /* Reset some langhooks. Do not reset types_compatible_p, it may
1132 still be used indirectly via the get_alias_set langhook. */
1133 lang_hooks.dwarf_name = lhd_dwarf_name;
1134 lang_hooks.decl_printable_name = gimple_decl_printable_name;
1135 lang_hooks.gimplify_expr = lhd_gimplify_expr;
1136 lang_hooks.overwrite_decl_assembler_name = lhd_overwrite_decl_assembler_name;
1137 lang_hooks.print_xnode = lhd_print_tree_nothing;
1138 lang_hooks.print_decl = lhd_print_tree_nothing;
1139 lang_hooks.print_type = lhd_print_tree_nothing;
1140 lang_hooks.print_identifier = lhd_print_tree_nothing;
1141
1142 lang_hooks.tree_inlining.var_mod_type_p = hook_bool_tree_tree_false;
1143
1144 if (flag_checking)
1145 {
1146 int i;
1147 tree t;
1148
1149 FOR_EACH_VEC_ELT (fld.types, i, t)
1150 verify_type (t);
1151 }
1152
1153 /* We do not want the default decl_assembler_name implementation,
1154 rather if we have fixed everything we want a wrapper around it
1155 asserting that all non-local symbols already got their assembler
1156 name and only produce assembler names for local symbols. Or rather
1157 make sure we never call decl_assembler_name on local symbols and
1158 devise a separate, middle-end private scheme for it. */
1159
1160 /* Reset diagnostic machinery. */
1161 tree_diagnostics_defaults (global_dc);
1162
1163 rebuild_type_inheritance_graph ();
1164
1165 delete fld_incomplete_types;
1166 delete fld_simplified_types;
1167
1168 return 0;
1169 }
1170
1171 const pass_data pass_data_ipa_free_lang_data =
1172 {
1173 SIMPLE_IPA_PASS, /* type */
1174 "*free_lang_data", /* name */
1175 OPTGROUP_NONE, /* optinfo_flags */
1176 TV_IPA_FREE_LANG_DATA, /* tv_id */
1177 0, /* properties_required */
1178 0, /* properties_provided */
1179 0, /* properties_destroyed */
1180 0, /* todo_flags_start */
1181 0, /* todo_flags_finish */
1182 };
1183
1184 class pass_ipa_free_lang_data : public simple_ipa_opt_pass
1185 {
1186 public:
1187 pass_ipa_free_lang_data (gcc::context *ctxt)
1188 : simple_ipa_opt_pass (pass_data_ipa_free_lang_data, ctxt)
1189 {}
1190
1191 /* opt_pass methods: */
1192 virtual unsigned int execute (function *) { return free_lang_data (); }
1193
1194 }; // class pass_ipa_free_lang_data
1195
1196 } // anon namespace
1197
1198 simple_ipa_opt_pass *
1199 make_pass_ipa_free_lang_data (gcc::context *ctxt)
1200 {
1201 return new pass_ipa_free_lang_data (ctxt);
1202 }
1203