Fix the build after the merge from trunk
[official-gcc.git] / gcc / tree-emutls.c
blobf456b4bd91ff7128020af815228f6ca2d83a6bf1
1 /* Lower TLS operations to emulation functions.
2 Copyright (C) 2006-2014 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 "tree.h"
24 #include "stor-layout.h"
25 #include "varasm.h"
26 #include "predict.h"
27 #include "vec.h"
28 #include "hashtab.h"
29 #include "hash-set.h"
30 #include "machmode.h"
31 #include "tm.h"
32 #include "hard-reg-set.h"
33 #include "input.h"
34 #include "function.h"
35 #include "dominance.h"
36 #include "cfg.h"
37 #include "basic-block.h"
38 #include "tree-ssa-alias.h"
39 #include "internal-fn.h"
40 #include "gimple-expr.h"
41 #include "is-a.h"
42 #include "gimple.h"
43 #include "gimple-iterator.h"
44 #include "gimple-walk.h"
45 #include "tree-pass.h"
46 #include "gimple-ssa.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"
56 #include "hash-map.h"
58 /* Whenever a target does not support thread-local storage (TLS) natively,
59 we can emulate it with some run-time support in libgcc. This will in
60 turn rely on "keyed storage" a-la pthread_key_create; essentially all
61 thread libraries provide such functionality.
63 In order to coordinate with the libgcc runtime, each TLS variable is
64 described by a "control variable". This control variable records the
65 required size, alignment, and initial value of the TLS variable for
66 instantiation at runtime. It also stores an integer token to be used
67 by the runtime to find the address of the variable within each thread.
69 On the compiler side, this means that we need to replace all instances
70 of "tls_var" in the code with "*__emutls_get_addr(&control_var)". We
71 also need to eliminate "tls_var" from the symbol table and introduce
72 "control_var".
74 We used to perform all of the transformations during conversion to rtl,
75 and the variable substitutions magically within assemble_variable.
76 However, this late fiddling of the symbol table conflicts with LTO and
77 whole-program compilation. Therefore we must now make all the changes
78 to the symbol table early in the GIMPLE optimization path, before we
79 write things out to LTO intermediate files. */
81 /* Value for TLS varpool node where a pointer to control variable and
82 access variable are stored. */
83 struct tls_var_data
85 varpool_node *control_var;
86 tree access;
89 /* TLS map accesses mapping between a TLS varpool node and a pair
90 made by control variable and access variable. */
91 static hash_map<varpool_node *, tls_var_data> *tls_map = NULL;
93 /* The type of the control structure, shared with the emutls.c runtime. */
94 static tree emutls_object_type;
96 #if !defined (NO_DOT_IN_LABEL)
97 # define EMUTLS_SEPARATOR "."
98 #elif !defined (NO_DOLLAR_IN_LABEL)
99 # define EMUTLS_SEPARATOR "$"
100 #else
101 # define EMUTLS_SEPARATOR "_"
102 #endif
104 /* Create an IDENTIFIER_NODE by prefixing PREFIX to the
105 IDENTIFIER_NODE NAME's name. */
107 static tree
108 prefix_name (const char *prefix, tree name)
110 unsigned plen = strlen (prefix);
111 unsigned nlen = strlen (IDENTIFIER_POINTER (name));
112 char *toname = (char *) alloca (plen + nlen + 1);
114 memcpy (toname, prefix, plen);
115 memcpy (toname + plen, IDENTIFIER_POINTER (name), nlen + 1);
117 return get_identifier (toname);
120 /* Create an identifier for the struct __emutls_object, given an identifier
121 of the DECL_ASSEMBLY_NAME of the original object. */
123 static tree
124 get_emutls_object_name (tree name)
126 const char *prefix = (targetm.emutls.var_prefix
127 ? targetm.emutls.var_prefix
128 : "__emutls_v" EMUTLS_SEPARATOR);
129 return prefix_name (prefix, name);
132 /* Create the fields of the type for the control variables. Ordinarily
133 this must match struct __emutls_object defined in emutls.c. However
134 this is a target hook so that VxWorks can define its own layout. */
136 tree
137 default_emutls_var_fields (tree type, tree *name ATTRIBUTE_UNUSED)
139 tree word_type_node, field, next_field;
141 field = build_decl (UNKNOWN_LOCATION,
142 FIELD_DECL, get_identifier ("__templ"), ptr_type_node);
143 DECL_CONTEXT (field) = type;
144 next_field = field;
146 field = build_decl (UNKNOWN_LOCATION,
147 FIELD_DECL, get_identifier ("__offset"),
148 ptr_type_node);
149 DECL_CONTEXT (field) = type;
150 DECL_CHAIN (field) = next_field;
151 next_field = field;
153 word_type_node = lang_hooks.types.type_for_mode (word_mode, 1);
154 field = build_decl (UNKNOWN_LOCATION,
155 FIELD_DECL, get_identifier ("__align"),
156 word_type_node);
157 DECL_CONTEXT (field) = type;
158 DECL_CHAIN (field) = next_field;
159 next_field = field;
161 field = build_decl (UNKNOWN_LOCATION,
162 FIELD_DECL, get_identifier ("__size"), word_type_node);
163 DECL_CONTEXT (field) = type;
164 DECL_CHAIN (field) = next_field;
166 return field;
169 /* Initialize emulated tls object TO, which refers to TLS variable DECL and
170 is initialized by PROXY. As above, this is the default implementation of
171 a target hook overridden by VxWorks. */
173 tree
174 default_emutls_var_init (tree to, tree decl, tree proxy)
176 vec<constructor_elt, va_gc> *v;
177 vec_alloc (v, 4);
178 constructor_elt elt;
179 tree type = TREE_TYPE (to);
180 tree field = TYPE_FIELDS (type);
182 elt.index = field;
183 elt.value = fold_convert (TREE_TYPE (field), DECL_SIZE_UNIT (decl));
184 v->quick_push (elt);
186 field = DECL_CHAIN (field);
187 elt.index = field;
188 elt.value = build_int_cst (TREE_TYPE (field),
189 DECL_ALIGN_UNIT (decl));
190 v->quick_push (elt);
192 field = DECL_CHAIN (field);
193 elt.index = field;
194 elt.value = null_pointer_node;
195 v->quick_push (elt);
197 field = DECL_CHAIN (field);
198 elt.index = field;
199 elt.value = proxy;
200 v->quick_push (elt);
202 return build_constructor (type, v);
205 /* Create the structure for struct __emutls_object. This should match the
206 structure at the top of emutls.c, modulo the union there. */
208 static tree
209 get_emutls_object_type (void)
211 tree type, type_name, field;
213 type = emutls_object_type;
214 if (type)
215 return type;
217 emutls_object_type = type = lang_hooks.types.make_type (RECORD_TYPE);
218 type_name = NULL;
219 field = targetm.emutls.var_fields (type, &type_name);
220 if (!type_name)
221 type_name = get_identifier ("__emutls_object");
222 type_name = build_decl (UNKNOWN_LOCATION,
223 TYPE_DECL, type_name, type);
224 TYPE_NAME (type) = type_name;
225 TYPE_FIELDS (type) = field;
226 layout_type (type);
228 return type;
231 /* Create a read-only variable like DECL, with the same DECL_INITIAL.
232 This will be used for initializing the emulated tls data area. */
234 static tree
235 get_emutls_init_templ_addr (tree decl)
237 tree name, to;
239 if (targetm.emutls.register_common && !DECL_INITIAL (decl)
240 && !DECL_SECTION_NAME (decl))
241 return null_pointer_node;
243 name = DECL_ASSEMBLER_NAME (decl);
244 if (!targetm.emutls.tmpl_prefix || targetm.emutls.tmpl_prefix[0])
246 const char *prefix = (targetm.emutls.tmpl_prefix
247 ? targetm.emutls.tmpl_prefix
248 : "__emutls_t" EMUTLS_SEPARATOR);
249 name = prefix_name (prefix, name);
252 to = build_decl (DECL_SOURCE_LOCATION (decl),
253 VAR_DECL, name, TREE_TYPE (decl));
254 SET_DECL_ASSEMBLER_NAME (to, DECL_NAME (to));
256 DECL_ARTIFICIAL (to) = 1;
257 TREE_USED (to) = TREE_USED (decl);
258 TREE_READONLY (to) = 1;
259 DECL_IGNORED_P (to) = 1;
260 DECL_CONTEXT (to) = DECL_CONTEXT (decl);
261 DECL_PRESERVE_P (to) = DECL_PRESERVE_P (decl);
263 DECL_WEAK (to) = DECL_WEAK (decl);
264 if (DECL_ONE_ONLY (decl))
266 TREE_STATIC (to) = TREE_STATIC (decl);
267 TREE_PUBLIC (to) = TREE_PUBLIC (decl);
268 DECL_VISIBILITY (to) = DECL_VISIBILITY (decl);
269 make_decl_one_only (to, DECL_ASSEMBLER_NAME (to));
271 else
272 TREE_STATIC (to) = 1;
274 DECL_VISIBILITY_SPECIFIED (to) = DECL_VISIBILITY_SPECIFIED (decl);
275 DECL_INITIAL (to) = DECL_INITIAL (decl);
276 DECL_INITIAL (decl) = NULL;
278 if (targetm.emutls.tmpl_section)
279 set_decl_section_name (to, targetm.emutls.tmpl_section);
280 else
281 set_decl_section_name (to, DECL_SECTION_NAME (decl));
283 /* Create varpool node for the new variable and finalize it if it is
284 not external one. */
285 if (DECL_EXTERNAL (to))
286 varpool_node::get_create (to);
287 else
288 varpool_node::add (to);
289 return build_fold_addr_expr (to);
292 /* Create and return the control variable for the TLS variable DECL. */
294 static tree
295 new_emutls_decl (tree decl, tree alias_of)
297 tree name, to;
299 name = DECL_ASSEMBLER_NAME (decl);
300 to = build_decl (DECL_SOURCE_LOCATION (decl), VAR_DECL,
301 get_emutls_object_name (name),
302 get_emutls_object_type ());
304 SET_DECL_ASSEMBLER_NAME (to, DECL_NAME (to));
306 DECL_ARTIFICIAL (to) = 1;
307 DECL_IGNORED_P (to) = 1;
308 TREE_READONLY (to) = 0;
309 TREE_STATIC (to) = 1;
311 DECL_PRESERVE_P (to) = DECL_PRESERVE_P (decl);
312 DECL_CONTEXT (to) = DECL_CONTEXT (decl);
313 TREE_USED (to) = TREE_USED (decl);
314 TREE_PUBLIC (to) = TREE_PUBLIC (decl);
315 DECL_EXTERNAL (to) = DECL_EXTERNAL (decl);
316 DECL_COMMON (to) = DECL_COMMON (decl);
317 DECL_WEAK (to) = DECL_WEAK (decl);
318 DECL_VISIBILITY (to) = DECL_VISIBILITY (decl);
319 DECL_VISIBILITY_SPECIFIED (to) = DECL_VISIBILITY_SPECIFIED (decl);
320 DECL_DLLIMPORT_P (to) = DECL_DLLIMPORT_P (decl);
322 DECL_ATTRIBUTES (to) = targetm.merge_decl_attributes (decl, to);
324 if (DECL_ONE_ONLY (decl))
325 make_decl_one_only (to, DECL_ASSEMBLER_NAME (to));
327 set_decl_tls_model (to, TLS_MODEL_EMULATED);
329 /* If we're not allowed to change the proxy object's alignment,
330 pretend it has been set by the user. */
331 if (targetm.emutls.var_align_fixed)
332 DECL_USER_ALIGN (to) = 1;
334 /* If the target wants the control variables grouped, do so. */
335 if (!DECL_COMMON (to) && targetm.emutls.var_section)
337 set_decl_section_name (to, targetm.emutls.var_section);
340 /* If this variable is defined locally, then we need to initialize the
341 control structure with size and alignment information. Initialization
342 of COMMON block variables happens elsewhere via a constructor. */
343 if (!DECL_EXTERNAL (to)
344 && (!DECL_COMMON (to)
345 || (DECL_INITIAL (decl)
346 && DECL_INITIAL (decl) != error_mark_node)))
348 tree tmpl = get_emutls_init_templ_addr (decl);
349 DECL_INITIAL (to) = targetm.emutls.var_init (to, decl, tmpl);
350 record_references_in_initializer (to, false);
353 /* Create varpool node for the new variable and finalize it if it is
354 not external one. */
355 if (DECL_EXTERNAL (to))
356 varpool_node::get_create (to);
357 else if (!alias_of)
358 varpool_node::add (to);
359 else
360 varpool_node::create_alias (to,
361 varpool_node::get_for_asmname
362 (DECL_ASSEMBLER_NAME (DECL_VALUE_EXPR (alias_of)))->decl);
363 return to;
366 /* Generate a call statement to initialize CONTROL_DECL for TLS_DECL.
367 This only needs to happen for TLS COMMON variables; non-COMMON
368 variables can be initialized statically. Insert the generated
369 call statement at the end of PSTMTS. */
371 static void
372 emutls_common_1 (tree tls_decl, tree control_decl, tree *pstmts)
374 tree x;
375 tree word_type_node;
377 if (! DECL_COMMON (tls_decl)
378 || (DECL_INITIAL (tls_decl)
379 && DECL_INITIAL (tls_decl) != error_mark_node))
380 return;
382 word_type_node = lang_hooks.types.type_for_mode (word_mode, 1);
384 x = build_call_expr (builtin_decl_explicit (BUILT_IN_EMUTLS_REGISTER_COMMON),
385 4, build_fold_addr_expr (control_decl),
386 fold_convert (word_type_node,
387 DECL_SIZE_UNIT (tls_decl)),
388 build_int_cst (word_type_node,
389 DECL_ALIGN_UNIT (tls_decl)),
390 get_emutls_init_templ_addr (tls_decl));
392 append_to_statement_list (x, pstmts);
395 struct lower_emutls_data
397 struct cgraph_node *cfun_node;
398 struct cgraph_node *builtin_node;
399 tree builtin_decl;
400 basic_block bb;
401 int bb_freq;
402 location_t loc;
403 gimple_seq seq;
406 /* Given a TLS variable DECL, return an SSA_NAME holding its address.
407 Append any new computation statements required to D->SEQ. */
409 static tree
410 gen_emutls_addr (tree decl, struct lower_emutls_data *d)
412 /* Compute the address of the TLS variable with help from runtime. */
413 tls_var_data *data = tls_map->get (varpool_node::get (decl));
414 tree addr = data->access;
416 if (addr == NULL)
418 varpool_node *cvar;
419 tree cdecl;
420 gcall *x;
422 cvar = data->control_var;
423 cdecl = cvar->decl;
424 TREE_ADDRESSABLE (cdecl) = 1;
426 addr = create_tmp_var (build_pointer_type (TREE_TYPE (decl)), NULL);
427 x = gimple_build_call (d->builtin_decl, 1, build_fold_addr_expr (cdecl));
428 gimple_set_location (x, d->loc);
430 addr = make_ssa_name (addr, x);
431 gimple_call_set_lhs (x, addr);
433 gimple_seq_add_stmt (&d->seq, x);
435 d->cfun_node->create_edge (d->builtin_node, x, d->bb->count, d->bb_freq);
437 /* We may be adding a new reference to a new variable to the function.
438 This means we have to play with the ipa-reference web. */
439 d->cfun_node->create_reference (cvar, IPA_REF_ADDR, x);
441 /* Record this ssa_name for possible use later in the basic block. */
442 data->access = addr;
445 return addr;
448 /* Callback for walk_gimple_op. D = WI->INFO is a struct lower_emutls_data.
449 Given an operand *PTR within D->STMT, if the operand references a TLS
450 variable, then lower the reference to a call to the runtime. Insert
451 any new statements required into D->SEQ; the caller is responsible for
452 placing those appropriately. */
454 static tree
455 lower_emutls_1 (tree *ptr, int *walk_subtrees, void *cb_data)
457 struct walk_stmt_info *wi = (struct walk_stmt_info *) cb_data;
458 struct lower_emutls_data *d = (struct lower_emutls_data *) wi->info;
459 tree t = *ptr;
460 bool is_addr = false;
461 tree addr;
463 *walk_subtrees = 0;
465 switch (TREE_CODE (t))
467 case ADDR_EXPR:
468 /* If this is not a straight-forward "&var", but rather something
469 like "&var.a", then we may need special handling. */
470 if (TREE_CODE (TREE_OPERAND (t, 0)) != VAR_DECL)
472 bool save_changed;
474 /* If we're allowed more than just is_gimple_val, continue. */
475 if (!wi->val_only)
477 *walk_subtrees = 1;
478 return NULL_TREE;
481 /* See if any substitution would be made. */
482 save_changed = wi->changed;
483 wi->changed = false;
484 wi->val_only = false;
485 walk_tree (&TREE_OPERAND (t, 0), lower_emutls_1, wi, NULL);
486 wi->val_only = true;
488 /* If so, then extract this entire sub-expression "&p->a" into a
489 new assignment statement, and substitute yet another SSA_NAME. */
490 if (wi->changed)
492 gimple x;
494 addr = create_tmp_var (TREE_TYPE (t), NULL);
495 x = gimple_build_assign (addr, t);
496 gimple_set_location (x, d->loc);
498 addr = make_ssa_name (addr, x);
499 gimple_assign_set_lhs (x, addr);
501 gimple_seq_add_stmt (&d->seq, x);
503 *ptr = addr;
505 else
506 wi->changed = save_changed;
508 return NULL_TREE;
511 t = TREE_OPERAND (t, 0);
512 is_addr = true;
513 /* FALLTHRU */
515 case VAR_DECL:
516 if (!DECL_THREAD_LOCAL_P (t))
517 return NULL_TREE;
518 break;
520 default:
521 /* We're not interested in other decls or types, only subexpressions. */
522 if (EXPR_P (t))
523 *walk_subtrees = 1;
524 /* FALLTHRU */
526 case SSA_NAME:
527 /* Special-case the return of SSA_NAME, since it's so common. */
528 return NULL_TREE;
531 addr = gen_emutls_addr (t, d);
532 if (is_addr)
534 /* Replace "&var" with "addr" in the statement. */
535 *ptr = addr;
537 else
539 /* Replace "var" with "*addr" in the statement. */
540 t = build2 (MEM_REF, TREE_TYPE (t), addr,
541 build_int_cst (TREE_TYPE (addr), 0));
542 *ptr = t;
545 wi->changed = true;
546 return NULL_TREE;
549 /* Lower all of the operands of STMT. */
551 static void
552 lower_emutls_stmt (gimple stmt, struct lower_emutls_data *d)
554 struct walk_stmt_info wi;
556 d->loc = gimple_location (stmt);
558 memset (&wi, 0, sizeof (wi));
559 wi.info = d;
560 wi.val_only = true;
561 walk_gimple_op (stmt, lower_emutls_1, &wi);
563 if (wi.changed)
564 update_stmt (stmt);
567 /* Lower the I'th operand of PHI. */
569 static void
570 lower_emutls_phi_arg (gphi *phi, unsigned int i,
571 struct lower_emutls_data *d)
573 struct walk_stmt_info wi;
574 struct phi_arg_d *pd = gimple_phi_arg (phi, i);
576 /* Early out for a very common case we don't care about. */
577 if (TREE_CODE (pd->def) == SSA_NAME)
578 return;
580 d->loc = pd->locus;
582 memset (&wi, 0, sizeof (wi));
583 wi.info = d;
584 wi.val_only = true;
585 walk_tree (&pd->def, lower_emutls_1, &wi, NULL);
587 /* For normal statements, we let update_stmt do its job. But for phi
588 nodes, we have to manipulate the immediate use list by hand. */
589 if (wi.changed)
591 gcc_assert (TREE_CODE (pd->def) == SSA_NAME);
592 link_imm_use_stmt (&pd->imm_use, pd->def, phi);
596 /* Reset access variable for a given TLS variable data DATA. */
598 bool
599 reset_access (varpool_node * const &, tls_var_data *data, void *)
601 data->access = NULL;
603 return true;
606 /* Clear the access variables, in order to begin a new block. */
608 static inline void
609 clear_access_vars (void)
611 tls_map->traverse<void *, reset_access> (NULL);
614 /* Lower the entire function NODE. */
616 static void
617 lower_emutls_function_body (struct cgraph_node *node)
619 struct lower_emutls_data d;
620 bool any_edge_inserts = false;
622 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
624 d.cfun_node = node;
625 d.builtin_decl = builtin_decl_explicit (BUILT_IN_EMUTLS_GET_ADDRESS);
626 /* This is where we introduce the declaration to the IL and so we have to
627 create a node for it. */
628 d.builtin_node = cgraph_node::get_create (d.builtin_decl);
630 FOR_EACH_BB_FN (d.bb, cfun)
632 unsigned int i, nedge;
634 /* Lower each of the PHI nodes of the block, as we may have
635 propagated &tlsvar into a PHI argument. These loops are
636 arranged so that we process each edge at once, and each
637 PHI argument for that edge. */
638 if (!gimple_seq_empty_p (phi_nodes (d.bb)))
640 /* The calls will be inserted on the edges, and the frequencies
641 will be computed during the commit process. */
642 d.bb_freq = 0;
644 nedge = EDGE_COUNT (d.bb->preds);
645 for (i = 0; i < nedge; ++i)
647 edge e = EDGE_PRED (d.bb, i);
649 /* We can re-use any SSA_NAME created on this edge. */
650 clear_access_vars ();
651 d.seq = NULL;
653 for (gphi_iterator gsi = gsi_start_phis (d.bb);
654 !gsi_end_p (gsi);
655 gsi_next (&gsi))
656 lower_emutls_phi_arg (gsi.phi (), i, &d);
658 /* Insert all statements generated by all phi nodes for this
659 particular edge all at once. */
660 if (d.seq)
662 gsi_insert_seq_on_edge (e, d.seq);
663 any_edge_inserts = true;
668 d.bb_freq = compute_call_stmt_bb_frequency (current_function_decl, d.bb);
670 /* We can re-use any SSA_NAME created during this basic block. */
671 clear_access_vars ();
673 /* Lower each of the statements of the block. */
674 for (gimple_stmt_iterator gsi = gsi_start_bb (d.bb); !gsi_end_p (gsi);
675 gsi_next (&gsi))
677 d.seq = NULL;
678 lower_emutls_stmt (gsi_stmt (gsi), &d);
680 /* If any new statements were created, insert them immediately
681 before the first use. This prevents variable lifetimes from
682 becoming unnecessarily long. */
683 if (d.seq)
684 gsi_insert_seq_before (&gsi, d.seq, GSI_SAME_STMT);
688 if (any_edge_inserts)
689 gsi_commit_edge_inserts ();
691 pop_cfun ();
694 /* Create emutls variable for VAR, DATA is pointer to static
695 ctor body we can add constructors to.
696 Callback for varpool_for_variable_and_aliases. */
698 static bool
699 create_emultls_var (varpool_node *var, void *data)
701 tree cdecl;
702 tls_var_data value;
704 cdecl = new_emutls_decl (var->decl,
705 var->alias && var->analyzed
706 ? var->get_alias_target ()->decl : NULL);
708 varpool_node *cvar = varpool_node::get (cdecl);
710 if (!var->alias)
712 /* Make sure the COMMON block control variable gets initialized.
713 Note that there's no point in doing this for aliases; we only
714 need to do this once for the main variable. */
715 emutls_common_1 (var->decl, cdecl, (tree *)data);
717 if (var->alias && !var->analyzed)
718 cvar->alias = true;
720 /* Indicate that the value of the TLS variable may be found elsewhere,
721 preventing the variable from re-appearing in the GIMPLE. We cheat
722 and use the control variable here (rather than a full call_expr),
723 which is special-cased inside the DWARF2 output routines. */
724 SET_DECL_VALUE_EXPR (var->decl, cdecl);
725 DECL_HAS_VALUE_EXPR_P (var->decl) = 1;
727 value.control_var = cvar;
728 tls_map->put (var, value);
730 return false;
733 /* Main entry point to the tls lowering pass. */
735 static unsigned int
736 ipa_lower_emutls (void)
738 varpool_node *var;
739 cgraph_node *func;
740 bool any_aliases = false;
741 tree ctor_body = NULL;
743 auto_vec <varpool_node *> tls_vars;
745 /* Examine all global variables for TLS variables. */
746 FOR_EACH_VARIABLE (var)
747 if (DECL_THREAD_LOCAL_P (var->decl))
749 gcc_checking_assert (TREE_STATIC (var->decl)
750 || DECL_EXTERNAL (var->decl));
751 tls_vars.safe_push (var);
752 if (var->alias && var->definition)
753 tls_vars.safe_push (var->ultimate_alias_target ());
756 /* If we found no TLS variables, then there is no further work to do. */
757 if (tls_vars.is_empty ())
759 if (dump_file)
760 fprintf (dump_file, "No TLS variables found.\n");
761 return 0;
764 tls_map = new hash_map <varpool_node *, tls_var_data> ();
766 /* Create the control variables for each TLS variable. */
767 for (unsigned i = 0; i < tls_vars.length (); i++)
769 var = tls_vars[i];
771 if (var->alias && !var->analyzed)
772 any_aliases = true;
773 else if (!var->alias)
774 var->call_for_node_and_aliases (create_emultls_var, &ctor_body, true);
777 /* If there were any aliases, then frob the alias_pairs vector. */
778 if (any_aliases)
780 alias_pair *p;
781 unsigned int i;
782 FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
783 if (DECL_THREAD_LOCAL_P (p->decl))
785 p->decl = tls_map->get
786 (varpool_node::get (p->decl))->control_var->decl;
787 p->target = get_emutls_object_name (p->target);
791 /* Adjust all uses of TLS variables within the function bodies. */
792 FOR_EACH_DEFINED_FUNCTION (func)
793 if (func->lowered)
794 lower_emutls_function_body (func);
796 /* Generate the constructor for any COMMON control variables created. */
797 if (ctor_body)
798 cgraph_build_static_cdtor ('I', ctor_body, DEFAULT_INIT_PRIORITY);
800 delete tls_map;
802 return 0;
805 namespace {
807 const pass_data pass_data_ipa_lower_emutls =
809 SIMPLE_IPA_PASS, /* type */
810 "emutls", /* name */
811 OPTGROUP_NONE, /* optinfo_flags */
812 TV_IPA_OPT, /* tv_id */
813 ( PROP_cfg | PROP_ssa ), /* properties_required */
814 0, /* properties_provided */
815 0, /* properties_destroyed */
816 0, /* todo_flags_start */
817 0, /* todo_flags_finish */
820 class pass_ipa_lower_emutls : public simple_ipa_opt_pass
822 public:
823 pass_ipa_lower_emutls (gcc::context *ctxt)
824 : simple_ipa_opt_pass (pass_data_ipa_lower_emutls, ctxt)
827 /* opt_pass methods: */
828 virtual bool gate (function *)
830 /* If the target supports TLS natively, we need do nothing here. */
831 return !targetm.have_tls;
834 virtual unsigned int execute (function *) { return ipa_lower_emutls (); }
836 }; // class pass_ipa_lower_emutls
838 } // anon namespace
840 simple_ipa_opt_pass *
841 make_pass_ipa_lower_emutls (gcc::context *ctxt)
843 return new pass_ipa_lower_emutls (ctxt);