Implement TARGET_IRA_CHANGE_PSEUDO_ALLOCNO_CLASS hook.
[official-gcc.git] / gcc / tree-emutls.c
bloba4ceeb368b5d0398a0d94450ea0b2b5901782e46
1 /* Lower TLS operations to emulation functions.
2 Copyright (C) 2006-2015 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 "input.h"
24 #include "alias.h"
25 #include "symtab.h"
26 #include "options.h"
27 #include "tree.h"
28 #include "fold-const.h"
29 #include "stor-layout.h"
30 #include "varasm.h"
31 #include "predict.h"
32 #include "tm.h"
33 #include "hard-reg-set.h"
34 #include "input.h"
35 #include "function.h"
36 #include "dominance.h"
37 #include "cfg.h"
38 #include "basic-block.h"
39 #include "tree-ssa-alias.h"
40 #include "internal-fn.h"
41 #include "gimple-expr.h"
42 #include "is-a.h"
43 #include "gimple.h"
44 #include "gimple-iterator.h"
45 #include "gimple-walk.h"
46 #include "tree-pass.h"
47 #include "gimple-ssa.h"
48 #include "plugin-api.h"
49 #include "ipa-ref.h"
50 #include "cgraph.h"
51 #include "tree-phinodes.h"
52 #include "ssa-iterators.h"
53 #include "stringpool.h"
54 #include "tree-ssanames.h"
55 #include "langhooks.h"
56 #include "target.h"
57 #include "targhooks.h"
58 #include "tree-iterator.h"
60 /* Whenever a target does not support thread-local storage (TLS) natively,
61 we can emulate it with some run-time support in libgcc. This will in
62 turn rely on "keyed storage" a-la pthread_key_create; essentially all
63 thread libraries provide such functionality.
65 In order to coordinate with the libgcc runtime, each TLS variable is
66 described by a "control variable". This control variable records the
67 required size, alignment, and initial value of the TLS variable for
68 instantiation at runtime. It also stores an integer token to be used
69 by the runtime to find the address of the variable within each thread.
71 On the compiler side, this means that we need to replace all instances
72 of "tls_var" in the code with "*__emutls_get_addr(&control_var)". We
73 also need to eliminate "tls_var" from the symbol table and introduce
74 "control_var".
76 We used to perform all of the transformations during conversion to rtl,
77 and the variable substitutions magically within assemble_variable.
78 However, this late fiddling of the symbol table conflicts with LTO and
79 whole-program compilation. Therefore we must now make all the changes
80 to the symbol table early in the GIMPLE optimization path, before we
81 write things out to LTO intermediate files. */
83 /* Value for TLS varpool node where a pointer to control variable and
84 access variable are stored. */
85 struct tls_var_data
87 varpool_node *control_var;
88 tree access;
91 /* TLS map accesses mapping between a TLS varpool node and a pair
92 made by control variable and access variable. */
93 static hash_map<varpool_node *, tls_var_data> *tls_map = NULL;
95 /* The type of the control structure, shared with the emutls.c runtime. */
96 static tree emutls_object_type;
98 #if !defined (NO_DOT_IN_LABEL)
99 # define EMUTLS_SEPARATOR "."
100 #elif !defined (NO_DOLLAR_IN_LABEL)
101 # define EMUTLS_SEPARATOR "$"
102 #else
103 # define EMUTLS_SEPARATOR "_"
104 #endif
106 /* Create an IDENTIFIER_NODE by prefixing PREFIX to the
107 IDENTIFIER_NODE NAME's name. */
109 static tree
110 prefix_name (const char *prefix, tree name)
112 unsigned plen = strlen (prefix);
113 unsigned nlen = strlen (IDENTIFIER_POINTER (name));
114 char *toname = (char *) alloca (plen + nlen + 1);
116 memcpy (toname, prefix, plen);
117 memcpy (toname + plen, IDENTIFIER_POINTER (name), nlen + 1);
119 return get_identifier (toname);
122 /* Create an identifier for the struct __emutls_object, given an identifier
123 of the DECL_ASSEMBLY_NAME of the original object. */
125 static tree
126 get_emutls_object_name (tree name)
128 const char *prefix = (targetm.emutls.var_prefix
129 ? targetm.emutls.var_prefix
130 : "__emutls_v" EMUTLS_SEPARATOR);
131 return prefix_name (prefix, name);
134 /* Create the fields of the type for the control variables. Ordinarily
135 this must match struct __emutls_object defined in emutls.c. However
136 this is a target hook so that VxWorks can define its own layout. */
138 tree
139 default_emutls_var_fields (tree type, tree *name ATTRIBUTE_UNUSED)
141 tree word_type_node, field, next_field;
143 field = build_decl (UNKNOWN_LOCATION,
144 FIELD_DECL, get_identifier ("__templ"), ptr_type_node);
145 DECL_CONTEXT (field) = type;
146 next_field = field;
148 field = build_decl (UNKNOWN_LOCATION,
149 FIELD_DECL, get_identifier ("__offset"),
150 ptr_type_node);
151 DECL_CONTEXT (field) = type;
152 DECL_CHAIN (field) = next_field;
153 next_field = field;
155 word_type_node = lang_hooks.types.type_for_mode (word_mode, 1);
156 field = build_decl (UNKNOWN_LOCATION,
157 FIELD_DECL, get_identifier ("__align"),
158 word_type_node);
159 DECL_CONTEXT (field) = type;
160 DECL_CHAIN (field) = next_field;
161 next_field = field;
163 field = build_decl (UNKNOWN_LOCATION,
164 FIELD_DECL, get_identifier ("__size"), word_type_node);
165 DECL_CONTEXT (field) = type;
166 DECL_CHAIN (field) = next_field;
168 return field;
171 /* Initialize emulated tls object TO, which refers to TLS variable DECL and
172 is initialized by PROXY. As above, this is the default implementation of
173 a target hook overridden by VxWorks. */
175 tree
176 default_emutls_var_init (tree to, tree decl, tree proxy)
178 vec<constructor_elt, va_gc> *v;
179 vec_alloc (v, 4);
180 constructor_elt elt;
181 tree type = TREE_TYPE (to);
182 tree field = TYPE_FIELDS (type);
184 elt.index = field;
185 elt.value = fold_convert (TREE_TYPE (field), DECL_SIZE_UNIT (decl));
186 v->quick_push (elt);
188 field = DECL_CHAIN (field);
189 elt.index = field;
190 elt.value = build_int_cst (TREE_TYPE (field),
191 DECL_ALIGN_UNIT (decl));
192 v->quick_push (elt);
194 field = DECL_CHAIN (field);
195 elt.index = field;
196 elt.value = null_pointer_node;
197 v->quick_push (elt);
199 field = DECL_CHAIN (field);
200 elt.index = field;
201 elt.value = proxy;
202 v->quick_push (elt);
204 return build_constructor (type, v);
207 /* Create the structure for struct __emutls_object. This should match the
208 structure at the top of emutls.c, modulo the union there. */
210 static tree
211 get_emutls_object_type (void)
213 tree type, type_name, field;
215 type = emutls_object_type;
216 if (type)
217 return type;
219 emutls_object_type = type = lang_hooks.types.make_type (RECORD_TYPE);
220 type_name = NULL;
221 field = targetm.emutls.var_fields (type, &type_name);
222 if (!type_name)
223 type_name = get_identifier ("__emutls_object");
224 type_name = build_decl (UNKNOWN_LOCATION,
225 TYPE_DECL, type_name, type);
226 TYPE_NAME (type) = type_name;
227 TYPE_FIELDS (type) = field;
228 layout_type (type);
230 return type;
233 /* Create a read-only variable like DECL, with the same DECL_INITIAL.
234 This will be used for initializing the emulated tls data area. */
236 static tree
237 get_emutls_init_templ_addr (tree decl)
239 tree name, to;
241 if (targetm.emutls.register_common && !DECL_INITIAL (decl)
242 && !DECL_SECTION_NAME (decl))
243 return null_pointer_node;
245 name = DECL_ASSEMBLER_NAME (decl);
246 if (!targetm.emutls.tmpl_prefix || targetm.emutls.tmpl_prefix[0])
248 const char *prefix = (targetm.emutls.tmpl_prefix
249 ? targetm.emutls.tmpl_prefix
250 : "__emutls_t" EMUTLS_SEPARATOR);
251 name = prefix_name (prefix, name);
254 to = build_decl (DECL_SOURCE_LOCATION (decl),
255 VAR_DECL, name, TREE_TYPE (decl));
256 SET_DECL_ASSEMBLER_NAME (to, DECL_NAME (to));
258 DECL_ARTIFICIAL (to) = 1;
259 TREE_USED (to) = TREE_USED (decl);
260 TREE_READONLY (to) = 1;
261 DECL_IGNORED_P (to) = 1;
262 DECL_CONTEXT (to) = DECL_CONTEXT (decl);
263 DECL_PRESERVE_P (to) = DECL_PRESERVE_P (decl);
265 DECL_WEAK (to) = DECL_WEAK (decl);
266 if (DECL_ONE_ONLY (decl))
268 TREE_STATIC (to) = TREE_STATIC (decl);
269 TREE_PUBLIC (to) = TREE_PUBLIC (decl);
270 DECL_VISIBILITY (to) = DECL_VISIBILITY (decl);
271 make_decl_one_only (to, DECL_ASSEMBLER_NAME (to));
273 else
274 TREE_STATIC (to) = 1;
276 DECL_VISIBILITY_SPECIFIED (to) = DECL_VISIBILITY_SPECIFIED (decl);
277 DECL_INITIAL (to) = DECL_INITIAL (decl);
278 DECL_INITIAL (decl) = NULL;
280 if (targetm.emutls.tmpl_section)
281 set_decl_section_name (to, targetm.emutls.tmpl_section);
282 else
283 set_decl_section_name (to, DECL_SECTION_NAME (decl));
285 /* Create varpool node for the new variable and finalize it if it is
286 not external one. */
287 if (DECL_EXTERNAL (to))
288 varpool_node::get_create (to);
289 else
290 varpool_node::add (to);
291 return build_fold_addr_expr (to);
294 /* Create and return the control variable for the TLS variable DECL. */
296 static tree
297 new_emutls_decl (tree decl, tree alias_of)
299 tree name, to;
301 name = DECL_ASSEMBLER_NAME (decl);
302 to = build_decl (DECL_SOURCE_LOCATION (decl), VAR_DECL,
303 get_emutls_object_name (name),
304 get_emutls_object_type ());
306 SET_DECL_ASSEMBLER_NAME (to, DECL_NAME (to));
308 DECL_ARTIFICIAL (to) = 1;
309 DECL_IGNORED_P (to) = 1;
310 TREE_READONLY (to) = 0;
311 TREE_STATIC (to) = 1;
313 DECL_PRESERVE_P (to) = DECL_PRESERVE_P (decl);
314 DECL_CONTEXT (to) = DECL_CONTEXT (decl);
315 TREE_USED (to) = TREE_USED (decl);
316 TREE_PUBLIC (to) = TREE_PUBLIC (decl);
317 DECL_EXTERNAL (to) = DECL_EXTERNAL (decl);
318 DECL_COMMON (to) = DECL_COMMON (decl);
319 DECL_WEAK (to) = DECL_WEAK (decl);
320 DECL_VISIBILITY (to) = DECL_VISIBILITY (decl);
321 DECL_VISIBILITY_SPECIFIED (to) = DECL_VISIBILITY_SPECIFIED (decl);
322 DECL_DLLIMPORT_P (to) = DECL_DLLIMPORT_P (decl);
324 DECL_ATTRIBUTES (to) = targetm.merge_decl_attributes (decl, to);
326 if (DECL_ONE_ONLY (decl))
327 make_decl_one_only (to, DECL_ASSEMBLER_NAME (to));
329 set_decl_tls_model (to, TLS_MODEL_EMULATED);
331 /* If we're not allowed to change the proxy object's alignment,
332 pretend it has been set by the user. */
333 if (targetm.emutls.var_align_fixed)
334 DECL_USER_ALIGN (to) = 1;
336 /* If the target wants the control variables grouped, do so. */
337 if (!DECL_COMMON (to) && targetm.emutls.var_section)
339 set_decl_section_name (to, targetm.emutls.var_section);
342 /* If this variable is defined locally, then we need to initialize the
343 control structure with size and alignment information. Initialization
344 of COMMON block variables happens elsewhere via a constructor. */
345 if (!DECL_EXTERNAL (to)
346 && (!DECL_COMMON (to)
347 || (DECL_INITIAL (decl)
348 && DECL_INITIAL (decl) != error_mark_node)))
350 tree tmpl = get_emutls_init_templ_addr (decl);
351 DECL_INITIAL (to) = targetm.emutls.var_init (to, decl, tmpl);
352 record_references_in_initializer (to, false);
355 /* Create varpool node for the new variable and finalize it if it is
356 not external one. */
357 if (DECL_EXTERNAL (to))
358 varpool_node::get_create (to);
359 else if (!alias_of)
360 varpool_node::add (to);
361 else
363 varpool_node *n;
364 varpool_node *t = varpool_node::get_for_asmname
365 (DECL_ASSEMBLER_NAME (DECL_VALUE_EXPR (alias_of)));
367 n = varpool_node::create_alias (to, t->decl);
368 n->resolve_alias (t);
370 return to;
373 /* Generate a call statement to initialize CONTROL_DECL for TLS_DECL.
374 This only needs to happen for TLS COMMON variables; non-COMMON
375 variables can be initialized statically. Insert the generated
376 call statement at the end of PSTMTS. */
378 static void
379 emutls_common_1 (tree tls_decl, tree control_decl, tree *pstmts)
381 tree x;
382 tree word_type_node;
384 if (! DECL_COMMON (tls_decl)
385 || (DECL_INITIAL (tls_decl)
386 && DECL_INITIAL (tls_decl) != error_mark_node))
387 return;
389 word_type_node = lang_hooks.types.type_for_mode (word_mode, 1);
391 x = build_call_expr (builtin_decl_explicit (BUILT_IN_EMUTLS_REGISTER_COMMON),
392 4, build_fold_addr_expr (control_decl),
393 fold_convert (word_type_node,
394 DECL_SIZE_UNIT (tls_decl)),
395 build_int_cst (word_type_node,
396 DECL_ALIGN_UNIT (tls_decl)),
397 get_emutls_init_templ_addr (tls_decl));
399 append_to_statement_list (x, pstmts);
402 struct lower_emutls_data
404 struct cgraph_node *cfun_node;
405 struct cgraph_node *builtin_node;
406 tree builtin_decl;
407 basic_block bb;
408 int bb_freq;
409 location_t loc;
410 gimple_seq seq;
413 /* Given a TLS variable DECL, return an SSA_NAME holding its address.
414 Append any new computation statements required to D->SEQ. */
416 static tree
417 gen_emutls_addr (tree decl, struct lower_emutls_data *d)
419 /* Compute the address of the TLS variable with help from runtime. */
420 tls_var_data *data = tls_map->get (varpool_node::get (decl));
421 tree addr = data->access;
423 if (addr == NULL)
425 varpool_node *cvar;
426 tree cdecl;
427 gcall *x;
429 cvar = data->control_var;
430 cdecl = cvar->decl;
431 TREE_ADDRESSABLE (cdecl) = 1;
433 addr = create_tmp_var (build_pointer_type (TREE_TYPE (decl)));
434 x = gimple_build_call (d->builtin_decl, 1, build_fold_addr_expr (cdecl));
435 gimple_set_location (x, d->loc);
437 addr = make_ssa_name (addr, x);
438 gimple_call_set_lhs (x, addr);
440 gimple_seq_add_stmt (&d->seq, x);
442 d->cfun_node->create_edge (d->builtin_node, x, d->bb->count, d->bb_freq);
444 /* We may be adding a new reference to a new variable to the function.
445 This means we have to play with the ipa-reference web. */
446 d->cfun_node->create_reference (cvar, IPA_REF_ADDR, x);
448 /* Record this ssa_name for possible use later in the basic block. */
449 data->access = addr;
452 return addr;
455 /* Callback for walk_gimple_op. D = WI->INFO is a struct lower_emutls_data.
456 Given an operand *PTR within D->STMT, if the operand references a TLS
457 variable, then lower the reference to a call to the runtime. Insert
458 any new statements required into D->SEQ; the caller is responsible for
459 placing those appropriately. */
461 static tree
462 lower_emutls_1 (tree *ptr, int *walk_subtrees, void *cb_data)
464 struct walk_stmt_info *wi = (struct walk_stmt_info *) cb_data;
465 struct lower_emutls_data *d = (struct lower_emutls_data *) wi->info;
466 tree t = *ptr;
467 bool is_addr = false;
468 tree addr;
470 *walk_subtrees = 0;
472 switch (TREE_CODE (t))
474 case ADDR_EXPR:
475 /* If this is not a straight-forward "&var", but rather something
476 like "&var.a", then we may need special handling. */
477 if (TREE_CODE (TREE_OPERAND (t, 0)) != VAR_DECL)
479 bool save_changed;
481 /* If we're allowed more than just is_gimple_val, continue. */
482 if (!wi->val_only)
484 *walk_subtrees = 1;
485 return NULL_TREE;
488 /* See if any substitution would be made. */
489 save_changed = wi->changed;
490 wi->changed = false;
491 wi->val_only = false;
492 walk_tree (&TREE_OPERAND (t, 0), lower_emutls_1, wi, NULL);
493 wi->val_only = true;
495 /* If so, then extract this entire sub-expression "&p->a" into a
496 new assignment statement, and substitute yet another SSA_NAME. */
497 if (wi->changed)
499 gimple x;
501 addr = create_tmp_var (TREE_TYPE (t));
502 x = gimple_build_assign (addr, t);
503 gimple_set_location (x, d->loc);
505 addr = make_ssa_name (addr, x);
506 gimple_assign_set_lhs (x, addr);
508 gimple_seq_add_stmt (&d->seq, x);
510 *ptr = addr;
512 else
513 wi->changed = save_changed;
515 return NULL_TREE;
518 t = TREE_OPERAND (t, 0);
519 is_addr = true;
520 /* FALLTHRU */
522 case VAR_DECL:
523 if (!DECL_THREAD_LOCAL_P (t))
524 return NULL_TREE;
525 break;
527 default:
528 /* We're not interested in other decls or types, only subexpressions. */
529 if (EXPR_P (t))
530 *walk_subtrees = 1;
531 /* FALLTHRU */
533 case SSA_NAME:
534 /* Special-case the return of SSA_NAME, since it's so common. */
535 return NULL_TREE;
538 addr = gen_emutls_addr (t, d);
539 if (is_addr)
541 /* Replace "&var" with "addr" in the statement. */
542 *ptr = addr;
544 else
546 /* Replace "var" with "*addr" in the statement. */
547 t = build2 (MEM_REF, TREE_TYPE (t), addr,
548 build_int_cst (TREE_TYPE (addr), 0));
549 *ptr = t;
552 wi->changed = true;
553 return NULL_TREE;
556 /* Lower all of the operands of STMT. */
558 static void
559 lower_emutls_stmt (gimple stmt, struct lower_emutls_data *d)
561 struct walk_stmt_info wi;
563 d->loc = gimple_location (stmt);
565 memset (&wi, 0, sizeof (wi));
566 wi.info = d;
567 wi.val_only = true;
568 walk_gimple_op (stmt, lower_emutls_1, &wi);
570 if (wi.changed)
571 update_stmt (stmt);
574 /* Lower the I'th operand of PHI. */
576 static void
577 lower_emutls_phi_arg (gphi *phi, unsigned int i,
578 struct lower_emutls_data *d)
580 struct walk_stmt_info wi;
581 struct phi_arg_d *pd = gimple_phi_arg (phi, i);
583 /* Early out for a very common case we don't care about. */
584 if (TREE_CODE (pd->def) == SSA_NAME)
585 return;
587 d->loc = pd->locus;
589 memset (&wi, 0, sizeof (wi));
590 wi.info = d;
591 wi.val_only = true;
592 walk_tree (&pd->def, lower_emutls_1, &wi, NULL);
594 /* For normal statements, we let update_stmt do its job. But for phi
595 nodes, we have to manipulate the immediate use list by hand. */
596 if (wi.changed)
598 gcc_assert (TREE_CODE (pd->def) == SSA_NAME);
599 link_imm_use_stmt (&pd->imm_use, pd->def, phi);
603 /* Reset access variable for a given TLS variable data DATA. */
605 bool
606 reset_access (varpool_node * const &, tls_var_data *data, void *)
608 data->access = NULL;
610 return true;
613 /* Clear the access variables, in order to begin a new block. */
615 static inline void
616 clear_access_vars (void)
618 tls_map->traverse<void *, reset_access> (NULL);
621 /* Lower the entire function NODE. */
623 static void
624 lower_emutls_function_body (struct cgraph_node *node)
626 struct lower_emutls_data d;
627 bool any_edge_inserts = false;
629 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
631 d.cfun_node = node;
632 d.builtin_decl = builtin_decl_explicit (BUILT_IN_EMUTLS_GET_ADDRESS);
633 /* This is where we introduce the declaration to the IL and so we have to
634 create a node for it. */
635 d.builtin_node = cgraph_node::get_create (d.builtin_decl);
637 FOR_EACH_BB_FN (d.bb, cfun)
639 unsigned int i, nedge;
641 /* Lower each of the PHI nodes of the block, as we may have
642 propagated &tlsvar into a PHI argument. These loops are
643 arranged so that we process each edge at once, and each
644 PHI argument for that edge. */
645 if (!gimple_seq_empty_p (phi_nodes (d.bb)))
647 /* The calls will be inserted on the edges, and the frequencies
648 will be computed during the commit process. */
649 d.bb_freq = 0;
651 nedge = EDGE_COUNT (d.bb->preds);
652 for (i = 0; i < nedge; ++i)
654 edge e = EDGE_PRED (d.bb, i);
656 /* We can re-use any SSA_NAME created on this edge. */
657 clear_access_vars ();
658 d.seq = NULL;
660 for (gphi_iterator gsi = gsi_start_phis (d.bb);
661 !gsi_end_p (gsi);
662 gsi_next (&gsi))
663 lower_emutls_phi_arg (gsi.phi (), i, &d);
665 /* Insert all statements generated by all phi nodes for this
666 particular edge all at once. */
667 if (d.seq)
669 gsi_insert_seq_on_edge (e, d.seq);
670 any_edge_inserts = true;
675 d.bb_freq = compute_call_stmt_bb_frequency (current_function_decl, d.bb);
677 /* We can re-use any SSA_NAME created during this basic block. */
678 clear_access_vars ();
680 /* Lower each of the statements of the block. */
681 for (gimple_stmt_iterator gsi = gsi_start_bb (d.bb); !gsi_end_p (gsi);
682 gsi_next (&gsi))
684 d.seq = NULL;
685 lower_emutls_stmt (gsi_stmt (gsi), &d);
687 /* If any new statements were created, insert them immediately
688 before the first use. This prevents variable lifetimes from
689 becoming unnecessarily long. */
690 if (d.seq)
691 gsi_insert_seq_before (&gsi, d.seq, GSI_SAME_STMT);
695 if (any_edge_inserts)
696 gsi_commit_edge_inserts ();
698 pop_cfun ();
701 /* Create emutls variable for VAR, DATA is pointer to static
702 ctor body we can add constructors to.
703 Callback for varpool_for_variable_and_aliases. */
705 static bool
706 create_emultls_var (varpool_node *var, void *data)
708 tree cdecl;
709 tls_var_data value;
711 cdecl = new_emutls_decl (var->decl,
712 var->alias && var->analyzed
713 ? var->get_alias_target ()->decl : NULL);
715 varpool_node *cvar = varpool_node::get (cdecl);
717 if (!var->alias)
719 /* Make sure the COMMON block control variable gets initialized.
720 Note that there's no point in doing this for aliases; we only
721 need to do this once for the main variable. */
722 emutls_common_1 (var->decl, cdecl, (tree *)data);
724 if (var->alias && !var->analyzed)
725 cvar->alias = true;
727 /* Indicate that the value of the TLS variable may be found elsewhere,
728 preventing the variable from re-appearing in the GIMPLE. We cheat
729 and use the control variable here (rather than a full call_expr),
730 which is special-cased inside the DWARF2 output routines. */
731 SET_DECL_VALUE_EXPR (var->decl, cdecl);
732 DECL_HAS_VALUE_EXPR_P (var->decl) = 1;
734 value.control_var = cvar;
735 tls_map->put (var, value);
737 return false;
740 /* Main entry point to the tls lowering pass. */
742 static unsigned int
743 ipa_lower_emutls (void)
745 varpool_node *var;
746 cgraph_node *func;
747 bool any_aliases = false;
748 tree ctor_body = NULL;
749 hash_set <varpool_node *> visited;
750 auto_vec <varpool_node *> tls_vars;
752 /* Examine all global variables for TLS variables. */
753 FOR_EACH_VARIABLE (var)
754 if (DECL_THREAD_LOCAL_P (var->decl)
755 && !visited.add (var))
757 gcc_checking_assert (TREE_STATIC (var->decl)
758 || DECL_EXTERNAL (var->decl));
759 tls_vars.safe_push (var);
760 if (var->alias && var->definition
761 && !visited.add (var->ultimate_alias_target ()))
762 tls_vars.safe_push (var->ultimate_alias_target ());
765 /* If we found no TLS variables, then there is no further work to do. */
766 if (tls_vars.is_empty ())
768 if (dump_file)
769 fprintf (dump_file, "No TLS variables found.\n");
770 return 0;
773 tls_map = new hash_map <varpool_node *, tls_var_data> ();
775 /* Create the control variables for each TLS variable. */
776 for (unsigned i = 0; i < tls_vars.length (); i++)
778 var = tls_vars[i];
780 if (var->alias && !var->analyzed)
781 any_aliases = true;
782 else if (!var->alias)
783 var->call_for_symbol_and_aliases (create_emultls_var, &ctor_body, true);
786 /* If there were any aliases, then frob the alias_pairs vector. */
787 if (any_aliases)
789 alias_pair *p;
790 unsigned int i;
791 FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
792 if (DECL_THREAD_LOCAL_P (p->decl))
794 p->decl = tls_map->get
795 (varpool_node::get (p->decl))->control_var->decl;
796 p->target = get_emutls_object_name (p->target);
800 /* Adjust all uses of TLS variables within the function bodies. */
801 FOR_EACH_DEFINED_FUNCTION (func)
802 if (func->lowered)
803 lower_emutls_function_body (func);
805 /* Generate the constructor for any COMMON control variables created. */
806 if (ctor_body)
807 cgraph_build_static_cdtor ('I', ctor_body, DEFAULT_INIT_PRIORITY);
809 delete tls_map;
811 return 0;
814 namespace {
816 const pass_data pass_data_ipa_lower_emutls =
818 SIMPLE_IPA_PASS, /* type */
819 "emutls", /* name */
820 OPTGROUP_NONE, /* optinfo_flags */
821 TV_IPA_OPT, /* tv_id */
822 ( PROP_cfg | PROP_ssa ), /* properties_required */
823 0, /* properties_provided */
824 0, /* properties_destroyed */
825 0, /* todo_flags_start */
826 0, /* todo_flags_finish */
829 class pass_ipa_lower_emutls : public simple_ipa_opt_pass
831 public:
832 pass_ipa_lower_emutls (gcc::context *ctxt)
833 : simple_ipa_opt_pass (pass_data_ipa_lower_emutls, ctxt)
836 /* opt_pass methods: */
837 virtual bool gate (function *)
839 /* If the target supports TLS natively, we need do nothing here. */
840 return !targetm.have_tls;
843 virtual unsigned int execute (function *) { 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);