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