2015-09-25 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / tree-emutls.c
blob71e2cbb93fdccd57ba42a56b7316add7b9895e3b
1 /* Lower TLS operations to emulation functions.
2 Copyright (C) 2006-2015 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 "alias.h"
24 #include "backend.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "hard-reg-set.h"
28 #include "ssa.h"
29 #include "options.h"
30 #include "fold-const.h"
31 #include "stor-layout.h"
32 #include "varasm.h"
33 #include "internal-fn.h"
34 #include "gimple-iterator.h"
35 #include "gimple-walk.h"
36 #include "tree-pass.h"
37 #include "cgraph.h"
38 #include "langhooks.h"
39 #include "target.h"
40 #include "targhooks.h"
41 #include "tree-iterator.h"
43 /* Whenever a target does not support thread-local storage (TLS) natively,
44 we can emulate it with some run-time support in libgcc. This will in
45 turn rely on "keyed storage" a-la pthread_key_create; essentially all
46 thread libraries provide such functionality.
48 In order to coordinate with the libgcc runtime, each TLS variable is
49 described by a "control variable". This control variable records the
50 required size, alignment, and initial value of the TLS variable for
51 instantiation at runtime. It also stores an integer token to be used
52 by the runtime to find the address of the variable within each thread.
54 On the compiler side, this means that we need to replace all instances
55 of "tls_var" in the code with "*__emutls_get_addr(&control_var)". We
56 also need to eliminate "tls_var" from the symbol table and introduce
57 "control_var".
59 We used to perform all of the transformations during conversion to rtl,
60 and the variable substitutions magically within assemble_variable.
61 However, this late fiddling of the symbol table conflicts with LTO and
62 whole-program compilation. Therefore we must now make all the changes
63 to the symbol table early in the GIMPLE optimization path, before we
64 write things out to LTO intermediate files. */
66 /* Value for TLS varpool node where a pointer to control variable and
67 access variable are stored. */
68 struct tls_var_data
70 varpool_node *control_var;
71 tree access;
74 /* TLS map accesses mapping between a TLS varpool node and a pair
75 made by control variable and access variable. */
76 static hash_map<varpool_node *, tls_var_data> *tls_map = NULL;
78 /* The type of the control structure, shared with the emutls.c runtime. */
79 static tree emutls_object_type;
81 #if !defined (NO_DOT_IN_LABEL)
82 # define EMUTLS_SEPARATOR "."
83 #elif !defined (NO_DOLLAR_IN_LABEL)
84 # define EMUTLS_SEPARATOR "$"
85 #else
86 # define EMUTLS_SEPARATOR "_"
87 #endif
89 /* Create an IDENTIFIER_NODE by prefixing PREFIX to the
90 IDENTIFIER_NODE NAME's name. */
92 static tree
93 prefix_name (const char *prefix, tree name)
95 unsigned plen = strlen (prefix);
96 unsigned nlen = strlen (IDENTIFIER_POINTER (name));
97 char *toname = (char *) alloca (plen + nlen + 1);
99 memcpy (toname, prefix, plen);
100 memcpy (toname + plen, IDENTIFIER_POINTER (name), nlen + 1);
102 return get_identifier (toname);
105 /* Create an identifier for the struct __emutls_object, given an identifier
106 of the DECL_ASSEMBLY_NAME of the original object. */
108 static tree
109 get_emutls_object_name (tree name)
111 const char *prefix = (targetm.emutls.var_prefix
112 ? targetm.emutls.var_prefix
113 : "__emutls_v" EMUTLS_SEPARATOR);
114 return prefix_name (prefix, name);
117 /* Create the fields of the type for the control variables. Ordinarily
118 this must match struct __emutls_object defined in emutls.c. However
119 this is a target hook so that VxWorks can define its own layout. */
121 tree
122 default_emutls_var_fields (tree type, tree *name ATTRIBUTE_UNUSED)
124 tree word_type_node, field, next_field;
126 field = build_decl (UNKNOWN_LOCATION,
127 FIELD_DECL, get_identifier ("__templ"), ptr_type_node);
128 DECL_CONTEXT (field) = type;
129 next_field = field;
131 field = build_decl (UNKNOWN_LOCATION,
132 FIELD_DECL, get_identifier ("__offset"),
133 ptr_type_node);
134 DECL_CONTEXT (field) = type;
135 DECL_CHAIN (field) = next_field;
136 next_field = field;
138 word_type_node = lang_hooks.types.type_for_mode (word_mode, 1);
139 field = build_decl (UNKNOWN_LOCATION,
140 FIELD_DECL, get_identifier ("__align"),
141 word_type_node);
142 DECL_CONTEXT (field) = type;
143 DECL_CHAIN (field) = next_field;
144 next_field = field;
146 field = build_decl (UNKNOWN_LOCATION,
147 FIELD_DECL, get_identifier ("__size"), word_type_node);
148 DECL_CONTEXT (field) = type;
149 DECL_CHAIN (field) = next_field;
151 return field;
154 /* Initialize emulated tls object TO, which refers to TLS variable DECL and
155 is initialized by PROXY. As above, this is the default implementation of
156 a target hook overridden by VxWorks. */
158 tree
159 default_emutls_var_init (tree to, tree decl, tree proxy)
161 vec<constructor_elt, va_gc> *v;
162 vec_alloc (v, 4);
163 constructor_elt elt;
164 tree type = TREE_TYPE (to);
165 tree field = TYPE_FIELDS (type);
167 elt.index = field;
168 elt.value = fold_convert (TREE_TYPE (field), DECL_SIZE_UNIT (decl));
169 v->quick_push (elt);
171 field = DECL_CHAIN (field);
172 elt.index = field;
173 elt.value = build_int_cst (TREE_TYPE (field),
174 DECL_ALIGN_UNIT (decl));
175 v->quick_push (elt);
177 field = DECL_CHAIN (field);
178 elt.index = field;
179 elt.value = null_pointer_node;
180 v->quick_push (elt);
182 field = DECL_CHAIN (field);
183 elt.index = field;
184 elt.value = proxy;
185 v->quick_push (elt);
187 return build_constructor (type, v);
190 /* Create the structure for struct __emutls_object. This should match the
191 structure at the top of emutls.c, modulo the union there. */
193 static tree
194 get_emutls_object_type (void)
196 tree type, type_name, field;
198 type = emutls_object_type;
199 if (type)
200 return type;
202 emutls_object_type = type = lang_hooks.types.make_type (RECORD_TYPE);
203 type_name = NULL;
204 field = targetm.emutls.var_fields (type, &type_name);
205 if (!type_name)
206 type_name = get_identifier ("__emutls_object");
207 type_name = build_decl (UNKNOWN_LOCATION,
208 TYPE_DECL, type_name, type);
209 TYPE_NAME (type) = type_name;
210 TYPE_FIELDS (type) = field;
211 layout_type (type);
213 return type;
216 /* Create a read-only variable like DECL, with the same DECL_INITIAL.
217 This will be used for initializing the emulated tls data area. */
219 static tree
220 get_emutls_init_templ_addr (tree decl)
222 tree name, to;
224 if (targetm.emutls.register_common && !DECL_INITIAL (decl)
225 && !DECL_SECTION_NAME (decl))
226 return null_pointer_node;
228 name = DECL_ASSEMBLER_NAME (decl);
229 if (!targetm.emutls.tmpl_prefix || targetm.emutls.tmpl_prefix[0])
231 const char *prefix = (targetm.emutls.tmpl_prefix
232 ? targetm.emutls.tmpl_prefix
233 : "__emutls_t" EMUTLS_SEPARATOR);
234 name = prefix_name (prefix, name);
237 to = build_decl (DECL_SOURCE_LOCATION (decl),
238 VAR_DECL, name, TREE_TYPE (decl));
239 SET_DECL_ASSEMBLER_NAME (to, DECL_NAME (to));
241 DECL_ARTIFICIAL (to) = 1;
242 TREE_USED (to) = TREE_USED (decl);
243 TREE_READONLY (to) = 1;
244 DECL_IGNORED_P (to) = 1;
245 DECL_CONTEXT (to) = DECL_CONTEXT (decl);
246 DECL_PRESERVE_P (to) = DECL_PRESERVE_P (decl);
248 DECL_WEAK (to) = DECL_WEAK (decl);
249 if (DECL_ONE_ONLY (decl))
251 TREE_STATIC (to) = TREE_STATIC (decl);
252 TREE_PUBLIC (to) = TREE_PUBLIC (decl);
253 DECL_VISIBILITY (to) = DECL_VISIBILITY (decl);
254 make_decl_one_only (to, DECL_ASSEMBLER_NAME (to));
256 else
257 TREE_STATIC (to) = 1;
259 DECL_VISIBILITY_SPECIFIED (to) = DECL_VISIBILITY_SPECIFIED (decl);
260 DECL_INITIAL (to) = DECL_INITIAL (decl);
261 DECL_INITIAL (decl) = NULL;
263 if (targetm.emutls.tmpl_section)
264 set_decl_section_name (to, targetm.emutls.tmpl_section);
265 else
266 set_decl_section_name (to, DECL_SECTION_NAME (decl));
268 /* Create varpool node for the new variable and finalize it if it is
269 not external one. */
270 if (DECL_EXTERNAL (to))
271 varpool_node::get_create (to);
272 else
273 varpool_node::add (to);
274 return build_fold_addr_expr (to);
277 /* Create and return the control variable for the TLS variable DECL. */
279 static tree
280 new_emutls_decl (tree decl, tree alias_of)
282 tree name, to;
284 name = DECL_ASSEMBLER_NAME (decl);
285 to = build_decl (DECL_SOURCE_LOCATION (decl), VAR_DECL,
286 get_emutls_object_name (name),
287 get_emutls_object_type ());
289 SET_DECL_ASSEMBLER_NAME (to, DECL_NAME (to));
291 DECL_ARTIFICIAL (to) = 1;
292 DECL_IGNORED_P (to) = 1;
293 TREE_READONLY (to) = 0;
294 TREE_STATIC (to) = 1;
296 DECL_PRESERVE_P (to) = DECL_PRESERVE_P (decl);
297 DECL_CONTEXT (to) = DECL_CONTEXT (decl);
298 TREE_USED (to) = TREE_USED (decl);
299 TREE_PUBLIC (to) = TREE_PUBLIC (decl);
300 DECL_EXTERNAL (to) = DECL_EXTERNAL (decl);
301 DECL_COMMON (to) = DECL_COMMON (decl);
302 DECL_WEAK (to) = DECL_WEAK (decl);
303 DECL_VISIBILITY (to) = DECL_VISIBILITY (decl);
304 DECL_VISIBILITY_SPECIFIED (to) = DECL_VISIBILITY_SPECIFIED (decl);
305 DECL_DLLIMPORT_P (to) = DECL_DLLIMPORT_P (decl);
307 DECL_ATTRIBUTES (to) = targetm.merge_decl_attributes (decl, to);
309 if (DECL_ONE_ONLY (decl))
310 make_decl_one_only (to, DECL_ASSEMBLER_NAME (to));
312 set_decl_tls_model (to, TLS_MODEL_EMULATED);
314 /* If we're not allowed to change the proxy object's alignment,
315 pretend it has been set by the user. */
316 if (targetm.emutls.var_align_fixed)
317 DECL_USER_ALIGN (to) = 1;
319 /* If the target wants the control variables grouped, do so. */
320 if (!DECL_COMMON (to) && targetm.emutls.var_section)
322 set_decl_section_name (to, targetm.emutls.var_section);
325 /* If this variable is defined locally, then we need to initialize the
326 control structure with size and alignment information. Initialization
327 of COMMON block variables happens elsewhere via a constructor. */
328 if (!DECL_EXTERNAL (to)
329 && (!DECL_COMMON (to)
330 || (DECL_INITIAL (decl)
331 && DECL_INITIAL (decl) != error_mark_node)))
333 tree tmpl = get_emutls_init_templ_addr (decl);
334 DECL_INITIAL (to) = targetm.emutls.var_init (to, decl, tmpl);
335 record_references_in_initializer (to, false);
338 /* Create varpool node for the new variable and finalize it if it is
339 not external one. */
340 if (DECL_EXTERNAL (to))
341 varpool_node::get_create (to);
342 else if (!alias_of)
343 varpool_node::add (to);
344 else
346 varpool_node *n;
347 varpool_node *t = varpool_node::get_for_asmname
348 (DECL_ASSEMBLER_NAME (DECL_VALUE_EXPR (alias_of)));
350 n = varpool_node::create_alias (to, t->decl);
351 n->resolve_alias (t);
353 return to;
356 /* Generate a call statement to initialize CONTROL_DECL for TLS_DECL.
357 This only needs to happen for TLS COMMON variables; non-COMMON
358 variables can be initialized statically. Insert the generated
359 call statement at the end of PSTMTS. */
361 static void
362 emutls_common_1 (tree tls_decl, tree control_decl, tree *pstmts)
364 tree x;
365 tree word_type_node;
367 if (! DECL_COMMON (tls_decl)
368 || (DECL_INITIAL (tls_decl)
369 && DECL_INITIAL (tls_decl) != error_mark_node))
370 return;
372 word_type_node = lang_hooks.types.type_for_mode (word_mode, 1);
374 x = build_call_expr (builtin_decl_explicit (BUILT_IN_EMUTLS_REGISTER_COMMON),
375 4, build_fold_addr_expr (control_decl),
376 fold_convert (word_type_node,
377 DECL_SIZE_UNIT (tls_decl)),
378 build_int_cst (word_type_node,
379 DECL_ALIGN_UNIT (tls_decl)),
380 get_emutls_init_templ_addr (tls_decl));
382 append_to_statement_list (x, pstmts);
385 struct lower_emutls_data
387 struct cgraph_node *cfun_node;
388 struct cgraph_node *builtin_node;
389 tree builtin_decl;
390 basic_block bb;
391 int bb_freq;
392 location_t loc;
393 gimple_seq seq;
396 /* Given a TLS variable DECL, return an SSA_NAME holding its address.
397 Append any new computation statements required to D->SEQ. */
399 static tree
400 gen_emutls_addr (tree decl, struct lower_emutls_data *d)
402 /* Compute the address of the TLS variable with help from runtime. */
403 tls_var_data *data = tls_map->get (varpool_node::get (decl));
404 tree addr = data->access;
406 if (addr == NULL)
408 varpool_node *cvar;
409 tree cdecl;
410 gcall *x;
412 cvar = data->control_var;
413 cdecl = cvar->decl;
414 TREE_ADDRESSABLE (cdecl) = 1;
416 addr = create_tmp_var (build_pointer_type (TREE_TYPE (decl)));
417 x = gimple_build_call (d->builtin_decl, 1, build_fold_addr_expr (cdecl));
418 gimple_set_location (x, d->loc);
420 addr = make_ssa_name (addr, x);
421 gimple_call_set_lhs (x, addr);
423 gimple_seq_add_stmt (&d->seq, x);
425 d->cfun_node->create_edge (d->builtin_node, x, d->bb->count, d->bb_freq);
427 /* We may be adding a new reference to a new variable to the function.
428 This means we have to play with the ipa-reference web. */
429 d->cfun_node->create_reference (cvar, IPA_REF_ADDR, x);
431 /* Record this ssa_name for possible use later in the basic block. */
432 data->access = addr;
435 return addr;
438 /* Callback for walk_gimple_op. D = WI->INFO is a struct lower_emutls_data.
439 Given an operand *PTR within D->STMT, if the operand references a TLS
440 variable, then lower the reference to a call to the runtime. Insert
441 any new statements required into D->SEQ; the caller is responsible for
442 placing those appropriately. */
444 static tree
445 lower_emutls_1 (tree *ptr, int *walk_subtrees, void *cb_data)
447 struct walk_stmt_info *wi = (struct walk_stmt_info *) cb_data;
448 struct lower_emutls_data *d = (struct lower_emutls_data *) wi->info;
449 tree t = *ptr;
450 bool is_addr = false;
451 tree addr;
453 *walk_subtrees = 0;
455 switch (TREE_CODE (t))
457 case ADDR_EXPR:
458 /* If this is not a straight-forward "&var", but rather something
459 like "&var.a", then we may need special handling. */
460 if (TREE_CODE (TREE_OPERAND (t, 0)) != VAR_DECL)
462 bool save_changed;
464 /* If we're allowed more than just is_gimple_val, continue. */
465 if (!wi->val_only)
467 *walk_subtrees = 1;
468 return NULL_TREE;
471 /* See if any substitution would be made. */
472 save_changed = wi->changed;
473 wi->changed = false;
474 wi->val_only = false;
475 walk_tree (&TREE_OPERAND (t, 0), lower_emutls_1, wi, NULL);
476 wi->val_only = true;
478 /* If so, then extract this entire sub-expression "&p->a" into a
479 new assignment statement, and substitute yet another SSA_NAME. */
480 if (wi->changed)
482 gimple *x;
484 addr = create_tmp_var (TREE_TYPE (t));
485 x = gimple_build_assign (addr, t);
486 gimple_set_location (x, d->loc);
488 addr = make_ssa_name (addr, x);
489 gimple_assign_set_lhs (x, addr);
491 gimple_seq_add_stmt (&d->seq, x);
493 *ptr = addr;
495 else
496 wi->changed = save_changed;
498 return NULL_TREE;
501 t = TREE_OPERAND (t, 0);
502 is_addr = true;
503 /* FALLTHRU */
505 case VAR_DECL:
506 if (!DECL_THREAD_LOCAL_P (t))
507 return NULL_TREE;
508 break;
510 default:
511 /* We're not interested in other decls or types, only subexpressions. */
512 if (EXPR_P (t))
513 *walk_subtrees = 1;
514 /* FALLTHRU */
516 case SSA_NAME:
517 /* Special-case the return of SSA_NAME, since it's so common. */
518 return NULL_TREE;
521 addr = gen_emutls_addr (t, d);
522 if (is_addr)
524 /* Replace "&var" with "addr" in the statement. */
525 *ptr = addr;
527 else
529 /* Replace "var" with "*addr" in the statement. */
530 t = build2 (MEM_REF, TREE_TYPE (t), addr,
531 build_int_cst (TREE_TYPE (addr), 0));
532 *ptr = t;
535 wi->changed = true;
536 return NULL_TREE;
539 /* Lower all of the operands of STMT. */
541 static void
542 lower_emutls_stmt (gimple *stmt, struct lower_emutls_data *d)
544 struct walk_stmt_info wi;
546 d->loc = gimple_location (stmt);
548 memset (&wi, 0, sizeof (wi));
549 wi.info = d;
550 wi.val_only = true;
551 walk_gimple_op (stmt, lower_emutls_1, &wi);
553 if (wi.changed)
554 update_stmt (stmt);
557 /* Lower the I'th operand of PHI. */
559 static void
560 lower_emutls_phi_arg (gphi *phi, unsigned int i,
561 struct lower_emutls_data *d)
563 struct walk_stmt_info wi;
564 struct phi_arg_d *pd = gimple_phi_arg (phi, i);
566 /* Early out for a very common case we don't care about. */
567 if (TREE_CODE (pd->def) == SSA_NAME)
568 return;
570 d->loc = pd->locus;
572 memset (&wi, 0, sizeof (wi));
573 wi.info = d;
574 wi.val_only = true;
575 walk_tree (&pd->def, lower_emutls_1, &wi, NULL);
577 /* For normal statements, we let update_stmt do its job. But for phi
578 nodes, we have to manipulate the immediate use list by hand. */
579 if (wi.changed)
581 gcc_assert (TREE_CODE (pd->def) == SSA_NAME);
582 link_imm_use_stmt (&pd->imm_use, pd->def, phi);
586 /* Reset access variable for a given TLS variable data DATA. */
588 bool
589 reset_access (varpool_node * const &, tls_var_data *data, void *)
591 data->access = NULL;
593 return true;
596 /* Clear the access variables, in order to begin a new block. */
598 static inline void
599 clear_access_vars (void)
601 tls_map->traverse<void *, reset_access> (NULL);
604 /* Lower the entire function NODE. */
606 static void
607 lower_emutls_function_body (struct cgraph_node *node)
609 struct lower_emutls_data d;
610 bool any_edge_inserts = false;
612 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
614 d.cfun_node = node;
615 d.builtin_decl = builtin_decl_explicit (BUILT_IN_EMUTLS_GET_ADDRESS);
616 /* This is where we introduce the declaration to the IL and so we have to
617 create a node for it. */
618 d.builtin_node = cgraph_node::get_create (d.builtin_decl);
620 FOR_EACH_BB_FN (d.bb, cfun)
622 unsigned int i, nedge;
624 /* Lower each of the PHI nodes of the block, as we may have
625 propagated &tlsvar into a PHI argument. These loops are
626 arranged so that we process each edge at once, and each
627 PHI argument for that edge. */
628 if (!gimple_seq_empty_p (phi_nodes (d.bb)))
630 /* The calls will be inserted on the edges, and the frequencies
631 will be computed during the commit process. */
632 d.bb_freq = 0;
634 nedge = EDGE_COUNT (d.bb->preds);
635 for (i = 0; i < nedge; ++i)
637 edge e = EDGE_PRED (d.bb, i);
639 /* We can re-use any SSA_NAME created on this edge. */
640 clear_access_vars ();
641 d.seq = NULL;
643 for (gphi_iterator gsi = gsi_start_phis (d.bb);
644 !gsi_end_p (gsi);
645 gsi_next (&gsi))
646 lower_emutls_phi_arg (gsi.phi (), i, &d);
648 /* Insert all statements generated by all phi nodes for this
649 particular edge all at once. */
650 if (d.seq)
652 gsi_insert_seq_on_edge (e, d.seq);
653 any_edge_inserts = true;
658 d.bb_freq = compute_call_stmt_bb_frequency (current_function_decl, d.bb);
660 /* We can re-use any SSA_NAME created during this basic block. */
661 clear_access_vars ();
663 /* Lower each of the statements of the block. */
664 for (gimple_stmt_iterator gsi = gsi_start_bb (d.bb); !gsi_end_p (gsi);
665 gsi_next (&gsi))
667 d.seq = NULL;
668 lower_emutls_stmt (gsi_stmt (gsi), &d);
670 /* If any new statements were created, insert them immediately
671 before the first use. This prevents variable lifetimes from
672 becoming unnecessarily long. */
673 if (d.seq)
674 gsi_insert_seq_before (&gsi, d.seq, GSI_SAME_STMT);
678 if (any_edge_inserts)
679 gsi_commit_edge_inserts ();
681 pop_cfun ();
684 /* Create emutls variable for VAR, DATA is pointer to static
685 ctor body we can add constructors to.
686 Callback for varpool_for_variable_and_aliases. */
688 static bool
689 create_emultls_var (varpool_node *var, void *data)
691 tree cdecl;
692 tls_var_data value;
694 cdecl = new_emutls_decl (var->decl,
695 var->alias && var->analyzed
696 ? var->get_alias_target ()->decl : NULL);
698 varpool_node *cvar = varpool_node::get (cdecl);
700 if (!var->alias)
702 /* Make sure the COMMON block control variable gets initialized.
703 Note that there's no point in doing this for aliases; we only
704 need to do this once for the main variable. */
705 emutls_common_1 (var->decl, cdecl, (tree *)data);
707 if (var->alias && !var->analyzed)
708 cvar->alias = true;
710 /* Indicate that the value of the TLS variable may be found elsewhere,
711 preventing the variable from re-appearing in the GIMPLE. We cheat
712 and use the control variable here (rather than a full call_expr),
713 which is special-cased inside the DWARF2 output routines. */
714 SET_DECL_VALUE_EXPR (var->decl, cdecl);
715 DECL_HAS_VALUE_EXPR_P (var->decl) = 1;
717 value.control_var = cvar;
718 tls_map->put (var, value);
720 return false;
723 /* Main entry point to the tls lowering pass. */
725 static unsigned int
726 ipa_lower_emutls (void)
728 varpool_node *var;
729 cgraph_node *func;
730 bool any_aliases = false;
731 tree ctor_body = NULL;
732 hash_set <varpool_node *> visited;
733 auto_vec <varpool_node *> tls_vars;
735 /* Examine all global variables for TLS variables. */
736 FOR_EACH_VARIABLE (var)
737 if (DECL_THREAD_LOCAL_P (var->decl)
738 && !visited.add (var))
740 gcc_checking_assert (TREE_STATIC (var->decl)
741 || DECL_EXTERNAL (var->decl));
742 tls_vars.safe_push (var);
743 if (var->alias && var->definition
744 && !visited.add (var->ultimate_alias_target ()))
745 tls_vars.safe_push (var->ultimate_alias_target ());
748 /* If we found no TLS variables, then there is no further work to do. */
749 if (tls_vars.is_empty ())
751 if (dump_file)
752 fprintf (dump_file, "No TLS variables found.\n");
753 return 0;
756 tls_map = new hash_map <varpool_node *, tls_var_data> ();
758 /* Create the control variables for each TLS variable. */
759 for (unsigned i = 0; i < tls_vars.length (); i++)
761 var = tls_vars[i];
763 if (var->alias && !var->analyzed)
764 any_aliases = true;
765 else if (!var->alias)
766 var->call_for_symbol_and_aliases (create_emultls_var, &ctor_body, true);
769 /* If there were any aliases, then frob the alias_pairs vector. */
770 if (any_aliases)
772 alias_pair *p;
773 unsigned int i;
774 FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
775 if (DECL_THREAD_LOCAL_P (p->decl))
777 p->decl = tls_map->get
778 (varpool_node::get (p->decl))->control_var->decl;
779 p->target = get_emutls_object_name (p->target);
783 /* Adjust all uses of TLS variables within the function bodies. */
784 FOR_EACH_DEFINED_FUNCTION (func)
785 if (func->lowered)
786 lower_emutls_function_body (func);
788 /* Generate the constructor for any COMMON control variables created. */
789 if (ctor_body)
790 cgraph_build_static_cdtor ('I', ctor_body, DEFAULT_INIT_PRIORITY);
792 delete tls_map;
794 return 0;
797 namespace {
799 const pass_data pass_data_ipa_lower_emutls =
801 SIMPLE_IPA_PASS, /* type */
802 "emutls", /* name */
803 OPTGROUP_NONE, /* optinfo_flags */
804 TV_IPA_OPT, /* tv_id */
805 ( PROP_cfg | PROP_ssa ), /* properties_required */
806 0, /* properties_provided */
807 0, /* properties_destroyed */
808 0, /* todo_flags_start */
809 0, /* todo_flags_finish */
812 class pass_ipa_lower_emutls : public simple_ipa_opt_pass
814 public:
815 pass_ipa_lower_emutls (gcc::context *ctxt)
816 : simple_ipa_opt_pass (pass_data_ipa_lower_emutls, ctxt)
819 /* opt_pass methods: */
820 virtual bool gate (function *)
822 /* If the target supports TLS natively, we need do nothing here. */
823 return !targetm.have_tls;
826 virtual unsigned int execute (function *) { return ipa_lower_emutls (); }
828 }; // class pass_ipa_lower_emutls
830 } // anon namespace
832 simple_ipa_opt_pass *
833 make_pass_ipa_lower_emutls (gcc::context *ctxt)
835 return new pass_ipa_lower_emutls (ctxt);