1 //===- LowerEmuTLS.cpp - Add __emutls_[vt].* variables --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This transformation is required for targets depending on libgcc style
11 // emulated thread local storage variables. For every defined TLS variable xyz,
12 // an __emutls_v.xyz is generated. If there is non-zero initialized value
13 // an __emutls_t.xyz is also generated.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/CodeGen/Passes.h"
19 #include "llvm/IR/LLVMContext.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/Pass.h"
22 #include "llvm/Target/TargetLowering.h"
26 #define DEBUG_TYPE "loweremutls"
30 class LowerEmuTLS : public ModulePass {
31 const TargetMachine *TM;
33 static char ID; // Pass identification, replacement for typeid
34 explicit LowerEmuTLS() : ModulePass(ID), TM(nullptr) { }
35 explicit LowerEmuTLS(const TargetMachine *TM)
36 : ModulePass(ID), TM(TM) {
37 initializeLowerEmuTLSPass(*PassRegistry::getPassRegistry());
39 bool runOnModule(Module &M) override;
41 bool addEmuTlsVar(Module &M, const GlobalVariable *GV);
42 static void copyLinkageVisibility(Module &M,
43 const GlobalVariable *from,
45 to->setLinkage(from->getLinkage());
46 to->setVisibility(from->getVisibility());
47 if (from->hasComdat()) {
48 to->setComdat(M.getOrInsertComdat(to->getName()));
49 to->getComdat()->setSelectionKind(from->getComdat()->getSelectionKind());
55 char LowerEmuTLS::ID = 0;
57 INITIALIZE_PASS(LowerEmuTLS, "loweremutls",
58 "Add __emutls_[vt]. variables for emultated TLS model",
61 ModulePass *llvm::createLowerEmuTLSPass(const TargetMachine *TM) {
62 return new LowerEmuTLS(TM);
65 bool LowerEmuTLS::runOnModule(Module &M) {
69 if (!TM || !TM->Options.EmulatedTLS)
73 SmallVector<const GlobalVariable*, 8> TlsVars;
74 for (const auto &G : M.globals()) {
75 if (G.isThreadLocal())
78 for (const auto G : TlsVars)
79 Changed |= addEmuTlsVar(M, G);
83 bool LowerEmuTLS::addEmuTlsVar(Module &M, const GlobalVariable *GV) {
84 LLVMContext &C = M.getContext();
85 PointerType *VoidPtrType = Type::getInt8PtrTy(C);
87 std::string EmuTlsVarName = ("__emutls_v." + GV->getName()).str();
88 GlobalVariable *EmuTlsVar = M.getNamedGlobal(EmuTlsVarName);
90 return false; // It has been added before.
92 const DataLayout &DL = M.getDataLayout();
93 Constant *NullPtr = ConstantPointerNull::get(VoidPtrType);
95 // Get non-zero initializer from GV's initializer.
96 const Constant *InitValue = nullptr;
97 if (GV->hasInitializer()) {
98 InitValue = GV->getInitializer();
99 const ConstantInt *InitIntValue = dyn_cast<ConstantInt>(InitValue);
100 // When GV's init value is all 0, omit the EmuTlsTmplVar and let
101 // the emutls library function to reset newly allocated TLS variables.
102 if (isa<ConstantAggregateZero>(InitValue) ||
103 (InitIntValue && InitIntValue->isZero()))
107 // Create the __emutls_v. symbol, whose type has 4 fields:
108 // word size; // size of GV in bytes
109 // word align; // alignment of GV
110 // void *ptr; // initialized to 0; set at run time per thread.
111 // void *templ; // 0 or point to __emutls_t.*
112 // sizeof(word) should be the same as sizeof(void*) on target.
113 IntegerType *WordType = DL.getIntPtrType(C);
114 PointerType *InitPtrType = InitValue ?
115 PointerType::getUnqual(InitValue->getType()) : VoidPtrType;
116 Type *ElementTypes[4] = {WordType, WordType, VoidPtrType, InitPtrType};
117 ArrayRef<Type*> ElementTypeArray(ElementTypes, 4);
118 StructType *EmuTlsVarType = StructType::create(ElementTypeArray);
119 EmuTlsVar = cast<GlobalVariable>(
120 M.getOrInsertGlobal(EmuTlsVarName, EmuTlsVarType));
121 copyLinkageVisibility(M, GV, EmuTlsVar);
123 // Define "__emutls_t.*" and "__emutls_v.*" only if GV is defined.
124 if (!GV->hasInitializer())
127 Type *GVType = GV->getValueType();
128 unsigned GVAlignment = GV->getAlignment();
130 // When LLVM IL declares a variable without alignment, use
131 // the ABI default alignment for the type.
132 GVAlignment = DL.getABITypeAlignment(GVType);
135 // Define "__emutls_t.*" if there is InitValue
136 GlobalVariable *EmuTlsTmplVar = nullptr;
138 std::string EmuTlsTmplName = ("__emutls_t." + GV->getName()).str();
139 EmuTlsTmplVar = dyn_cast_or_null<GlobalVariable>(
140 M.getOrInsertGlobal(EmuTlsTmplName, GVType));
141 assert(EmuTlsTmplVar && "Failed to create emualted TLS initializer");
142 EmuTlsTmplVar->setConstant(true);
143 EmuTlsTmplVar->setInitializer(const_cast<Constant*>(InitValue));
144 EmuTlsTmplVar->setAlignment(GVAlignment);
145 copyLinkageVisibility(M, GV, EmuTlsTmplVar);
148 // Define "__emutls_v.*" with initializer and alignment.
149 Constant *ElementValues[4] = {
150 ConstantInt::get(WordType, DL.getTypeStoreSize(GVType)),
151 ConstantInt::get(WordType, GVAlignment),
152 NullPtr, EmuTlsTmplVar ? EmuTlsTmplVar : NullPtr
154 ArrayRef<Constant*> ElementValueArray(ElementValues, 4);
155 EmuTlsVar->setInitializer(
156 ConstantStruct::get(EmuTlsVarType, ElementValueArray));
157 unsigned MaxAlignment = std::max(
158 DL.getABITypeAlignment(WordType),
159 DL.getABITypeAlignment(VoidPtrType));
160 EmuTlsVar->setAlignment(MaxAlignment);