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