Chris Lattner | 16bfdb5 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 1 | |
| 2 | // FIXME: document |
| 3 | |
Chris Lattner | 7608a46 | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 4 | #include "llvm/Transforms/Utils/CloneFunction.h" |
Chris Lattner | 16bfdb5 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 5 | #include "llvm/Function.h" |
| 6 | #include "llvm/BasicBlock.h" |
| 7 | #include "llvm/Instruction.h" |
| 8 | #include <map> |
Anand Shukla | 2ac04a0 | 2002-06-25 21:18:19 +0000 | [diff] [blame^] | 9 | #include <iostream> |
Chris Lattner | 16bfdb5 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 10 | |
Chris Lattner | f998685 | 2002-04-27 07:27:19 +0000 | [diff] [blame] | 11 | // FIXME: This should be merged with FunctionInlining |
Chris Lattner | 16bfdb5 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 12 | |
| 13 | // RemapInstruction - Convert the instruction operands from referencing the |
| 14 | // current values into those specified by ValueMap. |
| 15 | // |
| 16 | static inline void RemapInstruction(Instruction *I, |
| 17 | std::map<const Value *, Value*> &ValueMap) { |
| 18 | for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) { |
| 19 | const Value *Op = I->getOperand(op); |
| 20 | Value *V = ValueMap[Op]; |
| 21 | if (!V && (isa<GlobalValue>(Op) || isa<Constant>(Op))) |
| 22 | continue; // Globals and constants don't get relocated |
| 23 | |
| 24 | #ifndef NDEBUG |
| 25 | if (!V) { |
Anand Shukla | 2ac04a0 | 2002-06-25 21:18:19 +0000 | [diff] [blame^] | 26 | std::cerr << "Val = \n" << Op << "Addr = " << (void*)Op; |
| 27 | std::cerr << "\nInst = " << I; |
Chris Lattner | 16bfdb5 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 28 | } |
| 29 | #endif |
| 30 | assert(V && "Referenced value not in value map!"); |
| 31 | I->setOperand(op, V); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | // Clone OldFunc into NewFunc, transforming the old arguments into references to |
| 36 | // ArgMap values. |
| 37 | // |
| 38 | void CloneFunctionInto(Function *NewFunc, const Function *OldFunc, |
| 39 | const std::vector<Value*> &ArgMap) { |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 40 | assert(OldFunc->aempty() || !NewFunc->aempty() && |
Chris Lattner | 16bfdb5 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 41 | "Synthesization of arguments is not implemented yet!"); |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 42 | assert(OldFunc->asize() == ArgMap.size() && |
Chris Lattner | 16bfdb5 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 43 | "Improper number of argument values to map specified!"); |
| 44 | |
| 45 | // Keep a mapping between the original function's values and the new |
| 46 | // duplicated code's values. This includes all of: Function arguments, |
| 47 | // instruction values, constant pool entries, and basic blocks. |
| 48 | // |
| 49 | std::map<const Value *, Value*> ValueMap; |
| 50 | |
| 51 | // Add all of the function arguments to the mapping... |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 52 | unsigned i = 0; |
| 53 | for (Function::const_aiterator I = OldFunc->abegin(), E = OldFunc->aend(); |
| 54 | I != E; ++I, ++i) |
| 55 | ValueMap[I] = ArgMap[i]; |
Chris Lattner | 16bfdb5 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 56 | |
| 57 | |
| 58 | // Loop over all of the basic blocks in the function, cloning them as |
| 59 | // appropriate. |
| 60 | // |
| 61 | for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end(); |
| 62 | BI != BE; ++BI) { |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 63 | const BasicBlock &BB = *BI; |
| 64 | assert(BB.getTerminator() && "BasicBlock doesn't have terminator!?!?"); |
Chris Lattner | 16bfdb5 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 65 | |
| 66 | // Create a new basic block to copy instructions into! |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 67 | BasicBlock *CBB = new BasicBlock(BB.getName(), NewFunc); |
| 68 | ValueMap[&BB] = CBB; // Add basic block mapping. |
Chris Lattner | 16bfdb5 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 69 | |
| 70 | // Loop over all instructions copying them over... |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 71 | for (BasicBlock::const_iterator II = BB.begin(), IE = BB.end(); |
Chris Lattner | 16bfdb5 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 72 | II != IE; ++II) { |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 73 | Instruction *NewInst = II->clone(); |
| 74 | NewInst->setName(II->getName()); // Name is not cloned... |
Chris Lattner | 16bfdb5 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 75 | CBB->getInstList().push_back(NewInst); |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 76 | ValueMap[II] = NewInst; // Add instruction map to value. |
Chris Lattner | 16bfdb5 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
| 80 | // Loop over all of the instructions in the function, fixing up operand |
| 81 | // references as we go. This uses ValueMap to do all the hard work. |
| 82 | // |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 83 | for (Function::const_iterator BB = OldFunc->begin(), BE = OldFunc->end(); |
| 84 | BB != BE; ++BB) { |
Chris Lattner | 16bfdb5 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 85 | BasicBlock *NBB = cast<BasicBlock>(ValueMap[BB]); |
| 86 | |
| 87 | // Loop over all instructions, fixing each one as we find it... |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 88 | for (BasicBlock::iterator II = NBB->begin(); II != NBB->end(); ++II) |
| 89 | RemapInstruction(II, ValueMap); |
Chris Lattner | 16bfdb5 | 2002-03-29 19:03:54 +0000 | [diff] [blame] | 90 | } |
| 91 | } |