1 //===-- PoolAllocate.cpp - Pool Allocation Pass ---------------------------===//
3 // This transform changes programs so that disjoint data structures are
4 // allocated out of different pools of memory, increasing locality.
6 //===----------------------------------------------------------------------===//
8 #define DEBUG_TYPE "PoolAllocation"
9 #include "llvm/Transforms/PoolAllocate.h"
10 #include "llvm/Transforms/Utils/Cloning.h"
11 #include "llvm/Analysis/DataStructure.h"
12 #include "llvm/Analysis/DSGraph.h"
13 #include "llvm/Module.h"
14 #include "llvm/DerivedTypes.h"
15 #include "llvm/Constants.h"
16 #include "llvm/Instructions.h"
17 #include "llvm/Target/TargetData.h"
18 #include "llvm/Support/InstVisitor.h"
19 #include "Support/Debug.h"
20 #include "Support/VectorExtras.h"
24 const Type *VoidPtrTy = PointerType::get(Type::SByteTy);
26 // The type to allocate for a pool descriptor: { sbyte*, uint, uint }
27 // void *Data (the data)
28 // unsigned NodeSize (size of an allocated node)
29 // unsigned FreeablePool (are slabs in the pool freeable upon calls to
31 const Type *PoolDescType =
32 StructType::get(make_vector<const Type*>(VoidPtrTy, Type::UIntTy,
35 const PointerType *PoolDescPtr = PointerType::get(PoolDescType);
37 RegisterOpt<PoolAllocate>
38 X("poolalloc", "Pool allocate disjoint data structures");
41 void PoolAllocate::getAnalysisUsage(AnalysisUsage &AU) const {
42 AU.addRequired<BUDataStructures>();
43 AU.addRequired<TDDataStructures>();
44 AU.addRequired<TargetData>();
47 // Prints out the functions mapped to the leader of the equivalence class they
49 void PoolAllocate::printFuncECs() {
50 std::map<Function*, Function*> &leaderMap = FuncECs.getLeaderMap();
51 std::cerr << "Indirect Function Map \n";
52 for (std::map<Function*, Function*>::iterator LI = leaderMap.begin(),
53 LE = leaderMap.end(); LI != LE; ++LI) {
54 std::cerr << LI->first->getName() << ": leader is "
55 << LI->second->getName() << "\n";
59 static void printNTOMap(std::map<Value*, const Value*> &NTOM) {
60 std::cerr << "NTOM MAP\n";
61 for (std::map<Value*, const Value *>::iterator I = NTOM.begin(),
62 E = NTOM.end(); I != E; ++I) {
63 if (!isa<Function>(I->first) && !isa<BasicBlock>(I->first))
64 std::cerr << *I->first << " to " << *I->second << "\n";
68 void PoolAllocate::buildIndirectFunctionSets(Module &M) {
69 // Iterate over the module looking for indirect calls to functions
71 // Get top down DSGraph for the functions
72 TDDS = &getAnalysis<TDDataStructures>();
74 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI) {
76 DEBUG(std::cerr << "Processing indirect calls function:" << MI->getName() << "\n");
81 DSGraph &TDG = TDDS->getDSGraph(*MI);
83 std::vector<DSCallSite> callSites = TDG.getFunctionCalls();
85 // For each call site in the function
86 // All the functions that can be called at the call site are put in the
87 // same equivalence class.
88 for (std::vector<DSCallSite>::iterator CSI = callSites.begin(),
89 CSE = callSites.end(); CSI != CSE ; ++CSI) {
90 if (CSI->isIndirectCall()) {
91 DSNode *DSN = CSI->getCalleeNode();
92 if (DSN->isIncomplete())
93 std::cerr << "Incomplete node " << CSI->getCallInst();
94 // assert(DSN->isGlobalNode());
95 const std::vector<GlobalValue*> &Callees = DSN->getGlobals();
96 if (Callees.size() > 0) {
97 Function *firstCalledF = dyn_cast<Function>(*Callees.begin());
98 FuncECs.addElement(firstCalledF);
99 CallInstTargets.insert(std::pair<CallInst*,Function*>
100 (&CSI->getCallInst(),
102 if (Callees.size() > 1) {
103 for (std::vector<GlobalValue*>::const_iterator CalleesI =
104 Callees.begin()+1, CalleesE = Callees.end();
105 CalleesI != CalleesE; ++CalleesI) {
106 Function *calledF = dyn_cast<Function>(*CalleesI);
107 FuncECs.unionSetsWith(firstCalledF, calledF);
108 CallInstTargets.insert(std::pair<CallInst*,Function*>
109 (&CSI->getCallInst(), calledF));
113 std::cerr << "No targets " << CSI->getCallInst();
119 // Print the equivalence classes
120 DEBUG(printFuncECs());
123 bool PoolAllocate::run(Module &M) {
124 if (M.begin() == M.end()) return false;
128 BU = &getAnalysis<BUDataStructures>();
130 buildIndirectFunctionSets(M);
132 std::map<Function*, Function*> FuncMap;
134 // Loop over the functions in the original program finding the pool desc.
135 // arguments necessary for each function that is indirectly callable.
136 // For each equivalence class, make a list of pool arguments and update
137 // the PoolArgFirst and PoolArgLast values for each function.
138 Module::iterator LastOrigFunction = --M.end();
139 for (Module::iterator I = M.begin(); ; ++I) {
140 if (!I->isExternal())
141 FindFunctionPoolArgs(*I);
142 if (I == LastOrigFunction) break;
145 // Now clone a function using the pool arg list obtained in the previous
146 // pass over the modules.
147 // Loop over only the function initially in the program, don't traverse newly
148 // added ones. If the function uses memory, make its clone.
149 for (Module::iterator I = M.begin(); ; ++I) {
150 if (!I->isExternal())
151 if (Function *R = MakeFunctionClone(*I))
153 if (I == LastOrigFunction) break;
158 // Now that all call targets are available, rewrite the function bodies of the
160 for (Module::iterator I = M.begin(); I != LastOrigFunction; ++I)
161 if (!I->isExternal()) {
162 std::map<Function*, Function*>::iterator FI = FuncMap.find(I);
163 ProcessFunctionBody(*I, FI != FuncMap.end() ? *FI->second : *I);
167 std::cerr << "Pool Allocation successful! However all data structures may not be pool allocated\n";
173 // AddPoolPrototypes - Add prototypes for the pool functions to the specified
174 // module and update the Pool* instance variables to point to them.
176 void PoolAllocate::AddPoolPrototypes() {
177 CurModule->addTypeName("PoolDescriptor", PoolDescType);
179 // Get poolinit function...
180 FunctionType *PoolInitTy =
181 FunctionType::get(Type::VoidTy,
182 make_vector<const Type*>(PoolDescPtr, Type::UIntTy, 0),
184 PoolInit = CurModule->getOrInsertFunction("poolinit", PoolInitTy);
186 // Get pooldestroy function...
187 std::vector<const Type*> PDArgs(1, PoolDescPtr);
188 FunctionType *PoolDestroyTy =
189 FunctionType::get(Type::VoidTy, PDArgs, false);
190 PoolDestroy = CurModule->getOrInsertFunction("pooldestroy", PoolDestroyTy);
192 // Get the poolalloc function...
193 FunctionType *PoolAllocTy = FunctionType::get(VoidPtrTy, PDArgs, false);
194 PoolAlloc = CurModule->getOrInsertFunction("poolalloc", PoolAllocTy);
196 // Get the poolfree function...
197 PDArgs.push_back(VoidPtrTy); // Pointer to free
198 FunctionType *PoolFreeTy = FunctionType::get(Type::VoidTy, PDArgs, false);
199 PoolFree = CurModule->getOrInsertFunction("poolfree", PoolFreeTy);
201 // The poolallocarray function
202 FunctionType *PoolAllocArrayTy =
203 FunctionType::get(VoidPtrTy,
204 make_vector<const Type*>(PoolDescPtr, Type::UIntTy, 0),
206 PoolAllocArray = CurModule->getOrInsertFunction("poolallocarray",
211 // Inline the DSGraphs of functions corresponding to the potential targets at
212 // indirect call sites into the DS Graph of the callee.
213 // This is required to know what pools to create/pass at the call site in the
216 void PoolAllocate::InlineIndirectCalls(Function &F, DSGraph &G,
217 hash_set<Function*> &visited) {
218 std::vector<DSCallSite> callSites = G.getFunctionCalls();
222 // For each indirect call site in the function, inline all the potential
224 for (std::vector<DSCallSite>::iterator CSI = callSites.begin(),
225 CSE = callSites.end(); CSI != CSE; ++CSI) {
226 if (CSI->isIndirectCall()) {
227 CallInst &CI = CSI->getCallInst();
228 std::pair<std::multimap<CallInst*, Function*>::iterator,
229 std::multimap<CallInst*, Function*>::iterator> Targets =
230 CallInstTargets.equal_range(&CI);
231 for (std::multimap<CallInst*, Function*>::iterator TFI = Targets.first,
232 TFE = Targets.second; TFI != TFE; ++TFI) {
233 DSGraph &TargetG = BU->getDSGraph(*TFI->second);
234 // Call the function recursively if the callee is not yet inlined
235 // and if it hasn't been visited in this sequence of calls
236 // The latter is dependent on the fact that the graphs of all functions
237 // in an SCC are actually the same
238 if (InlinedFuncs.find(TFI->second) == InlinedFuncs.end() &&
239 visited.find(TFI->second) == visited.end()) {
240 InlineIndirectCalls(*TFI->second, TargetG, visited);
242 G.mergeInGraph(*CSI, *TFI->second, TargetG, DSGraph::KeepModRefBits |
243 DSGraph::KeepAllocaBit | DSGraph::DontCloneCallNodes |
244 DSGraph::DontCloneAuxCallNodes);
249 // Mark this function as one whose graph is inlined with its indirect
250 // function targets' DS Graphs. This ensures that every function is inlined
252 InlinedFuncs.insert(&F);
255 void PoolAllocate::FindFunctionPoolArgs(Function &F) {
257 // The DSGraph is merged with the globals graph.
258 DSGraph &G = BU->getDSGraph(F);
259 G.mergeInGlobalsGraph();
261 // Inline the potential targets of indirect calls
262 hash_set<Function*> visitedFuncs;
263 InlineIndirectCalls(F, G, visitedFuncs);
265 // At this point the DS Graphs have been modified in place including
266 // information about globals as well as indirect calls, making it useful
267 // for pool allocation
268 std::vector<DSNode*> &Nodes = G.getNodes();
269 if (Nodes.empty()) return ; // No memory activity, nothing is required
271 FuncInfo &FI = FunctionInfo[&F]; // Create a new entry for F
275 // Initialize the PoolArgFirst and PoolArgLast for the function depending
276 // on whether there have been other functions in the equivalence class
277 // that have pool arguments so far in the analysis.
278 if (!FuncECs.findClass(&F)) {
279 FI.PoolArgFirst = FI.PoolArgLast = 0;
281 if (EqClass2LastPoolArg.find(FuncECs.findClass(&F)) !=
282 EqClass2LastPoolArg.end())
283 FI.PoolArgFirst = FI.PoolArgLast =
284 EqClass2LastPoolArg[FuncECs.findClass(&F)] + 1;
286 FI.PoolArgFirst = FI.PoolArgLast = 0;
289 // Find DataStructure nodes which are allocated in pools non-local to the
290 // current function. This set will contain all of the DSNodes which require
291 // pools to be passed in from outside of the function.
292 hash_set<DSNode*> &MarkedNodes = FI.MarkedNodes;
294 // Mark globals and incomplete nodes as live... (this handles arguments)
295 if (F.getName() != "main")
296 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
297 if (Nodes[i]->isGlobalNode() && !Nodes[i]->isIncomplete())
298 DEBUG(std::cerr << "Global node is not Incomplete\n");
299 if ((Nodes[i]->isIncomplete() || Nodes[i]->isGlobalNode()) &&
300 Nodes[i]->isHeapNode())
301 Nodes[i]->markReachableNodes(MarkedNodes);
304 // Marked the returned node as alive...
305 if (DSNode *RetNode = G.getReturnNodeFor(F).getNode())
306 if (RetNode->isHeapNode())
307 RetNode->markReachableNodes(MarkedNodes);
309 if (MarkedNodes.empty()) // We don't need to clone the function if there
310 return; // are no incoming arguments to be added.
312 // Erase any marked node that is not a heap node
314 for (hash_set<DSNode*>::iterator I = MarkedNodes.begin(),
315 E = MarkedNodes.end(); I != E; ) {
316 // erase invalidates hash_set iterators if the iterator points to the
317 // element being erased
318 if (!(*I)->isHeapNode())
319 MarkedNodes.erase(I++);
324 FI.PoolArgLast += MarkedNodes.size();
327 if (FuncECs.findClass(&F)) {
328 // Update the equivalence class last pool argument information
329 // only if there actually were pool arguments to the function.
330 // Also, there is no entry for the Eq. class in EqClass2LastPoolArg
331 // if there are no functions in the equivalence class with pool arguments.
332 if (FI.PoolArgLast != FI.PoolArgFirst)
333 EqClass2LastPoolArg[FuncECs.findClass(&F)] = FI.PoolArgLast - 1;
338 // MakeFunctionClone - If the specified function needs to be modified for pool
339 // allocation support, make a clone of it, adding additional arguments as
340 // neccesary, and return it. If not, just return null.
342 Function *PoolAllocate::MakeFunctionClone(Function &F) {
344 DSGraph &G = BU->getDSGraph(F);
346 std::vector<DSNode*> &Nodes = G.getNodes();
350 FuncInfo &FI = FunctionInfo[&F];
352 hash_set<DSNode*> &MarkedNodes = FI.MarkedNodes;
354 if (!FuncECs.findClass(&F)) {
355 // Not in any equivalence class
356 if (MarkedNodes.empty())
359 // No need to clone if there are no pool arguments in any function in the
361 if (!EqClass2LastPoolArg.count(FuncECs.findClass(&F)))
365 // Figure out what the arguments are to be for the new version of the function
366 const FunctionType *OldFuncTy = F.getFunctionType();
367 std::vector<const Type*> ArgTys;
368 if (!FuncECs.findClass(&F)) {
369 ArgTys.reserve(OldFuncTy->getParamTypes().size() + MarkedNodes.size());
370 FI.ArgNodes.reserve(MarkedNodes.size());
371 for (hash_set<DSNode*>::iterator I = MarkedNodes.begin(),
372 E = MarkedNodes.end(); I != E; ++I) {
373 ArgTys.push_back(PoolDescPtr); // Add the appropriate # of pool descs
374 FI.ArgNodes.push_back(*I);
376 if (FI.ArgNodes.empty()) return 0; // No nodes to be pool allocated!
380 // This function is a member of an equivalence class and needs to be cloned
381 ArgTys.reserve(OldFuncTy->getParamTypes().size() +
382 EqClass2LastPoolArg[FuncECs.findClass(&F)] + 1);
383 FI.ArgNodes.reserve(EqClass2LastPoolArg[FuncECs.findClass(&F)] + 1);
385 for (int i = 0; i <= EqClass2LastPoolArg[FuncECs.findClass(&F)]; ++i) {
386 ArgTys.push_back(PoolDescPtr); // Add the appropriate # of pool
390 for (hash_set<DSNode*>::iterator I = MarkedNodes.begin(),
391 E = MarkedNodes.end(); I != E; ++I) {
392 FI.ArgNodes.push_back(*I);
395 assert ((FI.ArgNodes.size() == (unsigned) (FI.PoolArgLast -
397 "Number of ArgNodes equal to the number of pool arguments used by this function");
399 if (FI.ArgNodes.empty()) return 0;
403 ArgTys.insert(ArgTys.end(), OldFuncTy->getParamTypes().begin(),
404 OldFuncTy->getParamTypes().end());
407 // Create the new function prototype
408 FunctionType *FuncTy = FunctionType::get(OldFuncTy->getReturnType(), ArgTys,
409 OldFuncTy->isVarArg());
410 // Create the new function...
411 Function *New = new Function(FuncTy, GlobalValue::InternalLinkage,
412 F.getName(), F.getParent());
414 // Set the rest of the new arguments names to be PDa<n> and add entries to the
415 // pool descriptors map
416 std::map<DSNode*, Value*> &PoolDescriptors = FI.PoolDescriptors;
417 Function::aiterator NI = New->abegin();
419 if (FuncECs.findClass(&F)) {
420 // If the function belongs to an equivalence class
421 for (int i = 0; i <= EqClass2LastPoolArg[FuncECs.findClass(&F)]; ++i,
426 if (FI.PoolArgFirst > 0)
427 for (int i = 0; i < FI.PoolArgFirst; ++NI, ++i)
430 for (unsigned i = 0, e = FI.ArgNodes.size(); i != e; ++i, ++NI)
431 PoolDescriptors.insert(std::make_pair(FI.ArgNodes[i], NI));
434 if (EqClass2LastPoolArg.count(FuncECs.findClass(&F)))
435 for (int i = 0; i <= EqClass2LastPoolArg[FuncECs.findClass(&F)]; ++i, ++NI)
438 // If the function does not belong to an equivalence class
439 if (FI.ArgNodes.size())
440 for (unsigned i = 0, e = FI.ArgNodes.size(); i != e; ++i, ++NI) {
441 NI->setName("PDa"); // Add pd entry
442 PoolDescriptors.insert(std::make_pair(FI.ArgNodes[i], NI));
445 if (FI.ArgNodes.size())
446 for (unsigned i = 0; i < FI.ArgNodes.size(); ++NI, ++i)
450 // Map the existing arguments of the old function to the corresponding
451 // arguments of the new function.
452 std::map<const Value*, Value*> ValueMap;
453 if (NI != New->aend())
454 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I, ++NI) {
456 NI->setName(I->getName());
459 // Populate the value map with all of the globals in the program.
460 // FIXME: This should be unneccesary!
461 Module &M = *F.getParent();
462 for (Module::iterator I = M.begin(), E=M.end(); I!=E; ++I) ValueMap[I] = I;
463 for (Module::giterator I = M.gbegin(), E=M.gend(); I!=E; ++I) ValueMap[I] = I;
465 // Perform the cloning.
466 std::vector<ReturnInst*> Returns;
467 CloneFunctionInto(New, &F, ValueMap, Returns);
469 // Invert the ValueMap into the NewToOldValueMap
470 std::map<Value*, const Value*> &NewToOldValueMap = FI.NewToOldValueMap;
471 for (std::map<const Value*, Value*>::iterator I = ValueMap.begin(),
472 E = ValueMap.end(); I != E; ++I)
473 NewToOldValueMap.insert(std::make_pair(I->second, I->first));
475 return FI.Clone = New;
479 // processFunction - Pool allocate any data structures which are contained in
480 // the specified function...
482 void PoolAllocate::ProcessFunctionBody(Function &F, Function &NewF) {
483 DSGraph &G = BU->getDSGraph(F);
485 std::vector<DSNode*> &Nodes = G.getNodes();
486 if (Nodes.empty()) return; // Quick exit if nothing to do...
488 FuncInfo &FI = FunctionInfo[&F]; // Get FuncInfo for F
489 hash_set<DSNode*> &MarkedNodes = FI.MarkedNodes;
491 DEBUG(std::cerr << "[" << F.getName() << "] Pool Allocate: ");
493 // Loop over all of the nodes which are non-escaping, adding pool-allocatable
494 // ones to the NodesToPA vector.
495 std::vector<DSNode*> NodesToPA;
496 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
497 if (Nodes[i]->isHeapNode() && // Pick nodes with heap elems
498 !MarkedNodes.count(Nodes[i])) // Can't be marked
499 NodesToPA.push_back(Nodes[i]);
501 DEBUG(std::cerr << NodesToPA.size() << " nodes to pool allocate\n");
502 if (!NodesToPA.empty()) {
503 // Create pool construction/destruction code
504 std::map<DSNode*, Value*> &PoolDescriptors = FI.PoolDescriptors;
505 CreatePools(NewF, NodesToPA, PoolDescriptors);
508 // Transform the body of the function now...
509 TransformFunctionBody(NewF, F, G, FI);
513 // CreatePools - This creates the pool initialization and destruction code for
514 // the DSNodes specified by the NodesToPA list. This adds an entry to the
515 // PoolDescriptors map for each DSNode.
517 void PoolAllocate::CreatePools(Function &F,
518 const std::vector<DSNode*> &NodesToPA,
519 std::map<DSNode*, Value*> &PoolDescriptors) {
520 // Find all of the return nodes in the CFG...
521 std::vector<BasicBlock*> ReturnNodes;
522 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
523 if (isa<ReturnInst>(I->getTerminator()))
524 ReturnNodes.push_back(I);
526 TargetData &TD = getAnalysis<TargetData>();
528 // Loop over all of the pools, inserting code into the entry block of the
529 // function for the initialization and code in the exit blocks for
532 Instruction *InsertPoint = F.front().begin();
533 for (unsigned i = 0, e = NodesToPA.size(); i != e; ++i) {
534 DSNode *Node = NodesToPA[i];
536 // Create a new alloca instruction for the pool...
537 Value *AI = new AllocaInst(PoolDescType, 0, "PD", InsertPoint);
541 // Void types in DS graph are never used
542 if (Node->getType() != Type::VoidTy)
543 ElSize = ConstantUInt::get(Type::UIntTy, TD.getTypeSize(Node->getType()));
545 DEBUG(std::cerr << "Potential node collapsing in " << F.getName()
546 << ". All Data Structures may not be pool allocated\n");
547 ElSize = ConstantUInt::get(Type::UIntTy, 0);
550 // Insert the call to initialize the pool...
551 new CallInst(PoolInit, make_vector(AI, ElSize, 0), "", InsertPoint);
553 // Update the PoolDescriptors map
554 PoolDescriptors.insert(std::make_pair(Node, AI));
556 // Insert a call to pool destroy before each return inst in the function
557 for (unsigned r = 0, e = ReturnNodes.size(); r != e; ++r)
558 new CallInst(PoolDestroy, make_vector(AI, 0), "",
559 ReturnNodes[r]->getTerminator());
565 /// FuncTransform - This class implements transformation required of pool
566 /// allocated functions.
567 struct FuncTransform : public InstVisitor<FuncTransform> {
568 PoolAllocate &PAInfo;
569 DSGraph &G; // The Bottom-up DS Graph
570 DSGraph &TDG; // The Top-down DS Graph
573 FuncTransform(PoolAllocate &P, DSGraph &g, DSGraph &tdg, FuncInfo &fi)
574 : PAInfo(P), G(g), TDG(tdg), FI(fi) {
577 void visitMallocInst(MallocInst &MI);
578 void visitFreeInst(FreeInst &FI);
579 void visitCallInst(CallInst &CI);
581 // The following instructions are never modified by pool allocation
582 void visitBranchInst(BranchInst &I) { }
583 void visitBinaryOperator(Instruction &I) { }
584 void visitShiftInst (ShiftInst &I) { }
585 void visitSwitchInst (SwitchInst &I) { }
586 void visitCastInst (CastInst &I) { }
587 void visitAllocaInst(AllocaInst &I) { }
588 void visitLoadInst(LoadInst &I) { }
589 void visitGetElementPtrInst (GetElementPtrInst &I) { }
591 void visitReturnInst(ReturnInst &I);
592 void visitStoreInst (StoreInst &I);
593 void visitPHINode(PHINode &I);
595 void visitInstruction(Instruction &I) {
596 std::cerr << "PoolAllocate does not recognize this instruction\n";
601 DSNodeHandle& getDSNodeHFor(Value *V) {
602 // if (isa<Constant>(V))
603 // return DSNodeHandle();
605 if (!FI.NewToOldValueMap.empty()) {
606 // If the NewToOldValueMap is in effect, use it.
607 std::map<Value*,const Value*>::iterator I = FI.NewToOldValueMap.find(V);
608 if (I != FI.NewToOldValueMap.end())
609 V = (Value*)I->second;
612 return G.getScalarMap()[V];
615 DSNodeHandle& getTDDSNodeHFor(Value *V) {
616 if (!FI.NewToOldValueMap.empty()) {
617 // If the NewToOldValueMap is in effect, use it.
618 std::map<Value*,const Value*>::iterator I = FI.NewToOldValueMap.find(V);
619 if (I != FI.NewToOldValueMap.end())
620 V = (Value*)I->second;
623 return TDG.getScalarMap()[V];
626 Value *getPoolHandle(Value *V) {
627 DSNode *Node = getDSNodeHFor(V).getNode();
628 // Get the pool handle for this DSNode...
629 std::map<DSNode*, Value*>::iterator I = FI.PoolDescriptors.find(Node);
631 if (I != FI.PoolDescriptors.end()) {
632 // Check that the node pointed to by V in the TD DS graph is not
634 DSNode *TDNode = getTDDSNodeHFor(V).getNode();
635 if (TDNode->getType() != Type::VoidTy)
638 PAInfo.CollapseFlag = 1;
647 bool isFuncPtr(Value *V);
649 Function* getFuncClass(Value *V);
651 Value* retCloneIfFunc(Value *V);
655 void PoolAllocate::TransformFunctionBody(Function &F, Function &OldF,
656 DSGraph &G, FuncInfo &FI) {
657 FuncTransform(*this, G, TDDS->getDSGraph(OldF), FI).visit(F);
660 // Returns true if V is a function pointer
661 bool FuncTransform::isFuncPtr(Value *V) {
662 if (const PointerType *PTy = dyn_cast<PointerType>(V->getType()))
663 return isa<FunctionType>(PTy->getElementType());
667 // Given a function pointer, return the function eq. class if one exists
668 Function* FuncTransform::getFuncClass(Value *V) {
669 // Look at DSGraph and see if the set of of functions it could point to
670 // are pool allocated.
676 // if V is a constant
677 if (Function *theFunc = dyn_cast<Function>(V)) {
678 if (!PAInfo.FuncECs.findClass(theFunc))
679 // If this function does not belong to any equivalence class
681 if (PAInfo.EqClass2LastPoolArg.count(PAInfo.FuncECs.findClass(theFunc)))
682 return PAInfo.FuncECs.findClass(theFunc);
687 // if V is not a constant
688 DSNode *DSN = TDG.getNodeForValue(V).getNode();
692 const std::vector<GlobalValue*> &Callees = DSN->getGlobals();
693 if (Callees.size() > 0) {
694 Function *calledF = dyn_cast<Function>(*Callees.begin());
695 assert(PAInfo.FuncECs.findClass(calledF) && "should exist in some eq. class");
696 if (PAInfo.EqClass2LastPoolArg.count(PAInfo.FuncECs.findClass(calledF)))
697 return PAInfo.FuncECs.findClass(calledF);
703 // Returns the clone if V is a static function (not a pointer) and belongs
704 // to an equivalence class i.e. is pool allocated
705 Value* FuncTransform::retCloneIfFunc(Value *V) {
706 if (Function *fixedFunc = dyn_cast<Function>(V))
708 return PAInfo.getFuncInfo(*fixedFunc)->Clone;
713 void FuncTransform::visitReturnInst (ReturnInst &RI) {
714 if (RI.getNumOperands())
715 if (Value *clonedFunc = retCloneIfFunc(RI.getOperand(0))) {
716 // Cast the clone of RI.getOperand(0) to the non-pool-allocated type
717 CastInst *CastI = new CastInst(clonedFunc, RI.getOperand(0)->getType(),
719 // Insert return instruction that returns the casted value
720 ReturnInst *RetI = new ReturnInst(CastI, &RI);
722 // Remove original return instruction
723 RI.getParent()->getInstList().erase(&RI);
725 if (!FI.NewToOldValueMap.empty()) {
726 std::map<Value*,const Value*>::iterator II =
727 FI.NewToOldValueMap.find(&RI);
728 assert(II != FI.NewToOldValueMap.end() &&
729 "RI not found in clone?");
730 FI.NewToOldValueMap.insert(std::make_pair(RetI, II->second));
731 FI.NewToOldValueMap.erase(II);
736 void FuncTransform::visitStoreInst (StoreInst &SI) {
737 // Check if a constant function is being stored
738 if (Value *clonedFunc = retCloneIfFunc(SI.getOperand(0))) {
739 CastInst *CastI = new CastInst(clonedFunc, SI.getOperand(0)->getType(),
741 StoreInst *StoreI = new StoreInst(CastI, SI.getOperand(1), &SI);
742 SI.getParent()->getInstList().erase(&SI);
744 // Update the NewToOldValueMap if this is a clone
745 if (!FI.NewToOldValueMap.empty()) {
746 std::map<Value*,const Value*>::iterator II =
747 FI.NewToOldValueMap.find(&SI);
748 assert(II != FI.NewToOldValueMap.end() &&
749 "SI not found in clone?");
750 FI.NewToOldValueMap.insert(std::make_pair(StoreI, II->second));
751 FI.NewToOldValueMap.erase(II);
756 void FuncTransform::visitPHINode(PHINode &PI) {
757 // If any of the operands of the PHI node is a constant function pointer
758 // that is cloned, the cast instruction has to be inserted at the end of the
759 // previous basic block
761 if (isFuncPtr(&PI)) {
762 PHINode *V = new PHINode(PI.getType(), PI.getName(), &PI);
763 for (unsigned i = 0 ; i < PI.getNumIncomingValues(); ++i) {
764 if (Value *clonedFunc = retCloneIfFunc(PI.getIncomingValue(i))) {
765 // Insert CastInst at the end of PI.getIncomingBlock(i)
766 BasicBlock::iterator BBI = --PI.getIncomingBlock(i)->end();
767 // BBI now points to the terminator instruction of the basic block.
768 CastInst *CastI = new CastInst(clonedFunc, PI.getType(), "tmp", BBI);
769 V->addIncoming(CastI, PI.getIncomingBlock(i));
771 V->addIncoming(PI.getIncomingValue(i), PI.getIncomingBlock(i));
775 PI.replaceAllUsesWith(V);
776 PI.getParent()->getInstList().erase(&PI);
778 DSGraph::ScalarMapTy &SM = G.getScalarMap();
779 DSGraph::ScalarMapTy::iterator PII = SM.find(&PI);
781 // Update Scalar map of DSGraph if this is one of the original functions
782 // Otherwise update the NewToOldValueMap
783 if (PII != SM.end()) {
784 SM.insert(std::make_pair(V, PII->second));
785 SM.erase(PII); // Destroy the PHINode
787 std::map<Value*,const Value*>::iterator II =
788 FI.NewToOldValueMap.find(&PI);
789 assert(II != FI.NewToOldValueMap.end() &&
790 "PhiI not found in clone?");
791 FI.NewToOldValueMap.insert(std::make_pair(V, II->second));
792 FI.NewToOldValueMap.erase(II);
797 void FuncTransform::visitMallocInst(MallocInst &MI) {
798 // Get the pool handle for the node that this contributes to...
799 Value *PH = getPoolHandle(&MI);
801 // NB: PH is zero even if the node is collapsed
804 // Insert a call to poolalloc
806 if (MI.isArrayAllocation())
807 V = new CallInst(PAInfo.PoolAllocArray,
808 make_vector(PH, MI.getOperand(0), 0),
811 V = new CallInst(PAInfo.PoolAlloc, make_vector(PH, 0),
814 MI.setName(""); // Nuke MIs name
818 // Cast to the appropriate type if necessary
819 if (V->getType() != MI.getType()) {
820 Casted = new CastInst(V, MI.getType(), V->getName(), &MI);
823 // Update def-use info
824 MI.replaceAllUsesWith(Casted);
826 // Remove old malloc instruction
827 MI.getParent()->getInstList().erase(&MI);
829 DSGraph::ScalarMapTy &SM = G.getScalarMap();
830 DSGraph::ScalarMapTy::iterator MII = SM.find(&MI);
832 // If we are modifying the original function, update the DSGraph...
833 if (MII != SM.end()) {
834 // V and Casted now point to whatever the original malloc did...
835 SM.insert(std::make_pair(V, MII->second));
837 SM.insert(std::make_pair(Casted, MII->second));
838 SM.erase(MII); // The malloc is now destroyed
839 } else { // Otherwise, update the NewToOldValueMap
840 std::map<Value*,const Value*>::iterator MII =
841 FI.NewToOldValueMap.find(&MI);
842 assert(MII != FI.NewToOldValueMap.end() && "MI not found in clone?");
843 FI.NewToOldValueMap.insert(std::make_pair(V, MII->second));
845 FI.NewToOldValueMap.insert(std::make_pair(Casted, MII->second));
846 FI.NewToOldValueMap.erase(MII);
850 void FuncTransform::visitFreeInst(FreeInst &FrI) {
851 Value *Arg = FrI.getOperand(0);
852 Value *PH = getPoolHandle(Arg); // Get the pool handle for this DSNode...
854 // Insert a cast and a call to poolfree...
856 if (Arg->getType() != PointerType::get(Type::SByteTy))
857 Casted = new CastInst(Arg, PointerType::get(Type::SByteTy),
858 Arg->getName()+".casted", &FrI);
860 CallInst *FreeI = new CallInst(PAInfo.PoolFree, make_vector(PH, Casted, 0),
862 // Delete the now obsolete free instruction...
863 FrI.getParent()->getInstList().erase(&FrI);
865 // Update the NewToOldValueMap if this is a clone
866 if (!FI.NewToOldValueMap.empty()) {
867 std::map<Value*,const Value*>::iterator II =
868 FI.NewToOldValueMap.find(&FrI);
869 assert(II != FI.NewToOldValueMap.end() &&
870 "FrI not found in clone?");
871 FI.NewToOldValueMap.insert(std::make_pair(FreeI, II->second));
872 FI.NewToOldValueMap.erase(II);
876 static void CalcNodeMapping(DSNodeHandle& Caller, DSNodeHandle& Callee,
877 std::map<DSNode*, DSNode*> &NodeMapping) {
878 DSNode *CalleeNode = Callee.getNode();
879 DSNode *CallerNode = Caller.getNode();
881 unsigned CalleeOffset = Callee.getOffset();
882 unsigned CallerOffset = Caller.getOffset();
884 if (CalleeNode == 0) return;
886 // If callee has a node and caller doesn't, then a constant argument was
887 // passed by the caller
888 if (CallerNode == 0) {
889 NodeMapping.insert(NodeMapping.end(), std::make_pair(CalleeNode,
893 // Map the callee node to the caller node.
894 // NB: The callee node could be of a different type. Eg. if it points to the
895 // field of a struct that the caller points to
896 std::map<DSNode*, DSNode*>::iterator I = NodeMapping.find(CalleeNode);
897 if (I != NodeMapping.end()) { // Node already in map...
898 assert(I->second == CallerNode &&
899 "Node maps to different nodes on paths?");
901 NodeMapping.insert(I, std::make_pair(CalleeNode, CallerNode));
903 if (CalleeNode->getType() != CallerNode->getType() && CallerOffset == 0)
904 DEBUG(std::cerr << "NB: Mapping of nodes between different types\n");
906 // Recursively map the callee links to the caller links starting from the
907 // offset in the node into which they are mapped.
908 // Being a BU Graph, the callee ought to have smaller number of links unless
909 // there is collapsing in the caller
910 unsigned numCallerLinks = CallerNode->getNumLinks() - CallerOffset;
911 unsigned numCalleeLinks = CalleeNode->getNumLinks() - CalleeOffset;
913 if (numCallerLinks > 0) {
914 if (numCallerLinks < numCalleeLinks) {
915 DEBUG(std::cerr << "Potential node collapsing in caller\n");
916 for (unsigned i = 0, e = numCalleeLinks; i != e; ++i)
917 CalcNodeMapping(CallerNode->getLink(((i%numCallerLinks) << DS::PointerShift) + CallerOffset), CalleeNode->getLink((i << DS::PointerShift) + CalleeOffset), NodeMapping);
919 for (unsigned i = 0, e = numCalleeLinks; i != e; ++i)
920 CalcNodeMapping(CallerNode->getLink((i << DS::PointerShift) + CallerOffset), CalleeNode->getLink((i << DS::PointerShift) + CalleeOffset), NodeMapping);
922 } else if (numCalleeLinks > 0) {
924 "Caller has unexpanded node, due to indirect call perhaps!\n");
929 void FuncTransform::visitCallInst(CallInst &CI) {
930 Function *CF = CI.getCalledFunction();
932 // optimization for function pointers that are basically gotten from a cast
933 // with only one use and constant expressions with casts in them
935 if (CastInst* CastI = dyn_cast<CastInst>(CI.getCalledValue())) {
936 if (isa<Function>(CastI->getOperand(0)) &&
937 CastI->getOperand(0)->getType() == CastI->getType())
938 CF = dyn_cast<Function>(CastI->getOperand(0));
939 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(CI.getOperand(0))) {
940 if (CE->getOpcode() == Instruction::Cast) {
941 if (isa<ConstantPointerRef>(CE->getOperand(0)))
944 assert(0 && "Function pointer cast not handled as called function\n");
950 DSGraph &CallerG = G;
952 std::vector<Value*> Args;
953 if (!CF) { // Indirect call
954 DEBUG(std::cerr << " Handling call: " << CI);
956 std::map<unsigned, Value*> PoolArgs;
959 std::pair<std::multimap<CallInst*, Function*>::iterator,
960 std::multimap<CallInst*, Function*>::iterator> Targets =
961 PAInfo.CallInstTargets.equal_range(&CI);
962 for (std::multimap<CallInst*, Function*>::iterator TFI = Targets.first,
963 TFE = Targets.second; TFI != TFE; ++TFI) {
964 if (TFI == Targets.first) {
965 FuncClass = PAInfo.FuncECs.findClass(TFI->second);
966 // Nothing to transform if there are no pool arguments in this
967 // equivalence class of functions.
968 if (!PAInfo.EqClass2LastPoolArg.count(FuncClass))
972 FuncInfo *CFI = PAInfo.getFuncInfo(*TFI->second);
974 if (!CFI->ArgNodes.size()) continue; // Nothing to transform...
976 DSGraph &CG = PAInfo.getBUDataStructures().getDSGraph(*TFI->second);
977 std::map<DSNode*, DSNode*> NodeMapping;
979 Function::aiterator AI = TFI->second->abegin(), AE = TFI->second->aend();
981 for ( ; AI != AE; ++AI, ++OpNum) {
982 if (!isa<Constant>(CI.getOperand(OpNum)))
983 CalcNodeMapping(getDSNodeHFor(CI.getOperand(OpNum)),
984 CG.getScalarMap()[AI], NodeMapping);
986 assert(OpNum == CI.getNumOperands() && "Varargs calls not handled yet!");
988 if (CI.getType() != Type::VoidTy)
989 CalcNodeMapping(getDSNodeHFor(&CI),
990 CG.getReturnNodeFor(*TFI->second), NodeMapping);
992 // Map the nodes that are pointed to by globals.
993 // For all globals map getDSNodeForGlobal(g)->CG.getDSNodeForGlobal(g)
994 for (DSGraph::ScalarMapTy::iterator SMI = G.getScalarMap().begin(),
995 SME = G.getScalarMap().end(); SMI != SME; ++SMI)
996 if (isa<GlobalValue>(SMI->first)) {
997 CalcNodeMapping(SMI->second,
998 CG.getScalarMap()[SMI->first], NodeMapping);
1001 unsigned idx = CFI->PoolArgFirst;
1003 // The following loop determines the pool pointers corresponding to
1005 for (unsigned i = 0, e = CFI->ArgNodes.size(); i != e; ++i, ++idx) {
1006 if (NodeMapping.count(CFI->ArgNodes[i])) {
1007 assert(NodeMapping.count(CFI->ArgNodes[i]) && "Node not in mapping!");
1008 DSNode *LocalNode = NodeMapping.find(CFI->ArgNodes[i])->second;
1010 assert(FI.PoolDescriptors.count(LocalNode) &&
1011 "Node not pool allocated?");
1012 PoolArgs[idx] = FI.PoolDescriptors.find(LocalNode)->second;
1015 // LocalNode is null when a constant is passed in as a parameter
1016 PoolArgs[idx] = Constant::getNullValue(PoolDescPtr);
1018 PoolArgs[idx] = Constant::getNullValue(PoolDescPtr);
1023 // Push the pool arguments into Args.
1024 if (PAInfo.EqClass2LastPoolArg.count(FuncClass)) {
1025 for (int i = 0; i <= PAInfo.EqClass2LastPoolArg[FuncClass]; ++i) {
1026 if (PoolArgs.find(i) != PoolArgs.end())
1027 Args.push_back(PoolArgs[i]);
1029 Args.push_back(Constant::getNullValue(PoolDescPtr));
1032 assert(Args.size()== (unsigned) PAInfo.EqClass2LastPoolArg[FuncClass] + 1
1033 && "Call has same number of pool args as the called function");
1036 // Add the rest of the arguments (the original arguments of the function)...
1037 Args.insert(Args.end(), CI.op_begin()+1, CI.op_end());
1039 std::string Name = CI.getName();
1042 if (Args.size() > CI.getNumOperands() - 1) {
1043 // If there are any pool arguments
1045 new CastInst(CI.getOperand(0),
1046 PAInfo.getFuncInfo(*FuncClass)->Clone->getType(), "tmp",
1048 NewCall = new CallInst(CastI, Args, Name, &CI);
1050 NewCall = new CallInst(CI.getOperand(0), Args, Name, &CI);
1053 CI.replaceAllUsesWith(NewCall);
1054 DEBUG(std::cerr << " Result Call: " << *NewCall);
1056 if (CI.getType() != Type::VoidTy) {
1057 // If we are modifying the original function, update the DSGraph...
1058 DSGraph::ScalarMapTy &SM = G.getScalarMap();
1059 DSGraph::ScalarMapTy::iterator CII = SM.find(&CI);
1060 if (CII != SM.end()) {
1061 SM.insert(std::make_pair(NewCall, CII->second));
1062 SM.erase(CII); // Destroy the CallInst
1064 // Otherwise update the NewToOldValueMap with the new CI return value
1065 std::map<Value*,const Value*>::iterator CII =
1066 FI.NewToOldValueMap.find(&CI);
1067 assert(CII != FI.NewToOldValueMap.end() && "CI not found in clone?");
1068 FI.NewToOldValueMap.insert(std::make_pair(NewCall, CII->second));
1069 FI.NewToOldValueMap.erase(CII);
1071 } else if (!FI.NewToOldValueMap.empty()) {
1072 std::map<Value*,const Value*>::iterator II =
1073 FI.NewToOldValueMap.find(&CI);
1074 assert(II != FI.NewToOldValueMap.end() &&
1075 "CI not found in clone?");
1076 FI.NewToOldValueMap.insert(std::make_pair(NewCall, II->second));
1077 FI.NewToOldValueMap.erase(II);
1082 FuncInfo *CFI = PAInfo.getFuncInfo(*CF);
1084 if (CFI == 0 || CFI->Clone == 0) return; // Nothing to transform...
1086 DEBUG(std::cerr << " Handling call: " << CI);
1088 DSGraph &CG = PAInfo.getBUDataStructures().getDSGraph(*CF); // Callee graph
1090 // We need to figure out which local pool descriptors correspond to the pool
1091 // descriptor arguments passed into the function call. Calculate a mapping
1092 // from callee DSNodes to caller DSNodes. We construct a partial isomophism
1093 // between the graphs to figure out which pool descriptors need to be passed
1094 // in. The roots of this mapping is found from arguments and return values.
1096 std::map<DSNode*, DSNode*> NodeMapping;
1098 Function::aiterator AI = CF->abegin(), AE = CF->aend();
1100 for (; AI != AE; ++AI, ++OpNum) {
1101 Value *callOp = CI.getOperand(OpNum);
1102 if (!isa<Constant>(callOp))
1103 CalcNodeMapping(getDSNodeHFor(callOp), CG.getScalarMap()[AI],
1106 assert(OpNum == CI.getNumOperands() && "Varargs calls not handled yet!");
1108 // Map the return value as well...
1109 if (CI.getType() != Type::VoidTy)
1110 CalcNodeMapping(getDSNodeHFor(&CI), CG.getReturnNodeFor(*CF),
1113 // Map the nodes that are pointed to by globals.
1114 // For all globals map getDSNodeForGlobal(g)->CG.getDSNodeForGlobal(g)
1115 for (DSGraph::ScalarMapTy::iterator SMI = G.getScalarMap().begin(),
1116 SME = G.getScalarMap().end(); SMI != SME; ++SMI)
1117 if (isa<GlobalValue>(SMI->first)) {
1118 CalcNodeMapping(SMI->second,
1119 CG.getScalarMap()[SMI->first], NodeMapping);
1122 // Okay, now that we have established our mapping, we can figure out which
1123 // pool descriptors to pass in...
1125 // Add an argument for each pool which must be passed in...
1126 if (CFI->PoolArgFirst != 0) {
1127 for (int i = 0; i < CFI->PoolArgFirst; ++i)
1128 Args.push_back(Constant::getNullValue(PoolDescPtr));
1131 for (unsigned i = 0, e = CFI->ArgNodes.size(); i != e; ++i) {
1132 if (NodeMapping.count(CFI->ArgNodes[i])) {
1134 DSNode *LocalNode = NodeMapping.find(CFI->ArgNodes[i])->second;
1136 assert(FI.PoolDescriptors.count(LocalNode) &&
1137 "Node not pool allocated?");
1138 Args.push_back(FI.PoolDescriptors.find(LocalNode)->second);
1140 Args.push_back(Constant::getNullValue(PoolDescPtr));
1142 Args.push_back(Constant::getNullValue(PoolDescPtr));
1146 Function *FuncClass = PAInfo.FuncECs.findClass(CF);
1148 if (PAInfo.EqClass2LastPoolArg.count(FuncClass))
1149 for (int i = CFI->PoolArgLast;
1150 i <= PAInfo.EqClass2LastPoolArg[FuncClass]; ++i)
1151 Args.push_back(Constant::getNullValue(PoolDescPtr));
1153 // Add the rest of the arguments...
1154 Args.insert(Args.end(), CI.op_begin()+1, CI.op_end());
1156 std::string Name = CI.getName();
1158 std::map<Value*,const Value*>::iterator CNewII;
1160 Value *NewCall = new CallInst(CFI->Clone, Args, Name, &CI);
1162 CI.replaceAllUsesWith(NewCall);
1163 DEBUG(std::cerr << " Result Call: " << *NewCall);
1165 if (CI.getType() != Type::VoidTy) {
1166 // If we are modifying the original function, update the DSGraph...
1167 DSGraph::ScalarMapTy &SM = G.getScalarMap();
1168 DSGraph::ScalarMapTy::iterator CII = SM.find(&CI);
1169 if (CII != SM.end()) {
1170 SM.insert(std::make_pair(NewCall, CII->second));
1171 SM.erase(CII); // Destroy the CallInst
1173 // Otherwise update the NewToOldValueMap with the new CI return value
1174 std::map<Value*,const Value*>::iterator CNII =
1175 FI.NewToOldValueMap.find(&CI);
1176 assert(CNII != FI.NewToOldValueMap.end() && CNII->second &&
1177 "CI not found in clone?");
1178 FI.NewToOldValueMap.insert(std::make_pair(NewCall, CNII->second));
1179 FI.NewToOldValueMap.erase(CNII);
1181 } else if (!FI.NewToOldValueMap.empty()) {
1182 std::map<Value*,const Value*>::iterator II =
1183 FI.NewToOldValueMap.find(&CI);
1184 assert(II != FI.NewToOldValueMap.end() && "CI not found in clone?");
1185 FI.NewToOldValueMap.insert(std::make_pair(NewCall, II->second));
1186 FI.NewToOldValueMap.erase(II);
1190 CI.getParent()->getInstList().erase(&CI);