2015-06-24 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / gcc / tree-emutls.c
blobea6309000690a3bb939d9dbdc0c3835c5307fa3a
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 "symtab.h"
25 #include "options.h"
26 #include "tree.h"
27 #include "fold-const.h"
28 #include "stor-layout.h"
29 #include "varasm.h"
30 #include "predict.h"
31 #include "tm.h"
32 #include "hard-reg-set.h"
33 #include "function.h"
34 #include "dominance.h"
35 #include "cfg.h"
36 #include "basic-block.h"
37 #include "tree-ssa-alias.h"
38 #include "internal-fn.h"
39 #include "gimple-expr.h"
40 #include "gimple.h"
41 #include "gimple-iterator.h"
42 #include "gimple-walk.h"
43 #include "tree-pass.h"
44 #include "gimple-ssa.h"
45 #include "plugin-api.h"
46 #include "ipa-ref.h"
47 #include "cgraph.h"
48 #include "tree-phinodes.h"
49 #include "ssa-iterators.h"
50 #include "stringpool.h"
51 #include "tree-ssanames.h"
52 #include "langhooks.h"
53 #include "target.h"
54 #include "targhooks.h"
55 #include "tree-iterator.h"
57 /* Whenever a target does not support thread-local storage (TLS) natively,
58 we can emulate it with some run-time support in libgcc. This will in
59 turn rely on "keyed storage" a-la pthread_key_create; essentially all
60 thread libraries provide such functionality.
62 In order to coordinate with the libgcc runtime, each TLS variable is
63 described by a "control variable". This control variable records the
64 required size, alignment, and initial value of the TLS variable for
65 instantiation at runtime. It also stores an integer token to be used
66 by the runtime to find the address of the variable within each thread.
68 On the compiler side, this means that we need to replace all instances
69 of "tls_var" in the code with "*__emutls_get_addr(&control_var)". We
70 also need to eliminate "tls_var" from the symbol table and introduce
71 "control_var".
73 We used to perform all of the transformations during conversion to rtl,
74 and the variable substitutions magically within assemble_variable.
75 However, this late fiddling of the symbol table conflicts with LTO and
76 whole-program compilation. Therefore we must now make all the changes
77 to the symbol table early in the GIMPLE optimization path, before we
78 write things out to LTO intermediate files. */
80 /* Value for TLS varpool node where a pointer to control variable and
81 access variable are stored. */
82 struct tls_var_data
84 varpool_node *control_var;
85 tree access;
88 /* TLS map accesses mapping between a TLS varpool node and a pair
89 made by control variable and access variable. */
90 static hash_map<varpool_node *, tls_var_data> *tls_map = NULL;
92 /* The type of the control structure, shared with the emutls.c runtime. */
93 static tree emutls_object_type;
95 #if !defined (NO_DOT_IN_LABEL)
96 # define EMUTLS_SEPARATOR "."
97 #elif !defined (NO_DOLLAR_IN_LABEL)
98 # define EMUTLS_SEPARATOR "$"
99 #else
100 # define EMUTLS_SEPARATOR "_"
101 #endif
103 /* Create an IDENTIFIER_NODE by prefixing PREFIX to the
104 IDENTIFIER_NODE NAME's name. */
106 static tree
107 prefix_name (const char *prefix, tree name)
109 unsigned plen = strlen (prefix);
110 unsigned nlen = strlen (IDENTIFIER_POINTER (name));
111 char *toname = (char *) alloca (plen + nlen + 1);
113 memcpy (toname, prefix, plen);
114 memcpy (toname + plen, IDENTIFIER_POINTER (name), nlen + 1);
116 return get_identifier (toname);
119 /* Create an identifier for the struct __emutls_object, given an identifier
120 of the DECL_ASSEMBLY_NAME of the original object. */
122 static tree
123 get_emutls_object_name (tree name)
125 const char *prefix = (targetm.emutls.var_prefix
126 ? targetm.emutls.var_prefix
127 : "__emutls_v" EMUTLS_SEPARATOR);
128 return prefix_name (prefix, name);
131 /* Create the fields of the type for the control variables. Ordinarily
132 this must match struct __emutls_object defined in emutls.c. However
133 this is a target hook so that VxWorks can define its own layout. */
135 tree
136 default_emutls_var_fields (tree type, tree *name ATTRIBUTE_UNUSED)
138 tree word_type_node, field, next_field;
140 field = build_decl (UNKNOWN_LOCATION,
141 FIELD_DECL, get_identifier ("__templ"), ptr_type_node);
142 DECL_CONTEXT (field) = type;
143 next_field = field;
145 field = build_decl (UNKNOWN_LOCATION,
146 FIELD_DECL, get_identifier ("__offset"),
147 ptr_type_node);
148 DECL_CONTEXT (field) = type;
149 DECL_CHAIN (field) = next_field;
150 next_field = field;
152 word_type_node = lang_hooks.types.type_for_mode (word_mode, 1);
153 field = build_decl (UNKNOWN_LOCATION,
154 FIELD_DECL, get_identifier ("__align"),
155 word_type_node);
156 DECL_CONTEXT (field) = type;
157 DECL_CHAIN (field) = next_field;
158 next_field = field;
160 field = build_decl (UNKNOWN_LOCATION,
161 FIELD_DECL, get_identifier ("__size"), word_type_node);
162 DECL_CONTEXT (field) = type;
163 DECL_CHAIN (field) = next_field;
165 return field;
168 /* Initialize emulated tls object TO, which refers to TLS variable DECL and
169 is initialized by PROXY. As above, this is the default implementation of
170 a target hook overridden by VxWorks. */
172 tree
173 default_emutls_var_init (tree to, tree decl, tree proxy)
175 vec<constructor_elt, va_gc> *v;
176 vec_alloc (v, 4);
177 constructor_elt elt;
178 tree type = TREE_TYPE (to);
179 tree field = TYPE_FIELDS (type);
181 elt.index = field;
182 elt.value = fold_convert (TREE_TYPE (field), DECL_SIZE_UNIT (decl));
183 v->quick_push (elt);
185 field = DECL_CHAIN (field);
186 elt.index = field;
187 elt.value = build_int_cst (TREE_TYPE (field),
188 DECL_ALIGN_UNIT (decl));
189 v->quick_push (elt);
191 field = DECL_CHAIN (field);
192 elt.index = field;
193 elt.value = null_pointer_node;
194 v->quick_push (elt);
196 field = DECL_CHAIN (field);
197 elt.index = field;
198 elt.value = proxy;
199 v->quick_push (elt);
201 return build_constructor (type, v);
204 /* Create the structure for struct __emutls_object. This should match the
205 structure at the top of emutls.c, modulo the union there. */
207 static tree
208 get_emutls_object_type (void)
210 tree type, type_name, field;
212 type = emutls_object_type;
213 if (type)
214 return type;
216 emutls_object_type = type = lang_hooks.types.make_type (RECORD_TYPE);
217 type_name = NULL;
218 field = targetm.emutls.var_fields (type, &type_name);
219 if (!type_name)
220 type_name = get_identifier ("__emutls_object");
221 type_name = build_decl (UNKNOWN_LOCATION,
222 TYPE_DECL, type_name, type);
223 TYPE_NAME (type) = type_name;
224 TYPE_FIELDS (type) = field;
225 layout_type (type);
227 return type;
230 /* Create a read-only variable like DECL, with the same DECL_INITIAL.
231 This will be used for initializing the emulated tls data area. */
233 static tree
234 get_emutls_init_templ_addr (tree decl)
236 tree name, to;
238 if (targetm.emutls.register_common && !DECL_INITIAL (decl)
239 && !DECL_SECTION_NAME (decl))
240 return null_pointer_node;
242 name = DECL_ASSEMBLER_NAME (decl);
243 if (!targetm.emutls.tmpl_prefix || targetm.emutls.tmpl_prefix[0])
245 const char *prefix = (targetm.emutls.tmpl_prefix
246 ? targetm.emutls.tmpl_prefix
247 : "__emutls_t" EMUTLS_SEPARATOR);
248 name = prefix_name (prefix, name);
251 to = build_decl (DECL_SOURCE_LOCATION (decl),
252 VAR_DECL, name, TREE_TYPE (decl));
253 SET_DECL_ASSEMBLER_NAME (to, DECL_NAME (to));
255 DECL_ARTIFICIAL (to) = 1;
256 TREE_USED (to) = TREE_USED (decl);
257 TREE_READONLY (to) = 1;
258 DECL_IGNORED_P (to) = 1;
259 DECL_CONTEXT (to) = DECL_CONTEXT (decl);
260 DECL_PRESERVE_P (to) = DECL_PRESERVE_P (decl);
262 DECL_WEAK (to) = DECL_WEAK (decl);
263 if (DECL_ONE_ONLY (decl))
265 TREE_STATIC (to) = TREE_STATIC (decl);
266 TREE_PUBLIC (to) = TREE_PUBLIC (decl);
267 DECL_VISIBILITY (to) = DECL_VISIBILITY (decl);
268 make_decl_one_only (to, DECL_ASSEMBLER_NAME (to));
270 else
271 TREE_STATIC (to) = 1;
273 DECL_VISIBILITY_SPECIFIED (to) = DECL_VISIBILITY_SPECIFIED (decl);
274 DECL_INITIAL (to) = DECL_INITIAL (decl);
275 DECL_INITIAL (decl) = NULL;
277 if (targetm.emutls.tmpl_section)
278 set_decl_section_name (to, targetm.emutls.tmpl_section);
279 else
280 set_decl_section_name (to, DECL_SECTION_NAME (decl));
282 /* Create varpool node for the new variable and finalize it if it is
283 not external one. */
284 if (DECL_EXTERNAL (to))
285 varpool_node::get_create (to);
286 else
287 varpool_node::add (to);
288 return build_fold_addr_expr (to);
291 /* Create and return the control variable for the TLS variable DECL. */
293 static tree
294 new_emutls_decl (tree decl, tree alias_of)
296 tree name, to;
298 name = DECL_ASSEMBLER_NAME (decl);
299 to = build_decl (DECL_SOURCE_LOCATION (decl), VAR_DECL,
300 get_emutls_object_name (name),
301 get_emutls_object_type ());
303 SET_DECL_ASSEMBLER_NAME (to, DECL_NAME (to));
305 DECL_ARTIFICIAL (to) = 1;
306 DECL_IGNORED_P (to) = 1;
307 TREE_READONLY (to) = 0;
308 TREE_STATIC (to) = 1;
310 DECL_PRESERVE_P (to) = DECL_PRESERVE_P (decl);
311 DECL_CONTEXT (to) = DECL_CONTEXT (decl);
312 TREE_USED (to) = TREE_USED (decl);
313 TREE_PUBLIC (to) = TREE_PUBLIC (decl);
314 DECL_EXTERNAL (to) = DECL_EXTERNAL (decl);
315 DECL_COMMON (to) = DECL_COMMON (decl);
316 DECL_WEAK (to) = DECL_WEAK (decl);
317 DECL_VISIBILITY (to) = DECL_VISIBILITY (decl);
318 DECL_VISIBILITY_SPECIFIED (to) = DECL_VISIBILITY_SPECIFIED (decl);
319 DECL_DLLIMPORT_P (to) = DECL_DLLIMPORT_P (decl);
321 DECL_ATTRIBUTES (to) = targetm.merge_decl_attributes (decl, to);
323 if (DECL_ONE_ONLY (decl))
324 make_decl_one_only (to, DECL_ASSEMBLER_NAME (to));
326 set_decl_tls_model (to, TLS_MODEL_EMULATED);
328 /* If we're not allowed to change the proxy object's alignment,
329 pretend it has been set by the user. */
330 if (targetm.emutls.var_align_fixed)
331 DECL_USER_ALIGN (to) = 1;
333 /* If the target wants the control variables grouped, do so. */
334 if (!DECL_COMMON (to) && targetm.emutls.var_section)
336 set_decl_section_name (to, targetm.emutls.var_section);
339 /* If this variable is defined locally, then we need to initialize the
340 control structure with size and alignment information. Initialization
341 of COMMON block variables happens elsewhere via a constructor. */
342 if (!DECL_EXTERNAL (to)
343 && (!DECL_COMMON (to)
344 || (DECL_INITIAL (decl)
345 && DECL_INITIAL (decl) != error_mark_node)))
347 tree tmpl = get_emutls_init_templ_addr (decl);
348 DECL_INITIAL (to) = targetm.emutls.var_init (to, decl, tmpl);
349 record_references_in_initializer (to, false);
352 /* Create varpool node for the new variable and finalize it if it is
353 not external one. */
354 if (DECL_EXTERNAL (to))
355 varpool_node::get_create (to);
356 else if (!alias_of)
357 varpool_node::add (to);
358 else
360 varpool_node *n;
361 varpool_node *t = varpool_node::get_for_asmname
362 (DECL_ASSEMBLER_NAME (DECL_VALUE_EXPR (alias_of)));
364 n = varpool_node::create_alias (to, t->decl);
365 n->resolve_alias (t);
367 return to;
370 /* Generate a call statement to initialize CONTROL_DECL for TLS_DECL.
371 This only needs to happen for TLS COMMON variables; non-COMMON
372 variables can be initialized statically. Insert the generated
373 call statement at the end of PSTMTS. */
375 static void
376 emutls_common_1 (tree tls_decl, tree control_decl, tree *pstmts)
378 tree x;
379 tree word_type_node;
381 if (! DECL_COMMON (tls_decl)
382 || (DECL_INITIAL (tls_decl)
383 && DECL_INITIAL (tls_decl) != error_mark_node))
384 return;
386 word_type_node = lang_hooks.types.type_for_mode (word_mode, 1);
388 x = build_call_expr (builtin_decl_explicit (BUILT_IN_EMUTLS_REGISTER_COMMON),
389 4, build_fold_addr_expr (control_decl),
390 fold_convert (word_type_node,
391 DECL_SIZE_UNIT (tls_decl)),
392 build_int_cst (word_type_node,
393 DECL_ALIGN_UNIT (tls_decl)),
394 get_emutls_init_templ_addr (tls_decl));
396 append_to_statement_list (x, pstmts);
399 struct lower_emutls_data
401 struct cgraph_node *cfun_node;
402 struct cgraph_node *builtin_node;
403 tree builtin_decl;
404 basic_block bb;
405 int bb_freq;
406 location_t loc;
407 gimple_seq seq;
410 /* Given a TLS variable DECL, return an SSA_NAME holding its address.
411 Append any new computation statements required to D->SEQ. */
413 static tree
414 gen_emutls_addr (tree decl, struct lower_emutls_data *d)
416 /* Compute the address of the TLS variable with help from runtime. */
417 tls_var_data *data = tls_map->get (varpool_node::get (decl));
418 tree addr = data->access;
420 if (addr == NULL)
422 varpool_node *cvar;
423 tree cdecl;
424 gcall *x;
426 cvar = data->control_var;
427 cdecl = cvar->decl;
428 TREE_ADDRESSABLE (cdecl) = 1;
430 addr = create_tmp_var (build_pointer_type (TREE_TYPE (decl)));
431 x = gimple_build_call (d->builtin_decl, 1, build_fold_addr_expr (cdecl));
432 gimple_set_location (x, d->loc);
434 addr = make_ssa_name (addr, x);
435 gimple_call_set_lhs (x, addr);
437 gimple_seq_add_stmt (&d->seq, x);
439 d->cfun_node->create_edge (d->builtin_node, x, d->bb->count, d->bb_freq);
441 /* We may be adding a new reference to a new variable to the function.
442 This means we have to play with the ipa-reference web. */
443 d->cfun_node->create_reference (cvar, IPA_REF_ADDR, x);
445 /* Record this ssa_name for possible use later in the basic block. */
446 data->access = addr;
449 return addr;
452 /* Callback for walk_gimple_op. D = WI->INFO is a struct lower_emutls_data.
453 Given an operand *PTR within D->STMT, if the operand references a TLS
454 variable, then lower the reference to a call to the runtime. Insert
455 any new statements required into D->SEQ; the caller is responsible for
456 placing those appropriately. */
458 static tree
459 lower_emutls_1 (tree *ptr, int *walk_subtrees, void *cb_data)
461 struct walk_stmt_info *wi = (struct walk_stmt_info *) cb_data;
462 struct lower_emutls_data *d = (struct lower_emutls_data *) wi->info;
463 tree t = *ptr;
464 bool is_addr = false;
465 tree addr;
467 *walk_subtrees = 0;
469 switch (TREE_CODE (t))
471 case ADDR_EXPR:
472 /* If this is not a straight-forward "&var", but rather something
473 like "&var.a", then we may need special handling. */
474 if (TREE_CODE (TREE_OPERAND (t, 0)) != VAR_DECL)
476 bool save_changed;
478 /* If we're allowed more than just is_gimple_val, continue. */
479 if (!wi->val_only)
481 *walk_subtrees = 1;
482 return NULL_TREE;
485 /* See if any substitution would be made. */
486 save_changed = wi->changed;
487 wi->changed = false;
488 wi->val_only = false;
489 walk_tree (&TREE_OPERAND (t, 0), lower_emutls_1, wi, NULL);
490 wi->val_only = true;
492 /* If so, then extract this entire sub-expression "&p->a" into a
493 new assignment statement, and substitute yet another SSA_NAME. */
494 if (wi->changed)
496 gimple x;
498 addr = create_tmp_var (TREE_TYPE (t));
499 x = gimple_build_assign (addr, t);
500 gimple_set_location (x, d->loc);
502 addr = make_ssa_name (addr, x);
503 gimple_assign_set_lhs (x, addr);
505 gimple_seq_add_stmt (&d->seq, x);
507 *ptr = addr;
509 else
510 wi->changed = save_changed;
512 return NULL_TREE;
515 t = TREE_OPERAND (t, 0);
516 is_addr = true;
517 /* FALLTHRU */
519 case VAR_DECL:
520 if (!DECL_THREAD_LOCAL_P (t))
521 return NULL_TREE;
522 break;
524 default:
525 /* We're not interested in other decls or types, only subexpressions. */
526 if (EXPR_P (t))
527 *walk_subtrees = 1;
528 /* FALLTHRU */
530 case SSA_NAME:
531 /* Special-case the return of SSA_NAME, since it's so common. */
532 return NULL_TREE;
535 addr = gen_emutls_addr (t, d);
536 if (is_addr)
538 /* Replace "&var" with "addr" in the statement. */
539 *ptr = addr;
541 else
543 /* Replace "var" with "*addr" in the statement. */
544 t = build2 (MEM_REF, TREE_TYPE (t), addr,
545 build_int_cst (TREE_TYPE (addr), 0));
546 *ptr = t;
549 wi->changed = true;
550 return NULL_TREE;
553 /* Lower all of the operands of STMT. */
555 static void
556 lower_emutls_stmt (gimple stmt, struct lower_emutls_data *d)
558 struct walk_stmt_info wi;
560 d->loc = gimple_location (stmt);
562 memset (&wi, 0, sizeof (wi));
563 wi.info = d;
564 wi.val_only = true;
565 walk_gimple_op (stmt, lower_emutls_1, &wi);
567 if (wi.changed)
568 update_stmt (stmt);
571 /* Lower the I'th operand of PHI. */
573 static void
574 lower_emutls_phi_arg (gphi *phi, unsigned int i,
575 struct lower_emutls_data *d)
577 struct walk_stmt_info wi;
578 struct phi_arg_d *pd = gimple_phi_arg (phi, i);
580 /* Early out for a very common case we don't care about. */
581 if (TREE_CODE (pd->def) == SSA_NAME)
582 return;
584 d->loc = pd->locus;
586 memset (&wi, 0, sizeof (wi));
587 wi.info = d;
588 wi.val_only = true;
589 walk_tree (&pd->def, lower_emutls_1, &wi, NULL);
591 /* For normal statements, we let update_stmt do its job. But for phi
592 nodes, we have to manipulate the immediate use list by hand. */
593 if (wi.changed)
595 gcc_assert (TREE_CODE (pd->def) == SSA_NAME);
596 link_imm_use_stmt (&pd->imm_use, pd->def, phi);
600 /* Reset access variable for a given TLS variable data DATA. */
602 bool
603 reset_access (varpool_node * const &, tls_var_data *data, void *)
605 data->access = NULL;
607 return true;
610 /* Clear the access variables, in order to begin a new block. */
612 static inline void
613 clear_access_vars (void)
615 tls_map->traverse<void *, reset_access> (NULL);
618 /* Lower the entire function NODE. */
620 static void
621 lower_emutls_function_body (struct cgraph_node *node)
623 struct lower_emutls_data d;
624 bool any_edge_inserts = false;
626 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
628 d.cfun_node = node;
629 d.builtin_decl = builtin_decl_explicit (BUILT_IN_EMUTLS_GET_ADDRESS);
630 /* This is where we introduce the declaration to the IL and so we have to
631 create a node for it. */
632 d.builtin_node = cgraph_node::get_create (d.builtin_decl);
634 FOR_EACH_BB_FN (d.bb, cfun)
636 unsigned int i, nedge;
638 /* Lower each of the PHI nodes of the block, as we may have
639 propagated &tlsvar into a PHI argument. These loops are
640 arranged so that we process each edge at once, and each
641 PHI argument for that edge. */
642 if (!gimple_seq_empty_p (phi_nodes (d.bb)))
644 /* The calls will be inserted on the edges, and the frequencies
645 will be computed during the commit process. */
646 d.bb_freq = 0;
648 nedge = EDGE_COUNT (d.bb->preds);
649 for (i = 0; i < nedge; ++i)
651 edge e = EDGE_PRED (d.bb, i);
653 /* We can re-use any SSA_NAME created on this edge. */
654 clear_access_vars ();
655 d.seq = NULL;
657 for (gphi_iterator gsi = gsi_start_phis (d.bb);
658 !gsi_end_p (gsi);
659 gsi_next (&gsi))
660 lower_emutls_phi_arg (gsi.phi (), i, &d);
662 /* Insert all statements generated by all phi nodes for this
663 particular edge all at once. */
664 if (d.seq)
666 gsi_insert_seq_on_edge (e, d.seq);
667 any_edge_inserts = true;
672 d.bb_freq = compute_call_stmt_bb_frequency (current_function_decl, d.bb);
674 /* We can re-use any SSA_NAME created during this basic block. */
675 clear_access_vars ();
677 /* Lower each of the statements of the block. */
678 for (gimple_stmt_iterator gsi = gsi_start_bb (d.bb); !gsi_end_p (gsi);
679 gsi_next (&gsi))
681 d.seq = NULL;
682 lower_emutls_stmt (gsi_stmt (gsi), &d);
684 /* If any new statements were created, insert them immediately
685 before the first use. This prevents variable lifetimes from
686 becoming unnecessarily long. */
687 if (d.seq)
688 gsi_insert_seq_before (&gsi, d.seq, GSI_SAME_STMT);
692 if (any_edge_inserts)
693 gsi_commit_edge_inserts ();
695 pop_cfun ();
698 /* Create emutls variable for VAR, DATA is pointer to static
699 ctor body we can add constructors to.
700 Callback for varpool_for_variable_and_aliases. */
702 static bool
703 create_emultls_var (varpool_node *var, void *data)
705 tree cdecl;
706 tls_var_data value;
708 cdecl = new_emutls_decl (var->decl,
709 var->alias && var->analyzed
710 ? var->get_alias_target ()->decl : NULL);
712 varpool_node *cvar = varpool_node::get (cdecl);
714 if (!var->alias)
716 /* Make sure the COMMON block control variable gets initialized.
717 Note that there's no point in doing this for aliases; we only
718 need to do this once for the main variable. */
719 emutls_common_1 (var->decl, cdecl, (tree *)data);
721 if (var->alias && !var->analyzed)
722 cvar->alias = true;
724 /* Indicate that the value of the TLS variable may be found elsewhere,
725 preventing the variable from re-appearing in the GIMPLE. We cheat
726 and use the control variable here (rather than a full call_expr),
727 which is special-cased inside the DWARF2 output routines. */
728 SET_DECL_VALUE_EXPR (var->decl, cdecl);
729 DECL_HAS_VALUE_EXPR_P (var->decl) = 1;
731 value.control_var = cvar;
732 tls_map->put (var, value);
734 return false;
737 /* Main entry point to the tls lowering pass. */
739 static unsigned int
740 ipa_lower_emutls (void)
742 varpool_node *var;
743 cgraph_node *func;
744 bool any_aliases = false;
745 tree ctor_body = NULL;
746 hash_set <varpool_node *> visited;
747 auto_vec <varpool_node *> tls_vars;
749 /* Examine all global variables for TLS variables. */
750 FOR_EACH_VARIABLE (var)
751 if (DECL_THREAD_LOCAL_P (var->decl)
752 && !visited.add (var))
754 gcc_checking_assert (TREE_STATIC (var->decl)
755 || DECL_EXTERNAL (var->decl));
756 tls_vars.safe_push (var);
757 if (var->alias && var->definition
758 && !visited.add (var->ultimate_alias_target ()))
759 tls_vars.safe_push (var->ultimate_alias_target ());
762 /* If we found no TLS variables, then there is no further work to do. */
763 if (tls_vars.is_empty ())
765 if (dump_file)
766 fprintf (dump_file, "No TLS variables found.\n");
767 return 0;
770 tls_map = new hash_map <varpool_node *, tls_var_data> ();
772 /* Create the control variables for each TLS variable. */
773 for (unsigned i = 0; i < tls_vars.length (); i++)
775 var = tls_vars[i];
777 if (var->alias && !var->analyzed)
778 any_aliases = true;
779 else if (!var->alias)
780 var->call_for_symbol_and_aliases (create_emultls_var, &ctor_body, true);
783 /* If there were any aliases, then frob the alias_pairs vector. */
784 if (any_aliases)
786 alias_pair *p;
787 unsigned int i;
788 FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
789 if (DECL_THREAD_LOCAL_P (p->decl))
791 p->decl = tls_map->get
792 (varpool_node::get (p->decl))->control_var->decl;
793 p->target = get_emutls_object_name (p->target);
797 /* Adjust all uses of TLS variables within the function bodies. */
798 FOR_EACH_DEFINED_FUNCTION (func)
799 if (func->lowered)
800 lower_emutls_function_body (func);
802 /* Generate the constructor for any COMMON control variables created. */
803 if (ctor_body)
804 cgraph_build_static_cdtor ('I', ctor_body, DEFAULT_INIT_PRIORITY);
806 delete tls_map;
808 return 0;
811 namespace {
813 const pass_data pass_data_ipa_lower_emutls =
815 SIMPLE_IPA_PASS, /* type */
816 "emutls", /* name */
817 OPTGROUP_NONE, /* optinfo_flags */
818 TV_IPA_OPT, /* tv_id */
819 ( PROP_cfg | PROP_ssa ), /* properties_required */
820 0, /* properties_provided */
821 0, /* properties_destroyed */
822 0, /* todo_flags_start */
823 0, /* todo_flags_finish */
826 class pass_ipa_lower_emutls : public simple_ipa_opt_pass
828 public:
829 pass_ipa_lower_emutls (gcc::context *ctxt)
830 : simple_ipa_opt_pass (pass_data_ipa_lower_emutls, ctxt)
833 /* opt_pass methods: */
834 virtual bool gate (function *)
836 /* If the target supports TLS natively, we need do nothing here. */
837 return !targetm.have_tls;
840 virtual unsigned int execute (function *) { return ipa_lower_emutls (); }
842 }; // class pass_ipa_lower_emutls
844 } // anon namespace
846 simple_ipa_opt_pass *
847 make_pass_ipa_lower_emutls (gcc::context *ctxt)
849 return new pass_ipa_lower_emutls (ctxt);