Add new optional getPassName() virtual function that a Pass can override
[oota-llvm.git] / lib / Transforms / IPO / OldPoolAllocate.cpp
1 //===-- 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 // This pass requires a DCE & instcombine pass to be run after it for best
8 // results.
9 //
10 //===----------------------------------------------------------------------===//
11
12 #include "llvm/Transforms/IPO/PoolAllocate.h"
13 #include "llvm/Transforms/CloneFunction.h"
14 #include "llvm/Analysis/DataStructure.h"
15 #include "llvm/Analysis/DataStructureGraph.h"
16 #include "llvm/Module.h"
17 #include "llvm/Function.h"
18 #include "llvm/BasicBlock.h"
19 #include "llvm/iMemory.h"
20 #include "llvm/iTerminators.h"
21 #include "llvm/iPHINode.h"
22 #include "llvm/iOther.h"
23 #include "llvm/DerivedTypes.h"
24 #include "llvm/Constants.h"
25 #include "llvm/Target/TargetData.h"
26 #include "llvm/Support/InstVisitor.h"
27 #include "llvm/Argument.h"
28 #include "Support/DepthFirstIterator.h"
29 #include "Support/STLExtras.h"
30 #include <algorithm>
31
32 // DEBUG_CREATE_POOLS - Enable this to turn on debug output for the pool
33 // creation phase in the top level function of a transformed data structure.
34 //
35 //#define DEBUG_CREATE_POOLS 1
36
37 // DEBUG_TRANSFORM_PROGRESS - Enable this to get lots of debug output on what
38 // the transformation is doing.
39 //
40 //#define DEBUG_TRANSFORM_PROGRESS 1
41
42 // DEBUG_POOLBASE_LOAD_ELIMINATOR - Turn this on to get statistics about how
43 // many static loads were eliminated from a function...
44 //
45 #define DEBUG_POOLBASE_LOAD_ELIMINATOR 1
46
47 #include "Support/CommandLine.h"
48 enum PtrSize {
49   Ptr8bits, Ptr16bits, Ptr32bits
50 };
51
52 static cl::Enum<enum PtrSize> ReqPointerSize("ptrsize", 0,
53                                       "Set pointer size for -poolalloc pass",
54   clEnumValN(Ptr32bits, "32", "Use 32 bit indices for pointers"),
55   clEnumValN(Ptr16bits, "16", "Use 16 bit indices for pointers"),
56   clEnumValN(Ptr8bits ,  "8", "Use 8 bit indices for pointers"), 0);
57
58 static cl::Flag DisableRLE("no-pool-load-elim", "Disable pool load elimination after poolalloc pass", cl::Hidden);
59
60 const Type *POINTERTYPE;
61
62 // FIXME: This is dependant on the sparc backend layout conventions!!
63 static TargetData TargetData("test");
64
65 static const Type *getPointerTransformedType(const Type *Ty) {
66   if (PointerType *PT = dyn_cast<PointerType>(Ty)) {
67     return POINTERTYPE;
68   } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
69     vector<const Type *> NewElTypes;
70     NewElTypes.reserve(STy->getElementTypes().size());
71     for (StructType::ElementTypes::const_iterator
72            I = STy->getElementTypes().begin(),
73            E = STy->getElementTypes().end(); I != E; ++I)
74       NewElTypes.push_back(getPointerTransformedType(*I));
75     return StructType::get(NewElTypes);
76   } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
77     return ArrayType::get(getPointerTransformedType(ATy->getElementType()),
78                                                     ATy->getNumElements());
79   } else {
80     assert(Ty->isPrimitiveType() && "Unknown derived type!");
81     return Ty;
82   }
83 }
84
85 namespace {
86   struct PoolInfo {
87     DSNode *Node;           // The node this pool allocation represents
88     Value  *Handle;         // LLVM value of the pool in the current context
89     const Type *NewType;    // The transformed type of the memory objects
90     const Type *PoolType;   // The type of the pool
91
92     const Type *getOldType() const { return Node->getType(); }
93
94     PoolInfo() {  // Define a default ctor for map::operator[]
95       cerr << "Map subscript used to get element that doesn't exist!\n";
96       abort();  // Invalid
97     }
98
99     PoolInfo(DSNode *N, Value *H, const Type *NT, const Type *PT)
100       : Node(N), Handle(H), NewType(NT), PoolType(PT) {
101       // Handle can be null...
102       assert(N && NT && PT && "Pool info null!");
103     }
104
105     PoolInfo(DSNode *N) : Node(N), Handle(0), NewType(0), PoolType(0) {
106       assert(N && "Invalid pool info!");
107
108       // The new type of the memory object is the same as the old type, except
109       // that all of the pointer values are replaced with POINTERTYPE values.
110       NewType = getPointerTransformedType(getOldType());
111     }
112   };
113
114   // ScalarInfo - Information about an LLVM value that we know points to some
115   // datastructure we are processing.
116   //
117   struct ScalarInfo {
118     Value  *Val;            // Scalar value in Current Function
119     PoolInfo Pool;          // The pool the scalar points into
120     
121     ScalarInfo(Value *V, const PoolInfo &PI) : Val(V), Pool(PI) {
122       assert(V && "Null value passed to ScalarInfo ctor!");
123     }
124   };
125
126   // CallArgInfo - Information on one operand for a call that got expanded.
127   struct CallArgInfo {
128     int ArgNo;          // Call argument number this corresponds to
129     DSNode *Node;       // The graph node for the pool
130     Value *PoolHandle;  // The LLVM value that is the pool pointer
131
132     CallArgInfo(int Arg, DSNode *N, Value *PH)
133       : ArgNo(Arg), Node(N), PoolHandle(PH) {
134       assert(Arg >= -1 && N && PH && "Illegal values to CallArgInfo ctor!");
135     }
136
137     // operator< when sorting, sort by argument number.
138     bool operator<(const CallArgInfo &CAI) const {
139       return ArgNo < CAI.ArgNo;
140     }
141   };
142
143   // TransformFunctionInfo - Information about how a function eeds to be
144   // transformed.
145   //
146   struct TransformFunctionInfo {
147     // ArgInfo - Maintain information about the arguments that need to be
148     // processed.  Each CallArgInfo corresponds to an argument that needs to
149     // have a pool pointer passed into the transformed function with it.
150     //
151     // As a special case, "argument" number -1 corresponds to the return value.
152     //
153     vector<CallArgInfo> ArgInfo;
154
155     // Func - The function to be transformed...
156     Function *Func;
157
158     // The call instruction that is used to map CallArgInfo PoolHandle values
159     // into the new function values.
160     CallInst *Call;
161
162     // default ctor...
163     TransformFunctionInfo() : Func(0), Call(0) {}
164     
165     bool operator<(const TransformFunctionInfo &TFI) const {
166       if (Func < TFI.Func) return true;
167       if (Func > TFI.Func) return false;
168       if (ArgInfo.size() < TFI.ArgInfo.size()) return true;
169       if (ArgInfo.size() > TFI.ArgInfo.size()) return false;
170       return ArgInfo < TFI.ArgInfo;
171     }
172
173     void finalizeConstruction() {
174       // Sort the vector so that the return value is first, followed by the
175       // argument records, in order.  Note that this must be a stable sort so
176       // that the entries with the same sorting criteria (ie they are multiple
177       // pool entries for the same argument) are kept in depth first order.
178       stable_sort(ArgInfo.begin(), ArgInfo.end());
179     }
180
181     // addCallInfo - For a specified function call CI, figure out which pool
182     // descriptors need to be passed in as arguments, and which arguments need
183     // to be transformed into indices.  If Arg != -1, the specified call
184     // argument is passed in as a pointer to a data structure.
185     //
186     void addCallInfo(DataStructure *DS, CallInst *CI, int Arg,
187                      DSNode *GraphNode, map<DSNode*, PoolInfo> &PoolDescs);
188
189     // Make sure that all dependant arguments are added to this transformation
190     // info.  For example, if we call foo(null, P) and foo treats it's first and
191     // second arguments as belonging to the same data structure, the we MUST add
192     // entries to know that the null needs to be transformed into an index as
193     // well.
194     //
195     void ensureDependantArgumentsIncluded(DataStructure *DS,
196                                           map<DSNode*, PoolInfo> &PoolDescs);
197   };
198
199
200   // Define the pass class that we implement...
201   struct PoolAllocate : public Pass {
202     const char *getPassName() const { return "Pool Allocate"; }
203
204     PoolAllocate() {
205       switch (ReqPointerSize) {
206       case Ptr32bits: POINTERTYPE = Type::UIntTy; break;
207       case Ptr16bits: POINTERTYPE = Type::UShortTy; break;
208       case Ptr8bits:  POINTERTYPE = Type::UByteTy; break;
209       }
210
211       CurModule = 0; DS = 0;
212       PoolInit = PoolDestroy = PoolAlloc = PoolFree = 0;
213     }
214
215     // getPoolType - Get the type used by the backend for a pool of a particular
216     // type.  This pool record is used to allocate nodes of type NodeType.
217     //
218     // Here, PoolTy = { NodeType*, sbyte*, uint }*
219     //
220     const StructType *getPoolType(const Type *NodeType) {
221       vector<const Type*> PoolElements;
222       PoolElements.push_back(PointerType::get(NodeType));
223       PoolElements.push_back(PointerType::get(Type::SByteTy));
224       PoolElements.push_back(Type::UIntTy);
225       StructType *Result = StructType::get(PoolElements);
226
227       // Add a name to the symbol table to correspond to the backend
228       // representation of this pool...
229       assert(CurModule && "No current module!?");
230       string Name = CurModule->getTypeName(NodeType);
231       if (Name.empty()) Name = CurModule->getTypeName(PoolElements[0]);
232       CurModule->addTypeName(Name+"oolbe", Result);
233
234       return Result;
235     }
236
237     bool run(Module *M);
238
239     // getAnalysisUsage - This function requires data structure information
240     // to be able to see what is pool allocatable.
241     //
242     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
243       AU.addRequired(DataStructure::ID);
244     }
245
246   public:
247     // CurModule - The module being processed.
248     Module *CurModule;
249
250     // DS - The data structure graph for the module being processed.
251     DataStructure *DS;
252
253     // Prototypes that we add to support pool allocation...
254     Function *PoolInit, *PoolDestroy, *PoolAlloc, *PoolAllocArray, *PoolFree;
255
256     // The map of already transformed functions... note that the keys of this
257     // map do not have meaningful values for 'Call' or the 'PoolHandle' elements
258     // of the ArgInfo elements.
259     //
260     map<TransformFunctionInfo, Function*> TransformedFunctions;
261
262     // getTransformedFunction - Get a transformed function, or return null if
263     // the function specified hasn't been transformed yet.
264     //
265     Function *getTransformedFunction(TransformFunctionInfo &TFI) const {
266       map<TransformFunctionInfo, Function*>::const_iterator I =
267         TransformedFunctions.find(TFI);
268       if (I != TransformedFunctions.end()) return I->second;
269       return 0;
270     }
271
272
273     // addPoolPrototypes - Add prototypes for the pool functions to the
274     // specified module and update the Pool* instance variables to point to
275     // them.
276     //
277     void addPoolPrototypes(Module *M);
278
279
280     // CreatePools - Insert instructions into the function we are processing to
281     // create all of the memory pool objects themselves.  This also inserts
282     // destruction code.  Add an alloca for each pool that is allocated to the
283     // PoolDescs map.
284     //
285     void CreatePools(Function *F, const vector<AllocDSNode*> &Allocs,
286                      map<DSNode*, PoolInfo> &PoolDescs);
287
288     // processFunction - Convert a function to use pool allocation where
289     // available.
290     //
291     bool processFunction(Function *F);
292
293     // transformFunctionBody - This transforms the instruction in 'F' to use the
294     // pools specified in PoolDescs when modifying data structure nodes
295     // specified in the PoolDescs map.  IPFGraph is the closed data structure
296     // graph for F, of which the PoolDescriptor nodes come from.
297     //
298     void transformFunctionBody(Function *F, FunctionDSGraph &IPFGraph,
299                                map<DSNode*, PoolInfo> &PoolDescs);
300
301     // transformFunction - Transform the specified function the specified way.
302     // It we have already transformed that function that way, don't do anything.
303     // The nodes in the TransformFunctionInfo come out of callers data structure
304     // graph, and the PoolDescs passed in are the caller's.
305     //
306     void transformFunction(TransformFunctionInfo &TFI,
307                            FunctionDSGraph &CallerIPGraph,
308                            map<DSNode*, PoolInfo> &PoolDescs);
309
310   };
311 }
312
313 // isNotPoolableAlloc - This is a predicate that returns true if the specified
314 // allocation node in a data structure graph is eligable for pool allocation.
315 //
316 static bool isNotPoolableAlloc(const AllocDSNode *DS) {
317   if (DS->isAllocaNode()) return true;  // Do not pool allocate alloca's.
318   return false;
319 }
320
321 // processFunction - Convert a function to use pool allocation where
322 // available.
323 //
324 bool PoolAllocate::processFunction(Function *F) {
325   // Get the closed datastructure graph for the current function... if there are
326   // any allocations in this graph that are not escaping, we need to pool
327   // allocate them here!
328   //
329   FunctionDSGraph &IPGraph = DS->getClosedDSGraph(F);
330
331   // Get all of the allocations that do not escape the current function.  Since
332   // they are still live (they exist in the graph at all), this means we must
333   // have scalar references to these nodes, but the scalars are never returned.
334   // 
335   vector<AllocDSNode*> Allocs;
336   IPGraph.getNonEscapingAllocations(Allocs);
337
338   // Filter out allocations that we cannot handle.  Currently, this includes
339   // variable sized array allocations and alloca's (which we do not want to
340   // pool allocate)
341   //
342   Allocs.erase(remove_if(Allocs.begin(), Allocs.end(), isNotPoolableAlloc),
343                Allocs.end());
344
345
346   if (Allocs.empty()) return false;  // Nothing to do.
347
348 #ifdef DEBUG_TRANSFORM_PROGRESS
349   cerr << "Transforming Function: " << F->getName() << "\n";
350 #endif
351
352   // Insert instructions into the function we are processing to create all of
353   // the memory pool objects themselves.  This also inserts destruction code.
354   // This fills in the PoolDescs map to associate the alloc node with the
355   // allocation of the memory pool corresponding to it.
356   // 
357   map<DSNode*, PoolInfo> PoolDescs;
358   CreatePools(F, Allocs, PoolDescs);
359
360 #ifdef DEBUG_TRANSFORM_PROGRESS
361   cerr << "Transformed Entry Function: \n" << F;
362 #endif
363
364   // Now we need to figure out what called functions we need to transform, and
365   // how.  To do this, we look at all of the scalars, seeing which functions are
366   // either used as a scalar value (so they return a data structure), or are
367   // passed one of our scalar values.
368   //
369   transformFunctionBody(F, IPGraph, PoolDescs);
370
371   return true;
372 }
373
374
375 //===----------------------------------------------------------------------===//
376 //
377 // NewInstructionCreator - This class is used to traverse the function being
378 // modified, changing each instruction visit'ed to use and provide pointer
379 // indexes instead of real pointers.  This is what changes the body of a
380 // function to use pool allocation.
381 //
382 class NewInstructionCreator : public InstVisitor<NewInstructionCreator> {
383   PoolAllocate &PoolAllocator;
384   vector<ScalarInfo> &Scalars;
385   map<CallInst*, TransformFunctionInfo> &CallMap;
386   map<Value*, Value*> &XFormMap;   // Map old pointers to new indexes
387
388   struct RefToUpdate {
389     Instruction *I;       // Instruction to update
390     unsigned     OpNum;   // Operand number to update
391     Value       *OldVal;  // The old value it had
392
393     RefToUpdate(Instruction *i, unsigned o, Value *ov)
394       : I(i), OpNum(o), OldVal(ov) {}
395   };
396   vector<RefToUpdate> ReferencesToUpdate;
397
398   const ScalarInfo &getScalarRef(const Value *V) {
399     for (unsigned i = 0, e = Scalars.size(); i != e; ++i)
400       if (Scalars[i].Val == V) return Scalars[i];
401
402     cerr << "Could not find scalar " << V << " in scalar map!\n";
403     assert(0 && "Scalar not found in getScalar!");
404     abort();
405     return Scalars[0];
406   }
407   
408   const ScalarInfo *getScalar(const Value *V) {
409     for (unsigned i = 0, e = Scalars.size(); i != e; ++i)
410       if (Scalars[i].Val == V) return &Scalars[i];
411     return 0;
412   }
413
414   BasicBlock::iterator ReplaceInstWith(Instruction *I, Instruction *New) {
415     BasicBlock *BB = I->getParent();
416     BasicBlock::iterator RI = find(BB->begin(), BB->end(), I);
417     BB->getInstList().replaceWith(RI, New);
418     XFormMap[I] = New;
419     return RI;
420   }
421
422   LoadInst *createPoolBaseInstruction(Value *PtrVal) {
423     const ScalarInfo &SC = getScalarRef(PtrVal);
424     vector<Value*> Args(3);
425     Args[0] = ConstantUInt::get(Type::UIntTy, 0);  // No pointer offset
426     Args[1] = ConstantUInt::get(Type::UByteTy, 0); // Field #0 of pool descriptr
427     Args[2] = ConstantUInt::get(Type::UByteTy, 0); // Field #0 of poolalloc val
428     return new LoadInst(SC.Pool.Handle, Args, PtrVal->getName()+".poolbase");
429   }
430
431
432 public:
433   NewInstructionCreator(PoolAllocate &PA, vector<ScalarInfo> &S,
434                         map<CallInst*, TransformFunctionInfo> &C,
435                         map<Value*, Value*> &X)
436     : PoolAllocator(PA), Scalars(S), CallMap(C), XFormMap(X) {}
437
438
439   // updateReferences - The NewInstructionCreator is responsible for creating
440   // new instructions to replace the old ones in the function, and then link up
441   // references to values to their new values.  For it to do this, however, it
442   // keeps track of information about the value mapping of old values to new
443   // values that need to be patched up.  Given this value map and a set of
444   // instruction operands to patch, updateReferences performs the updates.
445   //
446   void updateReferences() {
447     for (unsigned i = 0, e = ReferencesToUpdate.size(); i != e; ++i) {
448       RefToUpdate &Ref = ReferencesToUpdate[i];
449       Value *NewVal = XFormMap[Ref.OldVal];
450
451       if (NewVal == 0) {
452         if (isa<Constant>(Ref.OldVal) &&  // Refering to a null ptr?
453             cast<Constant>(Ref.OldVal)->isNullValue()) {
454           // Transform the null pointer into a null index... caching in XFormMap
455           XFormMap[Ref.OldVal] = NewVal = Constant::getNullValue(POINTERTYPE);
456           //} else if (isa<Argument>(Ref.OldVal)) {
457         } else {
458           cerr << "Unknown reference to: " << Ref.OldVal << "\n";
459           assert(XFormMap[Ref.OldVal] &&
460                  "Reference to value that was not updated found!");
461         }
462       }
463         
464       Ref.I->setOperand(Ref.OpNum, NewVal);
465     }
466     ReferencesToUpdate.clear();
467   }
468
469   //===--------------------------------------------------------------------===//
470   // Transformation methods:
471   //   These methods specify how each type of instruction is transformed by the
472   // NewInstructionCreator instance...
473   //===--------------------------------------------------------------------===//
474
475   void visitGetElementPtrInst(GetElementPtrInst *I) {
476     assert(0 && "Cannot transform get element ptr instructions yet!");
477   }
478
479   // Replace the load instruction with a new one.
480   void visitLoadInst(LoadInst *I) {
481     Instruction *PoolBase = createPoolBaseInstruction(I->getOperand(0));
482
483     // Cast our index to be a UIntTy so we can use it to index into the pool...
484     CastInst *Index = new CastInst(Constant::getNullValue(POINTERTYPE),
485                                    Type::UIntTy, I->getOperand(0)->getName());
486
487     ReferencesToUpdate.push_back(RefToUpdate(Index, 0, I->getOperand(0)));
488
489     vector<Value*> Indices(I->idx_begin(), I->idx_end());
490     Instruction *IdxInst =
491       BinaryOperator::create(Instruction::Add, Indices[0], Index,
492                              I->getName()+".idx");
493     Indices[0] = IdxInst;
494     Instruction *NewLoad = new LoadInst(PoolBase, Indices, I->getName());
495
496     // Replace the load instruction with the new load instruction...
497     BasicBlock::iterator II = ReplaceInstWith(I, NewLoad);
498
499     // Add the pool base calculator instruction before the load...
500     II = NewLoad->getParent()->getInstList().insert(II, PoolBase) + 1;
501
502     // Add the idx calculator instruction before the load...
503     II = NewLoad->getParent()->getInstList().insert(II, Index) + 1;
504
505     // Add the cast before the load instruction...
506     NewLoad->getParent()->getInstList().insert(II, IdxInst);
507
508     // If not yielding a pool allocated pointer, use the new load value as the
509     // value in the program instead of the old load value...
510     //
511     if (!getScalar(I))
512       I->replaceAllUsesWith(NewLoad);
513   }
514
515   // Replace the store instruction with a new one.  In the store instruction,
516   // the value stored could be a pointer type, meaning that the new store may
517   // have to change one or both of it's operands.
518   //
519   void visitStoreInst(StoreInst *I) {
520     assert(getScalar(I->getOperand(1)) &&
521            "Store inst found only storing pool allocated pointer.  "
522            "Not imp yet!");
523
524     Value *Val = I->getOperand(0);  // The value to store...
525     // Check to see if the value we are storing is a data structure pointer...
526     //if (const ScalarInfo *ValScalar = getScalar(I->getOperand(0)))
527     if (isa<PointerType>(I->getOperand(0)->getType()))
528       Val = Constant::getNullValue(POINTERTYPE);  // Yes, store a dummy
529
530     Instruction *PoolBase = createPoolBaseInstruction(I->getOperand(1));
531
532     // Cast our index to be a UIntTy so we can use it to index into the pool...
533     CastInst *Index = new CastInst(Constant::getNullValue(POINTERTYPE),
534                                    Type::UIntTy, I->getOperand(1)->getName());
535     ReferencesToUpdate.push_back(RefToUpdate(Index, 0, I->getOperand(1)));
536
537     vector<Value*> Indices(I->idx_begin(), I->idx_end());
538     Instruction *IdxInst =
539       BinaryOperator::create(Instruction::Add, Indices[0], Index, "idx");
540     Indices[0] = IdxInst;
541
542     Instruction *NewStore = new StoreInst(Val, PoolBase, Indices);
543
544     if (Val != I->getOperand(0))    // Value stored was a pointer?
545       ReferencesToUpdate.push_back(RefToUpdate(NewStore, 0, I->getOperand(0)));
546
547
548     // Replace the store instruction with the cast instruction...
549     BasicBlock::iterator II = ReplaceInstWith(I, Index);
550
551     // Add the pool base calculator instruction before the index...
552     II = Index->getParent()->getInstList().insert(II, PoolBase) + 2;
553
554     // Add the indexing instruction...
555     II = Index->getParent()->getInstList().insert(II, IdxInst) + 1;
556
557     // Add the store after the cast instruction...
558     Index->getParent()->getInstList().insert(II, NewStore);
559   }
560
561
562   // Create call to poolalloc for every malloc instruction
563   void visitMallocInst(MallocInst *I) {
564     const ScalarInfo &SCI = getScalarRef(I);
565     vector<Value*> Args;
566
567     CallInst *Call;
568     if (!I->isArrayAllocation()) {
569       Args.push_back(SCI.Pool.Handle);
570       Call = new CallInst(PoolAllocator.PoolAlloc, Args, I->getName());
571     } else {
572       Args.push_back(I->getArraySize());
573       Args.push_back(SCI.Pool.Handle);
574       Call = new CallInst(PoolAllocator.PoolAllocArray, Args, I->getName());
575     }    
576
577     ReplaceInstWith(I, Call);
578   }
579
580   // Convert a call to poolfree for every free instruction...
581   void visitFreeInst(FreeInst *I) {
582     // Create a new call to poolfree before the free instruction
583     vector<Value*> Args;
584     Args.push_back(Constant::getNullValue(POINTERTYPE));
585     Args.push_back(getScalarRef(I->getOperand(0)).Pool.Handle);
586     Instruction *NewCall = new CallInst(PoolAllocator.PoolFree, Args);
587     ReplaceInstWith(I, NewCall);
588     ReferencesToUpdate.push_back(RefToUpdate(NewCall, 1, I->getOperand(0)));
589   }
590
591   // visitCallInst - Create a new call instruction with the extra arguments for
592   // all of the memory pools that the call needs.
593   //
594   void visitCallInst(CallInst *I) {
595     TransformFunctionInfo &TI = CallMap[I];
596
597     // Start with all of the old arguments...
598     vector<Value*> Args(I->op_begin()+1, I->op_end());
599
600     for (unsigned i = 0, e = TI.ArgInfo.size(); i != e; ++i) {
601       // Replace all of the pointer arguments with our new pointer typed values.
602       if (TI.ArgInfo[i].ArgNo != -1)
603         Args[TI.ArgInfo[i].ArgNo] = Constant::getNullValue(POINTERTYPE);
604
605       // Add all of the pool arguments...
606       Args.push_back(TI.ArgInfo[i].PoolHandle);
607     }
608     
609     Function *NF = PoolAllocator.getTransformedFunction(TI);
610     Instruction *NewCall = new CallInst(NF, Args, I->getName());
611     ReplaceInstWith(I, NewCall);
612
613     // Keep track of the mapping of operands so that we can resolve them to real
614     // values later.
615     Value *RetVal = NewCall;
616     for (unsigned i = 0, e = TI.ArgInfo.size(); i != e; ++i)
617       if (TI.ArgInfo[i].ArgNo != -1)
618         ReferencesToUpdate.push_back(RefToUpdate(NewCall, TI.ArgInfo[i].ArgNo+1,
619                                         I->getOperand(TI.ArgInfo[i].ArgNo+1)));
620       else
621         RetVal = 0;   // If returning a pointer, don't change retval...
622
623     // If not returning a pointer, use the new call as the value in the program
624     // instead of the old call...
625     //
626     if (RetVal)
627       I->replaceAllUsesWith(RetVal);
628   }
629
630   // visitPHINode - Create a new PHI node of POINTERTYPE for all of the old Phi
631   // nodes...
632   //
633   void visitPHINode(PHINode *PN) {
634     Value *DummyVal = Constant::getNullValue(POINTERTYPE);
635     PHINode *NewPhi = new PHINode(POINTERTYPE, PN->getName());
636     for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
637       NewPhi->addIncoming(DummyVal, PN->getIncomingBlock(i));
638       ReferencesToUpdate.push_back(RefToUpdate(NewPhi, i*2, 
639                                                PN->getIncomingValue(i)));
640     }
641
642     ReplaceInstWith(PN, NewPhi);
643   }
644
645   // visitReturnInst - Replace ret instruction with a new return...
646   void visitReturnInst(ReturnInst *I) {
647     Instruction *Ret = new ReturnInst(Constant::getNullValue(POINTERTYPE));
648     ReplaceInstWith(I, Ret);
649     ReferencesToUpdate.push_back(RefToUpdate(Ret, 0, I->getOperand(0)));
650   }
651
652   // visitSetCondInst - Replace a conditional test instruction with a new one
653   void visitSetCondInst(SetCondInst *SCI) {
654     BinaryOperator *I = (BinaryOperator*)SCI;
655     Value *DummyVal = Constant::getNullValue(POINTERTYPE);
656     BinaryOperator *New = BinaryOperator::create(I->getOpcode(), DummyVal,
657                                                  DummyVal, I->getName());
658     ReplaceInstWith(I, New);
659
660     ReferencesToUpdate.push_back(RefToUpdate(New, 0, I->getOperand(0)));
661     ReferencesToUpdate.push_back(RefToUpdate(New, 1, I->getOperand(1)));
662
663     // Make sure branches refer to the new condition...
664     I->replaceAllUsesWith(New);
665   }
666
667   void visitInstruction(Instruction *I) {
668     cerr << "Unknown instruction to FunctionBodyTransformer:\n" << I;
669   }
670 };
671
672
673 // PoolBaseLoadEliminator - Every load and store through a pool allocated
674 // pointer causes a load of the real pool base out of the pool descriptor.
675 // Iterate through the function, doing a local elimination pass of duplicate
676 // loads.  This attempts to turn the all too common:
677 //
678 // %reg109.poolbase22 = load %root.pool* %root.pool, uint 0, ubyte 0, ubyte 0
679 // %reg207 = load %root.p* %reg109.poolbase22, uint %reg109, ubyte 0, ubyte 0
680 // %reg109.poolbase23 = load %root.pool* %root.pool, uint 0, ubyte 0, ubyte 0
681 // store double %reg207, %root.p* %reg109.poolbase23, uint %reg109, ...
682 //
683 // into:
684 // %reg109.poolbase22 = load %root.pool* %root.pool, uint 0, ubyte 0, ubyte 0
685 // %reg207 = load %root.p* %reg109.poolbase22, uint %reg109, ubyte 0, ubyte 0
686 // store double %reg207, %root.p* %reg109.poolbase22, uint %reg109, ...
687 //
688 //
689 class PoolBaseLoadEliminator : public InstVisitor<PoolBaseLoadEliminator> {
690   // PoolDescValues - Keep track of the values in the current function that are
691   // pool descriptors (loads from which we want to eliminate).
692   //
693   vector<Value*>      PoolDescValues;
694
695   // PoolDescMap - As we are analyzing a BB, keep track of which load to use
696   // when referencing a pool descriptor.
697   //
698   map<Value*, LoadInst*> PoolDescMap;
699
700   // These two fields keep track of statistics of how effective we are, if
701   // debugging is enabled.
702   //
703   unsigned Eliminated, Remaining;
704 public:
705   // Compact the pool descriptor map into a list of the pool descriptors in the
706   // current context that we should know about...
707   //
708   PoolBaseLoadEliminator(const map<DSNode*, PoolInfo> &PoolDescs) {
709     Eliminated = Remaining = 0;
710     for (map<DSNode*, PoolInfo>::const_iterator I = PoolDescs.begin(),
711            E = PoolDescs.end(); I != E; ++I)
712       PoolDescValues.push_back(I->second.Handle);
713     
714     // Remove duplicates from the list of pool values
715     sort(PoolDescValues.begin(), PoolDescValues.end());
716     PoolDescValues.erase(unique(PoolDescValues.begin(), PoolDescValues.end()),
717                          PoolDescValues.end());
718   }
719
720 #ifdef DEBUG_POOLBASE_LOAD_ELIMINATOR
721   void visitFunction(Function *F) {
722     cerr << "Pool Load Elim '" << F->getName() << "'\t";
723   }
724   ~PoolBaseLoadEliminator() {
725     unsigned Total = Eliminated+Remaining;
726     if (Total)
727       cerr << "removed " << Eliminated << "["
728            << Eliminated*100/Total << "%] loads, leaving "
729            << Remaining << ".\n";
730   }
731 #endif
732
733   // Loop over the function, looking for loads to eliminate.  Because we are a
734   // local transformation, we reset all of our state when we enter a new basic
735   // block.
736   //
737   void visitBasicBlock(BasicBlock *) {
738     PoolDescMap.clear();  // Forget state.
739   }
740
741   // Starting with an empty basic block, we scan it looking for loads of the
742   // pool descriptor.  When we find a load, we add it to the PoolDescMap,
743   // indicating that we have a value available to recycle next time we see the
744   // poolbase of this instruction being loaded.
745   //
746   void visitLoadInst(LoadInst *LI) {
747     Value *LoadAddr = LI->getPointerOperand();
748     map<Value*, LoadInst*>::iterator VIt = PoolDescMap.find(LoadAddr);
749     if (VIt != PoolDescMap.end()) {  // We already have a value for this load?
750       LI->replaceAllUsesWith(VIt->second);   // Make the current load dead
751       ++Eliminated;
752     } else {
753       // This load might not be a load of a pool pointer, check to see if it is
754       if (LI->getNumOperands() == 4 &&  // load pool, uint 0, ubyte 0, ubyte 0
755           find(PoolDescValues.begin(), PoolDescValues.end(), LoadAddr) !=
756           PoolDescValues.end()) {
757
758         assert("Make sure it's a load of the pool base, not a chaining field" &&
759                LI->getOperand(1) == Constant::getNullValue(Type::UIntTy) &&
760                LI->getOperand(2) == Constant::getNullValue(Type::UByteTy) &&
761                LI->getOperand(3) == Constant::getNullValue(Type::UByteTy));
762
763         // If it is a load of a pool base, keep track of it for future reference
764         PoolDescMap.insert(make_pair(LoadAddr, LI));
765         ++Remaining;
766       }
767     }
768   }
769
770   // If we run across a function call, forget all state...  Calls to
771   // poolalloc/poolfree can invalidate the pool base pointer, so it should be
772   // reloaded the next time it is used.  Furthermore, a call to a random
773   // function might call one of these functions, so be conservative.  Through
774   // more analysis, this could be improved in the future.
775   //
776   void visitCallInst(CallInst *) {
777     PoolDescMap.clear();
778   }
779 };
780
781 static void addNodeMapping(DSNode *SrcNode, const PointerValSet &PVS,
782                            map<DSNode*, PointerValSet> &NodeMapping) {
783   for (unsigned i = 0, e = PVS.size(); i != e; ++i)
784     if (NodeMapping[SrcNode].add(PVS[i])) {  // Not in map yet?
785       assert(PVS[i].Index == 0 && "Node indexing not supported yet!");
786       DSNode *DestNode = PVS[i].Node;
787
788       // Loop over all of the outgoing links in the mapped graph
789       for (unsigned l = 0, le = DestNode->getNumOutgoingLinks(); l != le; ++l) {
790         PointerValSet &SrcSet = SrcNode->getOutgoingLink(l);
791         const PointerValSet &DestSet = DestNode->getOutgoingLink(l);
792
793         // Add all of the node mappings now!
794         for (unsigned si = 0, se = SrcSet.size(); si != se; ++si) {
795           assert(SrcSet[si].Index == 0 && "Can't handle node offset!");
796           addNodeMapping(SrcSet[si].Node, DestSet, NodeMapping);
797         }
798       }
799     }
800 }
801
802 // CalculateNodeMapping - There is a partial isomorphism between the graph
803 // passed in and the graph that is actually used by the function.  We need to
804 // figure out what this mapping is so that we can transformFunctionBody the
805 // instructions in the function itself.  Note that every node in the graph that
806 // we are interested in must be both in the local graph of the called function,
807 // and in the local graph of the calling function.  Because of this, we only
808 // define the mapping for these nodes [conveniently these are the only nodes we
809 // CAN define a mapping for...]
810 //
811 // The roots of the graph that we are transforming is rooted in the arguments
812 // passed into the function from the caller.  This is where we start our
813 // mapping calculation.
814 //
815 // The NodeMapping calculated maps from the callers graph to the called graph.
816 //
817 static void CalculateNodeMapping(Function *F, TransformFunctionInfo &TFI,
818                                  FunctionDSGraph &CallerGraph,
819                                  FunctionDSGraph &CalledGraph, 
820                                  map<DSNode*, PointerValSet> &NodeMapping) {
821   int LastArgNo = -2;
822   for (unsigned i = 0, e = TFI.ArgInfo.size(); i != e; ++i) {
823     // Figure out what nodes in the called graph the TFI.ArgInfo[i].Node node
824     // corresponds to...
825     //
826     // Only consider first node of sequence.  Extra nodes may may be added
827     // to the TFI if the data structure requires more nodes than just the
828     // one the argument points to.  We are only interested in the one the
829     // argument points to though.
830     //
831     if (TFI.ArgInfo[i].ArgNo != LastArgNo) {
832       if (TFI.ArgInfo[i].ArgNo == -1) {
833         addNodeMapping(TFI.ArgInfo[i].Node, CalledGraph.getRetNodes(),
834                        NodeMapping);
835       } else {
836         // Figure out which node argument # ArgNo points to in the called graph.
837         Value *Arg = F->getArgumentList()[TFI.ArgInfo[i].ArgNo];     
838         addNodeMapping(TFI.ArgInfo[i].Node, CalledGraph.getValueMap()[Arg],
839                        NodeMapping);
840       }
841       LastArgNo = TFI.ArgInfo[i].ArgNo;
842     }
843   }
844 }
845
846
847
848
849 // addCallInfo - For a specified function call CI, figure out which pool
850 // descriptors need to be passed in as arguments, and which arguments need to be
851 // transformed into indices.  If Arg != -1, the specified call argument is
852 // passed in as a pointer to a data structure.
853 //
854 void TransformFunctionInfo::addCallInfo(DataStructure *DS, CallInst *CI,
855                                         int Arg, DSNode *GraphNode,
856                                         map<DSNode*, PoolInfo> &PoolDescs) {
857   assert(CI->getCalledFunction() && "Cannot handle indirect calls yet!");
858   assert(Func == 0 || Func == CI->getCalledFunction() &&
859          "Function call record should always call the same function!");
860   assert(Call == 0 || Call == CI &&
861          "Call element already filled in with different value!");
862   Func = CI->getCalledFunction();
863   Call = CI;
864   //FunctionDSGraph &CalledGraph = DS->getClosedDSGraph(Func);
865
866   // For now, add the entire graph that is pointed to by the call argument.
867   // This graph can and should be pruned to only what the function itself will
868   // use, because often this will be a dramatically smaller subset of what we
869   // are providing.
870   //
871   // FIXME: This should use pool links instead of extra arguments!
872   //
873   for (df_iterator<DSNode*> I = df_begin(GraphNode), E = df_end(GraphNode);
874        I != E; ++I)
875     ArgInfo.push_back(CallArgInfo(Arg, *I, PoolDescs[*I].Handle));
876 }
877
878 static void markReachableNodes(const PointerValSet &Vals,
879                                set<DSNode*> &ReachableNodes) {
880   for (unsigned n = 0, ne = Vals.size(); n != ne; ++n) {
881     DSNode *N = Vals[n].Node;
882     if (ReachableNodes.count(N) == 0)   // Haven't already processed node?
883       ReachableNodes.insert(df_begin(N), df_end(N)); // Insert all
884   }
885 }
886
887 // Make sure that all dependant arguments are added to this transformation info.
888 // For example, if we call foo(null, P) and foo treats it's first and second
889 // arguments as belonging to the same data structure, the we MUST add entries to
890 // know that the null needs to be transformed into an index as well.
891 //
892 void TransformFunctionInfo::ensureDependantArgumentsIncluded(DataStructure *DS,
893                                            map<DSNode*, PoolInfo> &PoolDescs) {
894   // FIXME: This does not work for indirect function calls!!!
895   if (Func == 0) return;  // FIXME!
896
897   // Make sure argument entries are sorted.
898   finalizeConstruction();
899
900   // Loop over the function signature, checking to see if there are any pointer
901   // arguments that we do not convert...  if there is something we haven't
902   // converted, set done to false.
903   //
904   unsigned PtrNo = 0;
905   bool Done = true;
906   if (isa<PointerType>(Func->getReturnType()))    // Make sure we convert retval
907     if (PtrNo < ArgInfo.size() && ArgInfo[PtrNo++].ArgNo == -1) {
908       // We DO transform the ret val... skip all possible entries for retval
909       while (PtrNo < ArgInfo.size() && ArgInfo[PtrNo].ArgNo == -1)
910         PtrNo++;
911     } else {
912       Done = false;
913     }
914
915   for (unsigned i = 0, e = Func->getArgumentList().size(); i != e; ++i) {
916     Argument *Arg = Func->getArgumentList()[i];
917     if (isa<PointerType>(Arg->getType())) {
918       if (PtrNo < ArgInfo.size() && ArgInfo[PtrNo++].ArgNo == (int)i) {
919         // We DO transform this arg... skip all possible entries for argument
920         while (PtrNo < ArgInfo.size() && ArgInfo[PtrNo].ArgNo == (int)i)
921           PtrNo++;
922       } else {
923         Done = false;
924         break;
925       }
926     }
927   }
928
929   // If we already have entries for all pointer arguments and retvals, there
930   // certainly is no work to do.  Bail out early to avoid building relatively
931   // expensive data structures.
932   //
933   if (Done) return;
934
935 #ifdef DEBUG_TRANSFORM_PROGRESS
936   cerr << "Must ensure dependant arguments for: " << Func->getName() << "\n";
937 #endif
938
939   // Otherwise, we MIGHT have to add the arguments/retval if they are part of
940   // the same datastructure graph as some other argument or retval that we ARE
941   // processing.
942   //
943   // Get the data structure graph for the called function.
944   //
945   FunctionDSGraph &CalledDS = DS->getClosedDSGraph(Func);
946
947   // Build a mapping between the nodes in our current graph and the nodes in the
948   // called function's graph.  We build it based on our _incomplete_
949   // transformation information, because it contains all of the info that we
950   // should need.
951   //
952   map<DSNode*, PointerValSet> NodeMapping;
953   CalculateNodeMapping(Func, *this,
954                        DS->getClosedDSGraph(Call->getParent()->getParent()),
955                        CalledDS, NodeMapping);
956
957   // Build the inverted version of the node mapping, that maps from a node in
958   // the called functions graph to a single node in the caller graph.
959   // 
960   map<DSNode*, DSNode*> InverseNodeMap;
961   for (map<DSNode*, PointerValSet>::iterator I = NodeMapping.begin(),
962          E = NodeMapping.end(); I != E; ++I) {
963     PointerValSet &CalledNodes = I->second;
964     for (unsigned i = 0, e = CalledNodes.size(); i != e; ++i)
965       InverseNodeMap[CalledNodes[i].Node] = I->first;
966   }
967   NodeMapping.clear();  // Done with information, free memory
968   
969   // Build a set of reachable nodes from the arguments/retval that we ARE
970   // passing in...
971   set<DSNode*> ReachableNodes;
972
973   // Loop through all of the arguments, marking all of the reachable data
974   // structure nodes reachable if they are from this pointer...
975   //
976   for (unsigned i = 0, e = ArgInfo.size(); i != e; ++i) {
977     if (ArgInfo[i].ArgNo == -1) {
978       if (i == 0)   // Only process retvals once (performance opt)
979         markReachableNodes(CalledDS.getRetNodes(), ReachableNodes);
980     } else {  // If it's an argument value...
981       Argument *Arg = Func->getArgumentList()[ArgInfo[i].ArgNo];
982       if (isa<PointerType>(Arg->getType()))
983         markReachableNodes(CalledDS.getValueMap()[Arg], ReachableNodes);
984     }
985   }
986
987   // Now that we know which nodes are already reachable, see if any of the
988   // arguments that we are not passing values in for can reach one of the
989   // existing nodes...
990   //
991
992   // <FIXME> IN THEORY, we should allow arbitrary paths from the argument to
993   // nodes we know about.  The problem is that if we do this, then I don't know
994   // how to get pool pointers for this head list.  Since we are completely
995   // deadline driven, I'll just allow direct accesses to the graph. </FIXME>
996   //
997   
998   PtrNo = 0;
999   if (isa<PointerType>(Func->getReturnType()))    // Make sure we convert retval
1000     if (PtrNo < ArgInfo.size() && ArgInfo[PtrNo++].ArgNo == -1) {
1001       // We DO transform the ret val... skip all possible entries for retval
1002       while (PtrNo < ArgInfo.size() && ArgInfo[PtrNo].ArgNo == -1)
1003         PtrNo++;
1004     } else {
1005       // See what the return value points to...
1006
1007       // FIXME: This should generalize to any number of nodes, just see if any
1008       // are reachable.
1009       assert(CalledDS.getRetNodes().size() == 1 &&
1010              "Assumes only one node is returned");
1011       DSNode *N = CalledDS.getRetNodes()[0].Node;
1012       
1013       // If the return value is not marked as being passed in, but it NEEDS to
1014       // be transformed, then make it known now.
1015       //
1016       if (ReachableNodes.count(N)) {
1017 #ifdef DEBUG_TRANSFORM_PROGRESS
1018         cerr << "ensure dependant arguments adds return value entry!\n";
1019 #endif
1020         addCallInfo(DS, Call, -1, InverseNodeMap[N], PoolDescs);
1021
1022         // Keep sorted!
1023         finalizeConstruction();
1024       }
1025     }
1026
1027   for (unsigned i = 0, e = Func->getArgumentList().size(); i != e; ++i) {
1028     Argument *Arg = Func->getArgumentList()[i];
1029     if (isa<PointerType>(Arg->getType())) {
1030       if (PtrNo < ArgInfo.size() && ArgInfo[PtrNo++].ArgNo == (int)i) {
1031         // We DO transform this arg... skip all possible entries for argument
1032         while (PtrNo < ArgInfo.size() && ArgInfo[PtrNo].ArgNo == (int)i)
1033           PtrNo++;
1034       } else {
1035         // This should generalize to any number of nodes, just see if any are
1036         // reachable.
1037         assert(CalledDS.getValueMap()[Arg].size() == 1 &&
1038                "Only handle case where pointing to one node so far!");
1039
1040         // If the arg is not marked as being passed in, but it NEEDS to
1041         // be transformed, then make it known now.
1042         //
1043         DSNode *N = CalledDS.getValueMap()[Arg][0].Node;
1044         if (ReachableNodes.count(N)) {
1045 #ifdef DEBUG_TRANSFORM_PROGRESS
1046           cerr << "ensure dependant arguments adds for arg #" << i << "\n";
1047 #endif
1048           addCallInfo(DS, Call, i, InverseNodeMap[N], PoolDescs);
1049
1050           // Keep sorted!
1051           finalizeConstruction();
1052         }
1053       }
1054     }
1055   }
1056 }
1057
1058
1059 // transformFunctionBody - This transforms the instruction in 'F' to use the
1060 // pools specified in PoolDescs when modifying data structure nodes specified in
1061 // the PoolDescs map.  Specifically, scalar values specified in the Scalars
1062 // vector must be remapped.  IPFGraph is the closed data structure graph for F,
1063 // of which the PoolDescriptor nodes come from.
1064 //
1065 void PoolAllocate::transformFunctionBody(Function *F, FunctionDSGraph &IPFGraph,
1066                                          map<DSNode*, PoolInfo> &PoolDescs) {
1067
1068   // Loop through the value map looking for scalars that refer to nonescaping
1069   // allocations.  Add them to the Scalars vector.  Note that we may have
1070   // multiple entries in the Scalars vector for each value if it points to more
1071   // than one object.
1072   //
1073   map<Value*, PointerValSet> &ValMap = IPFGraph.getValueMap();
1074   vector<ScalarInfo> Scalars;
1075
1076 #ifdef DEBUG_TRANSFORM_PROGRESS
1077   cerr << "Building scalar map for fn '" << F->getName() << "' body:\n";
1078 #endif
1079
1080   for (map<Value*, PointerValSet>::iterator I = ValMap.begin(),
1081          E = ValMap.end(); I != E; ++I) {
1082     const PointerValSet &PVS = I->second;  // Set of things pointed to by scalar
1083
1084     // Check to see if the scalar points to a data structure node...
1085     for (unsigned i = 0, e = PVS.size(); i != e; ++i) {
1086       if (PVS[i].Index) { cerr << "Problem in " << F->getName() << " for " << I->first << "\n"; }
1087       assert(PVS[i].Index == 0 && "Nonzero not handled yet!");
1088         
1089       // If the allocation is in the nonescaping set...
1090       map<DSNode*, PoolInfo>::iterator AI = PoolDescs.find(PVS[i].Node);
1091       if (AI != PoolDescs.end()) {              // Add it to the list of scalars
1092         Scalars.push_back(ScalarInfo(I->first, AI->second));
1093 #ifdef DEBUG_TRANSFORM_PROGRESS
1094         cerr << "\nScalar Mapping from:" << I->first
1095              << "Scalar Mapping to: "; PVS.print(cerr);
1096 #endif
1097       }
1098     }
1099   }
1100
1101 #ifdef DEBUG_TRANSFORM_PROGRESS
1102   cerr << "\nIn '" << F->getName()
1103        << "': Found the following values that point to poolable nodes:\n";
1104
1105   for (unsigned i = 0, e = Scalars.size(); i != e; ++i)
1106     cerr << Scalars[i].Val;
1107   cerr << "\n";
1108 #endif
1109
1110   // CallMap - Contain an entry for every call instruction that needs to be
1111   // transformed.  Each entry in the map contains information about what we need
1112   // to do to each call site to change it to work.
1113   //
1114   map<CallInst*, TransformFunctionInfo> CallMap;
1115
1116   // Now we need to figure out what called functions we need to transform, and
1117   // how.  To do this, we look at all of the scalars, seeing which functions are
1118   // either used as a scalar value (so they return a data structure), or are
1119   // passed one of our scalar values.
1120   //
1121   for (unsigned i = 0, e = Scalars.size(); i != e; ++i) {
1122     Value *ScalarVal = Scalars[i].Val;
1123
1124     // Check to see if the scalar _IS_ a call...
1125     if (CallInst *CI = dyn_cast<CallInst>(ScalarVal))
1126       // If so, add information about the pool it will be returning...
1127       CallMap[CI].addCallInfo(DS, CI, -1, Scalars[i].Pool.Node, PoolDescs);
1128
1129     // Check to see if the scalar is an operand to a call...
1130     for (Value::use_iterator UI = ScalarVal->use_begin(),
1131            UE = ScalarVal->use_end(); UI != UE; ++UI) {
1132       if (CallInst *CI = dyn_cast<CallInst>(*UI)) {
1133         // Find out which operand this is to the call instruction...
1134         User::op_iterator OI = find(CI->op_begin(), CI->op_end(), ScalarVal);
1135         assert(OI != CI->op_end() && "Call on use list but not an operand!?");
1136         assert(OI != CI->op_begin() && "Pointer operand is call destination?");
1137
1138         // FIXME: This is broken if the same pointer is passed to a call more
1139         // than once!  It will get multiple entries for the first pointer.
1140
1141         // Add the operand number and pool handle to the call table...
1142         CallMap[CI].addCallInfo(DS, CI, OI-CI->op_begin()-1,
1143                                 Scalars[i].Pool.Node, PoolDescs);
1144       }
1145     }
1146   }
1147
1148   // Make sure that all dependant arguments are added as well.  For example, if
1149   // we call foo(null, P) and foo treats it's first and second arguments as
1150   // belonging to the same data structure, the we MUST set up the CallMap to
1151   // know that the null needs to be transformed into an index as well.
1152   //
1153   for (map<CallInst*, TransformFunctionInfo>::iterator I = CallMap.begin();
1154        I != CallMap.end(); ++I)
1155     I->second.ensureDependantArgumentsIncluded(DS, PoolDescs);
1156
1157 #ifdef DEBUG_TRANSFORM_PROGRESS
1158   // Print out call map...
1159   for (map<CallInst*, TransformFunctionInfo>::iterator I = CallMap.begin();
1160        I != CallMap.end(); ++I) {
1161     cerr << "For call: " << I->first;
1162     cerr << I->second.Func->getName() << " must pass pool pointer for args #";
1163     for (unsigned i = 0; i < I->second.ArgInfo.size(); ++i)
1164       cerr << I->second.ArgInfo[i].ArgNo << ", ";
1165     cerr << "\n\n";
1166   }
1167 #endif
1168
1169   // Loop through all of the call nodes, recursively creating the new functions
1170   // that we want to call...  This uses a map to prevent infinite recursion and
1171   // to avoid duplicating functions unneccesarily.
1172   //
1173   for (map<CallInst*, TransformFunctionInfo>::iterator I = CallMap.begin(),
1174          E = CallMap.end(); I != E; ++I) {
1175     // Transform all of the functions we need, or at least ensure there is a
1176     // cached version available.
1177     transformFunction(I->second, IPFGraph, PoolDescs);
1178   }
1179
1180   // Now that all of the functions that we want to call are available, transform
1181   // the local function so that it uses the pools locally and passes them to the
1182   // functions that we just hacked up.
1183   //
1184
1185   // First step, find the instructions to be modified.
1186   vector<Instruction*> InstToFix;
1187   for (unsigned i = 0, e = Scalars.size(); i != e; ++i) {
1188     Value *ScalarVal = Scalars[i].Val;
1189
1190     // Check to see if the scalar _IS_ an instruction.  If so, it is involved.
1191     if (Instruction *Inst = dyn_cast<Instruction>(ScalarVal))
1192       InstToFix.push_back(Inst);
1193
1194     // All all of the instructions that use the scalar as an operand...
1195     for (Value::use_iterator UI = ScalarVal->use_begin(),
1196            UE = ScalarVal->use_end(); UI != UE; ++UI)
1197       InstToFix.push_back(cast<Instruction>(*UI));
1198   }
1199
1200   // Make sure that we get return instructions that return a null value from the
1201   // function...
1202   //
1203   if (!IPFGraph.getRetNodes().empty()) {
1204     assert(IPFGraph.getRetNodes().size() == 1 && "Can only return one node?");
1205     PointerVal RetNode = IPFGraph.getRetNodes()[0];
1206     assert(RetNode.Index == 0 && "Subindexing not implemented yet!");
1207
1208     // Only process return instructions if the return value of this function is
1209     // part of one of the data structures we are transforming...
1210     //
1211     if (PoolDescs.count(RetNode.Node)) {
1212       // Loop over all of the basic blocks, adding return instructions...
1213       for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
1214         if (ReturnInst *RI = dyn_cast<ReturnInst>((*I)->getTerminator()))
1215           InstToFix.push_back(RI);
1216     }
1217   }
1218
1219
1220
1221   // Eliminate duplicates by sorting, then removing equal neighbors.
1222   sort(InstToFix.begin(), InstToFix.end());
1223   InstToFix.erase(unique(InstToFix.begin(), InstToFix.end()), InstToFix.end());
1224
1225   // Loop over all of the instructions to transform, creating the new
1226   // replacement instructions for them.  This also unlinks them from the
1227   // function so they can be safely deleted later.
1228   //
1229   map<Value*, Value*> XFormMap;  
1230   NewInstructionCreator NIC(*this, Scalars, CallMap, XFormMap);
1231
1232   // Visit all instructions... creating the new instructions that we need and
1233   // unlinking the old instructions from the function...
1234   //
1235 #ifdef DEBUG_TRANSFORM_PROGRESS
1236   for (unsigned i = 0, e = InstToFix.size(); i != e; ++i) {
1237     cerr << "Fixing: " << InstToFix[i];
1238     NIC.visit(InstToFix[i]);
1239   }
1240 #else
1241   NIC.visit(InstToFix.begin(), InstToFix.end());
1242 #endif
1243
1244   // Make all instructions we will delete "let go" of their operands... so that
1245   // we can safely delete Arguments whose types have changed...
1246   //
1247   for_each(InstToFix.begin(), InstToFix.end(),
1248            mem_fun(&Instruction::dropAllReferences));
1249
1250   // Loop through all of the pointer arguments coming into the function,
1251   // replacing them with arguments of POINTERTYPE to match the function type of
1252   // the function.
1253   //
1254   FunctionType::ParamTypes::const_iterator TI =
1255     F->getFunctionType()->getParamTypes().begin();
1256   for (Function::ArgumentListType::iterator I = F->getArgumentList().begin(),
1257          E = F->getArgumentList().end(); I != E; ++I, ++TI) {
1258     Argument *Arg = *I;
1259     if (Arg->getType() != *TI) {
1260       assert(isa<PointerType>(Arg->getType()) && *TI == POINTERTYPE);
1261       Argument *NewArg = new Argument(*TI, Arg->getName());
1262       XFormMap[Arg] = NewArg;  // Map old arg into new arg...
1263
1264       // Replace the old argument and then delete it...
1265       delete F->getArgumentList().replaceWith(I, NewArg);
1266     }
1267   }
1268
1269   // Now that all of the new instructions have been created, we can update all
1270   // of the references to dummy values to be references to the actual values
1271   // that are computed.
1272   //
1273   NIC.updateReferences();
1274
1275 #ifdef DEBUG_TRANSFORM_PROGRESS
1276   cerr << "TRANSFORMED FUNCTION:\n" << F;
1277 #endif
1278
1279   // Delete all of the "instructions to fix"
1280   for_each(InstToFix.begin(), InstToFix.end(), deleter<Instruction>);
1281
1282   // Eliminate pool base loads that we can easily prove are redundant
1283   if (!DisableRLE)
1284     PoolBaseLoadEliminator(PoolDescs).visit(F);
1285
1286   // Since we have liberally hacked the function to pieces, we want to inform
1287   // the datastructure pass that its internal representation is out of date.
1288   //
1289   DS->invalidateFunction(F);
1290 }
1291
1292
1293
1294 // transformFunction - Transform the specified function the specified way.  It
1295 // we have already transformed that function that way, don't do anything.  The
1296 // nodes in the TransformFunctionInfo come out of callers data structure graph.
1297 //
1298 void PoolAllocate::transformFunction(TransformFunctionInfo &TFI,
1299                                      FunctionDSGraph &CallerIPGraph,
1300                                      map<DSNode*, PoolInfo> &CallerPoolDesc) {
1301   if (getTransformedFunction(TFI)) return;  // Function xformation already done?
1302
1303 #ifdef DEBUG_TRANSFORM_PROGRESS
1304   cerr << "********** Entering transformFunction for "
1305        << TFI.Func->getName() << ":\n";
1306   for (unsigned i = 0, e = TFI.ArgInfo.size(); i != e; ++i)
1307     cerr << "  ArgInfo[" << i << "] = " << TFI.ArgInfo[i].ArgNo << "\n";
1308   cerr << "\n";
1309 #endif
1310
1311   const FunctionType *OldFuncType = TFI.Func->getFunctionType();
1312
1313   assert(!OldFuncType->isVarArg() && "Vararg functions not handled yet!");
1314
1315   // Build the type for the new function that we are transforming
1316   vector<const Type*> ArgTys;
1317   ArgTys.reserve(OldFuncType->getNumParams()+TFI.ArgInfo.size());
1318   for (unsigned i = 0, e = OldFuncType->getNumParams(); i != e; ++i)
1319     ArgTys.push_back(OldFuncType->getParamType(i));
1320
1321   const Type *RetType = OldFuncType->getReturnType();
1322   
1323   // Add one pool pointer for every argument that needs to be supplemented.
1324   for (unsigned i = 0, e = TFI.ArgInfo.size(); i != e; ++i) {
1325     if (TFI.ArgInfo[i].ArgNo == -1)
1326       RetType = POINTERTYPE;  // Return a pointer
1327     else
1328       ArgTys[TFI.ArgInfo[i].ArgNo] = POINTERTYPE; // Pass a pointer
1329     ArgTys.push_back(PointerType::get(CallerPoolDesc.find(TFI.ArgInfo[i].Node)
1330                                         ->second.PoolType));
1331   }
1332
1333   // Build the new function type...
1334   const FunctionType *NewFuncType = FunctionType::get(RetType, ArgTys,
1335                                                       OldFuncType->isVarArg());
1336
1337   // The new function is internal, because we know that only we can call it.
1338   // This also helps subsequent IP transformations to eliminate duplicated pool
1339   // pointers (which look like the same value is always passed into a parameter,
1340   // allowing it to be easily eliminated).
1341   //
1342   Function *NewFunc = new Function(NewFuncType, true,
1343                                    TFI.Func->getName()+".poolxform");
1344   CurModule->getFunctionList().push_back(NewFunc);
1345
1346
1347 #ifdef DEBUG_TRANSFORM_PROGRESS
1348   cerr << "Created function prototype: " << NewFunc << "\n";
1349 #endif
1350
1351   // Add the newly formed function to the TransformedFunctions table so that
1352   // infinite recursion does not occur!
1353   //
1354   TransformedFunctions[TFI] = NewFunc;
1355
1356   // Add arguments to the function... starting with all of the old arguments
1357   vector<Value*> ArgMap;
1358   for (unsigned i = 0, e = TFI.Func->getArgumentList().size(); i != e; ++i) {
1359     const Argument *OFA = TFI.Func->getArgumentList()[i];
1360     Argument *NFA = new Argument(OFA->getType(), OFA->getName());
1361     NewFunc->getArgumentList().push_back(NFA);
1362     ArgMap.push_back(NFA);  // Keep track of the arguments 
1363   }
1364
1365   // Now add all of the arguments corresponding to pools passed in...
1366   for (unsigned i = 0, e = TFI.ArgInfo.size(); i != e; ++i) {
1367     CallArgInfo &AI = TFI.ArgInfo[i];
1368     string Name;
1369     if (AI.ArgNo == -1)
1370       Name = "ret";
1371     else
1372       Name = ArgMap[AI.ArgNo]->getName();  // Get the arg name
1373     const Type *Ty = PointerType::get(CallerPoolDesc[AI.Node].PoolType);
1374     Argument *NFA = new Argument(Ty, Name+".pool");
1375     NewFunc->getArgumentList().push_back(NFA);
1376   }
1377
1378   // Now clone the body of the old function into the new function...
1379   CloneFunctionInto(NewFunc, TFI.Func, ArgMap);
1380   
1381   // Okay, now we have a function that is identical to the old one, except that
1382   // it has extra arguments for the pools coming in.  Now we have to get the 
1383   // data structure graph for the function we are replacing, and figure out how
1384   // our graph nodes map to the graph nodes in the dest function.
1385   //
1386   FunctionDSGraph &DSGraph = DS->getClosedDSGraph(NewFunc);  
1387
1388   // NodeMapping - Multimap from callers graph to called graph.  We are
1389   // guaranteed that the called function graph has more nodes than the caller,
1390   // or exactly the same number of nodes.  This is because the called function
1391   // might not know that two nodes are merged when considering the callers
1392   // context, but the caller obviously does.  Because of this, a single node in
1393   // the calling function's data structure graph can map to multiple nodes in
1394   // the called functions graph.
1395   //
1396   map<DSNode*, PointerValSet> NodeMapping;
1397
1398   CalculateNodeMapping(NewFunc, TFI, CallerIPGraph, DSGraph, 
1399                        NodeMapping);
1400
1401   // Print out the node mapping...
1402 #ifdef DEBUG_TRANSFORM_PROGRESS
1403   cerr << "\nNode mapping for call of " << NewFunc->getName() << "\n";
1404   for (map<DSNode*, PointerValSet>::iterator I = NodeMapping.begin();
1405        I != NodeMapping.end(); ++I) {
1406     cerr << "Map: "; I->first->print(cerr);
1407     cerr << "To:  "; I->second.print(cerr);
1408     cerr << "\n";
1409   }
1410 #endif
1411
1412   // Fill in the PoolDescriptor information for the transformed function so that
1413   // it can determine which value holds the pool descriptor for each data
1414   // structure node that it accesses.
1415   //
1416   map<DSNode*, PoolInfo> PoolDescs;
1417
1418 #ifdef DEBUG_TRANSFORM_PROGRESS
1419   cerr << "\nCalculating the pool descriptor map:\n";
1420 #endif
1421
1422   // Calculate as much of the pool descriptor map as possible.  Since we have
1423   // the node mapping between the caller and callee functions, and we have the
1424   // pool descriptor information of the caller, we can calculate a partical pool
1425   // descriptor map for the called function.
1426   //
1427   // The nodes that we do not have complete information for are the ones that
1428   // are accessed by loading pointers derived from arguments passed in, but that
1429   // are not passed in directly.  In this case, we have all of the information
1430   // except a pool value.  If the called function refers to this pool, the pool
1431   // value will be loaded from the pool graph and added to the map as neccesary.
1432   //
1433   for (map<DSNode*, PointerValSet>::iterator I = NodeMapping.begin();
1434        I != NodeMapping.end(); ++I) {
1435     DSNode *CallerNode = I->first;
1436     PoolInfo &CallerPI = CallerPoolDesc[CallerNode];
1437
1438     // Check to see if we have a node pointer passed in for this value...
1439     Value *CalleeValue = 0;
1440     for (unsigned a = 0, ae = TFI.ArgInfo.size(); a != ae; ++a)
1441       if (TFI.ArgInfo[a].Node == CallerNode) {
1442         // Calculate the argument number that the pool is to the function
1443         // call...  The call instruction should not have the pool operands added
1444         // yet.
1445         unsigned ArgNo = TFI.Call->getNumOperands()-1+a;
1446 #ifdef DEBUG_TRANSFORM_PROGRESS
1447         cerr << "Should be argument #: " << ArgNo << "[i = " << a << "]\n";
1448 #endif
1449         assert(ArgNo < NewFunc->getArgumentList().size() &&
1450                "Call already has pool arguments added??");
1451
1452         // Map the pool argument into the called function...
1453         CalleeValue = NewFunc->getArgumentList()[ArgNo];
1454         break;  // Found value, quit loop
1455       }
1456
1457     // Loop over all of the data structure nodes that this incoming node maps to
1458     // Creating a PoolInfo structure for them.
1459     for (unsigned i = 0, e = I->second.size(); i != e; ++i) {
1460       assert(I->second[i].Index == 0 && "Doesn't handle subindexing yet!");
1461       DSNode *CalleeNode = I->second[i].Node;
1462      
1463       // Add the descriptor.  We already know everything about it by now, much
1464       // of it is the same as the caller info.
1465       // 
1466       PoolDescs.insert(make_pair(CalleeNode,
1467                                  PoolInfo(CalleeNode, CalleeValue,
1468                                           CallerPI.NewType,
1469                                           CallerPI.PoolType)));
1470     }
1471   }
1472
1473   // We must destroy the node mapping so that we don't have latent references
1474   // into the data structure graph for the new function.  Otherwise we get
1475   // assertion failures when transformFunctionBody tries to invalidate the
1476   // graph.
1477   //
1478   NodeMapping.clear();
1479
1480   // Now that we know everything we need about the function, transform the body
1481   // now!
1482   //
1483   transformFunctionBody(NewFunc, DSGraph, PoolDescs);
1484   
1485 #ifdef DEBUG_TRANSFORM_PROGRESS
1486   cerr << "Function after transformation:\n" << NewFunc;
1487 #endif
1488 }
1489
1490 static unsigned countPointerTypes(const Type *Ty) {
1491   if (isa<PointerType>(Ty)) {
1492     return 1;
1493   } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
1494     unsigned Num = 0;
1495     for (unsigned i = 0, e = STy->getElementTypes().size(); i != e; ++i)
1496       Num += countPointerTypes(STy->getElementTypes()[i]);
1497     return Num;
1498   } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1499     return countPointerTypes(ATy->getElementType());
1500   } else {
1501     assert(Ty->isPrimitiveType() && "Unknown derived type!");
1502     return 0;
1503   }
1504 }
1505
1506 // CreatePools - Insert instructions into the function we are processing to
1507 // create all of the memory pool objects themselves.  This also inserts
1508 // destruction code.  Add an alloca for each pool that is allocated to the
1509 // PoolDescs vector.
1510 //
1511 void PoolAllocate::CreatePools(Function *F, const vector<AllocDSNode*> &Allocs,
1512                                map<DSNode*, PoolInfo> &PoolDescs) {
1513   // Find all of the return nodes in the function...
1514   vector<BasicBlock*> ReturnNodes;
1515   for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
1516     if (isa<ReturnInst>((*I)->getTerminator()))
1517       ReturnNodes.push_back(*I);
1518
1519 #ifdef DEBUG_CREATE_POOLS
1520   cerr << "Allocs that we are pool allocating:\n";
1521   for (unsigned i = 0, e = Allocs.size(); i != e; ++i)
1522     Allocs[i]->dump();
1523 #endif
1524
1525   map<DSNode*, PATypeHolder> AbsPoolTyMap;
1526
1527   // First pass over the allocations to process...
1528   for (unsigned i = 0, e = Allocs.size(); i != e; ++i) {
1529     // Create the pooldescriptor mapping... with null entries for everything
1530     // except the node & NewType fields.
1531     //
1532     map<DSNode*, PoolInfo>::iterator PI =
1533       PoolDescs.insert(make_pair(Allocs[i], PoolInfo(Allocs[i]))).first;
1534
1535     // Add a symbol table entry for the new type if there was one for the old
1536     // type...
1537     string OldName = CurModule->getTypeName(Allocs[i]->getType());
1538     if (OldName.empty()) OldName = "node";
1539     CurModule->addTypeName(OldName+".p", PI->second.NewType);
1540
1541     // Create the abstract pool types that will need to be resolved in a second
1542     // pass once an abstract type is created for each pool.
1543     //
1544     // Can only handle limited shapes for now...
1545     const Type *OldNodeTy = Allocs[i]->getType();
1546     vector<const Type*> PoolTypes;
1547
1548     // Pool type is the first element of the pool descriptor type...
1549     PoolTypes.push_back(getPoolType(PoolDescs[Allocs[i]].NewType));
1550
1551     unsigned NumPointers = countPointerTypes(OldNodeTy);
1552     while (NumPointers--)   // Add a different opaque type for each pointer
1553       PoolTypes.push_back(OpaqueType::get());
1554
1555     assert(Allocs[i]->getNumLinks() == PoolTypes.size()-1 &&
1556            "Node should have same number of pointers as pool!");
1557
1558     StructType *PoolType = StructType::get(PoolTypes);
1559
1560     // Add a symbol table entry for the pooltype if possible...
1561     CurModule->addTypeName(OldName+".pool", PoolType);
1562
1563     // Create the pool type, with opaque values for pointers...
1564     AbsPoolTyMap.insert(make_pair(Allocs[i], PoolType));
1565 #ifdef DEBUG_CREATE_POOLS
1566     cerr << "POOL TY: " << AbsPoolTyMap.find(Allocs[i])->second.get() << "\n";
1567 #endif
1568   }
1569   
1570   // Now that we have types for all of the pool types, link them all together.
1571   for (unsigned i = 0, e = Allocs.size(); i != e; ++i) {
1572     PATypeHolder &PoolTyH = AbsPoolTyMap.find(Allocs[i])->second;
1573
1574     // Resolve all of the outgoing pointer types of this pool node...
1575     for (unsigned p = 0, pe = Allocs[i]->getNumLinks(); p != pe; ++p) {
1576       PointerValSet &PVS = Allocs[i]->getLink(p);
1577       assert(!PVS.empty() && "Outgoing edge is empty, field unused, can"
1578              " probably just leave the type opaque or something dumb.");
1579       unsigned Out;
1580       for (Out = 0; AbsPoolTyMap.count(PVS[Out].Node) == 0; ++Out)
1581         assert(Out != PVS.size() && "No edge to an outgoing allocation node!?");
1582       
1583       assert(PVS[Out].Index == 0 && "Subindexing not implemented yet!");
1584
1585       // The actual struct type could change each time through the loop, so it's
1586       // NOT loop invariant.
1587       StructType *PoolTy = cast<StructType>(PoolTyH.get());
1588
1589       // Get the opaque type...
1590       DerivedType *ElTy =
1591         cast<DerivedType>(PoolTy->getElementTypes()[p+1].get());
1592
1593 #ifdef DEBUG_CREATE_POOLS
1594       cerr << "Refining " << ElTy << " of " << PoolTy << " to "
1595            << AbsPoolTyMap.find(PVS[Out].Node)->second.get() << "\n";
1596 #endif
1597
1598       const Type *RefPoolTy = AbsPoolTyMap.find(PVS[Out].Node)->second.get();
1599       ElTy->refineAbstractTypeTo(PointerType::get(RefPoolTy));
1600
1601 #ifdef DEBUG_CREATE_POOLS
1602       cerr << "Result pool type is: " << PoolTyH.get() << "\n";
1603 #endif
1604     }
1605   }
1606
1607   // Create the code that goes in the entry and exit nodes for the function...
1608   vector<Instruction*> EntryNodeInsts;
1609   for (unsigned i = 0, e = Allocs.size(); i != e; ++i) {
1610     PoolInfo &PI = PoolDescs[Allocs[i]];
1611     
1612     // Fill in the pool type for this pool...
1613     PI.PoolType = AbsPoolTyMap.find(Allocs[i])->second.get();
1614     assert(!PI.PoolType->isAbstract() &&
1615            "Pool type should not be abstract anymore!");
1616
1617     // Add an allocation and a free for each pool...
1618     AllocaInst *PoolAlloc
1619       = new AllocaInst(PointerType::get(PI.PoolType), 0,
1620                        CurModule->getTypeName(PI.PoolType));
1621     PI.Handle = PoolAlloc;
1622     EntryNodeInsts.push_back(PoolAlloc);
1623     AllocationInst *AI = Allocs[i]->getAllocation();
1624
1625     // Initialize the pool.  We need to know how big each allocation is.  For
1626     // our purposes here, we assume we are allocating a scalar, or array of
1627     // constant size.
1628     //
1629     unsigned ElSize = TargetData.getTypeSize(PI.NewType);
1630
1631     vector<Value*> Args;
1632     Args.push_back(ConstantUInt::get(Type::UIntTy, ElSize));
1633     Args.push_back(PoolAlloc);    // Pool to initialize
1634     EntryNodeInsts.push_back(new CallInst(PoolInit, Args));
1635
1636     // Add code to destroy the pool in all of the exit nodes of the function...
1637     Args.clear();
1638     Args.push_back(PoolAlloc);    // Pool to initialize
1639     
1640     for (unsigned EN = 0, ENE = ReturnNodes.size(); EN != ENE; ++EN) {
1641       Instruction *Destroy = new CallInst(PoolDestroy, Args);
1642
1643       // Insert it before the return instruction...
1644       BasicBlock *RetNode = ReturnNodes[EN];
1645       RetNode->getInstList().insert(RetNode->end()-1, Destroy);
1646     }
1647   }
1648
1649   // Now that all of the pool descriptors have been created, link them together
1650   // so that called functions can get links as neccesary...
1651   //
1652   for (unsigned i = 0, e = Allocs.size(); i != e; ++i) {
1653     PoolInfo &PI = PoolDescs[Allocs[i]];
1654
1655     // For every pointer in the data structure, initialize a link that
1656     // indicates which pool to access...
1657     //
1658     vector<Value*> Indices(2);
1659     Indices[0] = ConstantUInt::get(Type::UIntTy, 0);
1660     for (unsigned l = 0, le = PI.Node->getNumLinks(); l != le; ++l)
1661       // Only store an entry for the field if the field is used!
1662       if (!PI.Node->getLink(l).empty()) {
1663         assert(PI.Node->getLink(l).size() == 1 && "Should have only one link!");
1664         PointerVal PV = PI.Node->getLink(l)[0];
1665         assert(PV.Index == 0 && "Subindexing not supported yet!");
1666         PoolInfo &LinkedPool = PoolDescs[PV.Node];
1667         Indices[1] = ConstantUInt::get(Type::UByteTy, 1+l);
1668       
1669         EntryNodeInsts.push_back(new StoreInst(LinkedPool.Handle, PI.Handle,
1670                                                Indices));
1671       }
1672   }
1673
1674   // Insert the entry node code into the entry block...
1675   F->getEntryNode()->getInstList().insert(F->getEntryNode()->begin()+1,
1676                                           EntryNodeInsts.begin(),
1677                                           EntryNodeInsts.end());
1678 }
1679
1680
1681 // addPoolPrototypes - Add prototypes for the pool functions to the specified
1682 // module and update the Pool* instance variables to point to them.
1683 //
1684 void PoolAllocate::addPoolPrototypes(Module *M) {
1685   // Get poolinit function...
1686   vector<const Type*> Args;
1687   Args.push_back(Type::UIntTy);     // Num bytes per element
1688   FunctionType *PoolInitTy = FunctionType::get(Type::VoidTy, Args, true);
1689   PoolInit = M->getOrInsertFunction("poolinit", PoolInitTy);
1690
1691   // Get pooldestroy function...
1692   Args.pop_back();  // Only takes a pool...
1693   FunctionType *PoolDestroyTy = FunctionType::get(Type::VoidTy, Args, true);
1694   PoolDestroy = M->getOrInsertFunction("pooldestroy", PoolDestroyTy);
1695
1696   // Get the poolalloc function...
1697   FunctionType *PoolAllocTy = FunctionType::get(POINTERTYPE, Args, true);
1698   PoolAlloc = M->getOrInsertFunction("poolalloc", PoolAllocTy);
1699
1700   // Get the poolfree function...
1701   Args.push_back(POINTERTYPE);       // Pointer to free
1702   FunctionType *PoolFreeTy = FunctionType::get(Type::VoidTy, Args, true);
1703   PoolFree = M->getOrInsertFunction("poolfree", PoolFreeTy);
1704
1705   Args[0] = Type::UIntTy;            // Number of slots to allocate
1706   FunctionType *PoolAllocArrayTy = FunctionType::get(POINTERTYPE, Args, true);
1707   PoolAllocArray = M->getOrInsertFunction("poolallocarray", PoolAllocArrayTy);
1708 }
1709
1710
1711 bool PoolAllocate::run(Module *M) {
1712   addPoolPrototypes(M);
1713   CurModule = M;
1714   
1715   DS = &getAnalysis<DataStructure>();
1716   bool Changed = false;
1717
1718   // We cannot use an iterator here because it will get invalidated when we add
1719   // functions to the module later...
1720   for (unsigned i = 0; i != M->size(); ++i)
1721     if (!M->getFunctionList()[i]->isExternal()) {
1722       Changed |= processFunction(M->getFunctionList()[i]);
1723       if (Changed) {
1724         cerr << "Only processing one function\n";
1725         break;
1726       }
1727     }
1728
1729   CurModule = 0;
1730   DS = 0;
1731   return false;
1732 }
1733
1734
1735 // createPoolAllocatePass - Global function to access the functionality of this
1736 // pass...
1737 //
1738 Pass *createPoolAllocatePass() { return new PoolAllocate(); }