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