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