2010-07-27 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc/alias-decl.git] / gcc / tree-emutls.c
blob17f97be1104bf56fac85189e97da9330fc78d5e1
1 /* Lower TLS operations to emulation functions.
2 Copyright (C) 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
10 later version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tree.h"
25 #include "gimple.h"
26 #include "tree-pass.h"
27 #include "tree-flow.h"
28 #include "cgraph.h"
29 #include "langhooks.h"
30 #include "target.h"
31 #include "targhooks.h"
32 #include "tree-iterator.h"
35 /* Whenever a target does not support thread-local storage (TLS) natively,
36 we can emulate it with some run-time support in libgcc. This will in
37 turn rely on "keyed storage" a-la pthread_key_create; essentially all
38 thread libraries provide such functionality.
40 In order to coordinate with the libgcc runtime, each TLS variable is
41 described by a "control variable". This control variable records the
42 required size, alignment, and initial value of the TLS variable for
43 instantiation at runtime. It also stores an integer token to be used
44 by the runtime to find the address of the variable within each thread.
46 On the compiler side, this means that we need to replace all instances
47 of "tls_var" in the code with "*__emutls_get_addr(&control_var)". We
48 also need to eliminate "tls_var" from the symbol table and introduce
49 "control_var".
51 We used to perform all of the transformations during conversion to rtl,
52 and the variable substitutions magically within assemble_variable.
53 However, this late fiddling of the symbol table conflicts with LTO and
54 whole-program compilation. Therefore we must now make all the changes
55 to the symbol table early in the GIMPLE optimization path, before we
56 write things out to LTO intermediate files. */
58 /* These two vectors, once fully populated, are kept in lock-step so that
59 the index of a TLS variable equals the index of its control variable in
60 the other vector. */
61 static varpool_node_set tls_vars;
62 static VEC(varpool_node_ptr, heap) *control_vars;
64 /* For the current basic block, an SSA_NAME that has computed the address
65 of the TLS variable at the corresponding index. */
66 static VEC(tree, heap) *access_vars;
68 /* The type of the control structure, shared with the emutls.c runtime. */
69 static tree emutls_object_type;
71 #if !defined (NO_DOT_IN_LABEL)
72 # define EMUTLS_SEPARATOR "."
73 #elif !defined (NO_DOLLAR_IN_LABEL)
74 # define EMUTLS_SEPARATOR "$"
75 #else
76 # define EMUTLS_SEPARATOR "_"
77 #endif
79 /* Create an IDENTIFIER_NODE by prefixing PREFIX to the
80 IDENTIFIER_NODE NAME's name. */
82 static tree
83 prefix_name (const char *prefix, tree name)
85 unsigned plen = strlen (prefix);
86 unsigned nlen = strlen (IDENTIFIER_POINTER (name));
87 char *toname = (char *) alloca (plen + nlen + 1);
89 memcpy (toname, prefix, plen);
90 memcpy (toname + plen, IDENTIFIER_POINTER (name), nlen + 1);
92 return get_identifier (toname);
95 /* Create an identifier for the struct __emutls_object, given an identifier
96 of the DECL_ASSEMBLY_NAME of the original object. */
98 static tree
99 get_emutls_object_name (tree name)
101 const char *prefix = (targetm.emutls.var_prefix
102 ? targetm.emutls.var_prefix
103 : "__emutls_v" EMUTLS_SEPARATOR);
104 return prefix_name (prefix, name);
107 /* Create the fields of the type for the control variables. Ordinarily
108 this must match struct __emutls_object defined in emutls.c. However
109 this is a target hook so that VxWorks can define its own layout. */
111 tree
112 default_emutls_var_fields (tree type, tree *name ATTRIBUTE_UNUSED)
114 tree word_type_node, field, next_field;
116 field = build_decl (UNKNOWN_LOCATION,
117 FIELD_DECL, get_identifier ("__templ"), ptr_type_node);
118 DECL_CONTEXT (field) = type;
119 next_field = field;
121 field = build_decl (UNKNOWN_LOCATION,
122 FIELD_DECL, get_identifier ("__offset"),
123 ptr_type_node);
124 DECL_CONTEXT (field) = type;
125 DECL_CHAIN (field) = next_field;
126 next_field = field;
128 word_type_node = lang_hooks.types.type_for_mode (word_mode, 1);
129 field = build_decl (UNKNOWN_LOCATION,
130 FIELD_DECL, get_identifier ("__align"),
131 word_type_node);
132 DECL_CONTEXT (field) = type;
133 DECL_CHAIN (field) = next_field;
134 next_field = field;
136 field = build_decl (UNKNOWN_LOCATION,
137 FIELD_DECL, get_identifier ("__size"), word_type_node);
138 DECL_CONTEXT (field) = type;
139 DECL_CHAIN (field) = next_field;
141 return field;
144 /* Initialize emulated tls object TO, which refers to TLS variable DECL and
145 is initialized by PROXY. As above, this is the default implementation of
146 a target hook overridden by VxWorks. */
148 tree
149 default_emutls_var_init (tree to, tree decl, tree proxy)
151 VEC(constructor_elt,gc) *v = VEC_alloc (constructor_elt, gc, 4);
152 constructor_elt *elt;
153 tree type = TREE_TYPE (to);
154 tree field = TYPE_FIELDS (type);
156 elt = VEC_quick_push (constructor_elt, v, NULL);
157 elt->index = field;
158 elt->value = fold_convert (TREE_TYPE (field), DECL_SIZE_UNIT (decl));
160 elt = VEC_quick_push (constructor_elt, v, NULL);
161 field = DECL_CHAIN (field);
162 elt->index = field;
163 elt->value = build_int_cst (TREE_TYPE (field),
164 DECL_ALIGN_UNIT (decl));
166 elt = VEC_quick_push (constructor_elt, v, NULL);
167 field = DECL_CHAIN (field);
168 elt->index = field;
169 elt->value = null_pointer_node;
171 elt = VEC_quick_push (constructor_elt, v, NULL);
172 field = DECL_CHAIN (field);
173 elt->index = field;
174 elt->value = proxy;
176 return build_constructor (type, v);
179 /* Create the structure for struct __emutls_object. This should match the
180 structure at the top of emutls.c, modulo the union there. */
182 static tree
183 get_emutls_object_type (void)
185 tree type, type_name, field;
187 type = emutls_object_type;
188 if (type)
189 return type;
191 emutls_object_type = type = lang_hooks.types.make_type (RECORD_TYPE);
192 type_name = NULL;
193 field = targetm.emutls.var_fields (type, &type_name);
194 if (!type_name)
195 type_name = get_identifier ("__emutls_object");
196 type_name = build_decl (UNKNOWN_LOCATION,
197 TYPE_DECL, type_name, type);
198 TYPE_NAME (type) = type_name;
199 TYPE_FIELDS (type) = field;
200 layout_type (type);
202 return type;
205 /* Create a read-only variable like DECL, with the same DECL_INITIAL.
206 This will be used for initializing the emulated tls data area. */
208 static tree
209 get_emutls_init_templ_addr (tree decl)
211 tree name, to;
213 if (targetm.emutls.register_common && !DECL_INITIAL (decl)
214 && !DECL_SECTION_NAME (decl))
215 return null_pointer_node;
217 name = DECL_ASSEMBLER_NAME (decl);
218 if (!targetm.emutls.tmpl_prefix || targetm.emutls.tmpl_prefix[0])
220 const char *prefix = (targetm.emutls.tmpl_prefix
221 ? targetm.emutls.tmpl_prefix
222 : "__emutls_t" EMUTLS_SEPARATOR);
223 name = prefix_name (prefix, name);
226 to = build_decl (DECL_SOURCE_LOCATION (decl),
227 VAR_DECL, name, TREE_TYPE (decl));
228 SET_DECL_ASSEMBLER_NAME (to, DECL_NAME (to));
230 DECL_ARTIFICIAL (to) = 1;
231 TREE_USED (to) = TREE_USED (decl);
232 TREE_READONLY (to) = 1;
233 DECL_IGNORED_P (to) = 1;
234 DECL_CONTEXT (to) = DECL_CONTEXT (decl);
235 DECL_SECTION_NAME (to) = DECL_SECTION_NAME (decl);
236 DECL_PRESERVE_P (to) = DECL_PRESERVE_P (decl);
238 DECL_WEAK (to) = DECL_WEAK (decl);
239 if (DECL_ONE_ONLY (decl))
241 make_decl_one_only (to, DECL_ASSEMBLER_NAME (to));
242 TREE_STATIC (to) = TREE_STATIC (decl);
243 TREE_PUBLIC (to) = TREE_PUBLIC (decl);
244 DECL_VISIBILITY (to) = DECL_VISIBILITY (decl);
246 else
247 TREE_STATIC (to) = 1;
249 DECL_VISIBILITY_SPECIFIED (to) = DECL_VISIBILITY_SPECIFIED (decl);
250 DECL_INITIAL (to) = DECL_INITIAL (decl);
251 DECL_INITIAL (decl) = NULL;
253 if (targetm.emutls.tmpl_section)
255 DECL_SECTION_NAME (to)
256 = build_string (strlen (targetm.emutls.tmpl_section),
257 targetm.emutls.tmpl_section);
260 varpool_finalize_decl (to);
261 return build_fold_addr_expr (to);
264 /* Create and return the control variable for the TLS variable DECL. */
266 static tree
267 new_emutls_decl (tree decl)
269 tree name, to;
271 name = DECL_ASSEMBLER_NAME (decl);
272 to = build_decl (DECL_SOURCE_LOCATION (decl), VAR_DECL,
273 get_emutls_object_name (name),
274 get_emutls_object_type ());
276 SET_DECL_ASSEMBLER_NAME (to, DECL_NAME (to));
278 DECL_TLS_MODEL (to) = TLS_MODEL_EMULATED;
279 DECL_ARTIFICIAL (to) = 1;
280 DECL_IGNORED_P (to) = 1;
281 TREE_READONLY (to) = 0;
282 TREE_STATIC (to) = 1;
284 DECL_PRESERVE_P (to) = DECL_PRESERVE_P (decl);
285 DECL_CONTEXT (to) = DECL_CONTEXT (decl);
286 TREE_USED (to) = TREE_USED (decl);
287 TREE_PUBLIC (to) = TREE_PUBLIC (decl);
288 DECL_EXTERNAL (to) = DECL_EXTERNAL (decl);
289 DECL_COMMON (to) = DECL_COMMON (decl);
290 DECL_WEAK (to) = DECL_WEAK (decl);
291 DECL_VISIBILITY (to) = DECL_VISIBILITY (decl);
292 DECL_VISIBILITY_SPECIFIED (to) = DECL_VISIBILITY_SPECIFIED (decl);
293 DECL_RESTRICTED_P (to) = DECL_RESTRICTED_P (decl);
294 DECL_DLLIMPORT_P (to) = DECL_DLLIMPORT_P (decl);
296 DECL_ATTRIBUTES (to) = targetm.merge_decl_attributes (decl, to);
298 if (DECL_ONE_ONLY (decl))
299 make_decl_one_only (to, DECL_ASSEMBLER_NAME (to));
301 /* If we're not allowed to change the proxy object's alignment,
302 pretend it has been set by the user. */
303 if (targetm.emutls.var_align_fixed)
304 DECL_USER_ALIGN (to) = 1;
306 /* If the target wants the control variables grouped, do so. */
307 if (!DECL_COMMON (to) && targetm.emutls.var_section)
309 DECL_SECTION_NAME (to)
310 = build_string (strlen (targetm.emutls.tmpl_section),
311 targetm.emutls.tmpl_section);
314 /* If this variable is defined locally, then we need to initialize the
315 control structure with size and alignment information. Initialization
316 of COMMON block variables happens elsewhere via a constructor. */
317 if (!DECL_EXTERNAL (to)
318 && (!DECL_COMMON (to)
319 || (DECL_INITIAL (decl)
320 && DECL_INITIAL (decl) != error_mark_node)))
322 tree tmpl = get_emutls_init_templ_addr (decl);
323 DECL_INITIAL (to) = targetm.emutls.var_init (to, decl, tmpl);
324 record_references_in_initializer (to, false);
327 varpool_finalize_decl (to);
328 return to;
331 /* Look up the index of the TLS variable DECL. This index can then be
332 used in both the control_vars and access_vars arrays. */
334 static unsigned int
335 emutls_index (tree decl)
337 varpool_node_set_iterator i;
339 i = varpool_node_set_find (tls_vars, varpool_get_node (decl));
340 gcc_assert (i.index != ~0u);
342 return i.index;
345 /* Look up the control variable for the TLS variable DECL. */
347 static tree
348 emutls_decl (tree decl)
350 struct varpool_node *var;
351 unsigned int i;
353 i = emutls_index (decl);
354 var = VEC_index (varpool_node_ptr, control_vars, i);
355 return var->decl;
358 /* Generate a call statement to initialize CONTROL_DECL for TLS_DECL.
359 This only needs to happen for TLS COMMON variables; non-COMMON
360 variables can be initialized statically. Insert the generated
361 call statement at the end of PSTMTS. */
363 static void
364 emutls_common_1 (tree tls_decl, tree control_decl, tree *pstmts)
366 tree x;
367 tree word_type_node;
369 if (! DECL_COMMON (tls_decl)
370 || (DECL_INITIAL (tls_decl)
371 && DECL_INITIAL (tls_decl) != error_mark_node))
372 return;
374 word_type_node = lang_hooks.types.type_for_mode (word_mode, 1);
376 x = build_call_expr (built_in_decls[BUILT_IN_EMUTLS_REGISTER_COMMON], 4,
377 build_fold_addr_expr (control_decl),
378 fold_convert (word_type_node,
379 DECL_SIZE_UNIT (tls_decl)),
380 build_int_cst (word_type_node,
381 DECL_ALIGN_UNIT (tls_decl)),
382 get_emutls_init_templ_addr (tls_decl));
384 append_to_statement_list (x, pstmts);
387 struct lower_emutls_data
389 struct cgraph_node *cfun_node;
390 struct cgraph_node *builtin_node;
391 tree builtin_decl;
392 basic_block bb;
393 int bb_freq;
394 location_t loc;
395 gimple_seq seq;
398 /* Given a TLS variable DECL, return an SSA_NAME holding its address.
399 Append any new computation statements required to D->SEQ. */
401 static tree
402 gen_emutls_addr (tree decl, struct lower_emutls_data *d)
404 unsigned int index;
405 tree addr;
407 /* Compute the address of the TLS variable with help from runtime. */
408 index = emutls_index (decl);
409 addr = VEC_index (tree, access_vars, index);
410 if (addr == NULL)
412 struct varpool_node *cvar;
413 tree cdecl;
414 gimple x;
416 cvar = VEC_index (varpool_node_ptr, control_vars, index);
417 cdecl = cvar->decl;
418 TREE_ADDRESSABLE (cdecl) = 1;
420 addr = create_tmp_var (build_pointer_type (TREE_TYPE (decl)), NULL);
421 x = gimple_build_call (d->builtin_decl, 1, build_fold_addr_expr (cdecl));
422 gimple_set_location (x, d->loc);
424 addr = make_ssa_name (addr, x);
425 gimple_call_set_lhs (x, addr);
427 gimple_seq_add_stmt (&d->seq, x);
429 cgraph_create_edge (d->cfun_node, d->builtin_node, x,
430 d->bb->count, d->bb_freq, d->bb->loop_depth);
432 /* We may be adding a new reference to a new variable to the function.
433 This means we have to play with the ipa-reference web. */
434 ipa_record_reference (d->cfun_node, NULL, NULL, cvar, IPA_REF_ADDR, x);
436 /* Record this ssa_name for possible use later in the basic block. */
437 VEC_replace (tree, access_vars, index, addr);
440 return addr;
443 /* Callback for walk_gimple_op. D = WI->INFO is a struct lower_emutls_data.
444 Given an operand *PTR within D->STMT, if the operand references a TLS
445 variable, then lower the reference to a call to the runtime. Insert
446 any new statements required into D->SEQ; the caller is responsible for
447 placing those appropriately. */
449 static tree
450 lower_emutls_1 (tree *ptr, int *walk_subtrees, void *cb_data)
452 struct walk_stmt_info *wi = (struct walk_stmt_info *) cb_data;
453 struct lower_emutls_data *d = (struct lower_emutls_data *) wi->info;
454 tree t = *ptr;
455 bool is_addr = false;
456 tree addr;
458 *walk_subtrees = 0;
460 switch (TREE_CODE (t))
462 case ADDR_EXPR:
463 /* If this is not a straight-forward "&var", but rather something
464 like "&var.a", then we may need special handling. */
465 if (TREE_CODE (TREE_OPERAND (t, 0)) != VAR_DECL)
467 bool save_changed;
469 /* If we're allowed more than just is_gimple_val, continue. */
470 if (!wi->val_only)
472 *walk_subtrees = 1;
473 return NULL_TREE;
476 /* See if any substitution would be made. */
477 save_changed = wi->changed;
478 wi->changed = false;
479 wi->val_only = false;
480 walk_tree (&TREE_OPERAND (t, 0), lower_emutls_1, wi, NULL);
481 wi->val_only = true;
483 /* If so, then extract this entire sub-expression "&p->a" into a
484 new assignment statement, and substitute yet another SSA_NAME. */
485 if (wi->changed)
487 gimple x;
489 addr = create_tmp_var (TREE_TYPE (t), NULL);
490 x = gimple_build_assign (addr, t);
491 gimple_set_location (x, d->loc);
493 addr = make_ssa_name (addr, x);
494 gimple_assign_set_lhs (x, addr);
496 gimple_seq_add_stmt (&d->seq, x);
498 *ptr = addr;
500 else
501 wi->changed = save_changed;
503 return NULL_TREE;
506 t = TREE_OPERAND (t, 0);
507 is_addr = true;
508 /* FALLTHRU */
510 case VAR_DECL:
511 if (!DECL_THREAD_LOCAL_P (t))
512 return NULL_TREE;
513 break;
515 default:
516 /* We're not interested in other decls or types, only subexpressions. */
517 if (EXPR_P (t))
518 *walk_subtrees = 1;
519 /* FALLTHRU */
521 case SSA_NAME:
522 /* Special-case the return of SSA_NAME, since it's so common. */
523 return NULL_TREE;
526 addr = gen_emutls_addr (t, d);
527 if (is_addr)
529 /* Replace "&var" with "addr" in the statement. */
530 *ptr = addr;
532 else
534 /* Replace "var" with "*addr" in the statement. */
535 t = build2 (MEM_REF, TREE_TYPE (t), addr,
536 build_int_cst (TREE_TYPE (addr), 0));
537 *ptr = t;
540 wi->changed = true;
541 return NULL_TREE;
544 /* Lower all of the operands of STMT. */
546 static void
547 lower_emutls_stmt (gimple stmt, struct lower_emutls_data *d)
549 struct walk_stmt_info wi;
551 d->loc = gimple_location (stmt);
553 memset (&wi, 0, sizeof (wi));
554 wi.info = d;
555 wi.val_only = true;
556 walk_gimple_op (stmt, lower_emutls_1, &wi);
558 if (wi.changed)
559 update_stmt (stmt);
562 /* Lower the I'th operand of PHI. */
564 static void
565 lower_emutls_phi_arg (gimple phi, unsigned int i, struct lower_emutls_data *d)
567 struct walk_stmt_info wi;
568 struct phi_arg_d *pd = gimple_phi_arg (phi, i);
570 /* Early out for a very common case we don't care about. */
571 if (TREE_CODE (pd->def) == SSA_NAME)
572 return;
574 d->loc = pd->locus;
576 memset (&wi, 0, sizeof (wi));
577 wi.info = d;
578 wi.val_only = true;
579 walk_tree (&pd->def, lower_emutls_1, &wi, NULL);
581 /* For normal statements, we let update_stmt do its job. But for phi
582 nodes, we have to manipulate the immediate use list by hand. */
583 if (wi.changed)
585 gcc_assert (TREE_CODE (pd->def) == SSA_NAME);
586 link_imm_use_stmt (&pd->imm_use, pd->def, phi);
590 /* Clear the ACCESS_VARS array, in order to begin a new block. */
592 static inline void
593 clear_access_vars (void)
595 memset (VEC_address (tree, access_vars), 0,
596 VEC_length (tree, access_vars) * sizeof(tree));
599 /* Lower the entire function NODE. */
601 static void
602 lower_emutls_function_body (struct cgraph_node *node)
604 struct lower_emutls_data d;
605 bool any_edge_inserts = false;
607 current_function_decl = node->decl;
608 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
610 d.cfun_node = node;
611 d.builtin_decl = built_in_decls[BUILT_IN_EMUTLS_GET_ADDRESS];
612 d.builtin_node = cgraph_node (d.builtin_decl);
614 FOR_EACH_BB (d.bb)
616 gimple_stmt_iterator gsi;
617 unsigned int i, nedge;
619 /* Lower each of the PHI nodes of the block, as we may have
620 propagated &tlsvar into a PHI argument. These loops are
621 arranged so that we process each edge at once, and each
622 PHI argument for that edge. */
623 if (!gimple_seq_empty_p (phi_nodes (d.bb)))
625 /* The calls will be inserted on the edges, and the frequencies
626 will be computed during the commit process. */
627 d.bb_freq = 0;
629 nedge = EDGE_COUNT (d.bb->preds);
630 for (i = 0; i < nedge; ++i)
632 edge e = EDGE_PRED (d.bb, i);
634 /* We can re-use any SSA_NAME created on this edge. */
635 clear_access_vars ();
636 d.seq = NULL;
638 for (gsi = gsi_start_phis (d.bb);
639 !gsi_end_p (gsi);
640 gsi_next (&gsi))
641 lower_emutls_phi_arg (gsi_stmt (gsi), i, &d);
643 /* Insert all statements generated by all phi nodes for this
644 particular edge all at once. */
645 if (d.seq)
647 gsi_insert_seq_on_edge (e, d.seq);
648 any_edge_inserts = true;
653 d.bb_freq = compute_call_stmt_bb_frequency (current_function_decl, d.bb);
655 /* We can re-use any SSA_NAME created during this basic block. */
656 clear_access_vars ();
658 /* Lower each of the statements of the block. */
659 for (gsi = gsi_start_bb (d.bb); !gsi_end_p (gsi); gsi_next (&gsi))
661 d.seq = NULL;
662 lower_emutls_stmt (gsi_stmt (gsi), &d);
664 /* If any new statements were created, insert them immediately
665 before the first use. This prevents variable lifetimes from
666 becoming unnecessarily long. */
667 if (d.seq)
668 gsi_insert_seq_before (&gsi, d.seq, GSI_SAME_STMT);
672 if (any_edge_inserts)
673 gsi_commit_edge_inserts ();
675 pop_cfun ();
676 current_function_decl = NULL;
679 /* Main entry point to the tls lowering pass. */
681 static unsigned int
682 ipa_lower_emutls (void)
684 struct varpool_node *var;
685 struct cgraph_node *func;
686 bool any_aliases = false;
687 tree ctor_body = NULL;
688 unsigned int i, n_tls;
690 tls_vars = varpool_node_set_new ();
692 /* Examine all global variables for TLS variables. */
693 for (var = varpool_nodes; var ; var = var->next)
694 if (DECL_THREAD_LOCAL_P (var->decl))
696 gcc_checking_assert (TREE_STATIC (var->decl)
697 || DECL_EXTERNAL (var->decl));
698 varpool_node_set_add (tls_vars, var);
701 /* If we found no TLS variables, then there is no further work to do. */
702 if (tls_vars->nodes == NULL)
704 tls_vars = NULL;
705 if (dump_file)
706 fprintf (dump_file, "No TLS variables found.\n");
707 return 0;
710 /* Allocate the on-the-side arrays that share indicies with the TLS vars. */
711 n_tls = VEC_length (varpool_node_ptr, tls_vars->nodes);
712 control_vars = VEC_alloc (varpool_node_ptr, heap, n_tls);
713 access_vars = VEC_alloc (tree, heap, n_tls);
714 VEC_safe_grow (tree, heap, access_vars, n_tls);
716 /* Create the control variables for each TLS variable. */
717 for (i = 0; VEC_iterate (varpool_node_ptr, tls_vars->nodes, i, var); ++i)
719 tree cdecl;
720 struct varpool_node *cvar;
722 var = VEC_index (varpool_node_ptr, tls_vars->nodes, i);
723 cdecl = new_emutls_decl (var->decl);
725 cvar = varpool_get_node (cdecl);
726 VEC_quick_push (varpool_node_ptr, control_vars, cvar);
728 if (var->alias)
730 any_aliases = true;
731 cvar->alias = true;
733 else
735 /* Make sure the COMMON block control variable gets initialized.
736 Note that there's no point in doing this for aliases; we only
737 need to do this once for the main variable. */
738 emutls_common_1 (var->decl, cdecl, &ctor_body);
741 /* Indicate that the value of the TLS variable may be found elsewhere,
742 preventing the variable from re-appearing in the GIMPLE. We cheat
743 and use the control variable here (rather than a full call_expr),
744 which is special-cased inside the DWARF2 output routines. */
745 SET_DECL_VALUE_EXPR (var->decl, cdecl);
746 DECL_HAS_VALUE_EXPR_P (var->decl) = 1;
749 /* If there were any aliases, then frob the alias_pairs vector. */
750 if (any_aliases)
752 alias_pair *p;
753 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); ++i)
754 if (DECL_THREAD_LOCAL_P (p->decl))
756 p->decl = emutls_decl (p->decl);
757 p->target = get_emutls_object_name (p->target);
761 /* Adjust all uses of TLS variables within the function bodies. */
762 for (func = cgraph_nodes; func; func = func->next)
763 if (func->reachable && func->lowered)
764 lower_emutls_function_body (func);
766 /* Generate the constructor for any COMMON control variables created. */
767 if (ctor_body)
768 cgraph_build_static_cdtor ('I', ctor_body, DEFAULT_INIT_PRIORITY);
770 VEC_free (varpool_node_ptr, heap, control_vars);
771 VEC_free (tree, heap, access_vars);
772 tls_vars = NULL;
774 return TODO_dump_func | TODO_ggc_collect | TODO_verify_all;
777 /* If the target supports TLS natively, we need do nothing here. */
779 static bool
780 gate_emutls (void)
782 return !targetm.have_tls;
785 struct simple_ipa_opt_pass pass_ipa_lower_emutls =
788 SIMPLE_IPA_PASS,
789 "emutls", /* name */
790 gate_emutls, /* gate */
791 ipa_lower_emutls, /* execute */
792 NULL, /* sub */
793 NULL, /* next */
794 0, /* static_pass_number */
795 TV_NONE, /* tv_id */
796 PROP_cfg | PROP_ssa, /* properties_required */
797 0, /* properties_provided */
798 0, /* properties_destroyed */
799 0, /* todo_flags_start */
800 0, /* todo_flags_finish */