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