]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-emutls.c
Merger of git branch "gimple-classes-v2-option-3"
[thirdparty/gcc.git] / gcc / tree-emutls.c
CommitLineData
cffbbb9d 1/* Lower TLS operations to emulation functions.
3aea1f79 2 Copyright (C) 2006-2014 Free Software Foundation, Inc.
cffbbb9d 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 "tree.h"
9ed99284 24#include "stor-layout.h"
25#include "varasm.h"
94ea8568 26#include "predict.h"
27#include "vec.h"
28#include "hashtab.h"
29#include "hash-set.h"
30#include "machmode.h"
31#include "tm.h"
32#include "hard-reg-set.h"
33#include "input.h"
34#include "function.h"
35#include "dominance.h"
36#include "cfg.h"
bc61cadb 37#include "basic-block.h"
38#include "tree-ssa-alias.h"
39#include "internal-fn.h"
40#include "gimple-expr.h"
41#include "is-a.h"
cffbbb9d 42#include "gimple.h"
dcf1a1ec 43#include "gimple-iterator.h"
44#include "gimple-walk.h"
cffbbb9d 45#include "tree-pass.h"
073c1fd5 46#include "gimple-ssa.h"
1140c305 47#include "hash-map.h"
48#include "plugin-api.h"
49#include "ipa-ref.h"
cffbbb9d 50#include "cgraph.h"
073c1fd5 51#include "tree-phinodes.h"
52#include "ssa-iterators.h"
9ed99284 53#include "stringpool.h"
073c1fd5 54#include "tree-ssanames.h"
cffbbb9d 55#include "langhooks.h"
56#include "target.h"
57#include "targhooks.h"
58#include "tree-iterator.h"
cffbbb9d 59
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.
64
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.
70
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".
75
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. */
82
347a47cb 83/* Value for TLS varpool node where a pointer to control variable and
84 access variable are stored. */
85struct tls_var_data
86{
87 varpool_node *control_var;
88 tree access;
89};
cffbbb9d 90
347a47cb 91/* TLS map accesses mapping between a TLS varpool node and a pair
92 made by control variable and access variable. */
93static hash_map<varpool_node *, tls_var_data> *tls_map = NULL;
cffbbb9d 94
95/* The type of the control structure, shared with the emutls.c runtime. */
96static tree emutls_object_type;
97
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
105
106/* Create an IDENTIFIER_NODE by prefixing PREFIX to the
107 IDENTIFIER_NODE NAME's name. */
108
109static tree
110prefix_name (const char *prefix, tree name)
111{
112 unsigned plen = strlen (prefix);
113 unsigned nlen = strlen (IDENTIFIER_POINTER (name));
114 char *toname = (char *) alloca (plen + nlen + 1);
115
116 memcpy (toname, prefix, plen);
117 memcpy (toname + plen, IDENTIFIER_POINTER (name), nlen + 1);
118
119 return get_identifier (toname);
120}
121
122/* Create an identifier for the struct __emutls_object, given an identifier
123 of the DECL_ASSEMBLY_NAME of the original object. */
124
125static tree
126get_emutls_object_name (tree name)
127{
128 const char *prefix = (targetm.emutls.var_prefix
129 ? targetm.emutls.var_prefix
130 : "__emutls_v" EMUTLS_SEPARATOR);
131 return prefix_name (prefix, name);
132}
133
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. */
137
138tree
139default_emutls_var_fields (tree type, tree *name ATTRIBUTE_UNUSED)
140{
141 tree word_type_node, field, next_field;
142
143 field = build_decl (UNKNOWN_LOCATION,
144 FIELD_DECL, get_identifier ("__templ"), ptr_type_node);
145 DECL_CONTEXT (field) = type;
146 next_field = field;
147
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;
154
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;
162
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;
167
168 return field;
169}
170
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. */
174
175tree
176default_emutls_var_init (tree to, tree decl, tree proxy)
177{
f1f41a6c 178 vec<constructor_elt, va_gc> *v;
179 vec_alloc (v, 4);
e82e4eb5 180 constructor_elt elt;
cffbbb9d 181 tree type = TREE_TYPE (to);
182 tree field = TYPE_FIELDS (type);
183
e82e4eb5 184 elt.index = field;
185 elt.value = fold_convert (TREE_TYPE (field), DECL_SIZE_UNIT (decl));
f1f41a6c 186 v->quick_push (elt);
cffbbb9d 187
cffbbb9d 188 field = DECL_CHAIN (field);
e82e4eb5 189 elt.index = field;
190 elt.value = build_int_cst (TREE_TYPE (field),
191 DECL_ALIGN_UNIT (decl));
f1f41a6c 192 v->quick_push (elt);
cffbbb9d 193
cffbbb9d 194 field = DECL_CHAIN (field);
e82e4eb5 195 elt.index = field;
196 elt.value = null_pointer_node;
f1f41a6c 197 v->quick_push (elt);
cffbbb9d 198
cffbbb9d 199 field = DECL_CHAIN (field);
e82e4eb5 200 elt.index = field;
201 elt.value = proxy;
f1f41a6c 202 v->quick_push (elt);
cffbbb9d 203
204 return build_constructor (type, v);
205}
206
207/* Create the structure for struct __emutls_object. This should match the
208 structure at the top of emutls.c, modulo the union there. */
209
210static tree
211get_emutls_object_type (void)
212{
213 tree type, type_name, field;
214
215 type = emutls_object_type;
216 if (type)
217 return type;
218
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);
229
230 return type;
231}
232
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. */
235
236static tree
237get_emutls_init_templ_addr (tree decl)
238{
239 tree name, to;
240
241 if (targetm.emutls.register_common && !DECL_INITIAL (decl)
242 && !DECL_SECTION_NAME (decl))
243 return null_pointer_node;
244
245 name = DECL_ASSEMBLER_NAME (decl);
246 if (!targetm.emutls.tmpl_prefix || targetm.emutls.tmpl_prefix[0])
247 {
248 const char *prefix = (targetm.emutls.tmpl_prefix
249 ? targetm.emutls.tmpl_prefix
250 : "__emutls_t" EMUTLS_SEPARATOR);
251 name = prefix_name (prefix, name);
252 }
253
254 to = build_decl (DECL_SOURCE_LOCATION (decl),
255 VAR_DECL, name, TREE_TYPE (decl));
256 SET_DECL_ASSEMBLER_NAME (to, DECL_NAME (to));
257
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);
cffbbb9d 263 DECL_PRESERVE_P (to) = DECL_PRESERVE_P (decl);
264
265 DECL_WEAK (to) = DECL_WEAK (decl);
266 if (DECL_ONE_ONLY (decl))
267 {
cffbbb9d 268 TREE_STATIC (to) = TREE_STATIC (decl);
269 TREE_PUBLIC (to) = TREE_PUBLIC (decl);
270 DECL_VISIBILITY (to) = DECL_VISIBILITY (decl);
5e68df57 271 make_decl_one_only (to, DECL_ASSEMBLER_NAME (to));
cffbbb9d 272 }
273 else
274 TREE_STATIC (to) = 1;
275
276 DECL_VISIBILITY_SPECIFIED (to) = DECL_VISIBILITY_SPECIFIED (decl);
277 DECL_INITIAL (to) = DECL_INITIAL (decl);
278 DECL_INITIAL (decl) = NULL;
279
280 if (targetm.emutls.tmpl_section)
5e68df57 281 set_decl_section_name (to, targetm.emutls.tmpl_section);
71e19e54 282 else
283 set_decl_section_name (to, DECL_SECTION_NAME (decl));
cffbbb9d 284
a5c9e220 285 /* Create varpool node for the new variable and finalize it if it is
286 not external one. */
287 if (DECL_EXTERNAL (to))
97221fd7 288 varpool_node::get_create (to);
a5c9e220 289 else
35ee1c66 290 varpool_node::add (to);
cffbbb9d 291 return build_fold_addr_expr (to);
292}
293
294/* Create and return the control variable for the TLS variable DECL. */
295
296static tree
478e80e0 297new_emutls_decl (tree decl, tree alias_of)
cffbbb9d 298{
299 tree name, to;
300
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 ());
305
306 SET_DECL_ASSEMBLER_NAME (to, DECL_NAME (to));
307
cffbbb9d 308 DECL_ARTIFICIAL (to) = 1;
309 DECL_IGNORED_P (to) = 1;
310 TREE_READONLY (to) = 0;
311 TREE_STATIC (to) = 1;
312
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);
cffbbb9d 322 DECL_DLLIMPORT_P (to) = DECL_DLLIMPORT_P (decl);
323
324 DECL_ATTRIBUTES (to) = targetm.merge_decl_attributes (decl, to);
325
326 if (DECL_ONE_ONLY (decl))
327 make_decl_one_only (to, DECL_ASSEMBLER_NAME (to));
328
5e68df57 329 set_decl_tls_model (to, TLS_MODEL_EMULATED);
330
cffbbb9d 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;
335
336 /* If the target wants the control variables grouped, do so. */
337 if (!DECL_COMMON (to) && targetm.emutls.var_section)
338 {
738a6bda 339 set_decl_section_name (to, targetm.emutls.var_section);
cffbbb9d 340 }
341
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)))
349 {
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);
353 }
354
a5c9e220 355 /* Create varpool node for the new variable and finalize it if it is
356 not external one. */
357 if (DECL_EXTERNAL (to))
97221fd7 358 varpool_node::get_create (to);
478e80e0 359 else if (!alias_of)
35ee1c66 360 varpool_node::add (to);
478e80e0 361 else
97221fd7 362 varpool_node::create_alias (to,
363 varpool_node::get_for_asmname
364 (DECL_ASSEMBLER_NAME (DECL_VALUE_EXPR (alias_of)))->decl);
cffbbb9d 365 return to;
366}
367
cffbbb9d 368/* Generate a call statement to initialize CONTROL_DECL for TLS_DECL.
369 This only needs to happen for TLS COMMON variables; non-COMMON
370 variables can be initialized statically. Insert the generated
371 call statement at the end of PSTMTS. */
372
373static void
374emutls_common_1 (tree tls_decl, tree control_decl, tree *pstmts)
375{
376 tree x;
377 tree word_type_node;
378
379 if (! DECL_COMMON (tls_decl)
380 || (DECL_INITIAL (tls_decl)
381 && DECL_INITIAL (tls_decl) != error_mark_node))
382 return;
383
384 word_type_node = lang_hooks.types.type_for_mode (word_mode, 1);
385
b9a16870 386 x = build_call_expr (builtin_decl_explicit (BUILT_IN_EMUTLS_REGISTER_COMMON),
387 4, build_fold_addr_expr (control_decl),
cffbbb9d 388 fold_convert (word_type_node,
389 DECL_SIZE_UNIT (tls_decl)),
390 build_int_cst (word_type_node,
391 DECL_ALIGN_UNIT (tls_decl)),
392 get_emutls_init_templ_addr (tls_decl));
393
394 append_to_statement_list (x, pstmts);
395}
396
397struct lower_emutls_data
398{
399 struct cgraph_node *cfun_node;
400 struct cgraph_node *builtin_node;
401 tree builtin_decl;
402 basic_block bb;
403 int bb_freq;
404 location_t loc;
405 gimple_seq seq;
406};
407
408/* Given a TLS variable DECL, return an SSA_NAME holding its address.
409 Append any new computation statements required to D->SEQ. */
410
411static tree
412gen_emutls_addr (tree decl, struct lower_emutls_data *d)
413{
cffbbb9d 414 /* Compute the address of the TLS variable with help from runtime. */
347a47cb 415 tls_var_data *data = tls_map->get (varpool_node::get (decl));
416 tree addr = data->access;
417
cffbbb9d 418 if (addr == NULL)
419 {
098f44bc 420 varpool_node *cvar;
cffbbb9d 421 tree cdecl;
1a91d914 422 gcall *x;
cffbbb9d 423
347a47cb 424 cvar = data->control_var;
02774f2d 425 cdecl = cvar->decl;
cffbbb9d 426 TREE_ADDRESSABLE (cdecl) = 1;
427
428 addr = create_tmp_var (build_pointer_type (TREE_TYPE (decl)), NULL);
429 x = gimple_build_call (d->builtin_decl, 1, build_fold_addr_expr (cdecl));
430 gimple_set_location (x, d->loc);
431
432 addr = make_ssa_name (addr, x);
433 gimple_call_set_lhs (x, addr);
434
435 gimple_seq_add_stmt (&d->seq, x);
436
415d1b9a 437 d->cfun_node->create_edge (d->builtin_node, x, d->bb->count, d->bb_freq);
cffbbb9d 438
439 /* We may be adding a new reference to a new variable to the function.
440 This means we have to play with the ipa-reference web. */
35ee1c66 441 d->cfun_node->create_reference (cvar, IPA_REF_ADDR, x);
cffbbb9d 442
443 /* Record this ssa_name for possible use later in the basic block. */
347a47cb 444 data->access = addr;
cffbbb9d 445 }
446
447 return addr;
448}
449
450/* Callback for walk_gimple_op. D = WI->INFO is a struct lower_emutls_data.
451 Given an operand *PTR within D->STMT, if the operand references a TLS
452 variable, then lower the reference to a call to the runtime. Insert
453 any new statements required into D->SEQ; the caller is responsible for
454 placing those appropriately. */
455
456static tree
457lower_emutls_1 (tree *ptr, int *walk_subtrees, void *cb_data)
458{
459 struct walk_stmt_info *wi = (struct walk_stmt_info *) cb_data;
460 struct lower_emutls_data *d = (struct lower_emutls_data *) wi->info;
461 tree t = *ptr;
462 bool is_addr = false;
463 tree addr;
464
465 *walk_subtrees = 0;
466
467 switch (TREE_CODE (t))
468 {
469 case ADDR_EXPR:
470 /* If this is not a straight-forward "&var", but rather something
471 like "&var.a", then we may need special handling. */
472 if (TREE_CODE (TREE_OPERAND (t, 0)) != VAR_DECL)
473 {
474 bool save_changed;
475
476 /* If we're allowed more than just is_gimple_val, continue. */
477 if (!wi->val_only)
478 {
479 *walk_subtrees = 1;
480 return NULL_TREE;
481 }
482
483 /* See if any substitution would be made. */
484 save_changed = wi->changed;
485 wi->changed = false;
486 wi->val_only = false;
487 walk_tree (&TREE_OPERAND (t, 0), lower_emutls_1, wi, NULL);
488 wi->val_only = true;
489
490 /* If so, then extract this entire sub-expression "&p->a" into a
491 new assignment statement, and substitute yet another SSA_NAME. */
492 if (wi->changed)
493 {
494 gimple x;
495
496 addr = create_tmp_var (TREE_TYPE (t), NULL);
497 x = gimple_build_assign (addr, t);
498 gimple_set_location (x, d->loc);
499
500 addr = make_ssa_name (addr, x);
501 gimple_assign_set_lhs (x, addr);
502
503 gimple_seq_add_stmt (&d->seq, x);
504
505 *ptr = addr;
506 }
507 else
508 wi->changed = save_changed;
509
510 return NULL_TREE;
511 }
512
513 t = TREE_OPERAND (t, 0);
514 is_addr = true;
515 /* FALLTHRU */
516
517 case VAR_DECL:
518 if (!DECL_THREAD_LOCAL_P (t))
519 return NULL_TREE;
520 break;
521
522 default:
523 /* We're not interested in other decls or types, only subexpressions. */
524 if (EXPR_P (t))
525 *walk_subtrees = 1;
526 /* FALLTHRU */
527
528 case SSA_NAME:
529 /* Special-case the return of SSA_NAME, since it's so common. */
530 return NULL_TREE;
531 }
532
533 addr = gen_emutls_addr (t, d);
534 if (is_addr)
535 {
536 /* Replace "&var" with "addr" in the statement. */
537 *ptr = addr;
538 }
539 else
540 {
541 /* Replace "var" with "*addr" in the statement. */
542 t = build2 (MEM_REF, TREE_TYPE (t), addr,
543 build_int_cst (TREE_TYPE (addr), 0));
544 *ptr = t;
545 }
546
547 wi->changed = true;
548 return NULL_TREE;
549}
550
551/* Lower all of the operands of STMT. */
552
553static void
554lower_emutls_stmt (gimple stmt, struct lower_emutls_data *d)
555{
556 struct walk_stmt_info wi;
557
558 d->loc = gimple_location (stmt);
559
560 memset (&wi, 0, sizeof (wi));
561 wi.info = d;
562 wi.val_only = true;
563 walk_gimple_op (stmt, lower_emutls_1, &wi);
564
565 if (wi.changed)
566 update_stmt (stmt);
567}
568
569/* Lower the I'th operand of PHI. */
570
571static void
1a91d914 572lower_emutls_phi_arg (gphi *phi, unsigned int i,
573 struct lower_emutls_data *d)
cffbbb9d 574{
575 struct walk_stmt_info wi;
576 struct phi_arg_d *pd = gimple_phi_arg (phi, i);
577
578 /* Early out for a very common case we don't care about. */
579 if (TREE_CODE (pd->def) == SSA_NAME)
580 return;
581
582 d->loc = pd->locus;
583
584 memset (&wi, 0, sizeof (wi));
585 wi.info = d;
586 wi.val_only = true;
587 walk_tree (&pd->def, lower_emutls_1, &wi, NULL);
588
589 /* For normal statements, we let update_stmt do its job. But for phi
590 nodes, we have to manipulate the immediate use list by hand. */
591 if (wi.changed)
592 {
593 gcc_assert (TREE_CODE (pd->def) == SSA_NAME);
594 link_imm_use_stmt (&pd->imm_use, pd->def, phi);
595 }
596}
597
347a47cb 598/* Reset access variable for a given TLS variable data DATA. */
599
600bool
601reset_access (varpool_node * const &, tls_var_data *data, void *)
602{
603 data->access = NULL;
604
605 return true;
606}
607
608/* Clear the access variables, in order to begin a new block. */
cffbbb9d 609
610static inline void
611clear_access_vars (void)
612{
347a47cb 613 tls_map->traverse<void *, reset_access> (NULL);
cffbbb9d 614}
615
616/* Lower the entire function NODE. */
617
618static void
619lower_emutls_function_body (struct cgraph_node *node)
620{
621 struct lower_emutls_data d;
622 bool any_edge_inserts = false;
623
02774f2d 624 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
cffbbb9d 625
626 d.cfun_node = node;
b9a16870 627 d.builtin_decl = builtin_decl_explicit (BUILT_IN_EMUTLS_GET_ADDRESS);
4559c6fc 628 /* This is where we introduce the declaration to the IL and so we have to
629 create a node for it. */
415d1b9a 630 d.builtin_node = cgraph_node::get_create (d.builtin_decl);
cffbbb9d 631
fc00614f 632 FOR_EACH_BB_FN (d.bb, cfun)
cffbbb9d 633 {
cffbbb9d 634 unsigned int i, nedge;
635
636 /* Lower each of the PHI nodes of the block, as we may have
637 propagated &tlsvar into a PHI argument. These loops are
638 arranged so that we process each edge at once, and each
639 PHI argument for that edge. */
640 if (!gimple_seq_empty_p (phi_nodes (d.bb)))
641 {
642 /* The calls will be inserted on the edges, and the frequencies
643 will be computed during the commit process. */
644 d.bb_freq = 0;
645
646 nedge = EDGE_COUNT (d.bb->preds);
647 for (i = 0; i < nedge; ++i)
648 {
649 edge e = EDGE_PRED (d.bb, i);
650
651 /* We can re-use any SSA_NAME created on this edge. */
652 clear_access_vars ();
653 d.seq = NULL;
654
1a91d914 655 for (gphi_iterator gsi = gsi_start_phis (d.bb);
cffbbb9d 656 !gsi_end_p (gsi);
657 gsi_next (&gsi))
1a91d914 658 lower_emutls_phi_arg (gsi.phi (), i, &d);
cffbbb9d 659
660 /* Insert all statements generated by all phi nodes for this
661 particular edge all at once. */
662 if (d.seq)
663 {
664 gsi_insert_seq_on_edge (e, d.seq);
665 any_edge_inserts = true;
666 }
667 }
668 }
669
670 d.bb_freq = compute_call_stmt_bb_frequency (current_function_decl, d.bb);
671
672 /* We can re-use any SSA_NAME created during this basic block. */
673 clear_access_vars ();
674
675 /* Lower each of the statements of the block. */
1a91d914 676 for (gimple_stmt_iterator gsi = gsi_start_bb (d.bb); !gsi_end_p (gsi);
677 gsi_next (&gsi))
cffbbb9d 678 {
679 d.seq = NULL;
680 lower_emutls_stmt (gsi_stmt (gsi), &d);
681
682 /* If any new statements were created, insert them immediately
683 before the first use. This prevents variable lifetimes from
684 becoming unnecessarily long. */
685 if (d.seq)
686 gsi_insert_seq_before (&gsi, d.seq, GSI_SAME_STMT);
687 }
688 }
689
690 if (any_edge_inserts)
691 gsi_commit_edge_inserts ();
692
693 pop_cfun ();
cffbbb9d 694}
695
478e80e0 696/* Create emutls variable for VAR, DATA is pointer to static
697 ctor body we can add constructors to.
698 Callback for varpool_for_variable_and_aliases. */
699
700static bool
098f44bc 701create_emultls_var (varpool_node *var, void *data)
478e80e0 702{
703 tree cdecl;
347a47cb 704 tls_var_data value;
478e80e0 705
02774f2d 706 cdecl = new_emutls_decl (var->decl,
707 var->alias && var->analyzed
97221fd7 708 ? var->get_alias_target ()->decl : NULL);
478e80e0 709
347a47cb 710 varpool_node *cvar = varpool_node::get (cdecl);
478e80e0 711
02774f2d 712 if (!var->alias)
478e80e0 713 {
714 /* Make sure the COMMON block control variable gets initialized.
715 Note that there's no point in doing this for aliases; we only
716 need to do this once for the main variable. */
02774f2d 717 emutls_common_1 (var->decl, cdecl, (tree *)data);
478e80e0 718 }
02774f2d 719 if (var->alias && !var->analyzed)
720 cvar->alias = true;
478e80e0 721
722 /* Indicate that the value of the TLS variable may be found elsewhere,
723 preventing the variable from re-appearing in the GIMPLE. We cheat
724 and use the control variable here (rather than a full call_expr),
725 which is special-cased inside the DWARF2 output routines. */
02774f2d 726 SET_DECL_VALUE_EXPR (var->decl, cdecl);
727 DECL_HAS_VALUE_EXPR_P (var->decl) = 1;
347a47cb 728
729 value.control_var = cvar;
730 tls_map->put (var, value);
731
478e80e0 732 return false;
733}
734
cffbbb9d 735/* Main entry point to the tls lowering pass. */
736
737static unsigned int
738ipa_lower_emutls (void)
739{
098f44bc 740 varpool_node *var;
347a47cb 741 cgraph_node *func;
cffbbb9d 742 bool any_aliases = false;
743 tree ctor_body = NULL;
cffbbb9d 744
347a47cb 745 auto_vec <varpool_node *> tls_vars;
cffbbb9d 746
747 /* Examine all global variables for TLS variables. */
7c455d87 748 FOR_EACH_VARIABLE (var)
02774f2d 749 if (DECL_THREAD_LOCAL_P (var->decl))
cffbbb9d 750 {
02774f2d 751 gcc_checking_assert (TREE_STATIC (var->decl)
752 || DECL_EXTERNAL (var->decl));
347a47cb 753 tls_vars.safe_push (var);
02774f2d 754 if (var->alias && var->definition)
347a47cb 755 tls_vars.safe_push (var->ultimate_alias_target ());
cffbbb9d 756 }
757
758 /* If we found no TLS variables, then there is no further work to do. */
347a47cb 759 if (tls_vars.is_empty ())
cffbbb9d 760 {
cffbbb9d 761 if (dump_file)
762 fprintf (dump_file, "No TLS variables found.\n");
763 return 0;
764 }
765
347a47cb 766 tls_map = new hash_map <varpool_node *, tls_var_data> ();
cffbbb9d 767
768 /* Create the control variables for each TLS variable. */
347a47cb 769 for (unsigned i = 0; i < tls_vars.length (); i++)
cffbbb9d 770 {
347a47cb 771 var = tls_vars[i];
cffbbb9d 772
02774f2d 773 if (var->alias && !var->analyzed)
478e80e0 774 any_aliases = true;
02774f2d 775 else if (!var->alias)
97221fd7 776 var->call_for_node_and_aliases (create_emultls_var, &ctor_body, true);
cffbbb9d 777 }
778
779 /* If there were any aliases, then frob the alias_pairs vector. */
780 if (any_aliases)
781 {
782 alias_pair *p;
347a47cb 783 unsigned int i;
f1f41a6c 784 FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
cffbbb9d 785 if (DECL_THREAD_LOCAL_P (p->decl))
786 {
347a47cb 787 p->decl = tls_map->get
788 (varpool_node::get (p->decl))->control_var->decl;
cffbbb9d 789 p->target = get_emutls_object_name (p->target);
790 }
791 }
792
793 /* Adjust all uses of TLS variables within the function bodies. */
7c455d87 794 FOR_EACH_DEFINED_FUNCTION (func)
da751785 795 if (func->lowered)
cffbbb9d 796 lower_emutls_function_body (func);
797
798 /* Generate the constructor for any COMMON control variables created. */
799 if (ctor_body)
800 cgraph_build_static_cdtor ('I', ctor_body, DEFAULT_INIT_PRIORITY);
801
347a47cb 802 delete tls_map;
cffbbb9d 803
8b88439e 804 return 0;
cffbbb9d 805}
806
cbe8bda8 807namespace {
808
809const pass_data pass_data_ipa_lower_emutls =
cffbbb9d 810{
cbe8bda8 811 SIMPLE_IPA_PASS, /* type */
812 "emutls", /* name */
813 OPTGROUP_NONE, /* optinfo_flags */
cbe8bda8 814 TV_IPA_OPT, /* tv_id */
815 ( PROP_cfg | PROP_ssa ), /* properties_required */
816 0, /* properties_provided */
817 0, /* properties_destroyed */
818 0, /* todo_flags_start */
819 0, /* todo_flags_finish */
cffbbb9d 820};
cbe8bda8 821
822class pass_ipa_lower_emutls : public simple_ipa_opt_pass
823{
824public:
9af5ce0c 825 pass_ipa_lower_emutls (gcc::context *ctxt)
826 : simple_ipa_opt_pass (pass_data_ipa_lower_emutls, ctxt)
cbe8bda8 827 {}
828
829 /* opt_pass methods: */
31315c24 830 virtual bool gate (function *)
831 {
832 /* If the target supports TLS natively, we need do nothing here. */
833 return !targetm.have_tls;
834 }
835
65b0537f 836 virtual unsigned int execute (function *) { return ipa_lower_emutls (); }
cbe8bda8 837
838}; // class pass_ipa_lower_emutls
839
840} // anon namespace
841
842simple_ipa_opt_pass *
843make_pass_ipa_lower_emutls (gcc::context *ctxt)
844{
845 return new pass_ipa_lower_emutls (ctxt);
846}