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