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