Use gather loads for strided accesses
[official-gcc.git] / gcc / tree-emutls.c
blobae8c6e5f155b6cf39e3ca64036f001812399001f
1 /* Lower TLS operations to emulation functions.
2 Copyright (C) 2006-2018 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "backend.h"
24 #include "target.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "tree-pass.h"
28 #include "ssa.h"
29 #include "cgraph.h"
30 #include "fold-const.h"
31 #include "stor-layout.h"
32 #include "varasm.h"
33 #include "gimple-iterator.h"
34 #include "gimple-walk.h"
35 #include "langhooks.h"
36 #include "tree-iterator.h"
38 /* Whenever a target does not support thread-local storage (TLS) natively,
39 we can emulate it with some run-time support in libgcc. This will in
40 turn rely on "keyed storage" a-la pthread_key_create; essentially all
41 thread libraries provide such functionality.
43 In order to coordinate with the libgcc runtime, each TLS variable is
44 described by a "control variable". This control variable records the
45 required size, alignment, and initial value of the TLS variable for
46 instantiation at runtime. It also stores an integer token to be used
47 by the runtime to find the address of the variable within each thread.
49 On the compiler side, this means that we need to replace all instances
50 of "tls_var" in the code with "*__emutls_get_addr(&control_var)". We
51 also need to eliminate "tls_var" from the symbol table and introduce
52 "control_var".
54 We used to perform all of the transformations during conversion to rtl,
55 and the variable substitutions magically within assemble_variable.
56 However, this late fiddling of the symbol table conflicts with LTO and
57 whole-program compilation. Therefore we must now make all the changes
58 to the symbol table early in the GIMPLE optimization path, before we
59 write things out to LTO intermediate files. */
61 /* Value for TLS varpool node where a pointer to control variable and
62 access variable are stored. */
63 struct tls_var_data
65 varpool_node *control_var;
66 tree access;
69 /* TLS map accesses mapping between a TLS varpool node and a pair
70 made by control variable and access variable. */
71 static hash_map<varpool_node *, tls_var_data> *tls_map = NULL;
73 /* The type of the control structure, shared with the emutls.c runtime. */
74 static tree emutls_object_type;
76 #if !defined (NO_DOT_IN_LABEL)
77 # define EMUTLS_SEPARATOR "."
78 #elif !defined (NO_DOLLAR_IN_LABEL)
79 # define EMUTLS_SEPARATOR "$"
80 #else
81 # define EMUTLS_SEPARATOR "_"
82 #endif
84 /* Create an IDENTIFIER_NODE by prefixing PREFIX to the
85 IDENTIFIER_NODE NAME's name. */
87 static tree
88 prefix_name (const char *prefix, tree name)
90 unsigned plen = strlen (prefix);
91 unsigned nlen = strlen (IDENTIFIER_POINTER (name));
92 char *toname = (char *) alloca (plen + nlen + 1);
94 memcpy (toname, prefix, plen);
95 memcpy (toname + plen, IDENTIFIER_POINTER (name), nlen + 1);
97 return get_identifier (toname);
100 /* Create an identifier for the struct __emutls_object, given an identifier
101 of the DECL_ASSEMBLY_NAME of the original object. */
103 static tree
104 get_emutls_object_name (tree name)
106 const char *prefix = (targetm.emutls.var_prefix
107 ? targetm.emutls.var_prefix
108 : "__emutls_v" EMUTLS_SEPARATOR);
109 return prefix_name (prefix, name);
112 /* Create the fields of the type for the control variables. Ordinarily
113 this must match struct __emutls_object defined in emutls.c. However
114 this is a target hook so that VxWorks can define its own layout. */
116 tree
117 default_emutls_var_fields (tree type, tree *name ATTRIBUTE_UNUSED)
119 tree word_type_node, field, next_field;
121 field = build_decl (UNKNOWN_LOCATION,
122 FIELD_DECL, get_identifier ("__templ"), ptr_type_node);
123 DECL_CONTEXT (field) = type;
124 next_field = field;
126 field = build_decl (UNKNOWN_LOCATION,
127 FIELD_DECL, get_identifier ("__offset"),
128 ptr_type_node);
129 DECL_CONTEXT (field) = type;
130 DECL_CHAIN (field) = next_field;
131 next_field = field;
133 word_type_node = lang_hooks.types.type_for_mode (word_mode, 1);
134 field = build_decl (UNKNOWN_LOCATION,
135 FIELD_DECL, get_identifier ("__align"),
136 word_type_node);
137 DECL_CONTEXT (field) = type;
138 DECL_CHAIN (field) = next_field;
139 next_field = field;
141 field = build_decl (UNKNOWN_LOCATION,
142 FIELD_DECL, get_identifier ("__size"), word_type_node);
143 DECL_CONTEXT (field) = type;
144 DECL_CHAIN (field) = next_field;
146 return field;
149 /* Initialize emulated tls object TO, which refers to TLS variable DECL and
150 is initialized by PROXY. As above, this is the default implementation of
151 a target hook overridden by VxWorks. */
153 tree
154 default_emutls_var_init (tree to, tree decl, tree proxy)
156 vec<constructor_elt, va_gc> *v;
157 vec_alloc (v, 4);
158 constructor_elt elt;
159 tree type = TREE_TYPE (to);
160 tree field = TYPE_FIELDS (type);
162 elt.index = field;
163 elt.value = fold_convert (TREE_TYPE (field), DECL_SIZE_UNIT (decl));
164 v->quick_push (elt);
166 field = DECL_CHAIN (field);
167 elt.index = field;
168 elt.value = build_int_cst (TREE_TYPE (field),
169 DECL_ALIGN_UNIT (decl));
170 v->quick_push (elt);
172 field = DECL_CHAIN (field);
173 elt.index = field;
174 elt.value = null_pointer_node;
175 v->quick_push (elt);
177 field = DECL_CHAIN (field);
178 elt.index = field;
179 elt.value = proxy;
180 v->quick_push (elt);
182 return build_constructor (type, v);
185 /* Create the structure for struct __emutls_object. This should match the
186 structure at the top of emutls.c, modulo the union there. */
188 static tree
189 get_emutls_object_type (void)
191 tree type, type_name, field;
193 type = emutls_object_type;
194 if (type)
195 return type;
197 emutls_object_type = type = lang_hooks.types.make_type (RECORD_TYPE);
198 type_name = NULL;
199 field = targetm.emutls.var_fields (type, &type_name);
200 if (!type_name)
201 type_name = get_identifier ("__emutls_object");
202 type_name = build_decl (UNKNOWN_LOCATION,
203 TYPE_DECL, type_name, type);
204 TYPE_NAME (type) = type_name;
205 TYPE_FIELDS (type) = field;
206 layout_type (type);
208 return type;
211 /* Create a read-only variable like DECL, with the same DECL_INITIAL.
212 This will be used for initializing the emulated tls data area. */
214 static tree
215 get_emutls_init_templ_addr (tree decl)
217 tree name, to;
219 if (targetm.emutls.register_common && !DECL_INITIAL (decl)
220 && !DECL_SECTION_NAME (decl))
221 return null_pointer_node;
223 name = DECL_ASSEMBLER_NAME (decl);
224 if (!targetm.emutls.tmpl_prefix || targetm.emutls.tmpl_prefix[0])
226 const char *prefix = (targetm.emutls.tmpl_prefix
227 ? targetm.emutls.tmpl_prefix
228 : "__emutls_t" EMUTLS_SEPARATOR);
229 name = prefix_name (prefix, name);
232 to = build_decl (DECL_SOURCE_LOCATION (decl),
233 VAR_DECL, name, TREE_TYPE (decl));
234 SET_DECL_ASSEMBLER_NAME (to, DECL_NAME (to));
236 DECL_ARTIFICIAL (to) = 1;
237 TREE_USED (to) = TREE_USED (decl);
238 TREE_READONLY (to) = 1;
239 DECL_IGNORED_P (to) = 1;
240 DECL_CONTEXT (to) = DECL_CONTEXT (decl);
241 DECL_PRESERVE_P (to) = DECL_PRESERVE_P (decl);
243 DECL_WEAK (to) = DECL_WEAK (decl);
244 if (DECL_ONE_ONLY (decl))
246 TREE_STATIC (to) = TREE_STATIC (decl);
247 TREE_PUBLIC (to) = TREE_PUBLIC (decl);
248 DECL_VISIBILITY (to) = DECL_VISIBILITY (decl);
249 make_decl_one_only (to, DECL_ASSEMBLER_NAME (to));
251 else
252 TREE_STATIC (to) = 1;
254 DECL_VISIBILITY_SPECIFIED (to) = DECL_VISIBILITY_SPECIFIED (decl);
255 DECL_INITIAL (to) = DECL_INITIAL (decl);
256 DECL_INITIAL (decl) = NULL;
258 if (targetm.emutls.tmpl_section)
259 set_decl_section_name (to, targetm.emutls.tmpl_section);
260 else
261 set_decl_section_name (to, DECL_SECTION_NAME (decl));
263 /* Create varpool node for the new variable and finalize it if it is
264 not external one. */
265 if (DECL_EXTERNAL (to))
266 varpool_node::get_create (to);
267 else
268 varpool_node::add (to);
269 return build_fold_addr_expr (to);
272 /* Create and return the control variable for the TLS variable DECL. */
274 static tree
275 new_emutls_decl (tree decl, tree alias_of)
277 tree name, to;
279 name = DECL_ASSEMBLER_NAME (decl);
280 to = build_decl (DECL_SOURCE_LOCATION (decl), VAR_DECL,
281 get_emutls_object_name (name),
282 get_emutls_object_type ());
284 SET_DECL_ASSEMBLER_NAME (to, DECL_NAME (to));
286 DECL_ARTIFICIAL (to) = 1;
287 DECL_IGNORED_P (to) = 1;
288 TREE_READONLY (to) = 0;
289 TREE_STATIC (to) = 1;
291 DECL_PRESERVE_P (to) = DECL_PRESERVE_P (decl);
292 DECL_CONTEXT (to) = DECL_CONTEXT (decl);
293 TREE_USED (to) = TREE_USED (decl);
294 TREE_PUBLIC (to) = TREE_PUBLIC (decl);
295 DECL_EXTERNAL (to) = DECL_EXTERNAL (decl);
296 DECL_COMMON (to) = DECL_COMMON (decl);
297 DECL_WEAK (to) = DECL_WEAK (decl);
298 DECL_VISIBILITY (to) = DECL_VISIBILITY (decl);
299 DECL_VISIBILITY_SPECIFIED (to) = DECL_VISIBILITY_SPECIFIED (decl);
300 DECL_DLLIMPORT_P (to) = DECL_DLLIMPORT_P (decl);
302 DECL_ATTRIBUTES (to) = targetm.merge_decl_attributes (decl, to);
304 if (DECL_ONE_ONLY (decl))
305 make_decl_one_only (to, DECL_ASSEMBLER_NAME (to));
307 set_decl_tls_model (to, TLS_MODEL_EMULATED);
309 /* If we're not allowed to change the proxy object's alignment,
310 pretend it has been set by the user. */
311 if (targetm.emutls.var_align_fixed)
312 DECL_USER_ALIGN (to) = 1;
314 /* If the target wants the control variables grouped, do so. */
315 if (!DECL_COMMON (to) && targetm.emutls.var_section)
317 set_decl_section_name (to, targetm.emutls.var_section);
320 /* If this variable is defined locally, then we need to initialize the
321 control structure with size and alignment information. Initialization
322 of COMMON block variables happens elsewhere via a constructor. */
323 if (!DECL_EXTERNAL (to)
324 && (!DECL_COMMON (to)
325 || (DECL_INITIAL (decl)
326 && DECL_INITIAL (decl) != error_mark_node)))
328 tree tmpl = get_emutls_init_templ_addr (decl);
329 DECL_INITIAL (to) = targetm.emutls.var_init (to, decl, tmpl);
330 record_references_in_initializer (to, false);
333 /* Create varpool node for the new variable and finalize it if it is
334 not external one. */
335 if (DECL_EXTERNAL (to))
336 varpool_node::get_create (to);
337 else if (!alias_of)
338 varpool_node::add (to);
339 else
341 varpool_node *n;
342 varpool_node *t = varpool_node::get_for_asmname
343 (DECL_ASSEMBLER_NAME (DECL_VALUE_EXPR (alias_of)));
345 n = varpool_node::create_alias (to, t->decl);
346 n->resolve_alias (t);
348 return to;
351 /* Generate a call statement to initialize CONTROL_DECL for TLS_DECL.
352 This only needs to happen for TLS COMMON variables; non-COMMON
353 variables can be initialized statically. Insert the generated
354 call statement at the end of PSTMTS. */
356 static void
357 emutls_common_1 (tree tls_decl, tree control_decl, tree *pstmts)
359 tree x;
360 tree word_type_node;
362 if (! DECL_COMMON (tls_decl)
363 || (DECL_INITIAL (tls_decl)
364 && DECL_INITIAL (tls_decl) != error_mark_node))
365 return;
367 word_type_node = lang_hooks.types.type_for_mode (word_mode, 1);
369 x = build_call_expr (builtin_decl_explicit (BUILT_IN_EMUTLS_REGISTER_COMMON),
370 4, build_fold_addr_expr (control_decl),
371 fold_convert (word_type_node,
372 DECL_SIZE_UNIT (tls_decl)),
373 build_int_cst (word_type_node,
374 DECL_ALIGN_UNIT (tls_decl)),
375 get_emutls_init_templ_addr (tls_decl));
377 append_to_statement_list (x, pstmts);
380 struct lower_emutls_data
382 struct cgraph_node *cfun_node;
383 struct cgraph_node *builtin_node;
384 tree builtin_decl;
385 basic_block bb;
386 location_t loc;
387 gimple_seq seq;
390 /* Given a TLS variable DECL, return an SSA_NAME holding its address.
391 Append any new computation statements required to D->SEQ. */
393 static tree
394 gen_emutls_addr (tree decl, struct lower_emutls_data *d)
396 /* Compute the address of the TLS variable with help from runtime. */
397 tls_var_data *data = tls_map->get (varpool_node::get (decl));
398 tree addr = data->access;
400 if (addr == NULL)
402 varpool_node *cvar;
403 tree cdecl;
404 gcall *x;
406 cvar = data->control_var;
407 cdecl = cvar->decl;
408 TREE_ADDRESSABLE (cdecl) = 1;
410 addr = create_tmp_var (build_pointer_type (TREE_TYPE (decl)));
411 x = gimple_build_call (d->builtin_decl, 1, build_fold_addr_expr (cdecl));
412 gimple_set_location (x, d->loc);
414 addr = make_ssa_name (addr, x);
415 gimple_call_set_lhs (x, addr);
417 gimple_seq_add_stmt (&d->seq, x);
419 d->cfun_node->create_edge (d->builtin_node, x, d->bb->count);
421 /* We may be adding a new reference to a new variable to the function.
422 This means we have to play with the ipa-reference web. */
423 d->cfun_node->create_reference (cvar, IPA_REF_ADDR, x);
425 /* Record this ssa_name for possible use later in the basic block. */
426 data->access = addr;
429 return addr;
432 /* Callback for walk_gimple_op. D = WI->INFO is a struct lower_emutls_data.
433 Given an operand *PTR within D->STMT, if the operand references a TLS
434 variable, then lower the reference to a call to the runtime. Insert
435 any new statements required into D->SEQ; the caller is responsible for
436 placing those appropriately. */
438 static tree
439 lower_emutls_1 (tree *ptr, int *walk_subtrees, void *cb_data)
441 struct walk_stmt_info *wi = (struct walk_stmt_info *) cb_data;
442 struct lower_emutls_data *d = (struct lower_emutls_data *) wi->info;
443 tree t = *ptr;
444 bool is_addr = false;
445 tree addr;
447 *walk_subtrees = 0;
449 switch (TREE_CODE (t))
451 case ADDR_EXPR:
452 /* If this is not a straight-forward "&var", but rather something
453 like "&var.a", then we may need special handling. */
454 if (TREE_CODE (TREE_OPERAND (t, 0)) != VAR_DECL)
456 bool save_changed;
458 /* If we're allowed more than just is_gimple_val, continue. */
459 if (!wi->val_only)
461 *walk_subtrees = 1;
462 return NULL_TREE;
465 /* See if any substitution would be made. */
466 save_changed = wi->changed;
467 wi->changed = false;
468 wi->val_only = false;
469 walk_tree (&TREE_OPERAND (t, 0), lower_emutls_1, wi, NULL);
470 wi->val_only = true;
472 /* If so, then extract this entire sub-expression "&p->a" into a
473 new assignment statement, and substitute yet another SSA_NAME. */
474 if (wi->changed)
476 gimple *x;
478 addr = create_tmp_var (TREE_TYPE (t));
479 x = gimple_build_assign (addr, t);
480 gimple_set_location (x, d->loc);
482 addr = make_ssa_name (addr, x);
483 gimple_assign_set_lhs (x, addr);
485 gimple_seq_add_stmt (&d->seq, x);
487 *ptr = addr;
489 else
490 wi->changed = save_changed;
492 return NULL_TREE;
495 t = TREE_OPERAND (t, 0);
496 is_addr = true;
497 /* FALLTHRU */
499 case VAR_DECL:
500 if (!DECL_THREAD_LOCAL_P (t))
501 return NULL_TREE;
502 break;
504 default:
505 /* We're not interested in other decls or types, only subexpressions. */
506 if (EXPR_P (t))
507 *walk_subtrees = 1;
508 /* FALLTHRU */
510 case SSA_NAME:
511 /* Special-case the return of SSA_NAME, since it's so common. */
512 return NULL_TREE;
515 addr = gen_emutls_addr (t, d);
516 if (is_addr)
518 /* Replace "&var" with "addr" in the statement. */
519 *ptr = addr;
521 else
523 /* Replace "var" with "*addr" in the statement. */
524 t = build2 (MEM_REF, TREE_TYPE (t), addr,
525 build_int_cst (TREE_TYPE (addr), 0));
526 *ptr = t;
529 wi->changed = true;
530 return NULL_TREE;
533 /* Lower all of the operands of STMT. */
535 static void
536 lower_emutls_stmt (gimple *stmt, struct lower_emutls_data *d)
538 struct walk_stmt_info wi;
540 d->loc = gimple_location (stmt);
542 memset (&wi, 0, sizeof (wi));
543 wi.info = d;
544 wi.val_only = true;
545 walk_gimple_op (stmt, lower_emutls_1, &wi);
547 if (wi.changed)
548 update_stmt (stmt);
551 /* Lower the I'th operand of PHI. */
553 static void
554 lower_emutls_phi_arg (gphi *phi, unsigned int i,
555 struct lower_emutls_data *d)
557 struct walk_stmt_info wi;
558 struct phi_arg_d *pd = gimple_phi_arg (phi, i);
560 /* Early out for a very common case we don't care about. */
561 if (TREE_CODE (pd->def) == SSA_NAME)
562 return;
564 d->loc = pd->locus;
566 memset (&wi, 0, sizeof (wi));
567 wi.info = d;
568 wi.val_only = true;
569 walk_tree (&pd->def, lower_emutls_1, &wi, NULL);
571 /* For normal statements, we let update_stmt do its job. But for phi
572 nodes, we have to manipulate the immediate use list by hand. */
573 if (wi.changed)
575 gcc_assert (TREE_CODE (pd->def) == SSA_NAME);
576 link_imm_use_stmt (&pd->imm_use, pd->def, phi);
580 /* Reset access variable for a given TLS variable data DATA. */
582 bool
583 reset_access (varpool_node * const &, tls_var_data *data, void *)
585 data->access = NULL;
587 return true;
590 /* Clear the access variables, in order to begin a new block. */
592 static inline void
593 clear_access_vars (void)
595 tls_map->traverse<void *, reset_access> (NULL);
598 /* Lower the entire function NODE. */
600 static void
601 lower_emutls_function_body (struct cgraph_node *node)
603 struct lower_emutls_data d;
604 bool any_edge_inserts = false;
606 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
608 d.cfun_node = node;
609 d.builtin_decl = builtin_decl_explicit (BUILT_IN_EMUTLS_GET_ADDRESS);
610 /* This is where we introduce the declaration to the IL and so we have to
611 create a node for it. */
612 d.builtin_node = cgraph_node::get_create (d.builtin_decl);
614 FOR_EACH_BB_FN (d.bb, cfun)
616 unsigned int i, nedge;
618 /* Lower each of the PHI nodes of the block, as we may have
619 propagated &tlsvar into a PHI argument. These loops are
620 arranged so that we process each edge at once, and each
621 PHI argument for that edge. */
622 if (!gimple_seq_empty_p (phi_nodes (d.bb)))
624 nedge = EDGE_COUNT (d.bb->preds);
625 for (i = 0; i < nedge; ++i)
627 edge e = EDGE_PRED (d.bb, i);
629 /* We can re-use any SSA_NAME created on this edge. */
630 clear_access_vars ();
631 d.seq = NULL;
633 for (gphi_iterator gsi = gsi_start_phis (d.bb);
634 !gsi_end_p (gsi);
635 gsi_next (&gsi))
636 lower_emutls_phi_arg (gsi.phi (), i, &d);
638 /* Insert all statements generated by all phi nodes for this
639 particular edge all at once. */
640 if (d.seq)
642 gsi_insert_seq_on_edge (e, d.seq);
643 any_edge_inserts = true;
648 /* We can re-use any SSA_NAME created during this basic block. */
649 clear_access_vars ();
651 /* Lower each of the statements of the block. */
652 for (gimple_stmt_iterator gsi = gsi_start_bb (d.bb); !gsi_end_p (gsi);
653 gsi_next (&gsi))
655 d.seq = NULL;
656 lower_emutls_stmt (gsi_stmt (gsi), &d);
658 /* If any new statements were created, insert them immediately
659 before the first use. This prevents variable lifetimes from
660 becoming unnecessarily long. */
661 if (d.seq)
662 gsi_insert_seq_before (&gsi, d.seq, GSI_SAME_STMT);
666 if (any_edge_inserts)
667 gsi_commit_edge_inserts ();
669 pop_cfun ();
672 /* Create emutls variable for VAR, DATA is pointer to static
673 ctor body we can add constructors to.
674 Callback for varpool_for_variable_and_aliases. */
676 static bool
677 create_emultls_var (varpool_node *var, void *data)
679 tree cdecl;
680 tls_var_data value;
682 cdecl = new_emutls_decl (var->decl,
683 var->alias && var->analyzed
684 ? var->get_alias_target ()->decl : NULL);
686 varpool_node *cvar = varpool_node::get (cdecl);
688 if (!var->alias)
690 /* Make sure the COMMON block control variable gets initialized.
691 Note that there's no point in doing this for aliases; we only
692 need to do this once for the main variable. */
693 emutls_common_1 (var->decl, cdecl, (tree *)data);
695 if (var->alias && !var->analyzed)
696 cvar->alias = true;
698 /* Indicate that the value of the TLS variable may be found elsewhere,
699 preventing the variable from re-appearing in the GIMPLE. We cheat
700 and use the control variable here (rather than a full call_expr),
701 which is special-cased inside the DWARF2 output routines. */
702 SET_DECL_VALUE_EXPR (var->decl, cdecl);
703 DECL_HAS_VALUE_EXPR_P (var->decl) = 1;
705 value.control_var = cvar;
706 tls_map->put (var, value);
708 return false;
711 /* Main entry point to the tls lowering pass. */
713 static unsigned int
714 ipa_lower_emutls (void)
716 varpool_node *var;
717 cgraph_node *func;
718 bool any_aliases = false;
719 tree ctor_body = NULL;
720 hash_set <varpool_node *> visited;
721 auto_vec <varpool_node *> tls_vars;
723 /* Examine all global variables for TLS variables. */
724 FOR_EACH_VARIABLE (var)
725 if (DECL_THREAD_LOCAL_P (var->decl)
726 && !visited.add (var))
728 gcc_checking_assert (TREE_STATIC (var->decl)
729 || DECL_EXTERNAL (var->decl));
730 tls_vars.safe_push (var);
731 if (var->alias && var->definition
732 && !visited.add (var->ultimate_alias_target ()))
733 tls_vars.safe_push (var->ultimate_alias_target ());
736 /* If we found no TLS variables, then there is no further work to do. */
737 if (tls_vars.is_empty ())
739 if (dump_file)
740 fprintf (dump_file, "No TLS variables found.\n");
741 return 0;
744 tls_map = new hash_map <varpool_node *, tls_var_data> ();
746 /* Create the control variables for each TLS variable. */
747 for (unsigned i = 0; i < tls_vars.length (); i++)
749 var = tls_vars[i];
751 if (var->alias && !var->analyzed)
752 any_aliases = true;
753 else if (!var->alias)
754 var->call_for_symbol_and_aliases (create_emultls_var, &ctor_body, true);
757 /* If there were any aliases, then frob the alias_pairs vector. */
758 if (any_aliases)
760 alias_pair *p;
761 unsigned int i;
762 FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
763 if (DECL_THREAD_LOCAL_P (p->decl))
765 p->decl = tls_map->get
766 (varpool_node::get (p->decl))->control_var->decl;
767 p->target = get_emutls_object_name (p->target);
771 /* Adjust all uses of TLS variables within the function bodies. */
772 FOR_EACH_DEFINED_FUNCTION (func)
773 if (func->lowered)
774 lower_emutls_function_body (func);
776 /* Generate the constructor for any COMMON control variables created. */
777 if (ctor_body)
778 cgraph_build_static_cdtor ('I', ctor_body, DEFAULT_INIT_PRIORITY);
780 delete tls_map;
782 return 0;
785 namespace {
787 const pass_data pass_data_ipa_lower_emutls =
789 SIMPLE_IPA_PASS, /* type */
790 "emutls", /* name */
791 OPTGROUP_NONE, /* optinfo_flags */
792 TV_IPA_OPT, /* tv_id */
793 ( PROP_cfg | PROP_ssa ), /* properties_required */
794 0, /* properties_provided */
795 0, /* properties_destroyed */
796 0, /* todo_flags_start */
797 0, /* todo_flags_finish */
800 class pass_ipa_lower_emutls : public simple_ipa_opt_pass
802 public:
803 pass_ipa_lower_emutls (gcc::context *ctxt)
804 : simple_ipa_opt_pass (pass_data_ipa_lower_emutls, ctxt)
807 /* opt_pass methods: */
808 virtual bool gate (function *)
810 /* If the target supports TLS natively, we need do nothing here. */
811 return !targetm.have_tls;
814 virtual unsigned int execute (function *) { return ipa_lower_emutls (); }
816 }; // class pass_ipa_lower_emutls
818 } // anon namespace
820 simple_ipa_opt_pass *
821 make_pass_ipa_lower_emutls (gcc::context *ctxt)
823 return new pass_ipa_lower_emutls (ctxt);