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