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