Add extra case here to avoid getting spurious output
[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 //===----------------------------------------------------------------------===//
8
9 #include "llvm/Transforms/IPO/PoolAllocate.h"
10 #include "llvm/Transforms/CloneFunction.h"
11 #include "llvm/Analysis/DataStructure.h"
12 #include "llvm/Analysis/DataStructureGraph.h"
13 #include "llvm/Pass.h"
14 #include "llvm/Module.h"
15 #include "llvm/Function.h"
16 #include "llvm/iMemory.h"
17 #include "llvm/iTerminators.h"
18 #include "llvm/iOther.h"
19 #include "llvm/ConstantVals.h"
20 #include "llvm/Target/TargetData.h"
21 #include "llvm/Support/InstVisitor.h"
22 #include "Support/DepthFirstIterator.h"
23 #include "Support/STLExtras.h"
24 #include <algorithm>
25
26
27 // FIXME: This is dependant on the sparc backend layout conventions!!
28 static TargetData TargetData("test");
29
30 namespace {
31   // ScalarInfo - Information about an LLVM value that we know points to some
32   // datastructure we are processing.
33   //
34   struct ScalarInfo {
35     Value  *Val;            // Scalar value in Current Function
36     DSNode *Node;           // DataStructure node it points to
37     Value  *PoolHandle;     // PoolTy* LLVM value
38     
39     ScalarInfo(Value *V, DSNode *N, Value *PH)
40       : Val(V), Node(N), PoolHandle(PH) {
41       assert(V && N && PH && "Null value passed to ScalarInfo ctor!");
42     }
43   };
44
45   // CallArgInfo - Information on one operand for a call that got expanded.
46   struct CallArgInfo {
47     int ArgNo;          // Call argument number this corresponds to
48     DSNode *Node;       // The graph node for the pool
49     Value *PoolHandle;  // The LLVM value that is the pool pointer
50
51     CallArgInfo(int Arg, DSNode *N, Value *PH)
52       : ArgNo(Arg), Node(N), PoolHandle(PH) {
53       assert(Arg >= -1 && N && PH && "Illegal values to CallArgInfo ctor!");
54     }
55
56     // operator< when sorting, sort by argument number.
57     bool operator<(const CallArgInfo &CAI) const {
58       return ArgNo < CAI.ArgNo;
59     }
60   };
61
62   // TransformFunctionInfo - Information about how a function eeds to be
63   // transformed.
64   //
65   struct TransformFunctionInfo {
66     // ArgInfo - Maintain information about the arguments that need to be
67     // processed.  Each pair corresponds to an argument (whose number is the
68     // first element) that needs to have a pool pointer (the second element)
69     // passed into the transformed function with it.
70     //
71     // As a special case, "argument" number -1 corresponds to the return value.
72     //
73     vector<CallArgInfo> ArgInfo;
74
75     // Func - The function to be transformed...
76     Function *Func;
77
78     // The call instruction that is used to map CallArgInfo PoolHandle values
79     // into the new function values.
80     CallInst *Call;
81
82     // default ctor...
83     TransformFunctionInfo() : Func(0), Call(0) {}
84     
85     bool operator<(const TransformFunctionInfo &TFI) const {
86       if (Func < TFI.Func) return true;
87       if (Func > TFI.Func) return false;
88       if (ArgInfo.size() < TFI.ArgInfo.size()) return true;
89       if (ArgInfo.size() > TFI.ArgInfo.size()) return false;
90       return ArgInfo < TFI.ArgInfo;
91     }
92
93     void finalizeConstruction() {
94       // Sort the vector so that the return value is first, followed by the
95       // argument records, in order.  Note that this must be a stable sort so
96       // that the entries with the same sorting criteria (ie they are multiple
97       // pool entries for the same argument) are kept in depth first order.
98       stable_sort(ArgInfo.begin(), ArgInfo.end());
99     }
100   };
101
102
103   // Define the pass class that we implement...
104   class PoolAllocate : public Pass {
105     // PoolTy - The type of a scalar value that contains a pool pointer.
106     PointerType *PoolTy;
107   public:
108
109     PoolAllocate() {
110       // Initialize the PoolTy instance variable, since the type never changes.
111       vector<const Type*> PoolElements;
112       PoolElements.push_back(PointerType::get(Type::SByteTy));
113       PoolElements.push_back(Type::UIntTy);
114       PoolTy = PointerType::get(StructType::get(PoolElements));
115       // PoolTy = { sbyte*, uint }*
116
117       CurModule = 0; DS = 0;
118       PoolInit = PoolDestroy = PoolAlloc = PoolFree = 0;
119     }
120
121     bool run(Module *M);
122
123     // getAnalysisUsageInfo - This function requires data structure information
124     // to be able to see what is pool allocatable.
125     //
126     virtual void getAnalysisUsageInfo(Pass::AnalysisSet &Required,
127                                       Pass::AnalysisSet &,Pass::AnalysisSet &) {
128       Required.push_back(DataStructure::ID);
129     }
130
131   public:
132     // CurModule - The module being processed.
133     Module *CurModule;
134
135     // DS - The data structure graph for the module being processed.
136     DataStructure *DS;
137
138     // Prototypes that we add to support pool allocation...
139     Function *PoolInit, *PoolDestroy, *PoolAlloc, *PoolFree;
140
141     // The map of already transformed functions... note that the keys of this
142     // map do not have meaningful values for 'Call' or the 'PoolHandle' elements
143     // of the ArgInfo elements.
144     //
145     map<TransformFunctionInfo, Function*> TransformedFunctions;
146
147     // getTransformedFunction - Get a transformed function, or return null if
148     // the function specified hasn't been transformed yet.
149     //
150     Function *getTransformedFunction(TransformFunctionInfo &TFI) const {
151       map<TransformFunctionInfo, Function*>::const_iterator I =
152         TransformedFunctions.find(TFI);
153       if (I != TransformedFunctions.end()) return I->second;
154       return 0;
155     }
156
157
158     // addPoolPrototypes - Add prototypes for the pool methods to the specified
159     // module and update the Pool* instance variables to point to them.
160     //
161     void addPoolPrototypes(Module *M);
162
163
164     // CreatePools - Insert instructions into the function we are processing to
165     // create all of the memory pool objects themselves.  This also inserts
166     // destruction code.  Add an alloca for each pool that is allocated to the
167     // PoolDescriptors map.
168     //
169     void CreatePools(Function *F, const vector<AllocDSNode*> &Allocs,
170                      map<DSNode*, Value*> &PoolDescriptors);
171
172     // processFunction - Convert a function to use pool allocation where
173     // available.
174     //
175     bool processFunction(Function *F);
176
177     // transformFunctionBody - This transforms the instruction in 'F' to use the
178     // pools specified in PoolDescriptors when modifying data structure nodes
179     // specified in the PoolDescriptors map.  IPFGraph is the closed data
180     // structure graph for F, of which the PoolDescriptor nodes come from.
181     //
182     void transformFunctionBody(Function *F, FunctionDSGraph &IPFGraph,
183                                map<DSNode*, Value*> &PoolDescriptors);
184
185     // transformFunction - Transform the specified function the specified way.
186     // It we have already transformed that function that way, don't do anything.
187     // The nodes in the TransformFunctionInfo come out of callers data structure
188     // graph.
189     //
190     void transformFunction(TransformFunctionInfo &TFI,
191                            FunctionDSGraph &CallerIPGraph);
192
193   };
194 }
195
196
197
198 // isNotPoolableAlloc - This is a predicate that returns true if the specified
199 // allocation node in a data structure graph is eligable for pool allocation.
200 //
201 static bool isNotPoolableAlloc(const AllocDSNode *DS) {
202   if (DS->isAllocaNode()) return true;  // Do not pool allocate alloca's.
203
204   MallocInst *MI = cast<MallocInst>(DS->getAllocation());
205   if (MI->isArrayAllocation() && !isa<Constant>(MI->getArraySize()))
206     return true;   // Do not allow variable size allocations...
207
208   return false;
209 }
210
211 // processFunction - Convert a function to use pool allocation where
212 // available.
213 //
214 bool PoolAllocate::processFunction(Function *F) {
215   // Get the closed datastructure graph for the current function... if there are
216   // any allocations in this graph that are not escaping, we need to pool
217   // allocate them here!
218   //
219   FunctionDSGraph &IPGraph = DS->getClosedDSGraph(F);
220
221   // Get all of the allocations that do not escape the current function.  Since
222   // they are still live (they exist in the graph at all), this means we must
223   // have scalar references to these nodes, but the scalars are never returned.
224   // 
225   vector<AllocDSNode*> Allocs;
226   IPGraph.getNonEscapingAllocations(Allocs);
227
228   // Filter out allocations that we cannot handle.  Currently, this includes
229   // variable sized array allocations and alloca's (which we do not want to
230   // pool allocate)
231   //
232   Allocs.erase(remove_if(Allocs.begin(), Allocs.end(), isNotPoolableAlloc),
233                Allocs.end());
234
235
236   if (Allocs.empty()) return false;  // Nothing to do.
237
238   // Insert instructions into the function we are processing to create all of
239   // the memory pool objects themselves.  This also inserts destruction code.
240   // This fills in the PoolDescriptors map to associate the alloc node with the
241   // allocation of the memory pool corresponding to it.
242   // 
243   map<DSNode*, Value*> PoolDescriptors;
244   CreatePools(F, Allocs, PoolDescriptors);
245
246   // Now we need to figure out what called methods we need to transform, and
247   // how.  To do this, we look at all of the scalars, seeing which functions are
248   // either used as a scalar value (so they return a data structure), or are
249   // passed one of our scalar values.
250   //
251   transformFunctionBody(F, IPGraph, PoolDescriptors);
252
253   return true;
254 }
255
256
257 class FunctionBodyTransformer : public InstVisitor<FunctionBodyTransformer> {
258   PoolAllocate &PoolAllocator;
259   vector<ScalarInfo> &Scalars;
260   map<CallInst*, TransformFunctionInfo> &CallMap;
261
262   const ScalarInfo &getScalar(const Value *V) {
263     for (unsigned i = 0, e = Scalars.size(); i != e; ++i)
264       if (Scalars[i].Val == V) return Scalars[i];
265     assert(0 && "Scalar not found in getScalar!");
266     abort();
267     return Scalars[0];
268   }
269
270   // updateScalars - Map the scalars array entries that look like 'From' to look
271   // like 'To'.
272   //
273   void updateScalars(Value *From, Value *To) {
274     for (unsigned i = 0, e = Scalars.size(); i != e; ++i)
275       if (Scalars[i].Val == From) Scalars[i].Val = To;
276   }
277
278 public:
279   FunctionBodyTransformer(PoolAllocate &PA, vector<ScalarInfo> &S,
280                           map<CallInst*, TransformFunctionInfo> &C)
281     : PoolAllocator(PA), Scalars(S), CallMap(C) {}
282
283   void visitMemAccessInst(MemAccessInst *MAI) {
284     // Don't do anything to load, store, or GEP yet...
285   }
286
287   // Convert a malloc instruction into a call to poolalloc
288   void visitMallocInst(MallocInst *I) {
289     const ScalarInfo &SC = getScalar(I);
290     BasicBlock *BB = I->getParent();
291     BasicBlock::iterator MI = find(BB->begin(), BB->end(), I);
292     BB->getInstList().remove(MI);  // Remove the Malloc instruction from the BB
293
294     // Create a new call to poolalloc before the malloc instruction
295     vector<Value*> Args;
296     Args.push_back(SC.PoolHandle);
297     CallInst *Call = new CallInst(PoolAllocator.PoolAlloc, Args, I->getName());
298     MI = BB->getInstList().insert(MI, Call)+1;
299
300     // If the type desired is not void*, cast it now...
301     Value *Ptr = Call;
302     if (Call->getType() != I->getType()) {
303       CastInst *CI = new CastInst(Ptr, I->getType(), I->getName());
304       BB->getInstList().insert(MI, CI);
305       Ptr = CI;
306     }
307
308     // Change everything that used the malloc to now use the pool alloc...
309     I->replaceAllUsesWith(Ptr);
310
311     // Update the scalars array...
312     updateScalars(I, Ptr);
313
314     // Delete the instruction now.
315     delete I;
316   }
317
318   // Convert the free instruction into a call to poolfree
319   void visitFreeInst(FreeInst *I) {
320     Value *Ptr = I->getOperand(0);
321     const ScalarInfo &SC = getScalar(Ptr);
322     BasicBlock *BB = I->getParent();
323     BasicBlock::iterator FI = find(BB->begin(), BB->end(), I);
324
325     // If the value is not an sbyte*, convert it now!
326     if (Ptr->getType() != PointerType::get(Type::SByteTy)) {
327       CastInst *CI = new CastInst(Ptr, PointerType::get(Type::SByteTy),
328                                   Ptr->getName());
329       FI = BB->getInstList().insert(FI, CI)+1;
330       Ptr = CI;
331     }
332
333     // Create a new call to poolfree before the free instruction
334     vector<Value*> Args;
335     Args.push_back(SC.PoolHandle);
336     Args.push_back(Ptr);
337     CallInst *Call = new CallInst(PoolAllocator.PoolFree, Args);
338     FI = BB->getInstList().insert(FI, Call)+1;
339
340     // Remove the old free instruction...
341     delete BB->getInstList().remove(FI);
342   }
343
344   // visitCallInst - Create a new call instruction with the extra arguments for
345   // all of the memory pools that the call needs.
346   //
347   void visitCallInst(CallInst *I) {
348     TransformFunctionInfo &TI = CallMap[I];
349     BasicBlock *BB = I->getParent();
350     BasicBlock::iterator CI = find(BB->begin(), BB->end(), I);
351     BB->getInstList().remove(CI);  // Remove the old call instruction
352
353     // Start with all of the old arguments...
354     vector<Value*> Args(I->op_begin()+1, I->op_end());
355
356     // Add all of the pool arguments...
357     for (unsigned i = 0, e = TI.ArgInfo.size(); i != e; ++i)
358       Args.push_back(TI.ArgInfo[i].PoolHandle);
359     
360     Function *NF = PoolAllocator.getTransformedFunction(TI);
361     CallInst *NewCall = new CallInst(NF, Args, I->getName());
362     BB->getInstList().insert(CI, NewCall);
363
364     // Change everything that used the malloc to now use the pool alloc...
365     if (I->getType() != Type::VoidTy) {
366       I->replaceAllUsesWith(NewCall);
367
368       // Update the scalars array...
369       updateScalars(I, NewCall);
370     }
371
372     delete I;  // Delete the old call instruction now...
373   }
374
375   void visitPHINode(PHINode *PN) {
376     // Handle PHI Node
377   }
378
379   void visitReturnInst(ReturnInst *I) {
380     // Nothing of interest
381   }
382
383   void visitSetCondInst(SetCondInst *SCI) {
384     // hrm, notice a pattern?
385   }
386
387   void visitInstruction(Instruction *I) {
388     cerr << "Unknown instruction to FunctionBodyTransformer:\n";
389     I->dump();
390   }
391
392 };
393
394
395 static void addCallInfo(DataStructure *DS,
396                         TransformFunctionInfo &TFI, CallInst *CI, int Arg, 
397                         DSNode *GraphNode,
398                         map<DSNode*, Value*> &PoolDescriptors) {
399   assert(CI->getCalledFunction() && "Cannot handle indirect calls yet!");
400   assert(TFI.Func == 0 || TFI.Func == CI->getCalledFunction() &&
401          "Function call record should always call the same function!");
402   assert(TFI.Call == 0 || TFI.Call == CI &&
403          "Call element already filled in with different value!");
404   TFI.Func = CI->getCalledFunction();
405   TFI.Call = CI;
406   //FunctionDSGraph &CalledGraph = DS->getClosedDSGraph(TFI.Func);
407
408   // For now, add the entire graph that is pointed to by the call argument.
409   // This graph can and should be pruned to only what the function itself will
410   // use, because often this will be a dramatically smaller subset of what we
411   // are providing.
412   //
413   for (df_iterator<DSNode*> I = df_begin(GraphNode), E = df_end(GraphNode);
414        I != E; ++I) {
415     TFI.ArgInfo.push_back(CallArgInfo(Arg, *I, PoolDescriptors[*I]));
416   }
417 }
418
419
420 // transformFunctionBody - This transforms the instruction in 'F' to use the
421 // pools specified in PoolDescriptors when modifying data structure nodes
422 // specified in the PoolDescriptors map.  Specifically, scalar values specified
423 // in the Scalars vector must be remapped.  IPFGraph is the closed data
424 // structure graph for F, of which the PoolDescriptor nodes come from.
425 //
426 void PoolAllocate::transformFunctionBody(Function *F, FunctionDSGraph &IPFGraph,
427                                        map<DSNode*, Value*> &PoolDescriptors) {
428
429   // Loop through the value map looking for scalars that refer to nonescaping
430   // allocations.  Add them to the Scalars vector.  Note that we may have
431   // multiple entries in the Scalars vector for each value if it points to more
432   // than one object.
433   //
434   map<Value*, PointerValSet> &ValMap = IPFGraph.getValueMap();
435   vector<ScalarInfo> Scalars;
436
437   cerr << "Building scalar map:\n";
438
439   for (map<Value*, PointerValSet>::iterator I = ValMap.begin(),
440          E = ValMap.end(); I != E; ++I) {
441     const PointerValSet &PVS = I->second;  // Set of things pointed to by scalar
442
443     cerr << "Scalar Mapping from:"; I->first->dump();
444     cerr << "\nScalar Mapping to: "; PVS.print(cerr);
445
446     assert(PVS.size() == 1 &&
447            "Only handle scalars that point to one thing so far!");
448
449     // Check to see if the scalar points to a data structure node...
450     for (unsigned i = 0, e = PVS.size(); i != e; ++i) {
451       assert(PVS[i].Index == 0 && "Nonzero not handled yet!");
452         
453       // If the allocation is in the nonescaping set...
454       map<DSNode*, Value*>::iterator AI = PoolDescriptors.find(PVS[i].Node);
455       if (AI != PoolDescriptors.end()) // Add it to the list of scalars
456         Scalars.push_back(ScalarInfo(I->first, PVS[i].Node, AI->second));
457     }
458   }
459
460
461
462   cerr << "\nIn '" << F->getName()
463        << "': Found the following values that point to poolable nodes:\n";
464
465   for (unsigned i = 0, e = Scalars.size(); i != e; ++i)
466     Scalars[i].Val->dump();
467
468   // CallMap - Contain an entry for every call instruction that needs to be
469   // transformed.  Each entry in the map contains information about what we need
470   // to do to each call site to change it to work.
471   //
472   map<CallInst*, TransformFunctionInfo> CallMap;
473
474   // Now we need to figure out what called methods we need to transform, and
475   // how.  To do this, we look at all of the scalars, seeing which functions are
476   // either used as a scalar value (so they return a data structure), or are
477   // passed one of our scalar values.
478   //
479   for (unsigned i = 0, e = Scalars.size(); i != e; ++i) {
480     Value *ScalarVal = Scalars[i].Val;
481
482     // Check to see if the scalar _IS_ a call...
483     if (CallInst *CI = dyn_cast<CallInst>(ScalarVal))
484       // If so, add information about the pool it will be returning...
485       addCallInfo(DS, CallMap[CI], CI, -1, Scalars[i].Node, PoolDescriptors);
486
487     // Check to see if the scalar is an operand to a call...
488     for (Value::use_iterator UI = ScalarVal->use_begin(),
489            UE = ScalarVal->use_end(); UI != UE; ++UI) {
490       if (CallInst *CI = dyn_cast<CallInst>(*UI)) {
491         // Find out which operand this is to the call instruction...
492         User::op_iterator OI = find(CI->op_begin(), CI->op_end(), ScalarVal);
493         assert(OI != CI->op_end() && "Call on use list but not an operand!?");
494         assert(OI != CI->op_begin() && "Pointer operand is call destination?");
495
496         // FIXME: This is broken if the same pointer is passed to a call more
497         // than once!  It will get multiple entries for the first pointer.
498
499         // Add the operand number and pool handle to the call table...
500         addCallInfo(DS, CallMap[CI], CI, OI-CI->op_begin()-1, Scalars[i].Node,
501                     PoolDescriptors);
502       }
503     }
504   }
505
506   // Print out call map...
507   for (map<CallInst*, TransformFunctionInfo>::iterator I = CallMap.begin();
508        I != CallMap.end(); ++I) {
509     cerr << "\nFor call: ";
510     I->first->dump();
511     I->second.finalizeConstruction();
512     cerr << I->second.Func->getName() << " must pass pool pointer for args #";
513     for (unsigned i = 0; i < I->second.ArgInfo.size(); ++i)
514       cerr << I->second.ArgInfo[i].ArgNo << ", ";
515     cerr << "\n";
516   }
517
518   // Loop through all of the call nodes, recursively creating the new functions
519   // that we want to call...  This uses a map to prevent infinite recursion and
520   // to avoid duplicating functions unneccesarily.
521   //
522   for (map<CallInst*, TransformFunctionInfo>::iterator I = CallMap.begin(),
523          E = CallMap.end(); I != E; ++I) {
524     // Make sure the entries are sorted.
525     I->second.finalizeConstruction();
526
527     // Transform all of the functions we need, or at least ensure there is a
528     // cached version available.
529     transformFunction(I->second, IPFGraph);
530   }
531
532   // Now that all of the functions that we want to call are available, transform
533   // the local method so that it uses the pools locally and passes them to the
534   // functions that we just hacked up.
535   //
536
537   // First step, find the instructions to be modified.
538   vector<Instruction*> InstToFix;
539   for (unsigned i = 0, e = Scalars.size(); i != e; ++i) {
540     Value *ScalarVal = Scalars[i].Val;
541
542     // Check to see if the scalar _IS_ an instruction.  If so, it is involved.
543     if (Instruction *Inst = dyn_cast<Instruction>(ScalarVal))
544       InstToFix.push_back(Inst);
545
546     // All all of the instructions that use the scalar as an operand...
547     for (Value::use_iterator UI = ScalarVal->use_begin(),
548            UE = ScalarVal->use_end(); UI != UE; ++UI)
549       InstToFix.push_back(dyn_cast<Instruction>(*UI));
550   }
551
552   // Eliminate duplicates by sorting, then removing equal neighbors.
553   sort(InstToFix.begin(), InstToFix.end());
554   InstToFix.erase(unique(InstToFix.begin(), InstToFix.end()), InstToFix.end());
555
556   // Use a FunctionBodyTransformer to transform all of the involved instructions
557   FunctionBodyTransformer FBT(*this, Scalars, CallMap);
558   for (unsigned i = 0, e = InstToFix.size(); i != e; ++i)
559     FBT.visit(InstToFix[i]);
560
561
562   // Since we have liberally hacked the function to pieces, we want to inform
563   // the datastructure pass that its internal representation is out of date.
564   //
565   DS->invalidateFunction(F);
566 }
567
568 static void addNodeMapping(DSNode *SrcNode, const PointerValSet &PVS,
569                            map<DSNode*, PointerValSet> &NodeMapping) {
570   for (unsigned i = 0, e = PVS.size(); i != e; ++i)
571     if (NodeMapping[SrcNode].add(PVS[i])) {  // Not in map yet?
572       assert(PVS[i].Index == 0 && "Node indexing not supported yet!");
573       DSNode *DestNode = PVS[i].Node;
574
575       // Loop over all of the outgoing links in the mapped graph
576       for (unsigned l = 0, le = DestNode->getNumOutgoingLinks(); l != le; ++l) {
577         PointerValSet &SrcSet = SrcNode->getOutgoingLink(l);
578         const PointerValSet &DestSet = DestNode->getOutgoingLink(l);
579         assert((!SrcSet.empty() || DestSet.empty()) &&
580                "Dest graph should be a proper subset of the src graph!");
581
582         // Add all of the node mappings now!
583         for (unsigned si = 0, se = SrcSet.size(); si != se; ++si) {
584           assert(SrcSet[si].Index == 0 && "Can't handle node offset!");
585           addNodeMapping(SrcSet[si].Node, DestSet, NodeMapping);
586         }
587       }
588     }
589 }
590
591 // CalculateNodeMapping - There is a partial isomorphism between the graph
592 // passed in and the graph that is actually used by the function.  We need to
593 // figure out what this mapping is so that we can transformFunctionBody the
594 // instructions in the function itself.  Note that every node in the graph that
595 // we are interested in must be both in the local graph of the called function,
596 // and in the local graph of the calling function.  Because of this, we only
597 // define the mapping for these nodes [conveniently these are the only nodes we
598 // CAN define a mapping for...]
599 //
600 // The roots of the graph that we are transforming is rooted in the arguments
601 // passed into the function from the caller.  This is where we start our
602 // mapping calculation.
603 //
604 // The NodeMapping calculated maps from the callers graph to the called graph.
605 //
606 static void CalculateNodeMapping(Function *F, TransformFunctionInfo &TFI,
607                                  FunctionDSGraph &CallerGraph,
608                                  FunctionDSGraph &CalledGraph, 
609                                  map<DSNode*, PointerValSet> &NodeMapping) {
610   int LastArgNo = -2;
611   for (unsigned i = 0, e = TFI.ArgInfo.size(); i != e; ++i) {
612     // Figure out what nodes in the called graph the TFI.ArgInfo[i].Node node
613     // corresponds to...
614     //
615     // Only consider first node of sequence.  Extra nodes may may be added
616     // to the TFI if the data structure requires more nodes than just the
617     // one the argument points to.  We are only interested in the one the
618     // argument points to though.
619     //
620     if (TFI.ArgInfo[i].ArgNo != LastArgNo) {
621       if (TFI.ArgInfo[i].ArgNo == -1) {
622         addNodeMapping(TFI.ArgInfo[i].Node, CalledGraph.getRetNodes(),
623                        NodeMapping);
624       } else {
625         // Figure out which node argument # ArgNo points to in the called graph.
626         Value *Arg = F->getArgumentList()[TFI.ArgInfo[i].ArgNo];     
627         addNodeMapping(TFI.ArgInfo[i].Node, CalledGraph.getValueMap()[Arg],
628                        NodeMapping);
629       }
630       LastArgNo = TFI.ArgInfo[i].ArgNo;
631     }
632   }
633 }
634
635
636 // transformFunction - Transform the specified function the specified way.  It
637 // we have already transformed that function that way, don't do anything.  The
638 // nodes in the TransformFunctionInfo come out of callers data structure graph.
639 //
640 void PoolAllocate::transformFunction(TransformFunctionInfo &TFI,
641                                      FunctionDSGraph &CallerIPGraph) {
642   if (getTransformedFunction(TFI)) return;  // Function xformation already done?
643
644   cerr << "**********\nEntering transformFunction for "
645        << TFI.Func->getName() << ":\n";
646   for (unsigned i = 0, e = TFI.ArgInfo.size(); i != e; ++i)
647     cerr << "  ArgInfo[" << i << "] = " << TFI.ArgInfo[i].ArgNo << "\n";
648   cerr << "\n";
649
650
651   const FunctionType *OldFuncType = TFI.Func->getFunctionType();
652
653   assert(!OldFuncType->isVarArg() && "Vararg functions not handled yet!");
654
655   // Build the type for the new function that we are transforming
656   vector<const Type*> ArgTys;
657   for (unsigned i = 0, e = OldFuncType->getNumParams(); i != e; ++i)
658     ArgTys.push_back(OldFuncType->getParamType(i));
659
660   // Add one pool pointer for every argument that needs to be supplemented.
661   ArgTys.insert(ArgTys.end(), TFI.ArgInfo.size(), PoolTy);
662
663   // Build the new function type...
664   const // FIXME when types are not const
665   FunctionType *NewFuncType = FunctionType::get(OldFuncType->getReturnType(),
666                                                 ArgTys,OldFuncType->isVarArg());
667
668   // The new function is internal, because we know that only we can call it.
669   // This also helps subsequent IP transformations to eliminate duplicated pool
670   // pointers. [in the future when they are implemented].
671   //
672   Function *NewFunc = new Function(NewFuncType, true,
673                                    TFI.Func->getName()+".poolxform");
674   CurModule->getFunctionList().push_back(NewFunc);
675
676   // Add the newly formed function to the TransformedFunctions table so that
677   // infinite recursion does not occur!
678   //
679   TransformedFunctions[TFI] = NewFunc;
680
681   // Add arguments to the function... starting with all of the old arguments
682   vector<Value*> ArgMap;
683   for (unsigned i = 0, e = TFI.Func->getArgumentList().size(); i != e; ++i) {
684     const FunctionArgument *OFA = TFI.Func->getArgumentList()[i];
685     FunctionArgument *NFA = new FunctionArgument(OFA->getType(),OFA->getName());
686     NewFunc->getArgumentList().push_back(NFA);
687     ArgMap.push_back(NFA);  // Keep track of the arguments 
688   }
689
690   // Now add all of the arguments corresponding to pools passed in...
691   for (unsigned i = 0, e = TFI.ArgInfo.size(); i != e; ++i) {
692     string Name;
693     if (TFI.ArgInfo[i].ArgNo == -1)
694       Name = "retpool";
695     else
696       Name = ArgMap[TFI.ArgInfo[i].ArgNo]->getName();  // Get the arg name
697     FunctionArgument *NFA = new FunctionArgument(PoolTy, Name+".pool");
698     NewFunc->getArgumentList().push_back(NFA);
699   }
700
701   // Now clone the body of the old function into the new function...
702   CloneFunctionInto(NewFunc, TFI.Func, ArgMap);
703   
704   // Okay, now we have a function that is identical to the old one, except that
705   // it has extra arguments for the pools coming in.  Now we have to get the 
706   // data structure graph for the function we are replacing, and figure out how
707   // our graph nodes map to the graph nodes in the dest function.
708   //
709   FunctionDSGraph &DSGraph = DS->getClosedDSGraph(NewFunc);  
710
711   // NodeMapping - Multimap from callers graph to called graph.
712   //
713   map<DSNode*, PointerValSet> NodeMapping;
714
715   CalculateNodeMapping(NewFunc, TFI, CallerIPGraph, DSGraph, 
716                        NodeMapping);
717
718   // Print out the node mapping...
719   cerr << "\nNode mapping for call of " << NewFunc->getName() << "\n";
720   for (map<DSNode*, PointerValSet>::iterator I = NodeMapping.begin();
721        I != NodeMapping.end(); ++I) {
722     cerr << "Map: "; I->first->print(cerr);
723     cerr << "To:  "; I->second.print(cerr);
724     cerr << "\n";
725   }
726
727   // Fill in the PoolDescriptor information for the transformed function so that
728   // it can determine which value holds the pool descriptor for each data
729   // structure node that it accesses.
730   //
731   map<DSNode*, Value*> PoolDescriptors;
732
733   cerr << "\nCalculating the pool descriptor map:\n";
734
735   // All of the pool descriptors must be passed in as arguments...
736   for (unsigned i = 0, e = TFI.ArgInfo.size(); i != e; ++i) {
737     DSNode *CallerNode = TFI.ArgInfo[i].Node;
738     Value  *CallerPool = TFI.ArgInfo[i].PoolHandle;
739
740     cerr << "Mapped caller node: "; CallerNode->print(cerr);
741     cerr << "Mapped caller pool: "; CallerPool->dump();
742
743     // Calculate the argument number that the pool is to the function call...
744     // The call instruction should not have the pool operands added yet.
745     unsigned ArgNo = TFI.Call->getNumOperands()-1+i;
746     cerr << "Should be argument #: " << ArgNo << "[i = " << i << "]\n";
747     assert(ArgNo < NewFunc->getArgumentList().size() &&
748            "Call already has pool arguments added??");
749
750     // Map the pool argument into the called function...
751     Value *CalleePool = NewFunc->getArgumentList()[ArgNo];
752
753     // Map the DSNode into the callee's DSGraph
754     const PointerValSet &CalleeNodes = NodeMapping[CallerNode];
755     for (unsigned n = 0, ne = CalleeNodes.size(); n != ne; ++n) {
756       assert(CalleeNodes[n].Index == 0 && "Indexed node not handled yet!");
757       DSNode *CalleeNode = CalleeNodes[n].Node;
758
759       cerr << "*** to callee node: "; CalleeNode->print(cerr);
760       cerr << "*** to callee pool: "; CalleePool->dump();
761       cerr << "\n";
762       
763       assert(CalleeNode && CalleePool && "Invalid nodes!");
764       Value *&PV = PoolDescriptors[CalleeNode];
765       //assert((PV == 0 || PV == CalleePool) && "Invalid node remapping!");
766       PV = CalleePool;         // Update the pool descriptor map!
767     }
768   }
769
770   // We must destroy the node mapping so that we don't have latent references
771   // into the data structure graph for the new function.  Otherwise we get
772   // assertion failures when transformFunctionBody tries to invalidate the
773   // graph.
774   //
775   NodeMapping.clear();
776
777   // Now that we know everything we need about the function, transform the body
778   // now!
779   //
780   transformFunctionBody(NewFunc, DSGraph, PoolDescriptors);
781
782   cerr << "Function after transformation:\n";
783   NewFunc->dump();
784 }
785
786
787 // CreatePools - Insert instructions into the function we are processing to
788 // create all of the memory pool objects themselves.  This also inserts
789 // destruction code.  Add an alloca for each pool that is allocated to the
790 // PoolDescriptors vector.
791 //
792 void PoolAllocate::CreatePools(Function *F, const vector<AllocDSNode*> &Allocs,
793                                map<DSNode*, Value*> &PoolDescriptors) {
794   // FIXME: This should use an IP version of the UnifyAllExits pass!
795   vector<BasicBlock*> ReturnNodes;
796   for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
797     if (isa<ReturnInst>((*I)->getTerminator()))
798       ReturnNodes.push_back(*I);
799   
800
801   // Create the code that goes in the entry and exit nodes for the method...
802   vector<Instruction*> EntryNodeInsts;
803   for (unsigned i = 0, e = Allocs.size(); i != e; ++i) {
804     // Add an allocation and a free for each pool...
805     AllocaInst *PoolAlloc = new AllocaInst(PoolTy, 0, "pool");
806     EntryNodeInsts.push_back(PoolAlloc);
807     PoolDescriptors[Allocs[i]] = PoolAlloc;   // Keep track of pool allocas
808     AllocationInst *AI = Allocs[i]->getAllocation();
809
810     // Initialize the pool.  We need to know how big each allocation is.  For
811     // our purposes here, we assume we are allocating a scalar, or array of
812     // constant size.
813     //
814     unsigned ElSize = TargetData.getTypeSize(AI->getAllocatedType());
815     ElSize *= cast<ConstantUInt>(AI->getArraySize())->getValue();
816
817     vector<Value*> Args;
818     Args.push_back(PoolAlloc);    // Pool to initialize
819     Args.push_back(ConstantUInt::get(Type::UIntTy, ElSize));
820     EntryNodeInsts.push_back(new CallInst(PoolInit, Args));
821
822     // Destroy the pool...
823     Args.pop_back();
824
825     for (unsigned EN = 0, ENE = ReturnNodes.size(); EN != ENE; ++EN) {
826       Instruction *Destroy = new CallInst(PoolDestroy, Args);
827
828       // Insert it before the return instruction...
829       BasicBlock *RetNode = ReturnNodes[EN];
830       RetNode->getInstList().insert(RetNode->end()-1, Destroy);
831     }
832   }
833
834   // Insert the entry node code into the entry block...
835   F->getEntryNode()->getInstList().insert(F->getEntryNode()->begin()+1,
836                                           EntryNodeInsts.begin(),
837                                           EntryNodeInsts.end());
838 }
839
840
841 // addPoolPrototypes - Add prototypes for the pool methods to the specified
842 // module and update the Pool* instance variables to point to them.
843 //
844 void PoolAllocate::addPoolPrototypes(Module *M) {
845   // Get PoolInit function...
846   vector<const Type*> Args;
847   Args.push_back(PoolTy);           // Pool to initialize
848   Args.push_back(Type::UIntTy);     // Num bytes per element
849   FunctionType *PoolInitTy = FunctionType::get(Type::VoidTy, Args, false);
850   PoolInit = M->getOrInsertFunction("poolinit", PoolInitTy);
851
852   // Get pooldestroy function...
853   Args.pop_back();  // Only takes a pool...
854   FunctionType *PoolDestroyTy = FunctionType::get(Type::VoidTy, Args, false);
855   PoolDestroy = M->getOrInsertFunction("pooldestroy", PoolDestroyTy);
856
857   const Type *PtrVoid = PointerType::get(Type::SByteTy);
858
859   // Get the poolalloc function...
860   FunctionType *PoolAllocTy = FunctionType::get(PtrVoid, Args, false);
861   PoolAlloc = M->getOrInsertFunction("poolalloc", PoolAllocTy);
862
863   // Get the poolfree function...
864   Args.push_back(PtrVoid);
865   FunctionType *PoolFreeTy = FunctionType::get(Type::VoidTy, Args, false);
866   PoolFree = M->getOrInsertFunction("poolfree", PoolFreeTy);
867
868   // Add the %PoolTy type to the symbol table of the module...
869   M->addTypeName("PoolTy", PoolTy->getElementType());
870 }
871
872
873 bool PoolAllocate::run(Module *M) {
874   addPoolPrototypes(M);
875   CurModule = M;
876   
877   DS = &getAnalysis<DataStructure>();
878   bool Changed = false;
879
880   // We cannot use an iterator here because it will get invalidated when we add
881   // functions to the module later...
882   for (unsigned i = 0; i != M->size(); ++i)
883     if (!M->getFunctionList()[i]->isExternal()) {
884       Changed |= processFunction(M->getFunctionList()[i]);
885       if (Changed) {
886         cerr << "Only processing one function\n";
887         break;
888       }
889     }
890
891   CurModule = 0;
892   DS = 0;
893   return false;
894 }
895
896
897 // createPoolAllocatePass - Global function to access the functionality of this
898 // pass...
899 //
900 Pass *createPoolAllocatePass() { return new PoolAllocate(); }