1 //===- LowerEmuTLS.cpp - Add __emutls_[vt].* variables --------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This transformation is required for targets depending on libgcc style
10 // emulated thread local storage variables. For every defined TLS variable xyz,
11 // an __emutls_v.xyz is generated. If there is non-zero initialized value
12 // an __emutls_t.xyz is also generated.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/CodeGen/Passes.h"
18 #include "llvm/CodeGen/TargetPassConfig.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/LLVMContext.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/InitializePasses.h"
23 #include "llvm/Pass.h"
24 #include "llvm/Target/TargetMachine.h"
28 #define DEBUG_TYPE "loweremutls"
32 class LowerEmuTLS
: public ModulePass
{
34 static char ID
; // Pass identification, replacement for typeid
35 LowerEmuTLS() : ModulePass(ID
) {
36 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 to
->setDSOLocal(from
->isDSOLocal());
48 if (from
->hasComdat()) {
49 to
->setComdat(M
.getOrInsertComdat(to
->getName()));
50 to
->getComdat()->setSelectionKind(from
->getComdat()->getSelectionKind());
56 char LowerEmuTLS::ID
= 0;
58 INITIALIZE_PASS(LowerEmuTLS
, DEBUG_TYPE
,
59 "Add __emutls_[vt]. variables for emultated TLS model", false,
62 ModulePass
*llvm::createLowerEmuTLSPass() { return new LowerEmuTLS(); }
64 bool LowerEmuTLS::runOnModule(Module
&M
) {
68 auto *TPC
= getAnalysisIfAvailable
<TargetPassConfig
>();
72 auto &TM
= TPC
->getTM
<TargetMachine
>();
73 if (!TM
.useEmulatedTLS())
77 SmallVector
<const GlobalVariable
*, 8> TlsVars
;
78 for (const auto &G
: M
.globals()) {
79 if (G
.isThreadLocal())
82 for (const auto G
: TlsVars
)
83 Changed
|= addEmuTlsVar(M
, G
);
87 bool LowerEmuTLS::addEmuTlsVar(Module
&M
, const GlobalVariable
*GV
) {
88 LLVMContext
&C
= M
.getContext();
89 PointerType
*VoidPtrType
= Type::getInt8PtrTy(C
);
91 std::string EmuTlsVarName
= ("__emutls_v." + GV
->getName()).str();
92 GlobalVariable
*EmuTlsVar
= M
.getNamedGlobal(EmuTlsVarName
);
94 return false; // It has been added before.
96 const DataLayout
&DL
= M
.getDataLayout();
97 Constant
*NullPtr
= ConstantPointerNull::get(VoidPtrType
);
99 // Get non-zero initializer from GV's initializer.
100 const Constant
*InitValue
= nullptr;
101 if (GV
->hasInitializer()) {
102 InitValue
= GV
->getInitializer();
103 const ConstantInt
*InitIntValue
= dyn_cast
<ConstantInt
>(InitValue
);
104 // When GV's init value is all 0, omit the EmuTlsTmplVar and let
105 // the emutls library function to reset newly allocated TLS variables.
106 if (isa
<ConstantAggregateZero
>(InitValue
) ||
107 (InitIntValue
&& InitIntValue
->isZero()))
111 // Create the __emutls_v. symbol, whose type has 4 fields:
112 // word size; // size of GV in bytes
113 // word align; // alignment of GV
114 // void *ptr; // initialized to 0; set at run time per thread.
115 // void *templ; // 0 or point to __emutls_t.*
116 // sizeof(word) should be the same as sizeof(void*) on target.
117 IntegerType
*WordType
= DL
.getIntPtrType(C
);
118 PointerType
*InitPtrType
= InitValue
?
119 PointerType::getUnqual(InitValue
->getType()) : VoidPtrType
;
120 Type
*ElementTypes
[4] = {WordType
, WordType
, VoidPtrType
, InitPtrType
};
121 ArrayRef
<Type
*> ElementTypeArray(ElementTypes
, 4);
122 StructType
*EmuTlsVarType
= StructType::create(ElementTypeArray
);
123 EmuTlsVar
= cast
<GlobalVariable
>(
124 M
.getOrInsertGlobal(EmuTlsVarName
, EmuTlsVarType
));
125 copyLinkageVisibility(M
, GV
, EmuTlsVar
);
127 // Define "__emutls_t.*" and "__emutls_v.*" only if GV is defined.
128 if (!GV
->hasInitializer())
131 Type
*GVType
= GV
->getValueType();
132 Align GVAlignment
= DL
.getValueOrABITypeAlignment(GV
->getAlign(), GVType
);
134 // Define "__emutls_t.*" if there is InitValue
135 GlobalVariable
*EmuTlsTmplVar
= nullptr;
137 std::string EmuTlsTmplName
= ("__emutls_t." + GV
->getName()).str();
138 EmuTlsTmplVar
= dyn_cast_or_null
<GlobalVariable
>(
139 M
.getOrInsertGlobal(EmuTlsTmplName
, GVType
));
140 assert(EmuTlsTmplVar
&& "Failed to create emualted TLS initializer");
141 EmuTlsTmplVar
->setConstant(true);
142 EmuTlsTmplVar
->setInitializer(const_cast<Constant
*>(InitValue
));
143 EmuTlsTmplVar
->setAlignment(GVAlignment
);
144 copyLinkageVisibility(M
, GV
, EmuTlsTmplVar
);
147 // Define "__emutls_v.*" with initializer and alignment.
148 Constant
*ElementValues
[4] = {
149 ConstantInt::get(WordType
, DL
.getTypeStoreSize(GVType
)),
150 ConstantInt::get(WordType
, GVAlignment
.value()), NullPtr
,
151 EmuTlsTmplVar
? EmuTlsTmplVar
: NullPtr
};
152 ArrayRef
<Constant
*> ElementValueArray(ElementValues
, 4);
153 EmuTlsVar
->setInitializer(
154 ConstantStruct::get(EmuTlsVarType
, ElementValueArray
));
156 std::max(DL
.getABITypeAlign(WordType
), DL
.getABITypeAlign(VoidPtrType
));
157 EmuTlsVar
->setAlignment(MaxAlignment
);