* config/rx/rx.c (ADD_RX_BUILTIN0): New macro, used for builtins
[official-gcc.git] / gcc / tree-emutls.c
blobb5ca407d62e22fd9c52764f33228ff6ade76a761
1 /* Lower TLS operations to emulation functions.
2 Copyright (C) 2006-2013 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 "gimple.h"
25 #include "tree-pass.h"
26 #include "gimple-ssa.h"
27 #include "cgraph.h"
28 #include "tree-phinodes.h"
29 #include "ssa-iterators.h"
30 #include "tree-ssanames.h"
31 #include "langhooks.h"
32 #include "target.h"
33 #include "targhooks.h"
34 #include "tree-iterator.h"
37 /* Whenever a target does not support thread-local storage (TLS) natively,
38 we can emulate it with some run-time support in libgcc. This will in
39 turn rely on "keyed storage" a-la pthread_key_create; essentially all
40 thread libraries provide such functionality.
42 In order to coordinate with the libgcc runtime, each TLS variable is
43 described by a "control variable". This control variable records the
44 required size, alignment, and initial value of the TLS variable for
45 instantiation at runtime. It also stores an integer token to be used
46 by the runtime to find the address of the variable within each thread.
48 On the compiler side, this means that we need to replace all instances
49 of "tls_var" in the code with "*__emutls_get_addr(&control_var)". We
50 also need to eliminate "tls_var" from the symbol table and introduce
51 "control_var".
53 We used to perform all of the transformations during conversion to rtl,
54 and the variable substitutions magically within assemble_variable.
55 However, this late fiddling of the symbol table conflicts with LTO and
56 whole-program compilation. Therefore we must now make all the changes
57 to the symbol table early in the GIMPLE optimization path, before we
58 write things out to LTO intermediate files. */
60 /* These two vectors, once fully populated, are kept in lock-step so that
61 the index of a TLS variable equals the index of its control variable in
62 the other vector. */
63 static varpool_node_set tls_vars;
64 static vec<varpool_node_ptr> control_vars;
66 /* For the current basic block, an SSA_NAME that has computed the address
67 of the TLS variable at the corresponding index. */
68 static vec<tree> access_vars;
70 /* The type of the control structure, shared with the emutls.c runtime. */
71 static tree emutls_object_type;
73 #if !defined (NO_DOT_IN_LABEL)
74 # define EMUTLS_SEPARATOR "."
75 #elif !defined (NO_DOLLAR_IN_LABEL)
76 # define EMUTLS_SEPARATOR "$"
77 #else
78 # define EMUTLS_SEPARATOR "_"
79 #endif
81 /* Create an IDENTIFIER_NODE by prefixing PREFIX to the
82 IDENTIFIER_NODE NAME's name. */
84 static tree
85 prefix_name (const char *prefix, tree name)
87 unsigned plen = strlen (prefix);
88 unsigned nlen = strlen (IDENTIFIER_POINTER (name));
89 char *toname = (char *) alloca (plen + nlen + 1);
91 memcpy (toname, prefix, plen);
92 memcpy (toname + plen, IDENTIFIER_POINTER (name), nlen + 1);
94 return get_identifier (toname);
97 /* Create an identifier for the struct __emutls_object, given an identifier
98 of the DECL_ASSEMBLY_NAME of the original object. */
100 static tree
101 get_emutls_object_name (tree name)
103 const char *prefix = (targetm.emutls.var_prefix
104 ? targetm.emutls.var_prefix
105 : "__emutls_v" EMUTLS_SEPARATOR);
106 return prefix_name (prefix, name);
109 /* Create the fields of the type for the control variables. Ordinarily
110 this must match struct __emutls_object defined in emutls.c. However
111 this is a target hook so that VxWorks can define its own layout. */
113 tree
114 default_emutls_var_fields (tree type, tree *name ATTRIBUTE_UNUSED)
116 tree word_type_node, field, next_field;
118 field = build_decl (UNKNOWN_LOCATION,
119 FIELD_DECL, get_identifier ("__templ"), ptr_type_node);
120 DECL_CONTEXT (field) = type;
121 next_field = field;
123 field = build_decl (UNKNOWN_LOCATION,
124 FIELD_DECL, get_identifier ("__offset"),
125 ptr_type_node);
126 DECL_CONTEXT (field) = type;
127 DECL_CHAIN (field) = next_field;
128 next_field = field;
130 word_type_node = lang_hooks.types.type_for_mode (word_mode, 1);
131 field = build_decl (UNKNOWN_LOCATION,
132 FIELD_DECL, get_identifier ("__align"),
133 word_type_node);
134 DECL_CONTEXT (field) = type;
135 DECL_CHAIN (field) = next_field;
136 next_field = field;
138 field = build_decl (UNKNOWN_LOCATION,
139 FIELD_DECL, get_identifier ("__size"), word_type_node);
140 DECL_CONTEXT (field) = type;
141 DECL_CHAIN (field) = next_field;
143 return field;
146 /* Initialize emulated tls object TO, which refers to TLS variable DECL and
147 is initialized by PROXY. As above, this is the default implementation of
148 a target hook overridden by VxWorks. */
150 tree
151 default_emutls_var_init (tree to, tree decl, tree proxy)
153 vec<constructor_elt, va_gc> *v;
154 vec_alloc (v, 4);
155 constructor_elt elt;
156 tree type = TREE_TYPE (to);
157 tree field = TYPE_FIELDS (type);
159 elt.index = field;
160 elt.value = fold_convert (TREE_TYPE (field), DECL_SIZE_UNIT (decl));
161 v->quick_push (elt);
163 field = DECL_CHAIN (field);
164 elt.index = field;
165 elt.value = build_int_cst (TREE_TYPE (field),
166 DECL_ALIGN_UNIT (decl));
167 v->quick_push (elt);
169 field = DECL_CHAIN (field);
170 elt.index = field;
171 elt.value = null_pointer_node;
172 v->quick_push (elt);
174 field = DECL_CHAIN (field);
175 elt.index = field;
176 elt.value = proxy;
177 v->quick_push (elt);
179 return build_constructor (type, v);
182 /* Create the structure for struct __emutls_object. This should match the
183 structure at the top of emutls.c, modulo the union there. */
185 static tree
186 get_emutls_object_type (void)
188 tree type, type_name, field;
190 type = emutls_object_type;
191 if (type)
192 return type;
194 emutls_object_type = type = lang_hooks.types.make_type (RECORD_TYPE);
195 type_name = NULL;
196 field = targetm.emutls.var_fields (type, &type_name);
197 if (!type_name)
198 type_name = get_identifier ("__emutls_object");
199 type_name = build_decl (UNKNOWN_LOCATION,
200 TYPE_DECL, type_name, type);
201 TYPE_NAME (type) = type_name;
202 TYPE_FIELDS (type) = field;
203 layout_type (type);
205 return type;
208 /* Create a read-only variable like DECL, with the same DECL_INITIAL.
209 This will be used for initializing the emulated tls data area. */
211 static tree
212 get_emutls_init_templ_addr (tree decl)
214 tree name, to;
216 if (targetm.emutls.register_common && !DECL_INITIAL (decl)
217 && !DECL_SECTION_NAME (decl))
218 return null_pointer_node;
220 name = DECL_ASSEMBLER_NAME (decl);
221 if (!targetm.emutls.tmpl_prefix || targetm.emutls.tmpl_prefix[0])
223 const char *prefix = (targetm.emutls.tmpl_prefix
224 ? targetm.emutls.tmpl_prefix
225 : "__emutls_t" EMUTLS_SEPARATOR);
226 name = prefix_name (prefix, name);
229 to = build_decl (DECL_SOURCE_LOCATION (decl),
230 VAR_DECL, name, TREE_TYPE (decl));
231 SET_DECL_ASSEMBLER_NAME (to, DECL_NAME (to));
233 DECL_ARTIFICIAL (to) = 1;
234 TREE_USED (to) = TREE_USED (decl);
235 TREE_READONLY (to) = 1;
236 DECL_IGNORED_P (to) = 1;
237 DECL_CONTEXT (to) = DECL_CONTEXT (decl);
238 DECL_SECTION_NAME (to) = DECL_SECTION_NAME (decl);
239 DECL_PRESERVE_P (to) = DECL_PRESERVE_P (decl);
241 DECL_WEAK (to) = DECL_WEAK (decl);
242 if (DECL_ONE_ONLY (decl))
244 make_decl_one_only (to, DECL_ASSEMBLER_NAME (to));
245 TREE_STATIC (to) = TREE_STATIC (decl);
246 TREE_PUBLIC (to) = TREE_PUBLIC (decl);
247 DECL_VISIBILITY (to) = DECL_VISIBILITY (decl);
249 else
250 TREE_STATIC (to) = 1;
252 DECL_VISIBILITY_SPECIFIED (to) = DECL_VISIBILITY_SPECIFIED (decl);
253 DECL_INITIAL (to) = DECL_INITIAL (decl);
254 DECL_INITIAL (decl) = NULL;
256 if (targetm.emutls.tmpl_section)
258 DECL_SECTION_NAME (to)
259 = build_string (strlen (targetm.emutls.tmpl_section),
260 targetm.emutls.tmpl_section);
263 /* Create varpool node for the new variable and finalize it if it is
264 not external one. */
265 if (DECL_EXTERNAL (to))
266 varpool_node_for_decl (to);
267 else
268 varpool_add_new_variable (to);
269 return build_fold_addr_expr (to);
272 /* Create and return the control variable for the TLS variable DECL. */
274 static tree
275 new_emutls_decl (tree decl, tree alias_of)
277 tree name, to;
279 name = DECL_ASSEMBLER_NAME (decl);
280 to = build_decl (DECL_SOURCE_LOCATION (decl), VAR_DECL,
281 get_emutls_object_name (name),
282 get_emutls_object_type ());
284 SET_DECL_ASSEMBLER_NAME (to, DECL_NAME (to));
286 DECL_TLS_MODEL (to) = TLS_MODEL_EMULATED;
287 DECL_ARTIFICIAL (to) = 1;
288 DECL_IGNORED_P (to) = 1;
289 TREE_READONLY (to) = 0;
290 TREE_STATIC (to) = 1;
292 DECL_PRESERVE_P (to) = DECL_PRESERVE_P (decl);
293 DECL_CONTEXT (to) = DECL_CONTEXT (decl);
294 TREE_USED (to) = TREE_USED (decl);
295 TREE_PUBLIC (to) = TREE_PUBLIC (decl);
296 DECL_EXTERNAL (to) = DECL_EXTERNAL (decl);
297 DECL_COMMON (to) = DECL_COMMON (decl);
298 DECL_WEAK (to) = DECL_WEAK (decl);
299 DECL_VISIBILITY (to) = DECL_VISIBILITY (decl);
300 DECL_VISIBILITY_SPECIFIED (to) = DECL_VISIBILITY_SPECIFIED (decl);
301 DECL_DLLIMPORT_P (to) = DECL_DLLIMPORT_P (decl);
303 DECL_ATTRIBUTES (to) = targetm.merge_decl_attributes (decl, to);
305 if (DECL_ONE_ONLY (decl))
306 make_decl_one_only (to, DECL_ASSEMBLER_NAME (to));
308 /* If we're not allowed to change the proxy object's alignment,
309 pretend it has been set by the user. */
310 if (targetm.emutls.var_align_fixed)
311 DECL_USER_ALIGN (to) = 1;
313 /* If the target wants the control variables grouped, do so. */
314 if (!DECL_COMMON (to) && targetm.emutls.var_section)
316 DECL_SECTION_NAME (to)
317 = build_string (strlen (targetm.emutls.var_section),
318 targetm.emutls.var_section);
321 /* If this variable is defined locally, then we need to initialize the
322 control structure with size and alignment information. Initialization
323 of COMMON block variables happens elsewhere via a constructor. */
324 if (!DECL_EXTERNAL (to)
325 && (!DECL_COMMON (to)
326 || (DECL_INITIAL (decl)
327 && DECL_INITIAL (decl) != error_mark_node)))
329 tree tmpl = get_emutls_init_templ_addr (decl);
330 DECL_INITIAL (to) = targetm.emutls.var_init (to, decl, tmpl);
331 record_references_in_initializer (to, false);
334 /* Create varpool node for the new variable and finalize it if it is
335 not external one. */
336 if (DECL_EXTERNAL (to))
337 varpool_node_for_decl (to);
338 else if (!alias_of)
339 varpool_add_new_variable (to);
340 else
341 varpool_create_variable_alias (to,
342 varpool_node_for_asm
343 (DECL_ASSEMBLER_NAME (DECL_VALUE_EXPR (alias_of)))->decl);
344 return to;
347 /* Look up the index of the TLS variable DECL. This index can then be
348 used in both the control_vars and access_vars arrays. */
350 static unsigned int
351 emutls_index (tree decl)
353 varpool_node_set_iterator i;
355 i = varpool_node_set_find (tls_vars, varpool_get_node (decl));
356 gcc_assert (i.index != ~0u);
358 return i.index;
361 /* Look up the control variable for the TLS variable DECL. */
363 static tree
364 emutls_decl (tree decl)
366 struct varpool_node *var;
367 unsigned int i;
369 i = emutls_index (decl);
370 var = control_vars[i];
371 return var->decl;
374 /* Generate a call statement to initialize CONTROL_DECL for TLS_DECL.
375 This only needs to happen for TLS COMMON variables; non-COMMON
376 variables can be initialized statically. Insert the generated
377 call statement at the end of PSTMTS. */
379 static void
380 emutls_common_1 (tree tls_decl, tree control_decl, tree *pstmts)
382 tree x;
383 tree word_type_node;
385 if (! DECL_COMMON (tls_decl)
386 || (DECL_INITIAL (tls_decl)
387 && DECL_INITIAL (tls_decl) != error_mark_node))
388 return;
390 word_type_node = lang_hooks.types.type_for_mode (word_mode, 1);
392 x = build_call_expr (builtin_decl_explicit (BUILT_IN_EMUTLS_REGISTER_COMMON),
393 4, build_fold_addr_expr (control_decl),
394 fold_convert (word_type_node,
395 DECL_SIZE_UNIT (tls_decl)),
396 build_int_cst (word_type_node,
397 DECL_ALIGN_UNIT (tls_decl)),
398 get_emutls_init_templ_addr (tls_decl));
400 append_to_statement_list (x, pstmts);
403 struct lower_emutls_data
405 struct cgraph_node *cfun_node;
406 struct cgraph_node *builtin_node;
407 tree builtin_decl;
408 basic_block bb;
409 int bb_freq;
410 location_t loc;
411 gimple_seq seq;
414 /* Given a TLS variable DECL, return an SSA_NAME holding its address.
415 Append any new computation statements required to D->SEQ. */
417 static tree
418 gen_emutls_addr (tree decl, struct lower_emutls_data *d)
420 unsigned int index;
421 tree addr;
423 /* Compute the address of the TLS variable with help from runtime. */
424 index = emutls_index (decl);
425 addr = access_vars[index];
426 if (addr == NULL)
428 struct varpool_node *cvar;
429 tree cdecl;
430 gimple x;
432 cvar = control_vars[index];
433 cdecl = cvar->decl;
434 TREE_ADDRESSABLE (cdecl) = 1;
436 addr = create_tmp_var (build_pointer_type (TREE_TYPE (decl)), NULL);
437 x = gimple_build_call (d->builtin_decl, 1, build_fold_addr_expr (cdecl));
438 gimple_set_location (x, d->loc);
440 addr = make_ssa_name (addr, x);
441 gimple_call_set_lhs (x, addr);
443 gimple_seq_add_stmt (&d->seq, x);
445 cgraph_create_edge (d->cfun_node, d->builtin_node, x,
446 d->bb->count, d->bb_freq);
448 /* We may be adding a new reference to a new variable to the function.
449 This means we have to play with the ipa-reference web. */
450 ipa_record_reference (d->cfun_node, cvar, IPA_REF_ADDR, x);
452 /* Record this ssa_name for possible use later in the basic block. */
453 access_vars[index] = addr;
456 return addr;
459 /* Callback for walk_gimple_op. D = WI->INFO is a struct lower_emutls_data.
460 Given an operand *PTR within D->STMT, if the operand references a TLS
461 variable, then lower the reference to a call to the runtime. Insert
462 any new statements required into D->SEQ; the caller is responsible for
463 placing those appropriately. */
465 static tree
466 lower_emutls_1 (tree *ptr, int *walk_subtrees, void *cb_data)
468 struct walk_stmt_info *wi = (struct walk_stmt_info *) cb_data;
469 struct lower_emutls_data *d = (struct lower_emutls_data *) wi->info;
470 tree t = *ptr;
471 bool is_addr = false;
472 tree addr;
474 *walk_subtrees = 0;
476 switch (TREE_CODE (t))
478 case ADDR_EXPR:
479 /* If this is not a straight-forward "&var", but rather something
480 like "&var.a", then we may need special handling. */
481 if (TREE_CODE (TREE_OPERAND (t, 0)) != VAR_DECL)
483 bool save_changed;
485 /* If we're allowed more than just is_gimple_val, continue. */
486 if (!wi->val_only)
488 *walk_subtrees = 1;
489 return NULL_TREE;
492 /* See if any substitution would be made. */
493 save_changed = wi->changed;
494 wi->changed = false;
495 wi->val_only = false;
496 walk_tree (&TREE_OPERAND (t, 0), lower_emutls_1, wi, NULL);
497 wi->val_only = true;
499 /* If so, then extract this entire sub-expression "&p->a" into a
500 new assignment statement, and substitute yet another SSA_NAME. */
501 if (wi->changed)
503 gimple x;
505 addr = create_tmp_var (TREE_TYPE (t), NULL);
506 x = gimple_build_assign (addr, t);
507 gimple_set_location (x, d->loc);
509 addr = make_ssa_name (addr, x);
510 gimple_assign_set_lhs (x, addr);
512 gimple_seq_add_stmt (&d->seq, x);
514 *ptr = addr;
516 else
517 wi->changed = save_changed;
519 return NULL_TREE;
522 t = TREE_OPERAND (t, 0);
523 is_addr = true;
524 /* FALLTHRU */
526 case VAR_DECL:
527 if (!DECL_THREAD_LOCAL_P (t))
528 return NULL_TREE;
529 break;
531 default:
532 /* We're not interested in other decls or types, only subexpressions. */
533 if (EXPR_P (t))
534 *walk_subtrees = 1;
535 /* FALLTHRU */
537 case SSA_NAME:
538 /* Special-case the return of SSA_NAME, since it's so common. */
539 return NULL_TREE;
542 addr = gen_emutls_addr (t, d);
543 if (is_addr)
545 /* Replace "&var" with "addr" in the statement. */
546 *ptr = addr;
548 else
550 /* Replace "var" with "*addr" in the statement. */
551 t = build2 (MEM_REF, TREE_TYPE (t), addr,
552 build_int_cst (TREE_TYPE (addr), 0));
553 *ptr = t;
556 wi->changed = true;
557 return NULL_TREE;
560 /* Lower all of the operands of STMT. */
562 static void
563 lower_emutls_stmt (gimple stmt, struct lower_emutls_data *d)
565 struct walk_stmt_info wi;
567 d->loc = gimple_location (stmt);
569 memset (&wi, 0, sizeof (wi));
570 wi.info = d;
571 wi.val_only = true;
572 walk_gimple_op (stmt, lower_emutls_1, &wi);
574 if (wi.changed)
575 update_stmt (stmt);
578 /* Lower the I'th operand of PHI. */
580 static void
581 lower_emutls_phi_arg (gimple phi, unsigned int i, struct lower_emutls_data *d)
583 struct walk_stmt_info wi;
584 struct phi_arg_d *pd = gimple_phi_arg (phi, i);
586 /* Early out for a very common case we don't care about. */
587 if (TREE_CODE (pd->def) == SSA_NAME)
588 return;
590 d->loc = pd->locus;
592 memset (&wi, 0, sizeof (wi));
593 wi.info = d;
594 wi.val_only = true;
595 walk_tree (&pd->def, lower_emutls_1, &wi, NULL);
597 /* For normal statements, we let update_stmt do its job. But for phi
598 nodes, we have to manipulate the immediate use list by hand. */
599 if (wi.changed)
601 gcc_assert (TREE_CODE (pd->def) == SSA_NAME);
602 link_imm_use_stmt (&pd->imm_use, pd->def, phi);
606 /* Clear the ACCESS_VARS array, in order to begin a new block. */
608 static inline void
609 clear_access_vars (void)
611 memset (access_vars.address (), 0,
612 access_vars.length () * sizeof (tree));
615 /* Lower the entire function NODE. */
617 static void
618 lower_emutls_function_body (struct cgraph_node *node)
620 struct lower_emutls_data d;
621 bool any_edge_inserts = false;
623 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
625 d.cfun_node = node;
626 d.builtin_decl = builtin_decl_explicit (BUILT_IN_EMUTLS_GET_ADDRESS);
627 /* This is where we introduce the declaration to the IL and so we have to
628 create a node for it. */
629 d.builtin_node = cgraph_get_create_node (d.builtin_decl);
631 FOR_EACH_BB (d.bb)
633 gimple_stmt_iterator gsi;
634 unsigned int i, nedge;
636 /* Lower each of the PHI nodes of the block, as we may have
637 propagated &tlsvar into a PHI argument. These loops are
638 arranged so that we process each edge at once, and each
639 PHI argument for that edge. */
640 if (!gimple_seq_empty_p (phi_nodes (d.bb)))
642 /* The calls will be inserted on the edges, and the frequencies
643 will be computed during the commit process. */
644 d.bb_freq = 0;
646 nedge = EDGE_COUNT (d.bb->preds);
647 for (i = 0; i < nedge; ++i)
649 edge e = EDGE_PRED (d.bb, i);
651 /* We can re-use any SSA_NAME created on this edge. */
652 clear_access_vars ();
653 d.seq = NULL;
655 for (gsi = gsi_start_phis (d.bb);
656 !gsi_end_p (gsi);
657 gsi_next (&gsi))
658 lower_emutls_phi_arg (gsi_stmt (gsi), i, &d);
660 /* Insert all statements generated by all phi nodes for this
661 particular edge all at once. */
662 if (d.seq)
664 gsi_insert_seq_on_edge (e, d.seq);
665 any_edge_inserts = true;
670 d.bb_freq = compute_call_stmt_bb_frequency (current_function_decl, d.bb);
672 /* We can re-use any SSA_NAME created during this basic block. */
673 clear_access_vars ();
675 /* Lower each of the statements of the block. */
676 for (gsi = gsi_start_bb (d.bb); !gsi_end_p (gsi); gsi_next (&gsi))
678 d.seq = NULL;
679 lower_emutls_stmt (gsi_stmt (gsi), &d);
681 /* If any new statements were created, insert them immediately
682 before the first use. This prevents variable lifetimes from
683 becoming unnecessarily long. */
684 if (d.seq)
685 gsi_insert_seq_before (&gsi, d.seq, GSI_SAME_STMT);
689 if (any_edge_inserts)
690 gsi_commit_edge_inserts ();
692 pop_cfun ();
695 /* Create emutls variable for VAR, DATA is pointer to static
696 ctor body we can add constructors to.
697 Callback for varpool_for_variable_and_aliases. */
699 static bool
700 create_emultls_var (struct varpool_node *var, void *data)
702 tree cdecl;
703 struct varpool_node *cvar;
705 cdecl = new_emutls_decl (var->decl,
706 var->alias && var->analyzed
707 ? varpool_alias_target (var)->decl : NULL);
709 cvar = varpool_get_node (cdecl);
710 control_vars.quick_push (cvar);
712 if (!var->alias)
714 /* Make sure the COMMON block control variable gets initialized.
715 Note that there's no point in doing this for aliases; we only
716 need to do this once for the main variable. */
717 emutls_common_1 (var->decl, cdecl, (tree *)data);
719 if (var->alias && !var->analyzed)
720 cvar->alias = true;
722 /* Indicate that the value of the TLS variable may be found elsewhere,
723 preventing the variable from re-appearing in the GIMPLE. We cheat
724 and use the control variable here (rather than a full call_expr),
725 which is special-cased inside the DWARF2 output routines. */
726 SET_DECL_VALUE_EXPR (var->decl, cdecl);
727 DECL_HAS_VALUE_EXPR_P (var->decl) = 1;
728 return false;
731 /* Main entry point to the tls lowering pass. */
733 static unsigned int
734 ipa_lower_emutls (void)
736 struct varpool_node *var;
737 struct cgraph_node *func;
738 bool any_aliases = false;
739 tree ctor_body = NULL;
740 unsigned int i, n_tls;
742 tls_vars = varpool_node_set_new ();
744 /* Examine all global variables for TLS variables. */
745 FOR_EACH_VARIABLE (var)
746 if (DECL_THREAD_LOCAL_P (var->decl))
748 gcc_checking_assert (TREE_STATIC (var->decl)
749 || DECL_EXTERNAL (var->decl));
750 varpool_node_set_add (tls_vars, var);
751 if (var->alias && var->definition)
752 varpool_node_set_add (tls_vars, varpool_variable_node (var, NULL));
755 /* If we found no TLS variables, then there is no further work to do. */
756 if (!tls_vars->nodes.exists ())
758 tls_vars = NULL;
759 if (dump_file)
760 fprintf (dump_file, "No TLS variables found.\n");
761 return 0;
764 /* Allocate the on-the-side arrays that share indicies with the TLS vars. */
765 n_tls = tls_vars->nodes.length ();
766 control_vars.create (n_tls);
767 access_vars.create (n_tls);
768 access_vars.safe_grow_cleared (n_tls);
770 /* Create the control variables for each TLS variable. */
771 FOR_EACH_VEC_ELT (tls_vars->nodes, i, var)
773 var = tls_vars->nodes[i];
775 if (var->alias && !var->analyzed)
776 any_aliases = true;
777 else if (!var->alias)
778 varpool_for_node_and_aliases (var, create_emultls_var, &ctor_body, true);
781 /* If there were any aliases, then frob the alias_pairs vector. */
782 if (any_aliases)
784 alias_pair *p;
785 FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
786 if (DECL_THREAD_LOCAL_P (p->decl))
788 p->decl = emutls_decl (p->decl);
789 p->target = get_emutls_object_name (p->target);
793 /* Adjust all uses of TLS variables within the function bodies. */
794 FOR_EACH_DEFINED_FUNCTION (func)
795 if (func->lowered)
796 lower_emutls_function_body (func);
798 /* Generate the constructor for any COMMON control variables created. */
799 if (ctor_body)
800 cgraph_build_static_cdtor ('I', ctor_body, DEFAULT_INIT_PRIORITY);
802 control_vars.release ();
803 access_vars.release ();
804 free_varpool_node_set (tls_vars);
806 return TODO_verify_all;
809 /* If the target supports TLS natively, we need do nothing here. */
811 static bool
812 gate_emutls (void)
814 return !targetm.have_tls;
817 namespace {
819 const pass_data pass_data_ipa_lower_emutls =
821 SIMPLE_IPA_PASS, /* type */
822 "emutls", /* name */
823 OPTGROUP_NONE, /* optinfo_flags */
824 true, /* has_gate */
825 true, /* has_execute */
826 TV_IPA_OPT, /* tv_id */
827 ( PROP_cfg | PROP_ssa ), /* properties_required */
828 0, /* properties_provided */
829 0, /* properties_destroyed */
830 0, /* todo_flags_start */
831 0, /* todo_flags_finish */
834 class pass_ipa_lower_emutls : public simple_ipa_opt_pass
836 public:
837 pass_ipa_lower_emutls (gcc::context *ctxt)
838 : simple_ipa_opt_pass (pass_data_ipa_lower_emutls, ctxt)
841 /* opt_pass methods: */
842 bool gate () { return gate_emutls (); }
843 unsigned int execute () { return ipa_lower_emutls (); }
845 }; // class pass_ipa_lower_emutls
847 } // anon namespace
849 simple_ipa_opt_pass *
850 make_pass_ipa_lower_emutls (gcc::context *ctxt)
852 return new pass_ipa_lower_emutls (ctxt);