blob: 0a16eb9bb2709114f6d47ae487a0bf41bb54c082 [file] [log] [blame]
Chris Lattner8bce9882002-11-19 22:04:49 +00001//===- 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 Lattner16bfdb52002-03-29 19:03:54 +00008
Chris Lattner16667512002-11-19 20:59:41 +00009#include "llvm/Transforms/Utils/Cloning.h"
Chris Lattnerb1120052002-11-19 21:54:07 +000010#include "llvm/iTerminators.h"
Chris Lattnerfb311d22002-11-19 23:12:22 +000011#include "llvm/DerivedTypes.h"
Chris Lattner16bfdb52002-03-29 19:03:54 +000012#include "llvm/Function.h"
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000013#include "ValueMapper.h"
Chris Lattner16bfdb52002-03-29 19:03:54 +000014
Chris Lattner16bfdb52002-03-29 19:03:54 +000015// RemapInstruction - Convert the instruction operands from referencing the
16// current values into those specified by ValueMap.
17//
18static 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 Lattnere4dbb1a2002-11-20 20:47:41 +000022 Value *V = MapValue(Op, ValueMap);
Chris Lattner16bfdb52002-03-29 19:03:54 +000023#ifndef NDEBUG
24 if (!V) {
Anand Shukla2ac04a02002-06-25 21:18:19 +000025 std::cerr << "Val = \n" << Op << "Addr = " << (void*)Op;
26 std::cerr << "\nInst = " << I;
Chris Lattner16bfdb52002-03-29 19:03:54 +000027 }
28#endif
29 assert(V && "Referenced value not in value map!");
30 I->setOperand(op, V);
31 }
32}
33
Chris Lattnere9f42322003-04-18 03:50:09 +000034// CloneBasicBlock - See comments in Cloning.h
35BasicBlock *CloneBasicBlock(const BasicBlock *BB,
36 std::map<const Value*, Value*> &ValueMap,
37 const char *NameSuffix) {
38 BasicBlock *NewBB = new BasicBlock("");
39 if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix);
40
41 // Loop over all instructions copying them over...
42 for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end();
43 II != IE; ++II) {
44 Instruction *NewInst = II->clone();
45 if (II->hasName())
46 NewInst->setName(II->getName()+NameSuffix);
47 NewBB->getInstList().push_back(NewInst);
48 ValueMap[II] = NewInst; // Add instruction map to value.
49 }
50 return NewBB;
51}
52
Chris Lattner16bfdb52002-03-29 19:03:54 +000053// Clone OldFunc into NewFunc, transforming the old arguments into references to
54// ArgMap values.
55//
56void CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
Chris Lattnerc3626182002-11-19 22:54:01 +000057 std::map<const Value*, Value*> &ValueMap,
Chris Lattnerb1120052002-11-19 21:54:07 +000058 std::vector<ReturnInst*> &Returns,
59 const char *NameSuffix) {
60 assert(NameSuffix && "NameSuffix cannot be null!");
Chris Lattner16bfdb52002-03-29 19:03:54 +000061
Chris Lattnerc3626182002-11-19 22:54:01 +000062#ifndef NDEBUG
Chris Lattnerfda72b12002-06-25 16:12:52 +000063 for (Function::const_aiterator I = OldFunc->abegin(), E = OldFunc->aend();
Chris Lattnerc3626182002-11-19 22:54:01 +000064 I != E; ++I)
65 assert(ValueMap.count(I) && "No mapping from source argument specified!");
66#endif
Chris Lattner16bfdb52002-03-29 19:03:54 +000067
68 // Loop over all of the basic blocks in the function, cloning them as
Chris Lattnerb1120052002-11-19 21:54:07 +000069 // appropriate. Note that we save BE this way in order to handle cloning of
70 // recursive functions into themselves.
Chris Lattner16bfdb52002-03-29 19:03:54 +000071 //
72 for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
73 BI != BE; ++BI) {
Chris Lattnerfda72b12002-06-25 16:12:52 +000074 const BasicBlock &BB = *BI;
Chris Lattner16bfdb52002-03-29 19:03:54 +000075
Chris Lattnere9f42322003-04-18 03:50:09 +000076 // Create a new basic block and copy instructions into it!
77 BasicBlock *CBB = CloneBasicBlock(&BB, ValueMap, NameSuffix);
78 NewFunc->getBasicBlockList().push_back(CBB);
Chris Lattnerfda72b12002-06-25 16:12:52 +000079 ValueMap[&BB] = CBB; // Add basic block mapping.
Chris Lattner16bfdb52002-03-29 19:03:54 +000080
Chris Lattnerb1120052002-11-19 21:54:07 +000081 if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator()))
82 Returns.push_back(RI);
Chris Lattner16bfdb52002-03-29 19:03:54 +000083 }
84
85 // Loop over all of the instructions in the function, fixing up operand
86 // references as we go. This uses ValueMap to do all the hard work.
87 //
Chris Lattnerfda72b12002-06-25 16:12:52 +000088 for (Function::const_iterator BB = OldFunc->begin(), BE = OldFunc->end();
89 BB != BE; ++BB) {
Chris Lattner16bfdb52002-03-29 19:03:54 +000090 BasicBlock *NBB = cast<BasicBlock>(ValueMap[BB]);
91
92 // Loop over all instructions, fixing each one as we find it...
Chris Lattnerfda72b12002-06-25 16:12:52 +000093 for (BasicBlock::iterator II = NBB->begin(); II != NBB->end(); ++II)
94 RemapInstruction(II, ValueMap);
Chris Lattner16bfdb52002-03-29 19:03:54 +000095 }
96}
Chris Lattnerfb311d22002-11-19 23:12:22 +000097
98/// CloneFunction - Return a copy of the specified function, but without
99/// embedding the function into another module. Also, any references specified
100/// in the ValueMap are changed to refer to their mapped value instead of the
101/// original one. If any of the arguments to the function are in the ValueMap,
102/// the arguments are deleted from the resultant function. The ValueMap is
103/// updated to include mappings from all of the instructions and basicblocks in
104/// the function from their old to new values.
105///
106Function *CloneFunction(const Function *F,
107 std::map<const Value*, Value*> &ValueMap) {
108 std::vector<const Type*> ArgTypes;
109
110 // The user might be deleting arguments to the function by specifying them in
111 // the ValueMap. If so, we need to not add the arguments to the arg ty vector
112 //
113 for (Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
114 if (ValueMap.count(I) == 0) // Haven't mapped the argument to anything yet?
115 ArgTypes.push_back(I->getType());
116
117 // Create a new function type...
118 FunctionType *FTy = FunctionType::get(F->getFunctionType()->getReturnType(),
119 ArgTypes, F->getFunctionType()->isVarArg());
120
121 // Create the new function...
Chris Lattner379a8d22003-04-16 20:28:45 +0000122 Function *NewF = new Function(FTy, F->getLinkage(), F->getName());
Chris Lattnerfb311d22002-11-19 23:12:22 +0000123
124 // Loop over the arguments, copying the names of the mapped arguments over...
125 Function::aiterator DestI = NewF->abegin();
126 for (Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
Chris Lattner7c6d9d9e2002-11-20 18:32:31 +0000127 if (ValueMap.count(I) == 0) { // Is this argument preserved?
Chris Lattnerfb311d22002-11-19 23:12:22 +0000128 DestI->setName(I->getName()); // Copy the name over...
Chris Lattner7c6d9d9e2002-11-20 18:32:31 +0000129 ValueMap[I] = DestI++; // Add mapping to ValueMap
Chris Lattnerfb311d22002-11-19 23:12:22 +0000130 }
131
132 std::vector<ReturnInst*> Returns; // Ignore returns cloned...
133 CloneFunctionInto(NewF, F, ValueMap, Returns);
134 return NewF;
135}