1//===- LowerEmuTLS.cpp - Add __emutls_[vt].* variables --------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
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.
13//
14//===----------------------------------------------------------------------===//
15
16#include "llvm/CodeGen/LowerEmuTLS.h"
17#include "llvm/ADT/SmallVector.h"
18#include "llvm/Analysis/GlobalsModRef.h"
19#include "llvm/Analysis/ModuleSummaryAnalysis.h"
20#include "llvm/Analysis/StackSafetyAnalysis.h"
21#include "llvm/CodeGen/Passes.h"
22#include "llvm/CodeGen/TargetPassConfig.h"
23#include "llvm/IR/Constants.h"
24#include "llvm/IR/Module.h"
25#include "llvm/InitializePasses.h"
26#include "llvm/Pass.h"
27#include "llvm/Target/TargetMachine.h"
28
29using namespace llvm;
30
31#define DEBUG_TYPE "lower-emutls"
32
33namespace {
34
35class LowerEmuTLS : public ModulePass {
36public:
37 static char ID; // Pass identification, replacement for typeid
38 LowerEmuTLS() : ModulePass(ID) {}
39
40 bool runOnModule(Module &M) override;
41};
42}
43
44static bool addEmuTlsVar(Module &M, const GlobalVariable *GV);
45
46static void copyLinkageVisibility(Module &M, const GlobalVariable *from,
47 GlobalVariable *to) {
48 to->setLinkage(from->getLinkage());
49 to->setVisibility(from->getVisibility());
50 to->setDSOLocal(from->isDSOLocal());
51 if (from->hasComdat()) {
52 to->setComdat(M.getOrInsertComdat(Name: to->getName()));
53 to->getComdat()->setSelectionKind(from->getComdat()->getSelectionKind());
54 }
55}
56
57PreservedAnalyses LowerEmuTLSPass::run(Module &M, ModuleAnalysisManager &MAM) {
58 bool Changed = false;
59 SmallVector<const GlobalVariable *, 8> TlsVars;
60 for (const auto &G : M.globals()) {
61 if (G.isThreadLocal())
62 TlsVars.push_back(Elt: &G);
63 }
64 for (const auto *G : TlsVars)
65 Changed |= addEmuTlsVar(M, GV: G);
66
67 if (!Changed)
68 return PreservedAnalyses::all();
69 PreservedAnalyses PA = PreservedAnalyses::all();
70 PA.abandon<GlobalsAA>();
71 PA.abandon<ModuleSummaryIndexAnalysis>();
72 PA.abandon<StackSafetyGlobalAnalysis>();
73 return PA;
74}
75
76char LowerEmuTLS::ID = 0;
77
78INITIALIZE_PASS(LowerEmuTLS, DEBUG_TYPE,
79 "Add __emutls_[vt]. variables for emultated TLS model", false,
80 false)
81
82ModulePass *llvm::createLowerEmuTLSPass() { return new LowerEmuTLS(); }
83
84bool LowerEmuTLS::runOnModule(Module &M) {
85 if (skipModule(M))
86 return false;
87
88 auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
89 if (!TPC)
90 return false;
91
92 auto &TM = TPC->getTM<TargetMachine>();
93 if (!TM.useEmulatedTLS())
94 return false;
95
96 bool Changed = false;
97 SmallVector<const GlobalVariable*, 8> TlsVars;
98 for (const auto &G : M.globals()) {
99 if (G.isThreadLocal())
100 TlsVars.append(IL: {&G});
101 }
102 for (const auto *const G : TlsVars)
103 Changed |= addEmuTlsVar(M, GV: G);
104 return Changed;
105}
106
107bool addEmuTlsVar(Module &M, const GlobalVariable *GV) {
108 LLVMContext &C = M.getContext();
109 PointerType *VoidPtrType = PointerType::getUnqual(C);
110
111 std::string EmuTlsVarName = ("__emutls_v." + GV->getName()).str();
112 GlobalVariable *EmuTlsVar = M.getNamedGlobal(Name: EmuTlsVarName);
113 if (EmuTlsVar)
114 return false; // It has been added before.
115
116 const DataLayout &DL = M.getDataLayout();
117 Constant *NullPtr = ConstantPointerNull::get(T: VoidPtrType);
118
119 // Get non-zero initializer from GV's initializer.
120 const Constant *InitValue = nullptr;
121 if (GV->hasInitializer()) {
122 InitValue = GV->getInitializer();
123 const ConstantInt *InitIntValue = dyn_cast<ConstantInt>(Val: InitValue);
124 // When GV's init value is all 0, omit the EmuTlsTmplVar and let
125 // the emutls library function to reset newly allocated TLS variables.
126 if (isa<ConstantAggregateZero>(Val: InitValue) ||
127 (InitIntValue && InitIntValue->isZero()))
128 InitValue = nullptr;
129 }
130
131 // Create the __emutls_v. symbol, whose type has 4 fields:
132 // word size; // size of GV in bytes
133 // word align; // alignment of GV
134 // void *ptr; // initialized to 0; set at run time per thread.
135 // void *templ; // 0 or point to __emutls_t.*
136 // sizeof(word) should be the same as sizeof(void*) on target.
137 IntegerType *WordType = DL.getIntPtrType(C);
138 PointerType *InitPtrType = PointerType::getUnqual(C);
139 Type *ElementTypes[4] = {WordType, WordType, VoidPtrType, InitPtrType};
140 StructType *EmuTlsVarType = StructType::create(Elements: ElementTypes);
141 EmuTlsVar = M.getOrInsertGlobal(Name: EmuTlsVarName, Ty: EmuTlsVarType);
142 copyLinkageVisibility(M, from: GV, to: EmuTlsVar);
143
144 // Define "__emutls_t.*" and "__emutls_v.*" only if GV is defined.
145 if (!GV->hasInitializer())
146 return true;
147
148 Type *GVType = GV->getValueType();
149 Align GVAlignment = DL.getValueOrABITypeAlignment(Alignment: GV->getAlign(), Ty: GVType);
150
151 // Define "__emutls_t.*" if there is InitValue
152 GlobalVariable *EmuTlsTmplVar = nullptr;
153 if (InitValue) {
154 std::string EmuTlsTmplName = ("__emutls_t." + GV->getName()).str();
155 EmuTlsTmplVar = M.getOrInsertGlobal(Name: EmuTlsTmplName, Ty: GVType);
156 assert(EmuTlsTmplVar && "Failed to create emualted TLS initializer");
157 EmuTlsTmplVar->setConstant(true);
158 EmuTlsTmplVar->setInitializer(const_cast<Constant*>(InitValue));
159 EmuTlsTmplVar->setAlignment(GVAlignment);
160 copyLinkageVisibility(M, from: GV, to: EmuTlsTmplVar);
161 }
162
163 // Define "__emutls_v.*" with initializer and alignment.
164 Constant *ElementValues[4] = {
165 ConstantInt::get(Ty: WordType, V: DL.getTypeStoreSize(Ty: GVType)),
166 ConstantInt::get(Ty: WordType, V: GVAlignment.value()), NullPtr,
167 EmuTlsTmplVar ? EmuTlsTmplVar : NullPtr};
168 EmuTlsVar->setInitializer(ConstantStruct::get(T: EmuTlsVarType, V: ElementValues));
169 Align MaxAlignment =
170 std::max(a: DL.getABITypeAlign(Ty: WordType), b: DL.getABITypeAlign(Ty: VoidPtrType));
171 EmuTlsVar->setAlignment(MaxAlignment);
172 return true;
173}
174