1/* Lower TLS operations to emulation functions.
2 Copyright (C) 2006-2017 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
8Free Software Foundation; either version 3, or (at your option) any
9later version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT
12ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
19
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"
37
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.
42
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.
48
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".
53
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. */
60
61/* Value for TLS varpool node where a pointer to control variable and
62 access variable are stored. */
63struct tls_var_data
64{
65 varpool_node *control_var;
66 tree access;
67};
68
69/* TLS map accesses mapping between a TLS varpool node and a pair
70 made by control variable and access variable. */
71static hash_map<varpool_node *, tls_var_data> *tls_map = NULL;
72
73/* The type of the control structure, shared with the emutls.c runtime. */
74static tree emutls_object_type;
75
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
83
84/* Create an IDENTIFIER_NODE by prefixing PREFIX to the
85 IDENTIFIER_NODE NAME's name. */
86
87static tree
88prefix_name (const char *prefix, tree name)
89{
90 unsigned plen = strlen (prefix);
91 unsigned nlen = strlen (IDENTIFIER_POINTER (name));
92 char *toname = (char *) alloca (plen + nlen + 1);
93
94 memcpy (toname, prefix, plen);
95 memcpy (toname + plen, IDENTIFIER_POINTER (name), nlen + 1);
96
97 return get_identifier (toname);
98}
99
100/* Create an identifier for the struct __emutls_object, given an identifier
101 of the DECL_ASSEMBLY_NAME of the original object. */
102
103static tree
104get_emutls_object_name (tree name)
105{
106 const char *prefix = (targetm.emutls.var_prefix
107 ? targetm.emutls.var_prefix
108 : "__emutls_v" EMUTLS_SEPARATOR);
109 return prefix_name (prefix, name);
110}
111
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. */
115
116tree
117default_emutls_var_fields (tree type, tree *name ATTRIBUTE_UNUSED)
118{
119 tree word_type_node, field, next_field;
120
121 field = build_decl (UNKNOWN_LOCATION,
122 FIELD_DECL, get_identifier ("__templ"), ptr_type_node);
123 DECL_CONTEXT (field) = type;
124 next_field = field;
125
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;
132
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;
140
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;
145
146 return field;
147}
148
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. */
152
153tree
154default_emutls_var_init (tree to, tree decl, tree proxy)
155{
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);
161
162 elt.index = field;
163 elt.value = fold_convert (TREE_TYPE (field), DECL_SIZE_UNIT (decl));
164 v->quick_push (elt);
165
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);
171
172 field = DECL_CHAIN (field);
173 elt.index = field;
174 elt.value = null_pointer_node;
175 v->quick_push (elt);
176
177 field = DECL_CHAIN (field);
178 elt.index = field;
179 elt.value = proxy;
180 v->quick_push (elt);
181
182 return build_constructor (type, v);
183}
184
185/* Create the structure for struct __emutls_object. This should match the
186 structure at the top of emutls.c, modulo the union there. */
187
188static tree
189get_emutls_object_type (void)
190{
191 tree type, type_name, field;
192
193 type = emutls_object_type;
194 if (type)
195 return type;
196
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);
207
208 return type;
209}
210
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. */
213
214static tree
215get_emutls_init_templ_addr (tree decl)
216{
217 tree name, to;
218
219 if (targetm.emutls.register_common && !DECL_INITIAL (decl)
220 && !DECL_SECTION_NAME (decl))
221 return null_pointer_node;
222
223 name = DECL_ASSEMBLER_NAME (decl);
224 if (!targetm.emutls.tmpl_prefix || targetm.emutls.tmpl_prefix[0])
225 {
226 const char *prefix = (targetm.emutls.tmpl_prefix
227 ? targetm.emutls.tmpl_prefix
228 : "__emutls_t" EMUTLS_SEPARATOR);
229 name = prefix_name (prefix, name);
230 }
231
232 to = build_decl (DECL_SOURCE_LOCATION (decl),
233 VAR_DECL, name, TREE_TYPE (decl));
234 SET_DECL_ASSEMBLER_NAME (to, DECL_NAME (to));
235
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);
242
243 DECL_WEAK (to) = DECL_WEAK (decl);
244 if (DECL_ONE_ONLY (decl))
245 {
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));
250 }
251 else
252 TREE_STATIC (to) = 1;
253
254 DECL_VISIBILITY_SPECIFIED (to) = DECL_VISIBILITY_SPECIFIED (decl);
255 DECL_INITIAL (to) = DECL_INITIAL (decl);
256 DECL_INITIAL (decl) = NULL;
257
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));
262
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);
270}
271
272/* Create and return the control variable for the TLS variable DECL. */
273
274static tree
275new_emutls_decl (tree decl, tree alias_of)
276{
277 tree name, to;
278
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 ());
283
284 SET_DECL_ASSEMBLER_NAME (to, DECL_NAME (to));
285
286 DECL_ARTIFICIAL (to) = 1;
287 DECL_IGNORED_P (to) = 1;
288 TREE_READONLY (to) = 0;
289 TREE_STATIC (to) = 1;
290
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);
301
302 DECL_ATTRIBUTES (to) = targetm.merge_decl_attributes (decl, to);
303
304 if (DECL_ONE_ONLY (decl))
305 make_decl_one_only (to, DECL_ASSEMBLER_NAME (to));
306
307 set_decl_tls_model (to, TLS_MODEL_EMULATED);
308
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;
313
314 /* If the target wants the control variables grouped, do so. */
315 if (!DECL_COMMON (to) && targetm.emutls.var_section)
316 {
317 set_decl_section_name (to, targetm.emutls.var_section);
318 }
319
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)))
327 {
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);
331 }
332
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
340 {
341 varpool_node *n;
342 varpool_node *t = varpool_node::get_for_asmname
343 (DECL_ASSEMBLER_NAME (DECL_VALUE_EXPR (alias_of)));
344
345 n = varpool_node::create_alias (to, t->decl);
346 n->resolve_alias (t);
347 }
348 return to;
349}
350
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. */
355
356static void
357emutls_common_1 (tree tls_decl, tree control_decl, tree *pstmts)
358{
359 tree x;
360 tree word_type_node;
361
362 if (! DECL_COMMON (tls_decl)
363 || (DECL_INITIAL (tls_decl)
364 && DECL_INITIAL (tls_decl) != error_mark_node))
365 return;
366
367 word_type_node = lang_hooks.types.type_for_mode (word_mode, 1);
368
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));
376
377 append_to_statement_list (x, pstmts);
378}
379
380struct lower_emutls_data
381{
382 struct cgraph_node *cfun_node;
383 struct cgraph_node *builtin_node;
384 tree builtin_decl;
385 basic_block bb;
386 location_t loc;
387 gimple_seq seq;
388};
389
390/* Given a TLS variable DECL, return an SSA_NAME holding its address.
391 Append any new computation statements required to D->SEQ. */
392
393static tree
394gen_emutls_addr (tree decl, struct lower_emutls_data *d)
395{
396 /* Compute the address of the TLS variable with help from runtime. */
397 tls_var_data *data = tls_map->get (varpool_node::get (decl));
398 tree addr = data->access;
399
400 if (addr == NULL)
401 {
402 varpool_node *cvar;
403 tree cdecl;
404 gcall *x;
405
406 cvar = data->control_var;
407 cdecl = cvar->decl;
408 TREE_ADDRESSABLE (cdecl) = 1;
409
410 addr = create_tmp_var (build_pointer_type (TREE_TYPE (decl)));
411 x = gimple_build_call (d->builtin_decl, 1, build_fold_addr_expr (cdecl));
412 gimple_set_location (x, d->loc);
413
414 addr = make_ssa_name (addr, x);
415 gimple_call_set_lhs (x, addr);
416
417 gimple_seq_add_stmt (&d->seq, x);
418
419 d->cfun_node->create_edge (d->builtin_node, x, d->bb->count);
420
421 /* We may be adding a new reference to a new variable to the function.
422 This means we have to play with the ipa-reference web. */
423 d->cfun_node->create_reference (cvar, IPA_REF_ADDR, x);
424
425 /* Record this ssa_name for possible use later in the basic block. */
426 data->access = addr;
427 }
428
429 return addr;
430}
431
432/* Callback for walk_gimple_op. D = WI->INFO is a struct lower_emutls_data.
433 Given an operand *PTR within D->STMT, if the operand references a TLS
434 variable, then lower the reference to a call to the runtime. Insert
435 any new statements required into D->SEQ; the caller is responsible for
436 placing those appropriately. */
437
438static tree
439lower_emutls_1 (tree *ptr, int *walk_subtrees, void *cb_data)
440{
441 struct walk_stmt_info *wi = (struct walk_stmt_info *) cb_data;
442 struct lower_emutls_data *d = (struct lower_emutls_data *) wi->info;
443 tree t = *ptr;
444 bool is_addr = false;
445 tree addr;
446
447 *walk_subtrees = 0;
448
449 switch (TREE_CODE (t))
450 {
451 case ADDR_EXPR:
452 /* If this is not a straight-forward "&var", but rather something
453 like "&var.a", then we may need special handling. */
454 if (TREE_CODE (TREE_OPERAND (t, 0)) != VAR_DECL)
455 {
456 bool save_changed;
457
458 /* If we're allowed more than just is_gimple_val, continue. */
459 if (!wi->val_only)
460 {
461 *walk_subtrees = 1;
462 return NULL_TREE;
463 }
464
465 /* See if any substitution would be made. */
466 save_changed = wi->changed;
467 wi->changed = false;
468 wi->val_only = false;
469 walk_tree (&TREE_OPERAND (t, 0), lower_emutls_1, wi, NULL);
470 wi->val_only = true;
471
472 /* If so, then extract this entire sub-expression "&p->a" into a
473 new assignment statement, and substitute yet another SSA_NAME. */
474 if (wi->changed)
475 {
476 gimple *x;
477
478 addr = create_tmp_var (TREE_TYPE (t));
479 x = gimple_build_assign (addr, t);
480 gimple_set_location (x, d->loc);
481
482 addr = make_ssa_name (addr, x);
483 gimple_assign_set_lhs (x, addr);
484
485 gimple_seq_add_stmt (&d->seq, x);
486
487 *ptr = addr;
488 }
489 else
490 wi->changed = save_changed;
491
492 return NULL_TREE;
493 }
494
495 t = TREE_OPERAND (t, 0);
496 is_addr = true;
497 /* FALLTHRU */
498
499 case VAR_DECL:
500 if (!DECL_THREAD_LOCAL_P (t))
501 return NULL_TREE;
502 break;
503
504 default:
505 /* We're not interested in other decls or types, only subexpressions. */
506 if (EXPR_P (t))
507 *walk_subtrees = 1;
508 /* FALLTHRU */
509
510 case SSA_NAME:
511 /* Special-case the return of SSA_NAME, since it's so common. */
512 return NULL_TREE;
513 }
514
515 addr = gen_emutls_addr (t, d);
516 if (is_addr)
517 {
518 /* Replace "&var" with "addr" in the statement. */
519 *ptr = addr;
520 }
521 else
522 {
523 /* Replace "var" with "*addr" in the statement. */
524 t = build2 (MEM_REF, TREE_TYPE (t), addr,
525 build_int_cst (TREE_TYPE (addr), 0));
526 *ptr = t;
527 }
528
529 wi->changed = true;
530 return NULL_TREE;
531}
532
533/* Lower all of the operands of STMT. */
534
535static void
536lower_emutls_stmt (gimple *stmt, struct lower_emutls_data *d)
537{
538 struct walk_stmt_info wi;
539
540 d->loc = gimple_location (stmt);
541
542 memset (&wi, 0, sizeof (wi));
543 wi.info = d;
544 wi.val_only = true;
545 walk_gimple_op (stmt, lower_emutls_1, &wi);
546
547 if (wi.changed)
548 update_stmt (stmt);
549}
550
551/* Lower the I'th operand of PHI. */
552
553static void
554lower_emutls_phi_arg (gphi *phi, unsigned int i,
555 struct lower_emutls_data *d)
556{
557 struct walk_stmt_info wi;
558 struct phi_arg_d *pd = gimple_phi_arg (phi, i);
559
560 /* Early out for a very common case we don't care about. */
561 if (TREE_CODE (pd->def) == SSA_NAME)
562 return;
563
564 d->loc = pd->locus;
565
566 memset (&wi, 0, sizeof (wi));
567 wi.info = d;
568 wi.val_only = true;
569 walk_tree (&pd->def, lower_emutls_1, &wi, NULL);
570
571 /* For normal statements, we let update_stmt do its job. But for phi
572 nodes, we have to manipulate the immediate use list by hand. */
573 if (wi.changed)
574 {
575 gcc_assert (TREE_CODE (pd->def) == SSA_NAME);
576 link_imm_use_stmt (&pd->imm_use, pd->def, phi);
577 }
578}
579
580/* Reset access variable for a given TLS variable data DATA. */
581
582bool
583reset_access (varpool_node * const &, tls_var_data *data, void *)
584{
585 data->access = NULL;
586
587 return true;
588}
589
590/* Clear the access variables, in order to begin a new block. */
591
592static inline void
593clear_access_vars (void)
594{
595 tls_map->traverse<void *, reset_access> (NULL);
596}
597
598/* Lower the entire function NODE. */
599
600static void
601lower_emutls_function_body (struct cgraph_node *node)
602{
603 struct lower_emutls_data d;
604 bool any_edge_inserts = false;
605
606 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
607
608 d.cfun_node = node;
609 d.builtin_decl = builtin_decl_explicit (BUILT_IN_EMUTLS_GET_ADDRESS);
610 /* This is where we introduce the declaration to the IL and so we have to
611 create a node for it. */
612 d.builtin_node = cgraph_node::get_create (d.builtin_decl);
613
614 FOR_EACH_BB_FN (d.bb, cfun)
615 {
616 unsigned int i, nedge;
617
618 /* Lower each of the PHI nodes of the block, as we may have
619 propagated &tlsvar into a PHI argument. These loops are
620 arranged so that we process each edge at once, and each
621 PHI argument for that edge. */
622 if (!gimple_seq_empty_p (phi_nodes (d.bb)))
623 {
624 nedge = EDGE_COUNT (d.bb->preds);
625 for (i = 0; i < nedge; ++i)
626 {
627 edge e = EDGE_PRED (d.bb, i);
628
629 /* We can re-use any SSA_NAME created on this edge. */
630 clear_access_vars ();
631 d.seq = NULL;
632
633 for (gphi_iterator gsi = gsi_start_phis (d.bb);
634 !gsi_end_p (gsi);
635 gsi_next (&gsi))
636 lower_emutls_phi_arg (gsi.phi (), i, &d);
637
638 /* Insert all statements generated by all phi nodes for this
639 particular edge all at once. */
640 if (d.seq)
641 {
642 gsi_insert_seq_on_edge (e, d.seq);
643 any_edge_inserts = true;
644 }
645 }
646 }
647
648 /* We can re-use any SSA_NAME created during this basic block. */
649 clear_access_vars ();
650
651 /* Lower each of the statements of the block. */
652 for (gimple_stmt_iterator gsi = gsi_start_bb (d.bb); !gsi_end_p (gsi);
653 gsi_next (&gsi))
654 {
655 d.seq = NULL;
656 lower_emutls_stmt (gsi_stmt (gsi), &d);
657
658 /* If any new statements were created, insert them immediately
659 before the first use. This prevents variable lifetimes from
660 becoming unnecessarily long. */
661 if (d.seq)
662 gsi_insert_seq_before (&gsi, d.seq, GSI_SAME_STMT);
663 }
664 }
665
666 if (any_edge_inserts)
667 gsi_commit_edge_inserts ();
668
669 pop_cfun ();
670}
671
672/* Create emutls variable for VAR, DATA is pointer to static
673 ctor body we can add constructors to.
674 Callback for varpool_for_variable_and_aliases. */
675
676static bool
677create_emultls_var (varpool_node *var, void *data)
678{
679 tree cdecl;
680 tls_var_data value;
681
682 cdecl = new_emutls_decl (var->decl,
683 var->alias && var->analyzed
684 ? var->get_alias_target ()->decl : NULL);
685
686 varpool_node *cvar = varpool_node::get (cdecl);
687
688 if (!var->alias)
689 {
690 /* Make sure the COMMON block control variable gets initialized.
691 Note that there's no point in doing this for aliases; we only
692 need to do this once for the main variable. */
693 emutls_common_1 (var->decl, cdecl, (tree *)data);
694 }
695 if (var->alias && !var->analyzed)
696 cvar->alias = true;
697
698 /* Indicate that the value of the TLS variable may be found elsewhere,
699 preventing the variable from re-appearing in the GIMPLE. We cheat
700 and use the control variable here (rather than a full call_expr),
701 which is special-cased inside the DWARF2 output routines. */
702 SET_DECL_VALUE_EXPR (var->decl, cdecl);
703 DECL_HAS_VALUE_EXPR_P (var->decl) = 1;
704
705 value.control_var = cvar;
706 tls_map->put (var, value);
707
708 return false;
709}
710
711/* Main entry point to the tls lowering pass. */
712
713static unsigned int
714ipa_lower_emutls (void)
715{
716 varpool_node *var;
717 cgraph_node *func;
718 bool any_aliases = false;
719 tree ctor_body = NULL;
720 hash_set <varpool_node *> visited;
721 auto_vec <varpool_node *> tls_vars;
722
723 /* Examine all global variables for TLS variables. */
724 FOR_EACH_VARIABLE (var)
725 if (DECL_THREAD_LOCAL_P (var->decl)
726 && !visited.add (var))
727 {
728 gcc_checking_assert (TREE_STATIC (var->decl)
729 || DECL_EXTERNAL (var->decl));
730 tls_vars.safe_push (var);
731 if (var->alias && var->definition
732 && !visited.add (var->ultimate_alias_target ()))
733 tls_vars.safe_push (var->ultimate_alias_target ());
734 }
735
736 /* If we found no TLS variables, then there is no further work to do. */
737 if (tls_vars.is_empty ())
738 {
739 if (dump_file)
740 fprintf (dump_file, "No TLS variables found.\n");
741 return 0;
742 }
743
744 tls_map = new hash_map <varpool_node *, tls_var_data> ();
745
746 /* Create the control variables for each TLS variable. */
747 for (unsigned i = 0; i < tls_vars.length (); i++)
748 {
749 var = tls_vars[i];
750
751 if (var->alias && !var->analyzed)
752 any_aliases = true;
753 else if (!var->alias)
754 var->call_for_symbol_and_aliases (create_emultls_var, &ctor_body, true);
755 }
756
757 /* If there were any aliases, then frob the alias_pairs vector. */
758 if (any_aliases)
759 {
760 alias_pair *p;
761 unsigned int i;
762 FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
763 if (DECL_THREAD_LOCAL_P (p->decl))
764 {
765 p->decl = tls_map->get
766 (varpool_node::get (p->decl))->control_var->decl;
767 p->target = get_emutls_object_name (p->target);
768 }
769 }
770
771 /* Adjust all uses of TLS variables within the function bodies. */
772 FOR_EACH_DEFINED_FUNCTION (func)
773 if (func->lowered)
774 lower_emutls_function_body (func);
775
776 /* Generate the constructor for any COMMON control variables created. */
777 if (ctor_body)
778 cgraph_build_static_cdtor ('I', ctor_body, DEFAULT_INIT_PRIORITY);
779
780 delete tls_map;
781
782 return 0;
783}
784
785namespace {
786
787const pass_data pass_data_ipa_lower_emutls =
788{
789 SIMPLE_IPA_PASS, /* type */
790 "emutls", /* name */
791 OPTGROUP_NONE, /* optinfo_flags */
792 TV_IPA_OPT, /* tv_id */
793 ( PROP_cfg | PROP_ssa ), /* properties_required */
794 0, /* properties_provided */
795 0, /* properties_destroyed */
796 0, /* todo_flags_start */
797 0, /* todo_flags_finish */
798};
799
800class pass_ipa_lower_emutls : public simple_ipa_opt_pass
801{
802public:
803 pass_ipa_lower_emutls (gcc::context *ctxt)
804 : simple_ipa_opt_pass (pass_data_ipa_lower_emutls, ctxt)
805 {}
806
807 /* opt_pass methods: */
808 virtual bool gate (function *)
809 {
810 /* If the target supports TLS natively, we need do nothing here. */
811 return !targetm.have_tls;
812 }
813
814 virtual unsigned int execute (function *) { return ipa_lower_emutls (); }
815
816}; // class pass_ipa_lower_emutls
817
818} // anon namespace
819
820simple_ipa_opt_pass *
821make_pass_ipa_lower_emutls (gcc::context *ctxt)
822{
823 return new pass_ipa_lower_emutls (ctxt);
824}
825