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