1 //===- CloneFunction.cpp - Clone a function into another function ---------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/Transforms/Utils/Cloning.h"
17 #include "llvm/iTerminators.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/Function.h"
20 #include "ValueMapper.h"
23 // RemapInstruction - Convert the instruction operands from referencing the
24 // current values into those specified by ValueMap.
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);
30 Value *V = MapValue(Op, ValueMap);
33 std::cerr << "Val = \n" << Op << "Addr = " << (void*)Op;
34 std::cerr << "\nInst = " << I;
37 assert(V && "Referenced value not in value map!");
42 // CloneBasicBlock - See comments in Cloning.h
43 BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB,
44 std::map<const Value*, Value*> &ValueMap,
45 const char *NameSuffix) {
46 BasicBlock *NewBB = new BasicBlock("");
47 if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix);
49 // Loop over all instructions copying them over...
50 for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end();
52 Instruction *NewInst = II->clone();
54 NewInst->setName(II->getName()+NameSuffix);
55 NewBB->getInstList().push_back(NewInst);
56 ValueMap[II] = NewInst; // Add instruction map to value.
61 // Clone OldFunc into NewFunc, transforming the old arguments into references to
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) {
68 assert(NameSuffix && "NameSuffix cannot be null!");
71 for (Function::const_aiterator I = OldFunc->abegin(), E = OldFunc->aend();
73 assert(ValueMap.count(I) && "No mapping from source argument specified!");
76 // Loop over all of the basic blocks in the function, cloning them as
77 // appropriate. Note that we save BE this way in order to handle cloning of
78 // recursive functions into themselves.
80 for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
82 const BasicBlock &BB = *BI;
84 // Create a new basic block and copy instructions into it!
85 BasicBlock *CBB = CloneBasicBlock(&BB, ValueMap, NameSuffix);
86 NewFunc->getBasicBlockList().push_back(CBB);
87 ValueMap[&BB] = CBB; // Add basic block mapping.
89 if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator()))
90 Returns.push_back(RI);
93 // Loop over all of the instructions in the function, fixing up operand
94 // references as we go. This uses ValueMap to do all the hard work.
96 for (Function::const_iterator BB = OldFunc->begin(), BE = OldFunc->end();
98 BasicBlock *NBB = cast<BasicBlock>(ValueMap[BB]);
100 // Loop over all instructions, fixing each one as we find it...
101 for (BasicBlock::iterator II = NBB->begin(); II != NBB->end(); ++II)
102 RemapInstruction(II, ValueMap);
106 /// CloneFunction - Return a copy of the specified function, but without
107 /// embedding the function into another module. Also, any references specified
108 /// in the ValueMap are changed to refer to their mapped value instead of the
109 /// original one. If any of the arguments to the function are in the ValueMap,
110 /// the arguments are deleted from the resultant function. The ValueMap is
111 /// updated to include mappings from all of the instructions and basicblocks in
112 /// the function from their old to new values.
114 Function *llvm::CloneFunction(const Function *F,
115 std::map<const Value*, Value*> &ValueMap) {
116 std::vector<const Type*> ArgTypes;
118 // The user might be deleting arguments to the function by specifying them in
119 // the ValueMap. If so, we need to not add the arguments to the arg ty vector
121 for (Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
122 if (ValueMap.count(I) == 0) // Haven't mapped the argument to anything yet?
123 ArgTypes.push_back(I->getType());
125 // Create a new function type...
126 FunctionType *FTy = FunctionType::get(F->getFunctionType()->getReturnType(),
127 ArgTypes, F->getFunctionType()->isVarArg());
129 // Create the new function...
130 Function *NewF = new Function(FTy, F->getLinkage(), F->getName());
132 // Loop over the arguments, copying the names of the mapped arguments over...
133 Function::aiterator DestI = NewF->abegin();
134 for (Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
135 if (ValueMap.count(I) == 0) { // Is this argument preserved?
136 DestI->setName(I->getName()); // Copy the name over...
137 ValueMap[I] = DestI++; // Add mapping to ValueMap
140 std::vector<ReturnInst*> Returns; // Ignore returns cloned...
141 CloneFunctionInto(NewF, F, ValueMap, Returns);