blob: 395fe9e7f36f525049a1c1de56d0ad0299f9ccb7 [file] [log] [blame]
Chris Lattner64fd9352002-03-28 18:08:31 +00001//===-- PoolAllocate.cpp - Pool Allocation Pass ---------------------------===//
2//
3// This transform changes programs so that disjoint data structures are
4// allocated out of different pools of memory, increasing locality and shrinking
5// pointer size.
6//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/Transforms/IPO/PoolAllocate.h"
Chris Lattner291a1b12002-03-29 19:05:48 +000010#include "llvm/Transforms/CloneFunction.h"
Chris Lattner64fd9352002-03-28 18:08:31 +000011#include "llvm/Analysis/DataStructure.h"
12#include "llvm/Pass.h"
Chris Lattner175f37c2002-03-29 03:40:59 +000013#include "llvm/Module.h"
14#include "llvm/Function.h"
15#include "llvm/iMemory.h"
Chris Lattnere0618ca2002-03-29 05:50:20 +000016#include "llvm/iTerminators.h"
17#include "llvm/iOther.h"
18#include "llvm/ConstantVals.h"
19#include "llvm/Target/TargetData.h"
Chris Lattnerf32d65d2002-03-29 21:25:19 +000020#include "llvm/Support/InstVisitor.h"
Chris Lattnere0618ca2002-03-29 05:50:20 +000021#include "Support/STLExtras.h"
Chris Lattner175f37c2002-03-29 03:40:59 +000022#include <algorithm>
Chris Lattner64fd9352002-03-28 18:08:31 +000023
Chris Lattner692ad5d2002-03-29 17:13:46 +000024
Chris Lattnere0618ca2002-03-29 05:50:20 +000025// FIXME: This is dependant on the sparc backend layout conventions!!
26static TargetData TargetData("test");
27
Chris Lattner64fd9352002-03-28 18:08:31 +000028namespace {
Chris Lattner692ad5d2002-03-29 17:13:46 +000029 // ScalarInfo - Information about an LLVM value that we know points to some
30 // datastructure we are processing.
31 //
32 struct ScalarInfo {
33 Value *Val; // Scalar value in Current Function
34 AllocDSNode *AllocNode; // Allocation node it points to
35 Value *PoolHandle; // PoolTy* LLVM value
36
37 ScalarInfo(Value *V, AllocDSNode *AN, Value *PH)
38 : Val(V), AllocNode(AN), PoolHandle(PH) {}
39 };
40
41 // TransformFunctionInfo - Information about how a function eeds to be
42 // transformed.
43 //
44 struct TransformFunctionInfo {
45 // ArgInfo - Maintain information about the arguments that need to be
46 // processed. Each pair corresponds to an argument (whose number is the
47 // first element) that needs to have a pool pointer (the second element)
48 // passed into the transformed function with it.
49 //
50 // As a special case, "argument" number -1 corresponds to the return value.
51 //
52 vector<pair<int, Value*> > ArgInfo;
53
54 // Func - The function to be transformed...
55 Function *Func;
56
57 // default ctor...
58 TransformFunctionInfo() : Func(0) {}
59
60 inline bool operator<(const TransformFunctionInfo &TFI) const {
Chris Lattner291a1b12002-03-29 19:05:48 +000061 if (Func < TFI.Func) return true;
62 if (Func > TFI.Func) return false;
63
64 // Loop over the arguments, checking to see if only the arg _numbers_ are
65 // less...
66 if (ArgInfo.size() < TFI.ArgInfo.size()) return true;
67 if (ArgInfo.size() > TFI.ArgInfo.size()) return false;
68
69 for (unsigned i = 0, e = TFI.ArgInfo.size(); i != e; ++i) {
70 if (ArgInfo[i].first < TFI.ArgInfo[i].first) return true;
71 if (ArgInfo[i].first > TFI.ArgInfo[i].first) return false;
72 }
73 return false; // They must be equal
Chris Lattner692ad5d2002-03-29 17:13:46 +000074 }
75
76 void finalizeConstruction() {
77 // Sort the vector so that the return value is first, followed by the
78 // argument records, in order.
79 sort(ArgInfo.begin(), ArgInfo.end());
80 }
81 };
82
83
84 // Define the pass class that we implement...
Chris Lattner175f37c2002-03-29 03:40:59 +000085 class PoolAllocate : public Pass {
86 // PoolTy - The type of a scalar value that contains a pool pointer.
87 PointerType *PoolTy;
88 public:
89
90 PoolAllocate() {
91 // Initialize the PoolTy instance variable, since the type never changes.
92 vector<const Type*> PoolElements;
93 PoolElements.push_back(PointerType::get(Type::SByteTy));
94 PoolElements.push_back(Type::UIntTy);
95 PoolTy = PointerType::get(StructType::get(PoolElements));
96 // PoolTy = { sbyte*, uint }*
97
98 CurModule = 0; DS = 0;
99 PoolInit = PoolDestroy = PoolAlloc = PoolFree = 0;
Chris Lattner64fd9352002-03-28 18:08:31 +0000100 }
101
Chris Lattner175f37c2002-03-29 03:40:59 +0000102 bool run(Module *M);
103
104 // getAnalysisUsageInfo - This function requires data structure information
105 // to be able to see what is pool allocatable.
Chris Lattner64fd9352002-03-28 18:08:31 +0000106 //
107 virtual void getAnalysisUsageInfo(Pass::AnalysisSet &Required,
Chris Lattner175f37c2002-03-29 03:40:59 +0000108 Pass::AnalysisSet &,Pass::AnalysisSet &) {
Chris Lattner64fd9352002-03-28 18:08:31 +0000109 Required.push_back(DataStructure::ID);
110 }
Chris Lattner175f37c2002-03-29 03:40:59 +0000111
Chris Lattnerf32d65d2002-03-29 21:25:19 +0000112 public:
Chris Lattner175f37c2002-03-29 03:40:59 +0000113 // CurModule - The module being processed.
114 Module *CurModule;
115
116 // DS - The data structure graph for the module being processed.
117 DataStructure *DS;
118
119 // Prototypes that we add to support pool allocation...
120 Function *PoolInit, *PoolDestroy, *PoolAlloc, *PoolFree;
121
Chris Lattner692ad5d2002-03-29 17:13:46 +0000122 // The map of already transformed functions...
123 map<TransformFunctionInfo, Function*> TransformedFunctions;
124
125 // getTransformedFunction - Get a transformed function, or return null if
126 // the function specified hasn't been transformed yet.
127 //
128 Function *getTransformedFunction(TransformFunctionInfo &TFI) const {
129 map<TransformFunctionInfo, Function*>::const_iterator I =
130 TransformedFunctions.find(TFI);
131 if (I != TransformedFunctions.end()) return I->second;
132 return 0;
133 }
134
135
Chris Lattner175f37c2002-03-29 03:40:59 +0000136 // addPoolPrototypes - Add prototypes for the pool methods to the specified
137 // module and update the Pool* instance variables to point to them.
138 //
139 void addPoolPrototypes(Module *M);
140
Chris Lattner66df97d2002-03-29 06:21:38 +0000141
142 // CreatePools - Insert instructions into the function we are processing to
143 // create all of the memory pool objects themselves. This also inserts
144 // destruction code. Add an alloca for each pool that is allocated to the
145 // PoolDescriptors vector.
146 //
147 void CreatePools(Function *F, const vector<AllocDSNode*> &Allocs,
148 vector<AllocaInst*> &PoolDescriptors);
149
Chris Lattner175f37c2002-03-29 03:40:59 +0000150 // processFunction - Convert a function to use pool allocation where
151 // available.
152 //
153 bool processFunction(Function *F);
Chris Lattner692ad5d2002-03-29 17:13:46 +0000154
155
156 void transformFunctionBody(Function *F, vector<ScalarInfo> &Scalars);
157
158 // transformFunction - Transform the specified function the specified way.
159 // It we have already transformed that function that way, don't do anything.
160 //
161 void transformFunction(TransformFunctionInfo &TFI);
162
Chris Lattner64fd9352002-03-28 18:08:31 +0000163 };
164}
165
Chris Lattner175f37c2002-03-29 03:40:59 +0000166
167
Chris Lattner692ad5d2002-03-29 17:13:46 +0000168// isNotPoolableAlloc - This is a predicate that returns true if the specified
Chris Lattner175f37c2002-03-29 03:40:59 +0000169// allocation node in a data structure graph is eligable for pool allocation.
170//
171static bool isNotPoolableAlloc(const AllocDSNode *DS) {
Chris Lattnere0618ca2002-03-29 05:50:20 +0000172 if (DS->isAllocaNode()) return true; // Do not pool allocate alloca's.
Chris Lattner175f37c2002-03-29 03:40:59 +0000173
174 MallocInst *MI = cast<MallocInst>(DS->getAllocation());
175 if (MI->isArrayAllocation() && !isa<Constant>(MI->getArraySize()))
Chris Lattnere0618ca2002-03-29 05:50:20 +0000176 return true; // Do not allow variable size allocations...
Chris Lattner175f37c2002-03-29 03:40:59 +0000177
Chris Lattnere0618ca2002-03-29 05:50:20 +0000178 return false;
Chris Lattner175f37c2002-03-29 03:40:59 +0000179}
180
Chris Lattner175f37c2002-03-29 03:40:59 +0000181// processFunction - Convert a function to use pool allocation where
182// available.
183//
184bool PoolAllocate::processFunction(Function *F) {
185 // Get the closed datastructure graph for the current function... if there are
186 // any allocations in this graph that are not escaping, we need to pool
187 // allocate them here!
188 //
189 FunctionDSGraph &IPGraph = DS->getClosedDSGraph(F);
190
191 // Get all of the allocations that do not escape the current function. Since
192 // they are still live (they exist in the graph at all), this means we must
193 // have scalar references to these nodes, but the scalars are never returned.
194 //
Chris Lattner692ad5d2002-03-29 17:13:46 +0000195 vector<AllocDSNode*> Allocs;
Chris Lattner175f37c2002-03-29 03:40:59 +0000196 IPGraph.getNonEscapingAllocations(Allocs);
197
198 // Filter out allocations that we cannot handle. Currently, this includes
199 // variable sized array allocations and alloca's (which we do not want to
200 // pool allocate)
201 //
202 Allocs.erase(remove_if(Allocs.begin(), Allocs.end(), isNotPoolableAlloc),
203 Allocs.end());
204
205
206 if (Allocs.empty()) return false; // Nothing to do.
207
Chris Lattner692ad5d2002-03-29 17:13:46 +0000208 // Insert instructions into the function we are processing to create all of
209 // the memory pool objects themselves. This also inserts destruction code.
210 // This fills in the PoolDescriptors vector to be a array parallel with
211 // Allocs, but containing the alloca instructions that allocate the pool ptr.
212 //
213 vector<AllocaInst*> PoolDescriptors;
214 CreatePools(F, Allocs, PoolDescriptors);
215
216
Chris Lattner175f37c2002-03-29 03:40:59 +0000217 // Loop through the value map looking for scalars that refer to nonescaping
Chris Lattner692ad5d2002-03-29 17:13:46 +0000218 // allocations. Add them to the Scalars vector. Note that we may have
219 // multiple entries in the Scalars vector for each value if it points to more
220 // than one object.
Chris Lattner175f37c2002-03-29 03:40:59 +0000221 //
222 map<Value*, PointerValSet> &ValMap = IPGraph.getValueMap();
Chris Lattner692ad5d2002-03-29 17:13:46 +0000223 vector<ScalarInfo> Scalars;
Chris Lattner175f37c2002-03-29 03:40:59 +0000224
225 for (map<Value*, PointerValSet>::iterator I = ValMap.begin(),
226 E = ValMap.end(); I != E; ++I) {
227 const PointerValSet &PVS = I->second; // Set of things pointed to by scalar
Chris Lattner692ad5d2002-03-29 17:13:46 +0000228
229 assert(PVS.size() == 1 &&
230 "Only handle scalars that point to one thing so far!");
231
Chris Lattner175f37c2002-03-29 03:40:59 +0000232 // Check to see if the scalar points to anything that is an allocation...
233 for (unsigned i = 0, e = PVS.size(); i != e; ++i)
234 if (AllocDSNode *Alloc = dyn_cast<AllocDSNode>(PVS[i].Node)) {
235 assert(PVS[i].Index == 0 && "Nonzero not handled yet!");
236
237 // If the allocation is in the nonescaping set...
Chris Lattner692ad5d2002-03-29 17:13:46 +0000238 vector<AllocDSNode*>::iterator AI =
239 find(Allocs.begin(), Allocs.end(), Alloc);
240 if (AI != Allocs.end()) {
241 unsigned IDX = AI-Allocs.begin();
Chris Lattner175f37c2002-03-29 03:40:59 +0000242 // Add it to the list of scalars we have
Chris Lattner692ad5d2002-03-29 17:13:46 +0000243 Scalars.push_back(ScalarInfo(I->first, Alloc, PoolDescriptors[IDX]));
244 }
Chris Lattner175f37c2002-03-29 03:40:59 +0000245 }
246 }
247
Chris Lattner692ad5d2002-03-29 17:13:46 +0000248 // Now we need to figure out what called methods we need to transform, and
249 // how. To do this, we look at all of the scalars, seeing which functions are
250 // either used as a scalar value (so they return a data structure), or are
251 // passed one of our scalar values.
252 //
253 transformFunctionBody(F, Scalars);
254
255 return true;
256}
257
258static void addCallInfo(TransformFunctionInfo &TFI, CallInst *CI, int Arg,
259 Value *PoolHandle) {
260 assert(CI->getCalledFunction() && "Cannot handle indirect calls yet!");
261 TFI.ArgInfo.push_back(make_pair(Arg, PoolHandle));
262
263 assert(TFI.Func == 0 || TFI.Func == CI->getCalledFunction() &&
264 "Function call record should always call the same function!");
265 TFI.Func = CI->getCalledFunction();
266}
267
Chris Lattnerf32d65d2002-03-29 21:25:19 +0000268
269
270class FunctionBodyTransformer : public InstVisitor<FunctionBodyTransformer> {
271 PoolAllocate &PoolAllocator;
272 vector<ScalarInfo> &Scalars;
273 map<CallInst*, TransformFunctionInfo> &CallMap;
274
275 const ScalarInfo &getScalar(const Value *V) {
276 for (unsigned i = 0, e = Scalars.size(); i != e; ++i)
277 if (Scalars[i].Val == V) return Scalars[i];
278 assert(0 && "Scalar not found in getScalar!");
279 abort();
280 return Scalars[0];
281 }
282
283 // updateScalars - Map the scalars array entries that look like 'From' to look
284 // like 'To'.
285 //
286 void updateScalars(Value *From, Value *To) {
287 for (unsigned i = 0, e = Scalars.size(); i != e; ++i)
288 if (Scalars[i].Val == From) Scalars[i].Val = To;
289 }
290
291public:
292 FunctionBodyTransformer(PoolAllocate &PA, vector<ScalarInfo> &S,
293 map<CallInst*, TransformFunctionInfo> &C)
294 : PoolAllocator(PA), Scalars(S), CallMap(C) {}
295
296 void visitMemAccessInst(MemAccessInst *MAI) {
297 // Don't do anything to load, store, or GEP yet...
298 }
299
300 // Convert a malloc instruction into a call to poolalloc
301 void visitMallocInst(MallocInst *I) {
302 const ScalarInfo &SC = getScalar(I);
303 BasicBlock *BB = I->getParent();
304 BasicBlock::iterator MI = find(BB->begin(), BB->end(), I);
305 BB->getInstList().remove(MI); // Remove the Malloc instruction from the BB
306
307 // Create a new call to poolalloc before the malloc instruction
308 vector<Value*> Args;
309 Args.push_back(SC.PoolHandle);
310 CallInst *Call = new CallInst(PoolAllocator.PoolAlloc, Args, I->getName());
311 MI = BB->getInstList().insert(MI, Call)+1;
312
313 // If the type desired is not void*, cast it now...
314 Value *Ptr = Call;
315 if (Call->getType() != I->getType()) {
316 CastInst *CI = new CastInst(Ptr, I->getType(), I->getName());
317 BB->getInstList().insert(MI, CI);
318 Ptr = CI;
319 }
320
321 // Change everything that used the malloc to now use the pool alloc...
322 I->replaceAllUsesWith(Ptr);
323
324 // Update the scalars array...
325 updateScalars(I, Ptr);
326
327 // Delete the instruction now.
328 delete I;
329 }
330
331 // Convert the free instruction into a call to poolfree
332 void visitFreeInst(FreeInst *I) {
333 Value *Ptr = I->getOperand(0);
334 const ScalarInfo &SC = getScalar(Ptr);
335 BasicBlock *BB = I->getParent();
336 BasicBlock::iterator FI = find(BB->begin(), BB->end(), I);
337
338 // If the value is not an sbyte*, convert it now!
339 if (Ptr->getType() != PointerType::get(Type::SByteTy)) {
340 CastInst *CI = new CastInst(Ptr, PointerType::get(Type::SByteTy),
341 Ptr->getName());
342 FI = BB->getInstList().insert(FI, CI)+1;
343 Ptr = CI;
344 }
345
346 // Create a new call to poolfree before the free instruction
347 vector<Value*> Args;
348 Args.push_back(SC.PoolHandle);
349 Args.push_back(Ptr);
350 CallInst *Call = new CallInst(PoolAllocator.PoolFree, Args);
351 FI = BB->getInstList().insert(FI, Call)+1;
352
353 // Remove the old free instruction...
354 delete BB->getInstList().remove(FI);
355 }
356
357 // visitCallInst - Create a new call instruction with the extra arguments for
358 // all of the memory pools that the call needs.
359 //
360 void visitCallInst(CallInst *I) {
361 TransformFunctionInfo &TI = CallMap[I];
362 BasicBlock *BB = I->getParent();
363 BasicBlock::iterator CI = find(BB->begin(), BB->end(), I);
364 BB->getInstList().remove(CI); // Remove the old call instruction
365
366 // Start with all of the old arguments...
367 vector<Value*> Args(I->op_begin()+1, I->op_end());
368
369 // Add all of the pool arguments...
370 for (unsigned i = 0, e = TI.ArgInfo.size(); i != e; ++i)
371 Args.push_back(TI.ArgInfo[i].second);
372
373 Function *NF = PoolAllocator.getTransformedFunction(TI);
374 CallInst *NewCall = new CallInst(NF, Args, I->getName());
375 BB->getInstList().insert(CI, NewCall);
376
377 // Change everything that used the malloc to now use the pool alloc...
378 if (I->getType() != Type::VoidTy) {
379 I->replaceAllUsesWith(NewCall);
380
381 // Update the scalars array...
382 updateScalars(I, NewCall);
383 }
384
385 delete I; // Delete the old call instruction now...
386 }
387
388 void visitInstruction(Instruction *I) {
389 cerr << "Unknown instruction to FunctionBodyTransformer:\n";
390 I->dump();
391 }
392
393};
394
395
Chris Lattner692ad5d2002-03-29 17:13:46 +0000396void PoolAllocate::transformFunctionBody(Function *F,
397 vector<ScalarInfo> &Scalars) {
Chris Lattner175f37c2002-03-29 03:40:59 +0000398 cerr << "In '" << F->getName()
399 << "': Found the following values that point to poolable nodes:\n";
400
401 for (unsigned i = 0, e = Scalars.size(); i != e; ++i)
Chris Lattner692ad5d2002-03-29 17:13:46 +0000402 Scalars[i].Val->dump();
Chris Lattnere0618ca2002-03-29 05:50:20 +0000403
Chris Lattner692ad5d2002-03-29 17:13:46 +0000404 // CallMap - Contain an entry for every call instruction that needs to be
405 // transformed. Each entry in the map contains information about what we need
406 // to do to each call site to change it to work.
407 //
408 map<CallInst*, TransformFunctionInfo> CallMap;
Chris Lattner66df97d2002-03-29 06:21:38 +0000409
Chris Lattner692ad5d2002-03-29 17:13:46 +0000410 // Now we need to figure out what called methods we need to transform, and
411 // how. To do this, we look at all of the scalars, seeing which functions are
412 // either used as a scalar value (so they return a data structure), or are
413 // passed one of our scalar values.
414 //
415 for (unsigned i = 0, e = Scalars.size(); i != e; ++i) {
416 Value *ScalarVal = Scalars[i].Val;
417
418 // Check to see if the scalar _IS_ a call...
419 if (CallInst *CI = dyn_cast<CallInst>(ScalarVal))
420 // If so, add information about the pool it will be returning...
421 addCallInfo(CallMap[CI], CI, -1, Scalars[i].PoolHandle);
422
423 // Check to see if the scalar is an operand to a call...
424 for (Value::use_iterator UI = ScalarVal->use_begin(),
425 UE = ScalarVal->use_end(); UI != UE; ++UI) {
426 if (CallInst *CI = dyn_cast<CallInst>(*UI)) {
427 // Find out which operand this is to the call instruction...
428 User::op_iterator OI = find(CI->op_begin(), CI->op_end(), ScalarVal);
429 assert(OI != CI->op_end() && "Call on use list but not an operand!?");
430 assert(OI != CI->op_begin() && "Pointer operand is call destination?");
431
432 // FIXME: This is broken if the same pointer is passed to a call more
433 // than once! It will get multiple entries for the first pointer.
434
435 // Add the operand number and pool handle to the call table...
Chris Lattner291a1b12002-03-29 19:05:48 +0000436 addCallInfo(CallMap[CI], CI, OI-CI->op_begin()-1,Scalars[i].PoolHandle);
Chris Lattner692ad5d2002-03-29 17:13:46 +0000437 }
438 }
439 }
440
441 // Print out call map...
442 for (map<CallInst*, TransformFunctionInfo>::iterator I = CallMap.begin();
443 I != CallMap.end(); ++I) {
444 cerr << "\nFor call: ";
445 I->first->dump();
446 I->second.finalizeConstruction();
Chris Lattner291a1b12002-03-29 19:05:48 +0000447 cerr << I->second.Func->getName() << " must pass pool pointer for arg #";
Chris Lattner692ad5d2002-03-29 17:13:46 +0000448 for (unsigned i = 0; i < I->second.ArgInfo.size(); ++i)
449 cerr << I->second.ArgInfo[i].first << " ";
450 cerr << "\n";
451 }
452
453 // Loop through all of the call nodes, recursively creating the new functions
454 // that we want to call... This uses a map to prevent infinite recursion and
455 // to avoid duplicating functions unneccesarily.
456 //
457 for (map<CallInst*, TransformFunctionInfo>::iterator I = CallMap.begin(),
458 E = CallMap.end(); I != E; ++I) {
459 // Make sure the entries are sorted.
460 I->second.finalizeConstruction();
461 transformFunction(I->second);
462 }
463
Chris Lattnerf32d65d2002-03-29 21:25:19 +0000464 // Now that all of the functions that we want to call are available, transform
465 // the local method so that it uses the pools locally and passes them to the
466 // functions that we just hacked up.
467 //
468
469 // First step, find the instructions to be modified.
470 vector<Instruction*> InstToFix;
471 for (unsigned i = 0, e = Scalars.size(); i != e; ++i) {
472 Value *ScalarVal = Scalars[i].Val;
473
474 // Check to see if the scalar _IS_ an instruction. If so, it is involved.
475 if (Instruction *Inst = dyn_cast<Instruction>(ScalarVal))
476 InstToFix.push_back(Inst);
477
478 // All all of the instructions that use the scalar as an operand...
479 for (Value::use_iterator UI = ScalarVal->use_begin(),
480 UE = ScalarVal->use_end(); UI != UE; ++UI)
481 InstToFix.push_back(dyn_cast<Instruction>(*UI));
482 }
483
484 // Eliminate duplicates by sorting, then removing equal neighbors.
485 sort(InstToFix.begin(), InstToFix.end());
486 InstToFix.erase(unique(InstToFix.begin(), InstToFix.end()), InstToFix.end());
487
488 // Use a FunctionBodyTransformer to transform all of the involved instructions
489 FunctionBodyTransformer FBT(*this, Scalars, CallMap);
490 for (unsigned i = 0, e = InstToFix.size(); i != e; ++i)
491 FBT.visit(InstToFix[i]);
Chris Lattner692ad5d2002-03-29 17:13:46 +0000492
493
Chris Lattnerf32d65d2002-03-29 21:25:19 +0000494 // Since we have liberally hacked the function to pieces, we want to inform
495 // the datastructure pass that its internal representation is out of date.
496 //
497 DS->invalidateFunction(F);
Chris Lattner692ad5d2002-03-29 17:13:46 +0000498}
499
500
501// transformFunction - Transform the specified function the specified way.
502// It we have already transformed that function that way, don't do anything.
503//
504void PoolAllocate::transformFunction(TransformFunctionInfo &TFI) {
505 if (getTransformedFunction(TFI)) return; // Function xformation already done?
506
Chris Lattner291a1b12002-03-29 19:05:48 +0000507 Function *FuncToXForm = TFI.Func;
508 const FunctionType *OldFuncType = FuncToXForm->getFunctionType();
Chris Lattner692ad5d2002-03-29 17:13:46 +0000509
Chris Lattner291a1b12002-03-29 19:05:48 +0000510 assert(!OldFuncType->isVarArg() && "Vararg functions not handled yet!");
Chris Lattner692ad5d2002-03-29 17:13:46 +0000511
Chris Lattner291a1b12002-03-29 19:05:48 +0000512 // Build the type for the new function that we are transforming
513 vector<const Type*> ArgTys;
514 for (unsigned i = 0, e = OldFuncType->getNumParams(); i != e; ++i)
515 ArgTys.push_back(OldFuncType->getParamType(i));
516
517 // Add one pool pointer for every argument that needs to be supplemented.
518 ArgTys.insert(ArgTys.end(), TFI.ArgInfo.size(), PoolTy);
519
520 // Build the new function type...
521 const // FIXME when types are not const
522 FunctionType *NewFuncType = FunctionType::get(OldFuncType->getReturnType(),
523 ArgTys,OldFuncType->isVarArg());
524
525 // The new function is internal, because we know that only we can call it.
526 // This also helps subsequent IP transformations to eliminate duplicated pool
527 // pointers. [in the future when they are implemented].
528 //
529 Function *NewFunc = new Function(NewFuncType, true,
530 FuncToXForm->getName()+".poolxform");
531 CurModule->getFunctionList().push_back(NewFunc);
532
533 // Add the newly formed function to the TransformedFunctions table so that
534 // infinite recursion does not occur!
535 //
536 TransformedFunctions[TFI] = NewFunc;
537
538 // Add arguments to the function... starting with all of the old arguments
539 vector<Value*> ArgMap;
540 for (unsigned i = 0, e = FuncToXForm->getArgumentList().size(); i != e; ++i) {
541 const FunctionArgument *OFA = FuncToXForm->getArgumentList()[i];
542 FunctionArgument *NFA = new FunctionArgument(OFA->getType(),OFA->getName());
543 NewFunc->getArgumentList().push_back(NFA);
544 ArgMap.push_back(NFA); // Keep track of the arguments
545 }
546
547 // Now add all of the arguments corresponding to pools passed in...
548 for (unsigned i = 0, e = TFI.ArgInfo.size(); i != e; ++i) {
549 string Name;
550 if (TFI.ArgInfo[i].first == -1)
551 Name = "retpool";
552 else
553 Name = ArgMap[TFI.ArgInfo[i].first]->getName(); // Get the arg name
554 FunctionArgument *NFA = new FunctionArgument(PoolTy, Name+".pool");
555 NewFunc->getArgumentList().push_back(NFA);
556 }
557
558 // Now clone the body of the old function into the new function...
559 CloneFunctionInto(NewFunc, FuncToXForm, ArgMap);
560
Chris Lattnerf32d65d2002-03-29 21:25:19 +0000561 // Okay, now we have a function that is identical to the old one, except that
562 // it has extra arguments for the pools coming in.
563
564
Chris Lattner66df97d2002-03-29 06:21:38 +0000565}
566
567
568// CreatePools - Insert instructions into the function we are processing to
569// create all of the memory pool objects themselves. This also inserts
570// destruction code. Add an alloca for each pool that is allocated to the
571// PoolDescriptors vector.
572//
573void PoolAllocate::CreatePools(Function *F, const vector<AllocDSNode*> &Allocs,
574 vector<AllocaInst*> &PoolDescriptors) {
Chris Lattnere0618ca2002-03-29 05:50:20 +0000575 // FIXME: This should use an IP version of the UnifyAllExits pass!
576 vector<BasicBlock*> ReturnNodes;
577 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
578 if (isa<ReturnInst>((*I)->getTerminator()))
579 ReturnNodes.push_back(*I);
580
581
582 // Create the code that goes in the entry and exit nodes for the method...
583 vector<Instruction*> EntryNodeInsts;
584 for (unsigned i = 0, e = Allocs.size(); i != e; ++i) {
585 // Add an allocation and a free for each pool...
586 AllocaInst *PoolAlloc = new AllocaInst(PoolTy, 0, "pool");
587 EntryNodeInsts.push_back(PoolAlloc);
Chris Lattner692ad5d2002-03-29 17:13:46 +0000588 PoolDescriptors.push_back(PoolAlloc); // Keep track of pool allocas
Chris Lattnere0618ca2002-03-29 05:50:20 +0000589 AllocationInst *AI = Allocs[i]->getAllocation();
590
591 // Initialize the pool. We need to know how big each allocation is. For
592 // our purposes here, we assume we are allocating a scalar, or array of
593 // constant size.
594 //
595 unsigned ElSize = TargetData.getTypeSize(AI->getAllocatedType());
596 ElSize *= cast<ConstantUInt>(AI->getArraySize())->getValue();
597
598 vector<Value*> Args;
599 Args.push_back(PoolAlloc); // Pool to initialize
600 Args.push_back(ConstantUInt::get(Type::UIntTy, ElSize));
601 EntryNodeInsts.push_back(new CallInst(PoolInit, Args));
602
603 // Destroy the pool...
604 Args.pop_back();
605
606 for (unsigned EN = 0, ENE = ReturnNodes.size(); EN != ENE; ++EN) {
607 Instruction *Destroy = new CallInst(PoolDestroy, Args);
608
609 // Insert it before the return instruction...
610 BasicBlock *RetNode = ReturnNodes[EN];
611 RetNode->getInstList().insert(RetNode->end()-1, Destroy);
612 }
613 }
614
615 // Insert the entry node code into the entry block...
616 F->getEntryNode()->getInstList().insert(F->getEntryNode()->begin()+1,
617 EntryNodeInsts.begin(),
618 EntryNodeInsts.end());
Chris Lattner175f37c2002-03-29 03:40:59 +0000619}
620
621
Chris Lattner175f37c2002-03-29 03:40:59 +0000622// addPoolPrototypes - Add prototypes for the pool methods to the specified
623// module and update the Pool* instance variables to point to them.
624//
625void PoolAllocate::addPoolPrototypes(Module *M) {
Chris Lattnere0618ca2002-03-29 05:50:20 +0000626 // Get PoolInit function...
627 vector<const Type*> Args;
628 Args.push_back(PoolTy); // Pool to initialize
629 Args.push_back(Type::UIntTy); // Num bytes per element
630 FunctionType *PoolInitTy = FunctionType::get(Type::VoidTy, Args, false);
631 PoolInit = M->getOrInsertFunction("poolinit", PoolInitTy);
Chris Lattner175f37c2002-03-29 03:40:59 +0000632
Chris Lattnere0618ca2002-03-29 05:50:20 +0000633 // Get pooldestroy function...
634 Args.pop_back(); // Only takes a pool...
635 FunctionType *PoolDestroyTy = FunctionType::get(Type::VoidTy, Args, false);
636 PoolDestroy = M->getOrInsertFunction("pooldestroy", PoolDestroyTy);
637
638 const Type *PtrVoid = PointerType::get(Type::SByteTy);
639
640 // Get the poolalloc function...
641 FunctionType *PoolAllocTy = FunctionType::get(PtrVoid, Args, false);
642 PoolAlloc = M->getOrInsertFunction("poolalloc", PoolAllocTy);
643
644 // Get the poolfree function...
645 Args.push_back(PtrVoid);
646 FunctionType *PoolFreeTy = FunctionType::get(Type::VoidTy, Args, false);
647 PoolFree = M->getOrInsertFunction("poolfree", PoolFreeTy);
648
649 // Add the %PoolTy type to the symbol table of the module...
650 M->addTypeName("PoolTy", PoolTy->getElementType());
Chris Lattner175f37c2002-03-29 03:40:59 +0000651}
652
653
654bool PoolAllocate::run(Module *M) {
655 addPoolPrototypes(M);
656 CurModule = M;
657
658 DS = &getAnalysis<DataStructure>();
659 bool Changed = false;
Chris Lattner291a1b12002-03-29 19:05:48 +0000660
661 // We cannot use an iterator here because it will get invalidated when we add
662 // functions to the module later...
663 for (unsigned i = 0; i != M->size(); ++i)
Chris Lattnerf32d65d2002-03-29 21:25:19 +0000664 if (!M->getFunctionList()[i]->isExternal()) {
Chris Lattner291a1b12002-03-29 19:05:48 +0000665 Changed |= processFunction(M->getFunctionList()[i]);
Chris Lattnerf32d65d2002-03-29 21:25:19 +0000666 if (Changed) {
667 cerr << "Only processing one function\n";
668 break;
669 }
670 }
Chris Lattner175f37c2002-03-29 03:40:59 +0000671
672 CurModule = 0;
673 DS = 0;
674 return false;
675}
676
677
678// createPoolAllocatePass - Global function to access the functionality of this
679// pass...
680//
Chris Lattner64fd9352002-03-28 18:08:31 +0000681Pass *createPoolAllocatePass() { return new PoolAllocate(); }