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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
|
//===-- PoolAllocate.cpp - Pool Allocation Pass ---------------------------===//
//
// This transform changes programs so that disjoint data structures are
// allocated out of different pools of memory, increasing locality.
//
//===----------------------------------------------------------------------===//
#include "llvm/Transforms/PoolAllocate.h"
#include "llvm/Transforms/Utils/Cloning.h"
#include "llvm/Analysis/DataStructure.h"
#include "llvm/Analysis/DSGraph.h"
#include "llvm/Module.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Constants.h"
#include "llvm/Instructions.h"
#include "llvm/Target/TargetData.h"
#include "llvm/Support/InstVisitor.h"
#include "Support/Statistic.h"
#include "Support/VectorExtras.h"
using namespace PA;
namespace {
const Type *VoidPtrTy = PointerType::get(Type::SByteTy);
// The type to allocate for a pool descriptor: { sbyte*, uint }
const Type *PoolDescType =
StructType::get(make_vector<const Type*>(VoidPtrTy, Type::UIntTy, 0));
const PointerType *PoolDescPtr = PointerType::get(PoolDescType);
RegisterOpt<PoolAllocate>
X("poolalloc", "Pool allocate disjoint data structures");
}
void PoolAllocate::getAnalysisUsage(AnalysisUsage &AU) const {
AU.addRequired<BUDataStructures>();
AU.addRequired<TargetData>();
}
bool PoolAllocate::run(Module &M) {
if (M.begin() == M.end()) return false;
CurModule = &M;
AddPoolPrototypes();
BU = &getAnalysis<BUDataStructures>();
std::map<Function*, Function*> FuncMap;
// Loop over only the function initially in the program, don't traverse newly
// added ones. If the function uses memory, make it's clone.
Module::iterator LastOrigFunction = --M.end();
for (Module::iterator I = M.begin(); ; ++I) {
if (!I->isExternal())
if (Function *R = MakeFunctionClone(*I))
FuncMap[I] = R;
if (I == LastOrigFunction) break;
}
++LastOrigFunction;
// Now that all call targets are available, rewrite the function bodies of the
// clones.
for (Module::iterator I = M.begin(); I != LastOrigFunction; ++I)
if (!I->isExternal()) {
std::map<Function*, Function*>::iterator FI = FuncMap.find(I);
ProcessFunctionBody(*I, FI != FuncMap.end() ? *FI->second : *I);
}
FunctionInfo.clear();
return true;
}
// AddPoolPrototypes - Add prototypes for the pool functions to the specified
// module and update the Pool* instance variables to point to them.
//
void PoolAllocate::AddPoolPrototypes() {
CurModule->addTypeName("PoolDescriptor", PoolDescType);
// Get poolinit function...
FunctionType *PoolInitTy =
FunctionType::get(Type::VoidTy,
make_vector<const Type*>(PoolDescPtr, Type::UIntTy, 0),
false);
PoolInit = CurModule->getOrInsertFunction("poolinit", PoolInitTy);
// Get pooldestroy function...
std::vector<const Type*> PDArgs(1, PoolDescPtr);
FunctionType *PoolDestroyTy =
FunctionType::get(Type::VoidTy, PDArgs, false);
PoolDestroy = CurModule->getOrInsertFunction("pooldestroy", PoolDestroyTy);
// Get the poolalloc function...
FunctionType *PoolAllocTy = FunctionType::get(VoidPtrTy, PDArgs, false);
PoolAlloc = CurModule->getOrInsertFunction("poolalloc", PoolAllocTy);
// Get the poolfree function...
PDArgs.push_back(VoidPtrTy); // Pointer to free
FunctionType *PoolFreeTy = FunctionType::get(Type::VoidTy, PDArgs, false);
PoolFree = CurModule->getOrInsertFunction("poolfree", PoolFreeTy);
#if 0
Args[0] = Type::UIntTy; // Number of slots to allocate
FunctionType *PoolAllocArrayTy = FunctionType::get(VoidPtrTy, Args, true);
PoolAllocArray = CurModule->getOrInsertFunction("poolallocarray",
PoolAllocArrayTy);
#endif
}
// MakeFunctionClone - If the specified function needs to be modified for pool
// allocation support, make a clone of it, adding additional arguments as
// neccesary, and return it. If not, just return null.
//
Function *PoolAllocate::MakeFunctionClone(Function &F) {
DSGraph &G = BU->getDSGraph(F);
std::vector<DSNode*> &Nodes = G.getNodes();
if (Nodes.empty()) return 0; // No memory activity, nothing is required
FuncInfo &FI = FunctionInfo[&F]; // Create a new entry for F
FI.Clone = 0;
// Find DataStructure nodes which are allocated in pools non-local to the
// current function. This set will contain all of the DSNodes which require
// pools to be passed in from outside of the function.
hash_set<DSNode*> &MarkedNodes = FI.MarkedNodes;
// Mark globals and incomplete nodes as live... (this handles arguments)
if (F.getName() != "main")
for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
if (Nodes[i]->NodeType & (DSNode::GlobalNode | DSNode::Incomplete) &&
Nodes[i]->NodeType & (DSNode::HeapNode))
Nodes[i]->markReachableNodes(MarkedNodes);
// Marked the returned node as alive...
G.getRetNode().getNode()->markReachableNodes(MarkedNodes);
if (MarkedNodes.empty()) // We don't need to clone the function if there
return 0; // are no incoming arguments to be added.
// Figure out what the arguments are to be for the new version of the function
const FunctionType *OldFuncTy = F.getFunctionType();
std::vector<const Type*> ArgTys;
ArgTys.reserve(OldFuncTy->getParamTypes().size() + MarkedNodes.size());
FI.ArgNodes.reserve(MarkedNodes.size());
for (hash_set<DSNode*>::iterator I = MarkedNodes.begin(),
E = MarkedNodes.end(); I != E; ++I)
if ((*I)->NodeType & DSNode::Incomplete) {
ArgTys.push_back(PoolDescPtr); // Add the appropriate # of pool descs
FI.ArgNodes.push_back(*I);
}
if (FI.ArgNodes.empty()) return 0; // No nodes to be pool allocated!
ArgTys.insert(ArgTys.end(), OldFuncTy->getParamTypes().begin(),
OldFuncTy->getParamTypes().end());
// Create the new function prototype
FunctionType *FuncTy = FunctionType::get(OldFuncTy->getReturnType(), ArgTys,
OldFuncTy->isVarArg());
// Create the new function...
Function *New = new Function(FuncTy, true, F.getName(), F.getParent());
// Set the rest of the new arguments names to be PDa<n> and add entries to the
// pool descriptors map
std::map<DSNode*, Value*> &PoolDescriptors = FI.PoolDescriptors;
Function::aiterator NI = New->abegin();
for (unsigned i = 0, e = FI.ArgNodes.size(); i != e; ++i, ++NI) {
NI->setName("PDa"); // Add pd entry
PoolDescriptors.insert(std::make_pair(FI.ArgNodes[i], NI));
}
// Map the existing arguments of the old function to the corresponding
// arguments of the new function.
std::map<const Value*, Value*> ValueMap;
for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I, ++NI) {
ValueMap[I] = NI;
NI->setName(I->getName());
}
// Populate the value map with all of the globals in the program.
// FIXME: This should be unneccesary!
Module &M = *F.getParent();
for (Module::iterator I = M.begin(), E=M.end(); I!=E; ++I) ValueMap[I] = I;
for (Module::giterator I = M.gbegin(), E=M.gend(); I!=E; ++I) ValueMap[I] = I;
// Perform the cloning.
std::vector<ReturnInst*> Returns;
|