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