Chris Lattner | 6c2e2e5 | 2002-11-19 22:04:49 +0000 | [diff] [blame] | 1 | //===- CloneFunction.cpp - Clone a function into another function ---------===// |
| 2 | // |
| 3 | // This file implements the CloneFunctionInto interface, which is used as the |
| 4 | // low-level function cloner. This is used by the CloneFunction and function |
| 5 | // inliner to do the dirty work of copying the body of a function around. |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
Chris Lattner | fa703a4 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 8 | |
Chris Lattner | 309f193 | 2002-11-19 20:59:41 +0000 | [diff] [blame] | 9 | #include "llvm/Transforms/Utils/Cloning.h" |
Chris Lattner | dcd8040 | 2002-11-19 21:54:07 +0000 | [diff] [blame] | 10 | #include "llvm/iTerminators.h" |
Chris Lattner | 5a8932f | 2002-11-19 23:12:22 +0000 | [diff] [blame] | 11 | #include "llvm/DerivedTypes.h" |
Chris Lattner | fa703a4 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 12 | #include "llvm/Function.h" |
Chris Lattner | fa703a4 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 13 | |
Chris Lattner | fa703a4 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 14 | // RemapInstruction - Convert the instruction operands from referencing the |
| 15 | // current values into those specified by ValueMap. |
| 16 | // |
| 17 | static inline void RemapInstruction(Instruction *I, |
| 18 | std::map<const Value *, Value*> &ValueMap) { |
| 19 | for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) { |
| 20 | const Value *Op = I->getOperand(op); |
| 21 | Value *V = ValueMap[Op]; |
| 22 | if (!V && (isa<GlobalValue>(Op) || isa<Constant>(Op))) |
| 23 | continue; // Globals and constants don't get relocated |
| 24 | |
| 25 | #ifndef NDEBUG |
| 26 | if (!V) { |
Anand Shukla | 3b5eabb | 2002-06-25 21:18:19 +0000 | [diff] [blame] | 27 | std::cerr << "Val = \n" << Op << "Addr = " << (void*)Op; |
| 28 | std::cerr << "\nInst = " << I; |
Chris Lattner | fa703a4 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 29 | } |
| 30 | #endif |
| 31 | assert(V && "Referenced value not in value map!"); |
| 32 | I->setOperand(op, V); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | // Clone OldFunc into NewFunc, transforming the old arguments into references to |
| 37 | // ArgMap values. |
| 38 | // |
| 39 | void CloneFunctionInto(Function *NewFunc, const Function *OldFunc, |
Chris Lattner | d180155 | 2002-11-19 22:54:01 +0000 | [diff] [blame] | 40 | std::map<const Value*, Value*> &ValueMap, |
Chris Lattner | dcd8040 | 2002-11-19 21:54:07 +0000 | [diff] [blame] | 41 | std::vector<ReturnInst*> &Returns, |
| 42 | const char *NameSuffix) { |
| 43 | assert(NameSuffix && "NameSuffix cannot be null!"); |
Chris Lattner | fa703a4 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 44 | |
Chris Lattner | d180155 | 2002-11-19 22:54:01 +0000 | [diff] [blame] | 45 | #ifndef NDEBUG |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 46 | for (Function::const_aiterator I = OldFunc->abegin(), E = OldFunc->aend(); |
Chris Lattner | d180155 | 2002-11-19 22:54:01 +0000 | [diff] [blame] | 47 | I != E; ++I) |
| 48 | assert(ValueMap.count(I) && "No mapping from source argument specified!"); |
| 49 | #endif |
Chris Lattner | fa703a4 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 50 | |
| 51 | // Loop over all of the basic blocks in the function, cloning them as |
Chris Lattner | dcd8040 | 2002-11-19 21:54:07 +0000 | [diff] [blame] | 52 | // appropriate. Note that we save BE this way in order to handle cloning of |
| 53 | // recursive functions into themselves. |
Chris Lattner | fa703a4 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 54 | // |
| 55 | for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end(); |
| 56 | BI != BE; ++BI) { |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 57 | const BasicBlock &BB = *BI; |
Chris Lattner | fa703a4 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 58 | |
| 59 | // Create a new basic block to copy instructions into! |
Chris Lattner | dcd8040 | 2002-11-19 21:54:07 +0000 | [diff] [blame] | 60 | BasicBlock *CBB = new BasicBlock("", NewFunc); |
| 61 | if (BB.hasName()) CBB->setName(BB.getName()+NameSuffix); |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 62 | ValueMap[&BB] = CBB; // Add basic block mapping. |
Chris Lattner | fa703a4 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 63 | |
| 64 | // Loop over all instructions copying them over... |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 65 | for (BasicBlock::const_iterator II = BB.begin(), IE = BB.end(); |
Chris Lattner | fa703a4 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 66 | II != IE; ++II) { |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 67 | Instruction *NewInst = II->clone(); |
Chris Lattner | dcd8040 | 2002-11-19 21:54:07 +0000 | [diff] [blame] | 68 | if (II->hasName()) |
| 69 | NewInst->setName(II->getName()+NameSuffix); // Name is not cloned... |
Chris Lattner | fa703a4 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 70 | CBB->getInstList().push_back(NewInst); |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 71 | ValueMap[II] = NewInst; // Add instruction map to value. |
Chris Lattner | fa703a4 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 72 | } |
Chris Lattner | dcd8040 | 2002-11-19 21:54:07 +0000 | [diff] [blame] | 73 | |
| 74 | if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator())) |
| 75 | Returns.push_back(RI); |
Chris Lattner | fa703a4 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | // Loop over all of the instructions in the function, fixing up operand |
| 79 | // references as we go. This uses ValueMap to do all the hard work. |
| 80 | // |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 81 | for (Function::const_iterator BB = OldFunc->begin(), BE = OldFunc->end(); |
| 82 | BB != BE; ++BB) { |
Chris Lattner | fa703a4 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 83 | BasicBlock *NBB = cast<BasicBlock>(ValueMap[BB]); |
| 84 | |
| 85 | // Loop over all instructions, fixing each one as we find it... |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 86 | for (BasicBlock::iterator II = NBB->begin(); II != NBB->end(); ++II) |
| 87 | RemapInstruction(II, ValueMap); |
Chris Lattner | fa703a4 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
Chris Lattner | 5a8932f | 2002-11-19 23:12:22 +0000 | [diff] [blame] | 90 | |
| 91 | /// CloneFunction - Return a copy of the specified function, but without |
| 92 | /// embedding the function into another module. Also, any references specified |
| 93 | /// in the ValueMap are changed to refer to their mapped value instead of the |
| 94 | /// original one. If any of the arguments to the function are in the ValueMap, |
| 95 | /// the arguments are deleted from the resultant function. The ValueMap is |
| 96 | /// updated to include mappings from all of the instructions and basicblocks in |
| 97 | /// the function from their old to new values. |
| 98 | /// |
| 99 | Function *CloneFunction(const Function *F, |
| 100 | std::map<const Value*, Value*> &ValueMap) { |
| 101 | std::vector<const Type*> ArgTypes; |
| 102 | |
| 103 | // The user might be deleting arguments to the function by specifying them in |
| 104 | // the ValueMap. If so, we need to not add the arguments to the arg ty vector |
| 105 | // |
| 106 | for (Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I) |
| 107 | if (ValueMap.count(I) == 0) // Haven't mapped the argument to anything yet? |
| 108 | ArgTypes.push_back(I->getType()); |
| 109 | |
| 110 | // Create a new function type... |
| 111 | FunctionType *FTy = FunctionType::get(F->getFunctionType()->getReturnType(), |
| 112 | ArgTypes, F->getFunctionType()->isVarArg()); |
| 113 | |
| 114 | // Create the new function... |
| 115 | Function *NewF = new Function(FTy, F->hasInternalLinkage(), F->getName()); |
| 116 | |
| 117 | // Loop over the arguments, copying the names of the mapped arguments over... |
| 118 | Function::aiterator DestI = NewF->abegin(); |
| 119 | for (Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I) |
Chris Lattner | c09aab0 | 2002-11-20 18:32:31 +0000 | [diff] [blame] | 120 | if (ValueMap.count(I) == 0) { // Is this argument preserved? |
Chris Lattner | 5a8932f | 2002-11-19 23:12:22 +0000 | [diff] [blame] | 121 | DestI->setName(I->getName()); // Copy the name over... |
Chris Lattner | c09aab0 | 2002-11-20 18:32:31 +0000 | [diff] [blame] | 122 | ValueMap[I] = DestI++; // Add mapping to ValueMap |
Chris Lattner | 5a8932f | 2002-11-19 23:12:22 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | std::vector<ReturnInst*> Returns; // Ignore returns cloned... |
| 126 | CloneFunctionInto(NewF, F, ValueMap, Returns); |
| 127 | return NewF; |
| 128 | } |