* gimple-walk.h: New File. Relocate prototypes from gimple.h.
[official-gcc.git] / gcc / tree-emutls.c
blob11337c0c1273b389a4a7b583c8c2d44a41a43feb
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 "gimple-iterator.h"
26 #include "gimple-walk.h"
27 #include "tree-pass.h"
28 #include "gimple-ssa.h"
29 #include "cgraph.h"
30 #include "tree-phinodes.h"
31 #include "ssa-iterators.h"
32 #include "tree-ssanames.h"
33 #include "langhooks.h"
34 #include "target.h"
35 #include "targhooks.h"
36 #include "tree-iterator.h"
39 /* Whenever a target does not support thread-local storage (TLS) natively,
40 we can emulate it with some run-time support in libgcc. This will in
41 turn rely on "keyed storage" a-la pthread_key_create; essentially all
42 thread libraries provide such functionality.
44 In order to coordinate with the libgcc runtime, each TLS variable is
45 described by a "control variable". This control variable records the
46 required size, alignment, and initial value of the TLS variable for
47 instantiation at runtime. It also stores an integer token to be used
48 by the runtime to find the address of the variable within each thread.
50 On the compiler side, this means that we need to replace all instances
51 of "tls_var" in the code with "*__emutls_get_addr(&control_var)". We
52 also need to eliminate "tls_var" from the symbol table and introduce
53 "control_var".
55 We used to perform all of the transformations during conversion to rtl,
56 and the variable substitutions magically within assemble_variable.
57 However, this late fiddling of the symbol table conflicts with LTO and
58 whole-program compilation. Therefore we must now make all the changes
59 to the symbol table early in the GIMPLE optimization path, before we
60 write things out to LTO intermediate files. */
62 /* These two vectors, once fully populated, are kept in lock-step so that
63 the index of a TLS variable equals the index of its control variable in
64 the other vector. */
65 static varpool_node_set tls_vars;
66 static vec<varpool_node_ptr> control_vars;
68 /* For the current basic block, an SSA_NAME that has computed the address
69 of the TLS variable at the corresponding index. */
70 static vec<tree> access_vars;
72 /* The type of the control structure, shared with the emutls.c runtime. */
73 static tree emutls_object_type;
75 #if !defined (NO_DOT_IN_LABEL)
76 # define EMUTLS_SEPARATOR "."
77 #elif !defined (NO_DOLLAR_IN_LABEL)
78 # define EMUTLS_SEPARATOR "$"
79 #else
80 # define EMUTLS_SEPARATOR "_"
81 #endif
83 /* Create an IDENTIFIER_NODE by prefixing PREFIX to the
84 IDENTIFIER_NODE NAME's name. */
86 static tree
87 prefix_name (const char *prefix, tree name)
89 unsigned plen = strlen (prefix);
90 unsigned nlen = strlen (IDENTIFIER_POINTER (name));
91 char *toname = (char *) alloca (plen + nlen + 1);
93 memcpy (toname, prefix, plen);
94 memcpy (toname + plen, IDENTIFIER_POINTER (name), nlen + 1);
96 return get_identifier (toname);
99 /* Create an identifier for the struct __emutls_object, given an identifier
100 of the DECL_ASSEMBLY_NAME of the original object. */
102 static tree
103 get_emutls_object_name (tree name)
105 const char *prefix = (targetm.emutls.var_prefix
106 ? targetm.emutls.var_prefix
107 : "__emutls_v" EMUTLS_SEPARATOR);
108 return prefix_name (prefix, name);
111 /* Create the fields of the type for the control variables. Ordinarily
112 this must match struct __emutls_object defined in emutls.c. However
113 this is a target hook so that VxWorks can define its own layout. */
115 tree
116 default_emutls_var_fields (tree type, tree *name ATTRIBUTE_UNUSED)
118 tree word_type_node, field, next_field;
120 field = build_decl (UNKNOWN_LOCATION,
121 FIELD_DECL, get_identifier ("__templ"), ptr_type_node);
122 DECL_CONTEXT (field) = type;
123 next_field = field;
125 field = build_decl (UNKNOWN_LOCATION,
126 FIELD_DECL, get_identifier ("__offset"),
127 ptr_type_node);
128 DECL_CONTEXT (field) = type;
129 DECL_CHAIN (field) = next_field;
130 next_field = field;
132 word_type_node = lang_hooks.types.type_for_mode (word_mode, 1);
133 field = build_decl (UNKNOWN_LOCATION,
134 FIELD_DECL, get_identifier ("__align"),
135 word_type_node);
136 DECL_CONTEXT (field) = type;
137 DECL_CHAIN (field) = next_field;
138 next_field = field;
140 field = build_decl (UNKNOWN_LOCATION,
141 FIELD_DECL, get_identifier ("__size"), word_type_node);
142 DECL_CONTEXT (field) = type;
143 DECL_CHAIN (field) = next_field;
145 return field;
148 /* Initialize emulated tls object TO, which refers to TLS variable DECL and
149 is initialized by PROXY. As above, this is the default implementation of
150 a target hook overridden by VxWorks. */
152 tree
153 default_emutls_var_init (tree to, tree decl, tree proxy)
155 vec<constructor_elt, va_gc> *v;
156 vec_alloc (v, 4);
157 constructor_elt elt;
158 tree type = TREE_TYPE (to);
159 tree field = TYPE_FIELDS (type);
161 elt.index = field;
162 elt.value = fold_convert (TREE_TYPE (field), DECL_SIZE_UNIT (decl));
163 v->quick_push (elt);
165 field = DECL_CHAIN (field);
166 elt.index = field;
167 elt.value = build_int_cst (TREE_TYPE (field),
168 DECL_ALIGN_UNIT (decl));
169 v->quick_push (elt);
171 field = DECL_CHAIN (field);
172 elt.index = field;
173 elt.value = null_pointer_node;
174 v->quick_push (elt);
176 field = DECL_CHAIN (field);
177 elt.index = field;
178 elt.value = proxy;
179 v->quick_push (elt);
181 return build_constructor (type, v);
184 /* Create the structure for struct __emutls_object. This should match the
185 structure at the top of emutls.c, modulo the union there. */
187 static tree
188 get_emutls_object_type (void)
190 tree type, type_name, field;
192 type = emutls_object_type;
193 if (type)
194 return type;
196 emutls_object_type = type = lang_hooks.types.make_type (RECORD_TYPE);
197 type_name = NULL;
198 field = targetm.emutls.var_fields (type, &type_name);
199 if (!type_name)
200 type_name = get_identifier ("__emutls_object");
201 type_name = build_decl (UNKNOWN_LOCATION,
202 TYPE_DECL, type_name, type);
203 TYPE_NAME (type) = type_name;
204 TYPE_FIELDS (type) = field;
205 layout_type (type);
207 return type;
210 /* Create a read-only variable like DECL, with the same DECL_INITIAL.
211 This will be used for initializing the emulated tls data area. */
213 static tree
214 get_emutls_init_templ_addr (tree decl)
216 tree name, to;
218 if (targetm.emutls.register_common && !DECL_INITIAL (decl)
219 && !DECL_SECTION_NAME (decl))
220 return null_pointer_node;
222 name = DECL_ASSEMBLER_NAME (decl);
223 if (!targetm.emutls.tmpl_prefix || targetm.emutls.tmpl_prefix[0])
225 const char *prefix = (targetm.emutls.tmpl_prefix
226 ? targetm.emutls.tmpl_prefix
227 : "__emutls_t" EMUTLS_SEPARATOR);
228 name = prefix_name (prefix, name);
231 to = build_decl (DECL_SOURCE_LOCATION (decl),
232 VAR_DECL, name, TREE_TYPE (decl));
233 SET_DECL_ASSEMBLER_NAME (to, DECL_NAME (to));
235 DECL_ARTIFICIAL (to) = 1;
236 TREE_USED (to) = TREE_USED (decl);
237 TREE_READONLY (to) = 1;
238 DECL_IGNORED_P (to) = 1;
239 DECL_CONTEXT (to) = DECL_CONTEXT (decl);
240 DECL_SECTION_NAME (to) = DECL_SECTION_NAME (decl);
241 DECL_PRESERVE_P (to) = DECL_PRESERVE_P (decl);
243 DECL_WEAK (to) = DECL_WEAK (decl);
244 if (DECL_ONE_ONLY (decl))
246 make_decl_one_only (to, DECL_ASSEMBLER_NAME (to));
247 TREE_STATIC (to) = TREE_STATIC (decl);
248 TREE_PUBLIC (to) = TREE_PUBLIC (decl);
249 DECL_VISIBILITY (to) = DECL_VISIBILITY (decl);
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)
260 DECL_SECTION_NAME (to)
261 = build_string (strlen (targetm.emutls.tmpl_section),
262 targetm.emutls.tmpl_section);
265 /* Create varpool node for the new variable and finalize it if it is
266 not external one. */
267 if (DECL_EXTERNAL (to))
268 varpool_node_for_decl (to);
269 else
270 varpool_add_new_variable (to);
271 return build_fold_addr_expr (to);
274 /* Create and return the control variable for the TLS variable DECL. */
276 static tree
277 new_emutls_decl (tree decl, tree alias_of)
279 tree name, to;
281 name = DECL_ASSEMBLER_NAME (decl);
282 to = build_decl (DECL_SOURCE_LOCATION (decl), VAR_DECL,
283 get_emutls_object_name (name),
284 get_emutls_object_type ());
286 SET_DECL_ASSEMBLER_NAME (to, DECL_NAME (to));
288 DECL_TLS_MODEL (to) = TLS_MODEL_EMULATED;
289 DECL_ARTIFICIAL (to) = 1;
290 DECL_IGNORED_P (to) = 1;
291 TREE_READONLY (to) = 0;
292 TREE_STATIC (to) = 1;
294 DECL_PRESERVE_P (to) = DECL_PRESERVE_P (decl);
295 DECL_CONTEXT (to) = DECL_CONTEXT (decl);
296 TREE_USED (to) = TREE_USED (decl);
297 TREE_PUBLIC (to) = TREE_PUBLIC (decl);
298 DECL_EXTERNAL (to) = DECL_EXTERNAL (decl);
299 DECL_COMMON (to) = DECL_COMMON (decl);
300 DECL_WEAK (to) = DECL_WEAK (decl);
301 DECL_VISIBILITY (to) = DECL_VISIBILITY (decl);
302 DECL_VISIBILITY_SPECIFIED (to) = DECL_VISIBILITY_SPECIFIED (decl);
303 DECL_DLLIMPORT_P (to) = DECL_DLLIMPORT_P (decl);
305 DECL_ATTRIBUTES (to) = targetm.merge_decl_attributes (decl, to);
307 if (DECL_ONE_ONLY (decl))
308 make_decl_one_only (to, DECL_ASSEMBLER_NAME (to));
310 /* If we're not allowed to change the proxy object's alignment,
311 pretend it has been set by the user. */
312 if (targetm.emutls.var_align_fixed)
313 DECL_USER_ALIGN (to) = 1;
315 /* If the target wants the control variables grouped, do so. */
316 if (!DECL_COMMON (to) && targetm.emutls.var_section)
318 DECL_SECTION_NAME (to)
319 = build_string (strlen (targetm.emutls.var_section),
320 targetm.emutls.var_section);
323 /* If this variable is defined locally, then we need to initialize the
324 control structure with size and alignment information. Initialization
325 of COMMON block variables happens elsewhere via a constructor. */
326 if (!DECL_EXTERNAL (to)
327 && (!DECL_COMMON (to)
328 || (DECL_INITIAL (decl)
329 && DECL_INITIAL (decl) != error_mark_node)))
331 tree tmpl = get_emutls_init_templ_addr (decl);
332 DECL_INITIAL (to) = targetm.emutls.var_init (to, decl, tmpl);
333 record_references_in_initializer (to, false);
336 /* Create varpool node for the new variable and finalize it if it is
337 not external one. */
338 if (DECL_EXTERNAL (to))
339 varpool_node_for_decl (to);
340 else if (!alias_of)
341 varpool_add_new_variable (to);
342 else
343 varpool_create_variable_alias (to,
344 varpool_node_for_asm
345 (DECL_ASSEMBLER_NAME (DECL_VALUE_EXPR (alias_of)))->decl);
346 return to;
349 /* Look up the index of the TLS variable DECL. This index can then be
350 used in both the control_vars and access_vars arrays. */
352 static unsigned int
353 emutls_index (tree decl)
355 varpool_node_set_iterator i;
357 i = varpool_node_set_find (tls_vars, varpool_get_node (decl));
358 gcc_assert (i.index != ~0u);
360 return i.index;
363 /* Look up the control variable for the TLS variable DECL. */
365 static tree
366 emutls_decl (tree decl)
368 struct varpool_node *var;
369 unsigned int i;
371 i = emutls_index (decl);
372 var = control_vars[i];
373 return var->decl;
376 /* Generate a call statement to initialize CONTROL_DECL for TLS_DECL.
377 This only needs to happen for TLS COMMON variables; non-COMMON
378 variables can be initialized statically. Insert the generated
379 call statement at the end of PSTMTS. */
381 static void
382 emutls_common_1 (tree tls_decl, tree control_decl, tree *pstmts)
384 tree x;
385 tree word_type_node;
387 if (! DECL_COMMON (tls_decl)
388 || (DECL_INITIAL (tls_decl)
389 && DECL_INITIAL (tls_decl) != error_mark_node))
390 return;
392 word_type_node = lang_hooks.types.type_for_mode (word_mode, 1);
394 x = build_call_expr (builtin_decl_explicit (BUILT_IN_EMUTLS_REGISTER_COMMON),
395 4, build_fold_addr_expr (control_decl),
396 fold_convert (word_type_node,
397 DECL_SIZE_UNIT (tls_decl)),
398 build_int_cst (word_type_node,
399 DECL_ALIGN_UNIT (tls_decl)),
400 get_emutls_init_templ_addr (tls_decl));
402 append_to_statement_list (x, pstmts);
405 struct lower_emutls_data
407 struct cgraph_node *cfun_node;
408 struct cgraph_node *builtin_node;
409 tree builtin_decl;
410 basic_block bb;
411 int bb_freq;
412 location_t loc;
413 gimple_seq seq;
416 /* Given a TLS variable DECL, return an SSA_NAME holding its address.
417 Append any new computation statements required to D->SEQ. */
419 static tree
420 gen_emutls_addr (tree decl, struct lower_emutls_data *d)
422 unsigned int index;
423 tree addr;
425 /* Compute the address of the TLS variable with help from runtime. */
426 index = emutls_index (decl);
427 addr = access_vars[index];
428 if (addr == NULL)
430 struct varpool_node *cvar;
431 tree cdecl;
432 gimple x;
434 cvar = control_vars[index];
435 cdecl = cvar->decl;
436 TREE_ADDRESSABLE (cdecl) = 1;
438 addr = create_tmp_var (build_pointer_type (TREE_TYPE (decl)), NULL);
439 x = gimple_build_call (d->builtin_decl, 1, build_fold_addr_expr (cdecl));
440 gimple_set_location (x, d->loc);
442 addr = make_ssa_name (addr, x);
443 gimple_call_set_lhs (x, addr);
445 gimple_seq_add_stmt (&d->seq, x);
447 cgraph_create_edge (d->cfun_node, d->builtin_node, x,
448 d->bb->count, d->bb_freq);
450 /* We may be adding a new reference to a new variable to the function.
451 This means we have to play with the ipa-reference web. */
452 ipa_record_reference (d->cfun_node, cvar, IPA_REF_ADDR, x);
454 /* Record this ssa_name for possible use later in the basic block. */
455 access_vars[index] = addr;
458 return addr;
461 /* Callback for walk_gimple_op. D = WI->INFO is a struct lower_emutls_data.
462 Given an operand *PTR within D->STMT, if the operand references a TLS
463 variable, then lower the reference to a call to the runtime. Insert
464 any new statements required into D->SEQ; the caller is responsible for
465 placing those appropriately. */
467 static tree
468 lower_emutls_1 (tree *ptr, int *walk_subtrees, void *cb_data)
470 struct walk_stmt_info *wi = (struct walk_stmt_info *) cb_data;
471 struct lower_emutls_data *d = (struct lower_emutls_data *) wi->info;
472 tree t = *ptr;
473 bool is_addr = false;
474 tree addr;
476 *walk_subtrees = 0;
478 switch (TREE_CODE (t))
480 case ADDR_EXPR:
481 /* If this is not a straight-forward "&var", but rather something
482 like "&var.a", then we may need special handling. */
483 if (TREE_CODE (TREE_OPERAND (t, 0)) != VAR_DECL)
485 bool save_changed;
487 /* If we're allowed more than just is_gimple_val, continue. */
488 if (!wi->val_only)
490 *walk_subtrees = 1;
491 return NULL_TREE;
494 /* See if any substitution would be made. */
495 save_changed = wi->changed;
496 wi->changed = false;
497 wi->val_only = false;
498 walk_tree (&TREE_OPERAND (t, 0), lower_emutls_1, wi, NULL);
499 wi->val_only = true;
501 /* If so, then extract this entire sub-expression "&p->a" into a
502 new assignment statement, and substitute yet another SSA_NAME. */
503 if (wi->changed)
505 gimple x;
507 addr = create_tmp_var (TREE_TYPE (t), NULL);
508 x = gimple_build_assign (addr, t);
509 gimple_set_location (x, d->loc);
511 addr = make_ssa_name (addr, x);
512 gimple_assign_set_lhs (x, addr);
514 gimple_seq_add_stmt (&d->seq, x);
516 *ptr = addr;
518 else
519 wi->changed = save_changed;
521 return NULL_TREE;
524 t = TREE_OPERAND (t, 0);
525 is_addr = true;
526 /* FALLTHRU */
528 case VAR_DECL:
529 if (!DECL_THREAD_LOCAL_P (t))
530 return NULL_TREE;
531 break;
533 default:
534 /* We're not interested in other decls or types, only subexpressions. */
535 if (EXPR_P (t))
536 *walk_subtrees = 1;
537 /* FALLTHRU */
539 case SSA_NAME:
540 /* Special-case the return of SSA_NAME, since it's so common. */
541 return NULL_TREE;
544 addr = gen_emutls_addr (t, d);
545 if (is_addr)
547 /* Replace "&var" with "addr" in the statement. */
548 *ptr = addr;
550 else
552 /* Replace "var" with "*addr" in the statement. */
553 t = build2 (MEM_REF, TREE_TYPE (t), addr,
554 build_int_cst (TREE_TYPE (addr), 0));
555 *ptr = t;
558 wi->changed = true;
559 return NULL_TREE;
562 /* Lower all of the operands of STMT. */
564 static void
565 lower_emutls_stmt (gimple stmt, struct lower_emutls_data *d)
567 struct walk_stmt_info wi;
569 d->loc = gimple_location (stmt);
571 memset (&wi, 0, sizeof (wi));
572 wi.info = d;
573 wi.val_only = true;
574 walk_gimple_op (stmt, lower_emutls_1, &wi);
576 if (wi.changed)
577 update_stmt (stmt);
580 /* Lower the I'th operand of PHI. */
582 static void
583 lower_emutls_phi_arg (gimple phi, unsigned int i, struct lower_emutls_data *d)
585 struct walk_stmt_info wi;
586 struct phi_arg_d *pd = gimple_phi_arg (phi, i);
588 /* Early out for a very common case we don't care about. */
589 if (TREE_CODE (pd->def) == SSA_NAME)
590 return;
592 d->loc = pd->locus;
594 memset (&wi, 0, sizeof (wi));
595 wi.info = d;
596 wi.val_only = true;
597 walk_tree (&pd->def, lower_emutls_1, &wi, NULL);
599 /* For normal statements, we let update_stmt do its job. But for phi
600 nodes, we have to manipulate the immediate use list by hand. */
601 if (wi.changed)
603 gcc_assert (TREE_CODE (pd->def) == SSA_NAME);
604 link_imm_use_stmt (&pd->imm_use, pd->def, phi);
608 /* Clear the ACCESS_VARS array, in order to begin a new block. */
610 static inline void
611 clear_access_vars (void)
613 memset (access_vars.address (), 0,
614 access_vars.length () * sizeof (tree));
617 /* Lower the entire function NODE. */
619 static void
620 lower_emutls_function_body (struct cgraph_node *node)
622 struct lower_emutls_data d;
623 bool any_edge_inserts = false;
625 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
627 d.cfun_node = node;
628 d.builtin_decl = builtin_decl_explicit (BUILT_IN_EMUTLS_GET_ADDRESS);
629 /* This is where we introduce the declaration to the IL and so we have to
630 create a node for it. */
631 d.builtin_node = cgraph_get_create_node (d.builtin_decl);
633 FOR_EACH_BB (d.bb)
635 gimple_stmt_iterator gsi;
636 unsigned int i, nedge;
638 /* Lower each of the PHI nodes of the block, as we may have
639 propagated &tlsvar into a PHI argument. These loops are
640 arranged so that we process each edge at once, and each
641 PHI argument for that edge. */
642 if (!gimple_seq_empty_p (phi_nodes (d.bb)))
644 /* The calls will be inserted on the edges, and the frequencies
645 will be computed during the commit process. */
646 d.bb_freq = 0;
648 nedge = EDGE_COUNT (d.bb->preds);
649 for (i = 0; i < nedge; ++i)
651 edge e = EDGE_PRED (d.bb, i);
653 /* We can re-use any SSA_NAME created on this edge. */
654 clear_access_vars ();
655 d.seq = NULL;
657 for (gsi = gsi_start_phis (d.bb);
658 !gsi_end_p (gsi);
659 gsi_next (&gsi))
660 lower_emutls_phi_arg (gsi_stmt (gsi), i, &d);
662 /* Insert all statements generated by all phi nodes for this
663 particular edge all at once. */
664 if (d.seq)
666 gsi_insert_seq_on_edge (e, d.seq);
667 any_edge_inserts = true;
672 d.bb_freq = compute_call_stmt_bb_frequency (current_function_decl, d.bb);
674 /* We can re-use any SSA_NAME created during this basic block. */
675 clear_access_vars ();
677 /* Lower each of the statements of the block. */
678 for (gsi = gsi_start_bb (d.bb); !gsi_end_p (gsi); gsi_next (&gsi))
680 d.seq = NULL;
681 lower_emutls_stmt (gsi_stmt (gsi), &d);
683 /* If any new statements were created, insert them immediately
684 before the first use. This prevents variable lifetimes from
685 becoming unnecessarily long. */
686 if (d.seq)
687 gsi_insert_seq_before (&gsi, d.seq, GSI_SAME_STMT);
691 if (any_edge_inserts)
692 gsi_commit_edge_inserts ();
694 pop_cfun ();
697 /* Create emutls variable for VAR, DATA is pointer to static
698 ctor body we can add constructors to.
699 Callback for varpool_for_variable_and_aliases. */
701 static bool
702 create_emultls_var (struct varpool_node *var, void *data)
704 tree cdecl;
705 struct varpool_node *cvar;
707 cdecl = new_emutls_decl (var->decl,
708 var->alias && var->analyzed
709 ? varpool_alias_target (var)->decl : NULL);
711 cvar = varpool_get_node (cdecl);
712 control_vars.quick_push (cvar);
714 if (!var->alias)
716 /* Make sure the COMMON block control variable gets initialized.
717 Note that there's no point in doing this for aliases; we only
718 need to do this once for the main variable. */
719 emutls_common_1 (var->decl, cdecl, (tree *)data);
721 if (var->alias && !var->analyzed)
722 cvar->alias = true;
724 /* Indicate that the value of the TLS variable may be found elsewhere,
725 preventing the variable from re-appearing in the GIMPLE. We cheat
726 and use the control variable here (rather than a full call_expr),
727 which is special-cased inside the DWARF2 output routines. */
728 SET_DECL_VALUE_EXPR (var->decl, cdecl);
729 DECL_HAS_VALUE_EXPR_P (var->decl) = 1;
730 return false;
733 /* Main entry point to the tls lowering pass. */
735 static unsigned int
736 ipa_lower_emutls (void)
738 struct varpool_node *var;
739 struct cgraph_node *func;
740 bool any_aliases = false;
741 tree ctor_body = NULL;
742 unsigned int i, n_tls;
744 tls_vars = varpool_node_set_new ();
746 /* Examine all global variables for TLS variables. */
747 FOR_EACH_VARIABLE (var)
748 if (DECL_THREAD_LOCAL_P (var->decl))
750 gcc_checking_assert (TREE_STATIC (var->decl)
751 || DECL_EXTERNAL (var->decl));
752 varpool_node_set_add (tls_vars, var);
753 if (var->alias && var->definition)
754 varpool_node_set_add (tls_vars, varpool_variable_node (var, NULL));
757 /* If we found no TLS variables, then there is no further work to do. */
758 if (!tls_vars->nodes.exists ())
760 tls_vars = NULL;
761 if (dump_file)
762 fprintf (dump_file, "No TLS variables found.\n");
763 return 0;
766 /* Allocate the on-the-side arrays that share indicies with the TLS vars. */
767 n_tls = tls_vars->nodes.length ();
768 control_vars.create (n_tls);
769 access_vars.create (n_tls);
770 access_vars.safe_grow_cleared (n_tls);
772 /* Create the control variables for each TLS variable. */
773 FOR_EACH_VEC_ELT (tls_vars->nodes, i, var)
775 var = tls_vars->nodes[i];
777 if (var->alias && !var->analyzed)
778 any_aliases = true;
779 else if (!var->alias)
780 varpool_for_node_and_aliases (var, create_emultls_var, &ctor_body, true);
783 /* If there were any aliases, then frob the alias_pairs vector. */
784 if (any_aliases)
786 alias_pair *p;
787 FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
788 if (DECL_THREAD_LOCAL_P (p->decl))
790 p->decl = emutls_decl (p->decl);
791 p->target = get_emutls_object_name (p->target);
795 /* Adjust all uses of TLS variables within the function bodies. */
796 FOR_EACH_DEFINED_FUNCTION (func)
797 if (func->lowered)
798 lower_emutls_function_body (func);
800 /* Generate the constructor for any COMMON control variables created. */
801 if (ctor_body)
802 cgraph_build_static_cdtor ('I', ctor_body, DEFAULT_INIT_PRIORITY);
804 control_vars.release ();
805 access_vars.release ();
806 free_varpool_node_set (tls_vars);
808 return TODO_verify_all;
811 /* If the target supports TLS natively, we need do nothing here. */
813 static bool
814 gate_emutls (void)
816 return !targetm.have_tls;
819 namespace {
821 const pass_data pass_data_ipa_lower_emutls =
823 SIMPLE_IPA_PASS, /* type */
824 "emutls", /* name */
825 OPTGROUP_NONE, /* optinfo_flags */
826 true, /* has_gate */
827 true, /* has_execute */
828 TV_IPA_OPT, /* tv_id */
829 ( PROP_cfg | PROP_ssa ), /* properties_required */
830 0, /* properties_provided */
831 0, /* properties_destroyed */
832 0, /* todo_flags_start */
833 0, /* todo_flags_finish */
836 class pass_ipa_lower_emutls : public simple_ipa_opt_pass
838 public:
839 pass_ipa_lower_emutls (gcc::context *ctxt)
840 : simple_ipa_opt_pass (pass_data_ipa_lower_emutls, ctxt)
843 /* opt_pass methods: */
844 bool gate () { return gate_emutls (); }
845 unsigned int execute () { return ipa_lower_emutls (); }
847 }; // class pass_ipa_lower_emutls
849 } // anon namespace
851 simple_ipa_opt_pass *
852 make_pass_ipa_lower_emutls (gcc::context *ctxt)
854 return new pass_ipa_lower_emutls (ctxt);