Improve max_insns_skipped logic
[official-gcc.git] / gcc / tree-emutls.c
blob951e7d3f513b285fe4ea27724c1f1fcb2372864e
1 /* Lower TLS operations to emulation functions.
2 Copyright (C) 2006-2017 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 "backend.h"
24 #include "target.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "tree-pass.h"
28 #include "ssa.h"
29 #include "cgraph.h"
30 #include "fold-const.h"
31 #include "stor-layout.h"
32 #include "varasm.h"
33 #include "gimple-iterator.h"
34 #include "gimple-walk.h"
35 #include "langhooks.h"
36 #include "tree-iterator.h"
38 /* Whenever a target does not support thread-local storage (TLS) natively,
39 we can emulate it with some run-time support in libgcc. This will in
40 turn rely on "keyed storage" a-la pthread_key_create; essentially all
41 thread libraries provide such functionality.
43 In order to coordinate with the libgcc runtime, each TLS variable is
44 described by a "control variable". This control variable records the
45 required size, alignment, and initial value of the TLS variable for
46 instantiation at runtime. It also stores an integer token to be used
47 by the runtime to find the address of the variable within each thread.
49 On the compiler side, this means that we need to replace all instances
50 of "tls_var" in the code with "*__emutls_get_addr(&control_var)". We
51 also need to eliminate "tls_var" from the symbol table and introduce
52 "control_var".
54 We used to perform all of the transformations during conversion to rtl,
55 and the variable substitutions magically within assemble_variable.
56 However, this late fiddling of the symbol table conflicts with LTO and
57 whole-program compilation. Therefore we must now make all the changes
58 to the symbol table early in the GIMPLE optimization path, before we
59 write things out to LTO intermediate files. */
61 /* Value for TLS varpool node where a pointer to control variable and
62 access variable are stored. */
63 struct tls_var_data
65 varpool_node *control_var;
66 tree access;
69 /* TLS map accesses mapping between a TLS varpool node and a pair
70 made by control variable and access variable. */
71 static hash_map<varpool_node *, tls_var_data> *tls_map = NULL;
73 /* The type of the control structure, shared with the emutls.c runtime. */
74 static tree emutls_object_type;
76 #if !defined (NO_DOT_IN_LABEL)
77 # define EMUTLS_SEPARATOR "."
78 #elif !defined (NO_DOLLAR_IN_LABEL)
79 # define EMUTLS_SEPARATOR "$"
80 #else
81 # define EMUTLS_SEPARATOR "_"
82 #endif
84 /* Create an IDENTIFIER_NODE by prefixing PREFIX to the
85 IDENTIFIER_NODE NAME's name. */
87 static tree
88 prefix_name (const char *prefix, tree name)
90 unsigned plen = strlen (prefix);
91 unsigned nlen = strlen (IDENTIFIER_POINTER (name));
92 char *toname = (char *) alloca (plen + nlen + 1);
94 memcpy (toname, prefix, plen);
95 memcpy (toname + plen, IDENTIFIER_POINTER (name), nlen + 1);
97 return get_identifier (toname);
100 /* Create an identifier for the struct __emutls_object, given an identifier
101 of the DECL_ASSEMBLY_NAME of the original object. */
103 static tree
104 get_emutls_object_name (tree name)
106 const char *prefix = (targetm.emutls.var_prefix
107 ? targetm.emutls.var_prefix
108 : "__emutls_v" EMUTLS_SEPARATOR);
109 return prefix_name (prefix, name);
112 /* Create the fields of the type for the control variables. Ordinarily
113 this must match struct __emutls_object defined in emutls.c. However
114 this is a target hook so that VxWorks can define its own layout. */
116 tree
117 default_emutls_var_fields (tree type, tree *name ATTRIBUTE_UNUSED)
119 tree word_type_node, field, next_field;
121 field = build_decl (UNKNOWN_LOCATION,
122 FIELD_DECL, get_identifier ("__templ"), ptr_type_node);
123 DECL_CONTEXT (field) = type;
124 next_field = field;
126 field = build_decl (UNKNOWN_LOCATION,
127 FIELD_DECL, get_identifier ("__offset"),
128 ptr_type_node);
129 DECL_CONTEXT (field) = type;
130 DECL_CHAIN (field) = next_field;
131 next_field = field;
133 word_type_node = lang_hooks.types.type_for_mode (word_mode, 1);
134 field = build_decl (UNKNOWN_LOCATION,
135 FIELD_DECL, get_identifier ("__align"),
136 word_type_node);
137 DECL_CONTEXT (field) = type;
138 DECL_CHAIN (field) = next_field;
139 next_field = field;
141 field = build_decl (UNKNOWN_LOCATION,
142 FIELD_DECL, get_identifier ("__size"), word_type_node);
143 DECL_CONTEXT (field) = type;
144 DECL_CHAIN (field) = next_field;
146 return field;
149 /* Initialize emulated tls object TO, which refers to TLS variable DECL and
150 is initialized by PROXY. As above, this is the default implementation of
151 a target hook overridden by VxWorks. */
153 tree
154 default_emutls_var_init (tree to, tree decl, tree proxy)
156 vec<constructor_elt, va_gc> *v;
157 vec_alloc (v, 4);
158 constructor_elt elt;
159 tree type = TREE_TYPE (to);
160 tree field = TYPE_FIELDS (type);
162 elt.index = field;
163 elt.value = fold_convert (TREE_TYPE (field), DECL_SIZE_UNIT (decl));
164 v->quick_push (elt);
166 field = DECL_CHAIN (field);
167 elt.index = field;
168 elt.value = build_int_cst (TREE_TYPE (field),
169 DECL_ALIGN_UNIT (decl));
170 v->quick_push (elt);
172 field = DECL_CHAIN (field);
173 elt.index = field;
174 elt.value = null_pointer_node;
175 v->quick_push (elt);
177 field = DECL_CHAIN (field);
178 elt.index = field;
179 elt.value = proxy;
180 v->quick_push (elt);
182 return build_constructor (type, v);
185 /* Create the structure for struct __emutls_object. This should match the
186 structure at the top of emutls.c, modulo the union there. */
188 static tree
189 get_emutls_object_type (void)
191 tree type, type_name, field;
193 type = emutls_object_type;
194 if (type)
195 return type;
197 emutls_object_type = type = lang_hooks.types.make_type (RECORD_TYPE);
198 type_name = NULL;
199 field = targetm.emutls.var_fields (type, &type_name);
200 if (!type_name)
201 type_name = get_identifier ("__emutls_object");
202 type_name = build_decl (UNKNOWN_LOCATION,
203 TYPE_DECL, type_name, type);
204 TYPE_NAME (type) = type_name;
205 TYPE_FIELDS (type) = field;
206 layout_type (type);
208 return type;
211 /* Create a read-only variable like DECL, with the same DECL_INITIAL.
212 This will be used for initializing the emulated tls data area. */
214 static tree
215 get_emutls_init_templ_addr (tree decl)
217 tree name, to;
219 if (targetm.emutls.register_common && !DECL_INITIAL (decl)
220 && !DECL_SECTION_NAME (decl))
221 return null_pointer_node;
223 name = DECL_ASSEMBLER_NAME (decl);
224 if (!targetm.emutls.tmpl_prefix || targetm.emutls.tmpl_prefix[0])
226 const char *prefix = (targetm.emutls.tmpl_prefix
227 ? targetm.emutls.tmpl_prefix
228 : "__emutls_t" EMUTLS_SEPARATOR);
229 name = prefix_name (prefix, name);
232 to = build_decl (DECL_SOURCE_LOCATION (decl),
233 VAR_DECL, name, TREE_TYPE (decl));
234 SET_DECL_ASSEMBLER_NAME (to, DECL_NAME (to));
236 DECL_ARTIFICIAL (to) = 1;
237 TREE_USED (to) = TREE_USED (decl);
238 TREE_READONLY (to) = 1;
239 DECL_IGNORED_P (to) = 1;
240 DECL_CONTEXT (to) = DECL_CONTEXT (decl);
241 DECL_PRESERVE_P (to) = DECL_PRESERVE_P (decl);
243 DECL_WEAK (to) = DECL_WEAK (decl);
244 if (DECL_ONE_ONLY (decl))
246 TREE_STATIC (to) = TREE_STATIC (decl);
247 TREE_PUBLIC (to) = TREE_PUBLIC (decl);
248 DECL_VISIBILITY (to) = DECL_VISIBILITY (decl);
249 make_decl_one_only (to, DECL_ASSEMBLER_NAME (to));
251 else
252 TREE_STATIC (to) = 1;
254 DECL_VISIBILITY_SPECIFIED (to) = DECL_VISIBILITY_SPECIFIED (decl);
255 DECL_INITIAL (to) = DECL_INITIAL (decl);
256 DECL_INITIAL (decl) = NULL;
258 if (targetm.emutls.tmpl_section)
259 set_decl_section_name (to, targetm.emutls.tmpl_section);
260 else
261 set_decl_section_name (to, DECL_SECTION_NAME (decl));
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::get_create (to);
267 else
268 varpool_node::add (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_ARTIFICIAL (to) = 1;
287 DECL_IGNORED_P (to) = 1;
288 TREE_READONLY (to) = 0;
289 TREE_STATIC (to) = 1;
291 DECL_PRESERVE_P (to) = DECL_PRESERVE_P (decl);
292 DECL_CONTEXT (to) = DECL_CONTEXT (decl);
293 TREE_USED (to) = TREE_USED (decl);
294 TREE_PUBLIC (to) = TREE_PUBLIC (decl);
295 DECL_EXTERNAL (to) = DECL_EXTERNAL (decl);
296 DECL_COMMON (to) = DECL_COMMON (decl);
297 DECL_WEAK (to) = DECL_WEAK (decl);
298 DECL_VISIBILITY (to) = DECL_VISIBILITY (decl);
299 DECL_VISIBILITY_SPECIFIED (to) = DECL_VISIBILITY_SPECIFIED (decl);
300 DECL_DLLIMPORT_P (to) = DECL_DLLIMPORT_P (decl);
302 DECL_ATTRIBUTES (to) = targetm.merge_decl_attributes (decl, to);
304 if (DECL_ONE_ONLY (decl))
305 make_decl_one_only (to, DECL_ASSEMBLER_NAME (to));
307 set_decl_tls_model (to, TLS_MODEL_EMULATED);
309 /* If we're not allowed to change the proxy object's alignment,
310 pretend it has been set by the user. */
311 if (targetm.emutls.var_align_fixed)
312 DECL_USER_ALIGN (to) = 1;
314 /* If the target wants the control variables grouped, do so. */
315 if (!DECL_COMMON (to) && targetm.emutls.var_section)
317 set_decl_section_name (to, targetm.emutls.var_section);
320 /* If this variable is defined locally, then we need to initialize the
321 control structure with size and alignment information. Initialization
322 of COMMON block variables happens elsewhere via a constructor. */
323 if (!DECL_EXTERNAL (to)
324 && (!DECL_COMMON (to)
325 || (DECL_INITIAL (decl)
326 && DECL_INITIAL (decl) != error_mark_node)))
328 tree tmpl = get_emutls_init_templ_addr (decl);
329 DECL_INITIAL (to) = targetm.emutls.var_init (to, decl, tmpl);
330 record_references_in_initializer (to, false);
333 /* Create varpool node for the new variable and finalize it if it is
334 not external one. */
335 if (DECL_EXTERNAL (to))
336 varpool_node::get_create (to);
337 else if (!alias_of)
338 varpool_node::add (to);
339 else
341 varpool_node *n;
342 varpool_node *t = varpool_node::get_for_asmname
343 (DECL_ASSEMBLER_NAME (DECL_VALUE_EXPR (alias_of)));
345 n = varpool_node::create_alias (to, t->decl);
346 n->resolve_alias (t);
348 return to;
351 /* Generate a call statement to initialize CONTROL_DECL for TLS_DECL.
352 This only needs to happen for TLS COMMON variables; non-COMMON
353 variables can be initialized statically. Insert the generated
354 call statement at the end of PSTMTS. */
356 static void
357 emutls_common_1 (tree tls_decl, tree control_decl, tree *pstmts)
359 tree x;
360 tree word_type_node;
362 if (! DECL_COMMON (tls_decl)
363 || (DECL_INITIAL (tls_decl)
364 && DECL_INITIAL (tls_decl) != error_mark_node))
365 return;
367 word_type_node = lang_hooks.types.type_for_mode (word_mode, 1);
369 x = build_call_expr (builtin_decl_explicit (BUILT_IN_EMUTLS_REGISTER_COMMON),
370 4, build_fold_addr_expr (control_decl),
371 fold_convert (word_type_node,
372 DECL_SIZE_UNIT (tls_decl)),
373 build_int_cst (word_type_node,
374 DECL_ALIGN_UNIT (tls_decl)),
375 get_emutls_init_templ_addr (tls_decl));
377 append_to_statement_list (x, pstmts);
380 struct lower_emutls_data
382 struct cgraph_node *cfun_node;
383 struct cgraph_node *builtin_node;
384 tree builtin_decl;
385 basic_block bb;
386 int bb_freq;
387 location_t loc;
388 gimple_seq seq;
391 /* Given a TLS variable DECL, return an SSA_NAME holding its address.
392 Append any new computation statements required to D->SEQ. */
394 static tree
395 gen_emutls_addr (tree decl, struct lower_emutls_data *d)
397 /* Compute the address of the TLS variable with help from runtime. */
398 tls_var_data *data = tls_map->get (varpool_node::get (decl));
399 tree addr = data->access;
401 if (addr == NULL)
403 varpool_node *cvar;
404 tree cdecl;
405 gcall *x;
407 cvar = data->control_var;
408 cdecl = cvar->decl;
409 TREE_ADDRESSABLE (cdecl) = 1;
411 addr = create_tmp_var (build_pointer_type (TREE_TYPE (decl)));
412 x = gimple_build_call (d->builtin_decl, 1, build_fold_addr_expr (cdecl));
413 gimple_set_location (x, d->loc);
415 addr = make_ssa_name (addr, x);
416 gimple_call_set_lhs (x, addr);
418 gimple_seq_add_stmt (&d->seq, x);
420 d->cfun_node->create_edge (d->builtin_node, x, d->bb->count, d->bb_freq);
422 /* We may be adding a new reference to a new variable to the function.
423 This means we have to play with the ipa-reference web. */
424 d->cfun_node->create_reference (cvar, IPA_REF_ADDR, x);
426 /* Record this ssa_name for possible use later in the basic block. */
427 data->access = addr;
430 return addr;
433 /* Callback for walk_gimple_op. D = WI->INFO is a struct lower_emutls_data.
434 Given an operand *PTR within D->STMT, if the operand references a TLS
435 variable, then lower the reference to a call to the runtime. Insert
436 any new statements required into D->SEQ; the caller is responsible for
437 placing those appropriately. */
439 static tree
440 lower_emutls_1 (tree *ptr, int *walk_subtrees, void *cb_data)
442 struct walk_stmt_info *wi = (struct walk_stmt_info *) cb_data;
443 struct lower_emutls_data *d = (struct lower_emutls_data *) wi->info;
444 tree t = *ptr;
445 bool is_addr = false;
446 tree addr;
448 *walk_subtrees = 0;
450 switch (TREE_CODE (t))
452 case ADDR_EXPR:
453 /* If this is not a straight-forward "&var", but rather something
454 like "&var.a", then we may need special handling. */
455 if (TREE_CODE (TREE_OPERAND (t, 0)) != VAR_DECL)
457 bool save_changed;
459 /* If we're allowed more than just is_gimple_val, continue. */
460 if (!wi->val_only)
462 *walk_subtrees = 1;
463 return NULL_TREE;
466 /* See if any substitution would be made. */
467 save_changed = wi->changed;
468 wi->changed = false;
469 wi->val_only = false;
470 walk_tree (&TREE_OPERAND (t, 0), lower_emutls_1, wi, NULL);
471 wi->val_only = true;
473 /* If so, then extract this entire sub-expression "&p->a" into a
474 new assignment statement, and substitute yet another SSA_NAME. */
475 if (wi->changed)
477 gimple *x;
479 addr = create_tmp_var (TREE_TYPE (t));
480 x = gimple_build_assign (addr, t);
481 gimple_set_location (x, d->loc);
483 addr = make_ssa_name (addr, x);
484 gimple_assign_set_lhs (x, addr);
486 gimple_seq_add_stmt (&d->seq, x);
488 *ptr = addr;
490 else
491 wi->changed = save_changed;
493 return NULL_TREE;
496 t = TREE_OPERAND (t, 0);
497 is_addr = true;
498 /* FALLTHRU */
500 case VAR_DECL:
501 if (!DECL_THREAD_LOCAL_P (t))
502 return NULL_TREE;
503 break;
505 default:
506 /* We're not interested in other decls or types, only subexpressions. */
507 if (EXPR_P (t))
508 *walk_subtrees = 1;
509 /* FALLTHRU */
511 case SSA_NAME:
512 /* Special-case the return of SSA_NAME, since it's so common. */
513 return NULL_TREE;
516 addr = gen_emutls_addr (t, d);
517 if (is_addr)
519 /* Replace "&var" with "addr" in the statement. */
520 *ptr = addr;
522 else
524 /* Replace "var" with "*addr" in the statement. */
525 t = build2 (MEM_REF, TREE_TYPE (t), addr,
526 build_int_cst (TREE_TYPE (addr), 0));
527 *ptr = t;
530 wi->changed = true;
531 return NULL_TREE;
534 /* Lower all of the operands of STMT. */
536 static void
537 lower_emutls_stmt (gimple *stmt, struct lower_emutls_data *d)
539 struct walk_stmt_info wi;
541 d->loc = gimple_location (stmt);
543 memset (&wi, 0, sizeof (wi));
544 wi.info = d;
545 wi.val_only = true;
546 walk_gimple_op (stmt, lower_emutls_1, &wi);
548 if (wi.changed)
549 update_stmt (stmt);
552 /* Lower the I'th operand of PHI. */
554 static void
555 lower_emutls_phi_arg (gphi *phi, unsigned int i,
556 struct lower_emutls_data *d)
558 struct walk_stmt_info wi;
559 struct phi_arg_d *pd = gimple_phi_arg (phi, i);
561 /* Early out for a very common case we don't care about. */
562 if (TREE_CODE (pd->def) == SSA_NAME)
563 return;
565 d->loc = pd->locus;
567 memset (&wi, 0, sizeof (wi));
568 wi.info = d;
569 wi.val_only = true;
570 walk_tree (&pd->def, lower_emutls_1, &wi, NULL);
572 /* For normal statements, we let update_stmt do its job. But for phi
573 nodes, we have to manipulate the immediate use list by hand. */
574 if (wi.changed)
576 gcc_assert (TREE_CODE (pd->def) == SSA_NAME);
577 link_imm_use_stmt (&pd->imm_use, pd->def, phi);
581 /* Reset access variable for a given TLS variable data DATA. */
583 bool
584 reset_access (varpool_node * const &, tls_var_data *data, void *)
586 data->access = NULL;
588 return true;
591 /* Clear the access variables, in order to begin a new block. */
593 static inline void
594 clear_access_vars (void)
596 tls_map->traverse<void *, reset_access> (NULL);
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 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
609 d.cfun_node = node;
610 d.builtin_decl = builtin_decl_explicit (BUILT_IN_EMUTLS_GET_ADDRESS);
611 /* This is where we introduce the declaration to the IL and so we have to
612 create a node for it. */
613 d.builtin_node = cgraph_node::get_create (d.builtin_decl);
615 FOR_EACH_BB_FN (d.bb, cfun)
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 (gphi_iterator gsi = gsi_start_phis (d.bb);
639 !gsi_end_p (gsi);
640 gsi_next (&gsi))
641 lower_emutls_phi_arg (gsi.phi (), 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 (gimple_stmt_iterator gsi = gsi_start_bb (d.bb); !gsi_end_p (gsi);
660 gsi_next (&gsi))
662 d.seq = NULL;
663 lower_emutls_stmt (gsi_stmt (gsi), &d);
665 /* If any new statements were created, insert them immediately
666 before the first use. This prevents variable lifetimes from
667 becoming unnecessarily long. */
668 if (d.seq)
669 gsi_insert_seq_before (&gsi, d.seq, GSI_SAME_STMT);
673 if (any_edge_inserts)
674 gsi_commit_edge_inserts ();
676 pop_cfun ();
679 /* Create emutls variable for VAR, DATA is pointer to static
680 ctor body we can add constructors to.
681 Callback for varpool_for_variable_and_aliases. */
683 static bool
684 create_emultls_var (varpool_node *var, void *data)
686 tree cdecl;
687 tls_var_data value;
689 cdecl = new_emutls_decl (var->decl,
690 var->alias && var->analyzed
691 ? var->get_alias_target ()->decl : NULL);
693 varpool_node *cvar = varpool_node::get (cdecl);
695 if (!var->alias)
697 /* Make sure the COMMON block control variable gets initialized.
698 Note that there's no point in doing this for aliases; we only
699 need to do this once for the main variable. */
700 emutls_common_1 (var->decl, cdecl, (tree *)data);
702 if (var->alias && !var->analyzed)
703 cvar->alias = true;
705 /* Indicate that the value of the TLS variable may be found elsewhere,
706 preventing the variable from re-appearing in the GIMPLE. We cheat
707 and use the control variable here (rather than a full call_expr),
708 which is special-cased inside the DWARF2 output routines. */
709 SET_DECL_VALUE_EXPR (var->decl, cdecl);
710 DECL_HAS_VALUE_EXPR_P (var->decl) = 1;
712 value.control_var = cvar;
713 tls_map->put (var, value);
715 return false;
718 /* Main entry point to the tls lowering pass. */
720 static unsigned int
721 ipa_lower_emutls (void)
723 varpool_node *var;
724 cgraph_node *func;
725 bool any_aliases = false;
726 tree ctor_body = NULL;
727 hash_set <varpool_node *> visited;
728 auto_vec <varpool_node *> tls_vars;
730 /* Examine all global variables for TLS variables. */
731 FOR_EACH_VARIABLE (var)
732 if (DECL_THREAD_LOCAL_P (var->decl)
733 && !visited.add (var))
735 gcc_checking_assert (TREE_STATIC (var->decl)
736 || DECL_EXTERNAL (var->decl));
737 tls_vars.safe_push (var);
738 if (var->alias && var->definition
739 && !visited.add (var->ultimate_alias_target ()))
740 tls_vars.safe_push (var->ultimate_alias_target ());
743 /* If we found no TLS variables, then there is no further work to do. */
744 if (tls_vars.is_empty ())
746 if (dump_file)
747 fprintf (dump_file, "No TLS variables found.\n");
748 return 0;
751 tls_map = new hash_map <varpool_node *, tls_var_data> ();
753 /* Create the control variables for each TLS variable. */
754 for (unsigned i = 0; i < tls_vars.length (); i++)
756 var = tls_vars[i];
758 if (var->alias && !var->analyzed)
759 any_aliases = true;
760 else if (!var->alias)
761 var->call_for_symbol_and_aliases (create_emultls_var, &ctor_body, true);
764 /* If there were any aliases, then frob the alias_pairs vector. */
765 if (any_aliases)
767 alias_pair *p;
768 unsigned int i;
769 FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
770 if (DECL_THREAD_LOCAL_P (p->decl))
772 p->decl = tls_map->get
773 (varpool_node::get (p->decl))->control_var->decl;
774 p->target = get_emutls_object_name (p->target);
778 /* Adjust all uses of TLS variables within the function bodies. */
779 FOR_EACH_DEFINED_FUNCTION (func)
780 if (func->lowered)
781 lower_emutls_function_body (func);
783 /* Generate the constructor for any COMMON control variables created. */
784 if (ctor_body)
785 cgraph_build_static_cdtor ('I', ctor_body, DEFAULT_INIT_PRIORITY);
787 delete tls_map;
789 return 0;
792 namespace {
794 const pass_data pass_data_ipa_lower_emutls =
796 SIMPLE_IPA_PASS, /* type */
797 "emutls", /* name */
798 OPTGROUP_NONE, /* optinfo_flags */
799 TV_IPA_OPT, /* tv_id */
800 ( PROP_cfg | PROP_ssa ), /* properties_required */
801 0, /* properties_provided */
802 0, /* properties_destroyed */
803 0, /* todo_flags_start */
804 0, /* todo_flags_finish */
807 class pass_ipa_lower_emutls : public simple_ipa_opt_pass
809 public:
810 pass_ipa_lower_emutls (gcc::context *ctxt)
811 : simple_ipa_opt_pass (pass_data_ipa_lower_emutls, ctxt)
814 /* opt_pass methods: */
815 virtual bool gate (function *)
817 /* If the target supports TLS natively, we need do nothing here. */
818 return !targetm.have_tls;
821 virtual unsigned int execute (function *) { return ipa_lower_emutls (); }
823 }; // class pass_ipa_lower_emutls
825 } // anon namespace
827 simple_ipa_opt_pass *
828 make_pass_ipa_lower_emutls (gcc::context *ctxt)
830 return new pass_ipa_lower_emutls (ctxt);