[AArch64 Testsuite] Add a test of the vst[234](q?) intrinics
[official-gcc.git] / gcc / tree-emutls.c
blobc915e3301e422c5f43dab69b85c9395a7647d79a
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 "basic-block.h"
27 #include "tree-ssa-alias.h"
28 #include "internal-fn.h"
29 #include "gimple-expr.h"
30 #include "is-a.h"
31 #include "gimple.h"
32 #include "gimple-iterator.h"
33 #include "gimple-walk.h"
34 #include "tree-pass.h"
35 #include "gimple-ssa.h"
36 #include "cgraph.h"
37 #include "tree-phinodes.h"
38 #include "ssa-iterators.h"
39 #include "stringpool.h"
40 #include "tree-ssanames.h"
41 #include "langhooks.h"
42 #include "target.h"
43 #include "targhooks.h"
44 #include "tree-iterator.h"
45 #include "hash-map.h"
47 /* Whenever a target does not support thread-local storage (TLS) natively,
48 we can emulate it with some run-time support in libgcc. This will in
49 turn rely on "keyed storage" a-la pthread_key_create; essentially all
50 thread libraries provide such functionality.
52 In order to coordinate with the libgcc runtime, each TLS variable is
53 described by a "control variable". This control variable records the
54 required size, alignment, and initial value of the TLS variable for
55 instantiation at runtime. It also stores an integer token to be used
56 by the runtime to find the address of the variable within each thread.
58 On the compiler side, this means that we need to replace all instances
59 of "tls_var" in the code with "*__emutls_get_addr(&control_var)". We
60 also need to eliminate "tls_var" from the symbol table and introduce
61 "control_var".
63 We used to perform all of the transformations during conversion to rtl,
64 and the variable substitutions magically within assemble_variable.
65 However, this late fiddling of the symbol table conflicts with LTO and
66 whole-program compilation. Therefore we must now make all the changes
67 to the symbol table early in the GIMPLE optimization path, before we
68 write things out to LTO intermediate files. */
70 /* Value for TLS varpool node where a pointer to control variable and
71 access variable are stored. */
72 struct tls_var_data
74 varpool_node *control_var;
75 tree access;
78 /* TLS map accesses mapping between a TLS varpool node and a pair
79 made by control variable and access variable. */
80 static hash_map<varpool_node *, tls_var_data> *tls_map = NULL;
82 /* The type of the control structure, shared with the emutls.c runtime. */
83 static tree emutls_object_type;
85 #if !defined (NO_DOT_IN_LABEL)
86 # define EMUTLS_SEPARATOR "."
87 #elif !defined (NO_DOLLAR_IN_LABEL)
88 # define EMUTLS_SEPARATOR "$"
89 #else
90 # define EMUTLS_SEPARATOR "_"
91 #endif
93 /* Create an IDENTIFIER_NODE by prefixing PREFIX to the
94 IDENTIFIER_NODE NAME's name. */
96 static tree
97 prefix_name (const char *prefix, tree name)
99 unsigned plen = strlen (prefix);
100 unsigned nlen = strlen (IDENTIFIER_POINTER (name));
101 char *toname = (char *) alloca (plen + nlen + 1);
103 memcpy (toname, prefix, plen);
104 memcpy (toname + plen, IDENTIFIER_POINTER (name), nlen + 1);
106 return get_identifier (toname);
109 /* Create an identifier for the struct __emutls_object, given an identifier
110 of the DECL_ASSEMBLY_NAME of the original object. */
112 static tree
113 get_emutls_object_name (tree name)
115 const char *prefix = (targetm.emutls.var_prefix
116 ? targetm.emutls.var_prefix
117 : "__emutls_v" EMUTLS_SEPARATOR);
118 return prefix_name (prefix, name);
121 /* Create the fields of the type for the control variables. Ordinarily
122 this must match struct __emutls_object defined in emutls.c. However
123 this is a target hook so that VxWorks can define its own layout. */
125 tree
126 default_emutls_var_fields (tree type, tree *name ATTRIBUTE_UNUSED)
128 tree word_type_node, field, next_field;
130 field = build_decl (UNKNOWN_LOCATION,
131 FIELD_DECL, get_identifier ("__templ"), ptr_type_node);
132 DECL_CONTEXT (field) = type;
133 next_field = field;
135 field = build_decl (UNKNOWN_LOCATION,
136 FIELD_DECL, get_identifier ("__offset"),
137 ptr_type_node);
138 DECL_CONTEXT (field) = type;
139 DECL_CHAIN (field) = next_field;
140 next_field = field;
142 word_type_node = lang_hooks.types.type_for_mode (word_mode, 1);
143 field = build_decl (UNKNOWN_LOCATION,
144 FIELD_DECL, get_identifier ("__align"),
145 word_type_node);
146 DECL_CONTEXT (field) = type;
147 DECL_CHAIN (field) = next_field;
148 next_field = field;
150 field = build_decl (UNKNOWN_LOCATION,
151 FIELD_DECL, get_identifier ("__size"), word_type_node);
152 DECL_CONTEXT (field) = type;
153 DECL_CHAIN (field) = next_field;
155 return field;
158 /* Initialize emulated tls object TO, which refers to TLS variable DECL and
159 is initialized by PROXY. As above, this is the default implementation of
160 a target hook overridden by VxWorks. */
162 tree
163 default_emutls_var_init (tree to, tree decl, tree proxy)
165 vec<constructor_elt, va_gc> *v;
166 vec_alloc (v, 4);
167 constructor_elt elt;
168 tree type = TREE_TYPE (to);
169 tree field = TYPE_FIELDS (type);
171 elt.index = field;
172 elt.value = fold_convert (TREE_TYPE (field), DECL_SIZE_UNIT (decl));
173 v->quick_push (elt);
175 field = DECL_CHAIN (field);
176 elt.index = field;
177 elt.value = build_int_cst (TREE_TYPE (field),
178 DECL_ALIGN_UNIT (decl));
179 v->quick_push (elt);
181 field = DECL_CHAIN (field);
182 elt.index = field;
183 elt.value = null_pointer_node;
184 v->quick_push (elt);
186 field = DECL_CHAIN (field);
187 elt.index = field;
188 elt.value = proxy;
189 v->quick_push (elt);
191 return build_constructor (type, v);
194 /* Create the structure for struct __emutls_object. This should match the
195 structure at the top of emutls.c, modulo the union there. */
197 static tree
198 get_emutls_object_type (void)
200 tree type, type_name, field;
202 type = emutls_object_type;
203 if (type)
204 return type;
206 emutls_object_type = type = lang_hooks.types.make_type (RECORD_TYPE);
207 type_name = NULL;
208 field = targetm.emutls.var_fields (type, &type_name);
209 if (!type_name)
210 type_name = get_identifier ("__emutls_object");
211 type_name = build_decl (UNKNOWN_LOCATION,
212 TYPE_DECL, type_name, type);
213 TYPE_NAME (type) = type_name;
214 TYPE_FIELDS (type) = field;
215 layout_type (type);
217 return type;
220 /* Create a read-only variable like DECL, with the same DECL_INITIAL.
221 This will be used for initializing the emulated tls data area. */
223 static tree
224 get_emutls_init_templ_addr (tree decl)
226 tree name, to;
228 if (targetm.emutls.register_common && !DECL_INITIAL (decl)
229 && !DECL_SECTION_NAME (decl))
230 return null_pointer_node;
232 name = DECL_ASSEMBLER_NAME (decl);
233 if (!targetm.emutls.tmpl_prefix || targetm.emutls.tmpl_prefix[0])
235 const char *prefix = (targetm.emutls.tmpl_prefix
236 ? targetm.emutls.tmpl_prefix
237 : "__emutls_t" EMUTLS_SEPARATOR);
238 name = prefix_name (prefix, name);
241 to = build_decl (DECL_SOURCE_LOCATION (decl),
242 VAR_DECL, name, TREE_TYPE (decl));
243 SET_DECL_ASSEMBLER_NAME (to, DECL_NAME (to));
245 DECL_ARTIFICIAL (to) = 1;
246 TREE_USED (to) = TREE_USED (decl);
247 TREE_READONLY (to) = 1;
248 DECL_IGNORED_P (to) = 1;
249 DECL_CONTEXT (to) = DECL_CONTEXT (decl);
250 DECL_PRESERVE_P (to) = DECL_PRESERVE_P (decl);
252 DECL_WEAK (to) = DECL_WEAK (decl);
253 if (DECL_ONE_ONLY (decl))
255 TREE_STATIC (to) = TREE_STATIC (decl);
256 TREE_PUBLIC (to) = TREE_PUBLIC (decl);
257 DECL_VISIBILITY (to) = DECL_VISIBILITY (decl);
258 make_decl_one_only (to, DECL_ASSEMBLER_NAME (to));
260 else
261 TREE_STATIC (to) = 1;
263 DECL_VISIBILITY_SPECIFIED (to) = DECL_VISIBILITY_SPECIFIED (decl);
264 DECL_INITIAL (to) = DECL_INITIAL (decl);
265 DECL_INITIAL (decl) = NULL;
267 if (targetm.emutls.tmpl_section)
268 set_decl_section_name (to, targetm.emutls.tmpl_section);
269 else
270 set_decl_section_name (to, DECL_SECTION_NAME (decl));
272 /* Create varpool node for the new variable and finalize it if it is
273 not external one. */
274 if (DECL_EXTERNAL (to))
275 varpool_node::get_create (to);
276 else
277 varpool_node::add (to);
278 return build_fold_addr_expr (to);
281 /* Create and return the control variable for the TLS variable DECL. */
283 static tree
284 new_emutls_decl (tree decl, tree alias_of)
286 tree name, to;
288 name = DECL_ASSEMBLER_NAME (decl);
289 to = build_decl (DECL_SOURCE_LOCATION (decl), VAR_DECL,
290 get_emutls_object_name (name),
291 get_emutls_object_type ());
293 SET_DECL_ASSEMBLER_NAME (to, DECL_NAME (to));
295 DECL_ARTIFICIAL (to) = 1;
296 DECL_IGNORED_P (to) = 1;
297 TREE_READONLY (to) = 0;
298 TREE_STATIC (to) = 1;
300 DECL_PRESERVE_P (to) = DECL_PRESERVE_P (decl);
301 DECL_CONTEXT (to) = DECL_CONTEXT (decl);
302 TREE_USED (to) = TREE_USED (decl);
303 TREE_PUBLIC (to) = TREE_PUBLIC (decl);
304 DECL_EXTERNAL (to) = DECL_EXTERNAL (decl);
305 DECL_COMMON (to) = DECL_COMMON (decl);
306 DECL_WEAK (to) = DECL_WEAK (decl);
307 DECL_VISIBILITY (to) = DECL_VISIBILITY (decl);
308 DECL_VISIBILITY_SPECIFIED (to) = DECL_VISIBILITY_SPECIFIED (decl);
309 DECL_DLLIMPORT_P (to) = DECL_DLLIMPORT_P (decl);
311 DECL_ATTRIBUTES (to) = targetm.merge_decl_attributes (decl, to);
313 if (DECL_ONE_ONLY (decl))
314 make_decl_one_only (to, DECL_ASSEMBLER_NAME (to));
316 set_decl_tls_model (to, TLS_MODEL_EMULATED);
318 /* If we're not allowed to change the proxy object's alignment,
319 pretend it has been set by the user. */
320 if (targetm.emutls.var_align_fixed)
321 DECL_USER_ALIGN (to) = 1;
323 /* If the target wants the control variables grouped, do so. */
324 if (!DECL_COMMON (to) && targetm.emutls.var_section)
326 set_decl_section_name (to, targetm.emutls.var_section);
329 /* If this variable is defined locally, then we need to initialize the
330 control structure with size and alignment information. Initialization
331 of COMMON block variables happens elsewhere via a constructor. */
332 if (!DECL_EXTERNAL (to)
333 && (!DECL_COMMON (to)
334 || (DECL_INITIAL (decl)
335 && DECL_INITIAL (decl) != error_mark_node)))
337 tree tmpl = get_emutls_init_templ_addr (decl);
338 DECL_INITIAL (to) = targetm.emutls.var_init (to, decl, tmpl);
339 record_references_in_initializer (to, false);
342 /* Create varpool node for the new variable and finalize it if it is
343 not external one. */
344 if (DECL_EXTERNAL (to))
345 varpool_node::get_create (to);
346 else if (!alias_of)
347 varpool_node::add (to);
348 else
349 varpool_node::create_alias (to,
350 varpool_node::get_for_asmname
351 (DECL_ASSEMBLER_NAME (DECL_VALUE_EXPR (alias_of)))->decl);
352 return to;
355 /* Generate a call statement to initialize CONTROL_DECL for TLS_DECL.
356 This only needs to happen for TLS COMMON variables; non-COMMON
357 variables can be initialized statically. Insert the generated
358 call statement at the end of PSTMTS. */
360 static void
361 emutls_common_1 (tree tls_decl, tree control_decl, tree *pstmts)
363 tree x;
364 tree word_type_node;
366 if (! DECL_COMMON (tls_decl)
367 || (DECL_INITIAL (tls_decl)
368 && DECL_INITIAL (tls_decl) != error_mark_node))
369 return;
371 word_type_node = lang_hooks.types.type_for_mode (word_mode, 1);
373 x = build_call_expr (builtin_decl_explicit (BUILT_IN_EMUTLS_REGISTER_COMMON),
374 4, build_fold_addr_expr (control_decl),
375 fold_convert (word_type_node,
376 DECL_SIZE_UNIT (tls_decl)),
377 build_int_cst (word_type_node,
378 DECL_ALIGN_UNIT (tls_decl)),
379 get_emutls_init_templ_addr (tls_decl));
381 append_to_statement_list (x, pstmts);
384 struct lower_emutls_data
386 struct cgraph_node *cfun_node;
387 struct cgraph_node *builtin_node;
388 tree builtin_decl;
389 basic_block bb;
390 int bb_freq;
391 location_t loc;
392 gimple_seq seq;
395 /* Given a TLS variable DECL, return an SSA_NAME holding its address.
396 Append any new computation statements required to D->SEQ. */
398 static tree
399 gen_emutls_addr (tree decl, struct lower_emutls_data *d)
401 /* Compute the address of the TLS variable with help from runtime. */
402 tls_var_data *data = tls_map->get (varpool_node::get (decl));
403 tree addr = data->access;
405 if (addr == NULL)
407 varpool_node *cvar;
408 tree cdecl;
409 gimple x;
411 cvar = data->control_var;
412 cdecl = cvar->decl;
413 TREE_ADDRESSABLE (cdecl) = 1;
415 addr = create_tmp_var (build_pointer_type (TREE_TYPE (decl)), NULL);
416 x = gimple_build_call (d->builtin_decl, 1, build_fold_addr_expr (cdecl));
417 gimple_set_location (x, d->loc);
419 addr = make_ssa_name (addr, x);
420 gimple_call_set_lhs (x, addr);
422 gimple_seq_add_stmt (&d->seq, x);
424 d->cfun_node->create_edge (d->builtin_node, x, d->bb->count, d->bb_freq);
426 /* We may be adding a new reference to a new variable to the function.
427 This means we have to play with the ipa-reference web. */
428 d->cfun_node->create_reference (cvar, IPA_REF_ADDR, x);
430 /* Record this ssa_name for possible use later in the basic block. */
431 data->access = addr;
434 return addr;
437 /* Callback for walk_gimple_op. D = WI->INFO is a struct lower_emutls_data.
438 Given an operand *PTR within D->STMT, if the operand references a TLS
439 variable, then lower the reference to a call to the runtime. Insert
440 any new statements required into D->SEQ; the caller is responsible for
441 placing those appropriately. */
443 static tree
444 lower_emutls_1 (tree *ptr, int *walk_subtrees, void *cb_data)
446 struct walk_stmt_info *wi = (struct walk_stmt_info *) cb_data;
447 struct lower_emutls_data *d = (struct lower_emutls_data *) wi->info;
448 tree t = *ptr;
449 bool is_addr = false;
450 tree addr;
452 *walk_subtrees = 0;
454 switch (TREE_CODE (t))
456 case ADDR_EXPR:
457 /* If this is not a straight-forward "&var", but rather something
458 like "&var.a", then we may need special handling. */
459 if (TREE_CODE (TREE_OPERAND (t, 0)) != VAR_DECL)
461 bool save_changed;
463 /* If we're allowed more than just is_gimple_val, continue. */
464 if (!wi->val_only)
466 *walk_subtrees = 1;
467 return NULL_TREE;
470 /* See if any substitution would be made. */
471 save_changed = wi->changed;
472 wi->changed = false;
473 wi->val_only = false;
474 walk_tree (&TREE_OPERAND (t, 0), lower_emutls_1, wi, NULL);
475 wi->val_only = true;
477 /* If so, then extract this entire sub-expression "&p->a" into a
478 new assignment statement, and substitute yet another SSA_NAME. */
479 if (wi->changed)
481 gimple x;
483 addr = create_tmp_var (TREE_TYPE (t), NULL);
484 x = gimple_build_assign (addr, t);
485 gimple_set_location (x, d->loc);
487 addr = make_ssa_name (addr, x);
488 gimple_assign_set_lhs (x, addr);
490 gimple_seq_add_stmt (&d->seq, x);
492 *ptr = addr;
494 else
495 wi->changed = save_changed;
497 return NULL_TREE;
500 t = TREE_OPERAND (t, 0);
501 is_addr = true;
502 /* FALLTHRU */
504 case VAR_DECL:
505 if (!DECL_THREAD_LOCAL_P (t))
506 return NULL_TREE;
507 break;
509 default:
510 /* We're not interested in other decls or types, only subexpressions. */
511 if (EXPR_P (t))
512 *walk_subtrees = 1;
513 /* FALLTHRU */
515 case SSA_NAME:
516 /* Special-case the return of SSA_NAME, since it's so common. */
517 return NULL_TREE;
520 addr = gen_emutls_addr (t, d);
521 if (is_addr)
523 /* Replace "&var" with "addr" in the statement. */
524 *ptr = addr;
526 else
528 /* Replace "var" with "*addr" in the statement. */
529 t = build2 (MEM_REF, TREE_TYPE (t), addr,
530 build_int_cst (TREE_TYPE (addr), 0));
531 *ptr = t;
534 wi->changed = true;
535 return NULL_TREE;
538 /* Lower all of the operands of STMT. */
540 static void
541 lower_emutls_stmt (gimple stmt, struct lower_emutls_data *d)
543 struct walk_stmt_info wi;
545 d->loc = gimple_location (stmt);
547 memset (&wi, 0, sizeof (wi));
548 wi.info = d;
549 wi.val_only = true;
550 walk_gimple_op (stmt, lower_emutls_1, &wi);
552 if (wi.changed)
553 update_stmt (stmt);
556 /* Lower the I'th operand of PHI. */
558 static void
559 lower_emutls_phi_arg (gimple phi, unsigned int i, struct lower_emutls_data *d)
561 struct walk_stmt_info wi;
562 struct phi_arg_d *pd = gimple_phi_arg (phi, i);
564 /* Early out for a very common case we don't care about. */
565 if (TREE_CODE (pd->def) == SSA_NAME)
566 return;
568 d->loc = pd->locus;
570 memset (&wi, 0, sizeof (wi));
571 wi.info = d;
572 wi.val_only = true;
573 walk_tree (&pd->def, lower_emutls_1, &wi, NULL);
575 /* For normal statements, we let update_stmt do its job. But for phi
576 nodes, we have to manipulate the immediate use list by hand. */
577 if (wi.changed)
579 gcc_assert (TREE_CODE (pd->def) == SSA_NAME);
580 link_imm_use_stmt (&pd->imm_use, pd->def, phi);
584 /* Reset access variable for a given TLS variable data DATA. */
586 bool
587 reset_access (varpool_node * const &, tls_var_data *data, void *)
589 data->access = NULL;
591 return true;
594 /* Clear the access variables, in order to begin a new block. */
596 static inline void
597 clear_access_vars (void)
599 tls_map->traverse<void *, reset_access> (NULL);
602 /* Lower the entire function NODE. */
604 static void
605 lower_emutls_function_body (struct cgraph_node *node)
607 struct lower_emutls_data d;
608 bool any_edge_inserts = false;
610 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
612 d.cfun_node = node;
613 d.builtin_decl = builtin_decl_explicit (BUILT_IN_EMUTLS_GET_ADDRESS);
614 /* This is where we introduce the declaration to the IL and so we have to
615 create a node for it. */
616 d.builtin_node = cgraph_node::get_create (d.builtin_decl);
618 FOR_EACH_BB_FN (d.bb, cfun)
620 gimple_stmt_iterator gsi;
621 unsigned int i, nedge;
623 /* Lower each of the PHI nodes of the block, as we may have
624 propagated &tlsvar into a PHI argument. These loops are
625 arranged so that we process each edge at once, and each
626 PHI argument for that edge. */
627 if (!gimple_seq_empty_p (phi_nodes (d.bb)))
629 /* The calls will be inserted on the edges, and the frequencies
630 will be computed during the commit process. */
631 d.bb_freq = 0;
633 nedge = EDGE_COUNT (d.bb->preds);
634 for (i = 0; i < nedge; ++i)
636 edge e = EDGE_PRED (d.bb, i);
638 /* We can re-use any SSA_NAME created on this edge. */
639 clear_access_vars ();
640 d.seq = NULL;
642 for (gsi = gsi_start_phis (d.bb);
643 !gsi_end_p (gsi);
644 gsi_next (&gsi))
645 lower_emutls_phi_arg (gsi_stmt (gsi), i, &d);
647 /* Insert all statements generated by all phi nodes for this
648 particular edge all at once. */
649 if (d.seq)
651 gsi_insert_seq_on_edge (e, d.seq);
652 any_edge_inserts = true;
657 d.bb_freq = compute_call_stmt_bb_frequency (current_function_decl, d.bb);
659 /* We can re-use any SSA_NAME created during this basic block. */
660 clear_access_vars ();
662 /* Lower each of the statements of the block. */
663 for (gsi = gsi_start_bb (d.bb); !gsi_end_p (gsi); gsi_next (&gsi))
665 d.seq = NULL;
666 lower_emutls_stmt (gsi_stmt (gsi), &d);
668 /* If any new statements were created, insert them immediately
669 before the first use. This prevents variable lifetimes from
670 becoming unnecessarily long. */
671 if (d.seq)
672 gsi_insert_seq_before (&gsi, d.seq, GSI_SAME_STMT);
676 if (any_edge_inserts)
677 gsi_commit_edge_inserts ();
679 pop_cfun ();
682 /* Create emutls variable for VAR, DATA is pointer to static
683 ctor body we can add constructors to.
684 Callback for varpool_for_variable_and_aliases. */
686 static bool
687 create_emultls_var (varpool_node *var, void *data)
689 tree cdecl;
690 tls_var_data value;
692 cdecl = new_emutls_decl (var->decl,
693 var->alias && var->analyzed
694 ? var->get_alias_target ()->decl : NULL);
696 varpool_node *cvar = varpool_node::get (cdecl);
698 if (!var->alias)
700 /* Make sure the COMMON block control variable gets initialized.
701 Note that there's no point in doing this for aliases; we only
702 need to do this once for the main variable. */
703 emutls_common_1 (var->decl, cdecl, (tree *)data);
705 if (var->alias && !var->analyzed)
706 cvar->alias = true;
708 /* Indicate that the value of the TLS variable may be found elsewhere,
709 preventing the variable from re-appearing in the GIMPLE. We cheat
710 and use the control variable here (rather than a full call_expr),
711 which is special-cased inside the DWARF2 output routines. */
712 SET_DECL_VALUE_EXPR (var->decl, cdecl);
713 DECL_HAS_VALUE_EXPR_P (var->decl) = 1;
715 value.control_var = cvar;
716 tls_map->put (var, value);
718 return false;
721 /* Main entry point to the tls lowering pass. */
723 static unsigned int
724 ipa_lower_emutls (void)
726 varpool_node *var;
727 cgraph_node *func;
728 bool any_aliases = false;
729 tree ctor_body = NULL;
731 auto_vec <varpool_node *> tls_vars;
733 /* Examine all global variables for TLS variables. */
734 FOR_EACH_VARIABLE (var)
735 if (DECL_THREAD_LOCAL_P (var->decl))
737 gcc_checking_assert (TREE_STATIC (var->decl)
738 || DECL_EXTERNAL (var->decl));
739 tls_vars.safe_push (var);
740 if (var->alias && var->definition)
741 tls_vars.safe_push (var->ultimate_alias_target ());
744 /* If we found no TLS variables, then there is no further work to do. */
745 if (tls_vars.is_empty ())
747 if (dump_file)
748 fprintf (dump_file, "No TLS variables found.\n");
749 return 0;
752 tls_map = new hash_map <varpool_node *, tls_var_data> ();
754 /* Create the control variables for each TLS variable. */
755 for (unsigned i = 0; i < tls_vars.length (); i++)
757 var = tls_vars[i];
759 if (var->alias && !var->analyzed)
760 any_aliases = true;
761 else if (!var->alias)
762 var->call_for_node_and_aliases (create_emultls_var, &ctor_body, true);
765 /* If there were any aliases, then frob the alias_pairs vector. */
766 if (any_aliases)
768 alias_pair *p;
769 unsigned int i;
770 FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
771 if (DECL_THREAD_LOCAL_P (p->decl))
773 p->decl = tls_map->get
774 (varpool_node::get (p->decl))->control_var->decl;
775 p->target = get_emutls_object_name (p->target);
779 /* Adjust all uses of TLS variables within the function bodies. */
780 FOR_EACH_DEFINED_FUNCTION (func)
781 if (func->lowered)
782 lower_emutls_function_body (func);
784 /* Generate the constructor for any COMMON control variables created. */
785 if (ctor_body)
786 cgraph_build_static_cdtor ('I', ctor_body, DEFAULT_INIT_PRIORITY);
788 delete tls_map;
790 return 0;
793 namespace {
795 const pass_data pass_data_ipa_lower_emutls =
797 SIMPLE_IPA_PASS, /* type */
798 "emutls", /* name */
799 OPTGROUP_NONE, /* optinfo_flags */
800 TV_IPA_OPT, /* tv_id */
801 ( PROP_cfg | PROP_ssa ), /* properties_required */
802 0, /* properties_provided */
803 0, /* properties_destroyed */
804 0, /* todo_flags_start */
805 0, /* todo_flags_finish */
808 class pass_ipa_lower_emutls : public simple_ipa_opt_pass
810 public:
811 pass_ipa_lower_emutls (gcc::context *ctxt)
812 : simple_ipa_opt_pass (pass_data_ipa_lower_emutls, ctxt)
815 /* opt_pass methods: */
816 virtual bool gate (function *)
818 /* If the target supports TLS natively, we need do nothing here. */
819 return !targetm.have_tls;
822 virtual unsigned int execute (function *) { return ipa_lower_emutls (); }
824 }; // class pass_ipa_lower_emutls
826 } // anon namespace
828 simple_ipa_opt_pass *
829 make_pass_ipa_lower_emutls (gcc::context *ctxt)
831 return new pass_ipa_lower_emutls (ctxt);