Repository navigation
Expand file tree
/
Copy pathTLSLocality.cpp
More file actions
323 lines (279 loc) · 10.2 KB
/
Copy pathTLSLocality.cpp
File metadata and controls
323 lines (279 loc) · 10.2 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include <map>
#include <set>
#include <string>
#include <unordered_set>
#include <vector>
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/PassManager.h"
#include "llvm/Support/raw_ostream.h"
// #include "llvm/Transforms/Utils/TLSLocality.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/LoopPass.h"
#include "llvm/IR/DebugLoc.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalAlias.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Instructions.h"
#include "llvm/Pass.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Utils/LoopSimplify.h"
using namespace llvm;
#define HEATNUM 0
bool skipLoop(Loop *L);
bool isTLSVariable(Value *V);
void debugInfo(int tabs, std::string message);
int handleAliasgisters(Loop *L, std::set<std::string> &AliasRegs);
std::string getRegName(std::string name);
std::vector<std::string> globalRegs = {
"RAX_", "RBX_", "RCX_", "RDX_", "RSI_", "RDI_", "RBP_", "R8_",
"R9_", "R10_", "R11_", "R12_", "R13_", "R14_", "R15_", "CF_",
"PF_", "AF_", "ZF_", "SF_", "OF_", "RIP_", "RSP_"};
std::unordered_set<std::string> skipCalls = {"ext_",
"sub_",
"__mcsema_init_reg_state",
"__sysv_signal",
"sysv_signal",
"signal",
"ssignal",
"sigset",
"atexit",
"at_quick_exit",
"on_exit",
"bsearch",
"qsort",
"qsort_r",
"clone",
"pthread_create",
"pthread_once",
"pthread_key_create",
"pthread_atfork",
"scandir",
"scandir64",
"scandirat",
"scandirat64",
"_obstack_begin",
"_obstack_begin_1",
"glob",
"glob64",
"ext_5267c0_qsort"};
PreservedAnalyses TLSLocalityPass::run(Function &F,
FunctionAnalysisManager &AM) {
outs() << "into Function: " << F.getName() << "\n";
LoopInfo &LI = AM.getResult<LoopAnalysis>(F);
for (auto &L : LI) {
if (!L->isLoopSimplifyForm()) {
LoopSimplifyPass LS;
LS.run(F, AM);
}
}
for (Loop *L : LI) {
debugInfo(0, "find loop");
if (skipLoop(L)) {
debugInfo(1, "skip loop");
continue;
}
std::set<std::string> AliasRegs;
int regCnt = handleAliasgisters(L, AliasRegs);
debugInfo(2, std::to_string(regCnt) + " alias regs");
DenseMap<Value *, AllocaInst *> VariableMap;
BasicBlock *Preheader = L->getLoopPreheader();
BasicBlock *ExitBlock = L->getExitBlock();
if (!Preheader || !ExitBlock) {
debugInfo(1, "invalid loop");
continue;
}
IRBuilder<> Builder(Preheader->getTerminator());
DenseMap<Value *, int> HeatMap;
for (BasicBlock *BB : L->blocks()) {
if (!BB) {
continue;
}
for (Instruction &I : *BB) {
for (unsigned int i = 0; i < I.getNumOperands(); i++) {
Value *TLSGV = I.getOperand(i);
if (isTLSVariable(TLSGV)) {
if (HeatMap.count(TLSGV)) {
HeatMap[TLSGV]++;
} else {
HeatMap[TLSGV] = 1;
}
}
}
}
}
for (BasicBlock *BB : L->blocks()) {
if (!BB) {
debugInfo(1, "Error: null BasicBlock");
continue;
}
for (Instruction &I : *BB) {
if (!&I) {
debugInfo(2, "Error: null Instruction");
continue;
}
if (LoadInst *LI = dyn_cast<LoadInst>(&I)) {
Value *TLSGV = LI->getPointerOperand();
if (HeatMap.count(TLSGV) < HEATNUM) {
continue;
}
if (isTLSVariable(TLSGV)) {
debugInfo(2,
"TSL " + TLSGV->getName().str() + " in Load instruction");
if (AliasRegs.count(getRegName(TLSGV->getName().str())) > 0) {
debugInfo(2, "alias register");
continue;
}
if (!VariableMap.count(TLSGV)) {
AllocaInst *LV = Builder.CreateAlloca(
LI->getType(), nullptr, TLSGV->getName() + "_local");
Builder.CreateStore(Builder.CreateLoad(LI->getType(), TLSGV), LV);
VariableMap[TLSGV] = LV;
}
LI->setOperand(0, VariableMap[TLSGV]);
debugInfo(3, "success change in load");
}
} else if (StoreInst *SI = dyn_cast<StoreInst>(&I)) {
Value *TLSGV = SI->getPointerOperand();
if (HeatMap.count(TLSGV) < HEATNUM) {
continue;
}
if (isTLSVariable(TLSGV)) {
debugInfo(2, "TSLGV " + TLSGV->getName().str() +
" in Store instruction");
if (AliasRegs.count(getRegName(TLSGV->getName().str())) > 0) {
debugInfo(2, "alias register");
continue;
}
if (!VariableMap.count(TLSGV)) {
debugInfo(3, "store before load");
AllocaInst *LV = Builder.CreateAlloca(
SI->getValueOperand()->getType(), nullptr,
"__" + TLSGV->getName() + "_local");
Builder.CreateStore(
Builder.CreateLoad(SI->getValueOperand()->getType(), TLSGV),
LV);
VariableMap[TLSGV] = LV;
}
SI->setOperand(1, VariableMap[TLSGV]);
debugInfo(3, "success change in store");
}
} else if (PHINode *Phi = dyn_cast<PHINode>(&I)) {
for (unsigned i = 0; i < Phi->getNumIncomingValues(); ++i) {
if (Value *GA = Phi->getIncomingValue(i)) {
if (isTLSVariable(GA) && (HeatMap.count(GA) >= HEATNUM)) {
if (AliasRegs.count(getRegName(GA->getName().str())) > 0) {
debugInfo(2, "alias register");
continue;
}
if (!VariableMap.count(GA)) {
debugInfo(3, "phi node before load and store");
AllocaInst *LV = Builder.CreateAlloca(
Phi->getType(), nullptr, "__" + GA->getName() + "_local");
debugInfo(3, "new local variable for " + GA->getName().str());
Builder.CreateStore(Builder.CreateLoad(Phi->getType(), GA),
LV);
VariableMap[GA] = LV;
}
Phi->setIncomingValue(i, VariableMap[GA]);
debugInfo(3, "success change in phi");
}
}
}
}
}
}
SmallVector<BasicBlock *> ExitBlocks;
L->getExitBlocks(ExitBlocks);
for (BasicBlock *ExitBlock : ExitBlocks) {
if (!ExitBlock) {
debugInfo(1, "Error: null ExitBlock");
continue;
}
IRBuilder<> ExitBLockBuilder(ExitBlock->getFirstNonPHI());
for (auto &Entry : VariableMap) {
Value *global = Entry.first;
AllocaInst *local = Entry.second;
ExitBLockBuilder.CreateStore(
ExitBLockBuilder.CreateLoad(local->getType(), local), global);
}
}
}
return PreservedAnalyses::all();
}
bool TLSLocalityPass::isTLSVariable(Value *V) {
StringRef name = V->getName();
for (auto prefix : globalRegs) {
if (name.starts_with(prefix)) {
return true;
}
}
return false;
}
bool TLSLocalityPass::skipLoop(Loop *L) {
if (!L->getSubLoops().empty()) {
return true;
}
for (BasicBlock *BB : L->blocks()) {
for (Instruction &I : *BB) {
if (CallBase *CB = dyn_cast<CallBase>(&I)) {
if (Function *calledFunction = CB->getCalledFunction()) {
StringRef functionName = calledFunction->getName();
for (auto &forbiddenFuncName : skipCalls) {
if (functionName.contains(forbiddenFuncName)) {
return true;
}
}
}
}
}
}
return false;
}
void TLSLocalityPass::debugInfo(int tabs, std::string message) {
for (int i = 0; i < tabs; i++) {
outs() << "\t";
}
outs() << message << "\n";
}
int TLSLocalityPass::handleAliasgisters(Loop *L,
std::set<std::string> &AliasRegs) {
std::map<std::string, Type *> registerType;
for (BasicBlock *BB : L->blocks()) {
for (Instruction &I : *BB) {
for (Use &U : I.operands()) {
Value *V = U.get();
if (isTLSVariable(V)) {
std::string RegisterName = getRegName(V->getName().str());
Type *VType = I.getType();
if (llvm::StoreInst *SI = llvm::dyn_cast<llvm::StoreInst>(&I)) {
VType = SI->getValueOperand()->getType();
}
if (registerType.count(RegisterName)) {
if (registerType[RegisterName] != VType) {
AliasRegs.insert(RegisterName);
}
} else {
registerType[RegisterName] = VType;
}
}
}
}
}
debugInfo(2, "found forbidden registers:");
for (const std::string ® : AliasRegs) {
errs() << reg << " ";
}
errs() << "\n";
debugInfo(2, "got " + std::to_string(registerType.size() - AliasRegs.size()) +
" registers to optimize");
return AliasRegs.size();
}
std::string TLSLocalityPass::getRegName(std::string name) {
if (name.length() >= 3) {
return name.substr(0, 3);
debugInfo(3, "getRegName found name way too short");
return name;
}
}