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