1 //===- CloneFunction.cpp - Clone a function into another function ---------===//
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.
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Transforms/Utils/Cloning.h"
10 #include "llvm/iTerminators.h"
11 #include "llvm/DerivedTypes.h"
12 #include "llvm/Function.h"
14 // RemapInstruction - Convert the instruction operands from referencing the
15 // current values into those specified by ValueMap.
17 static inline void RemapInstruction(Instruction *I,
18 std::map<const Value *, Value*> &ValueMap) {
19 for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) {
20 const Value *Op = I->getOperand(op);
21 Value *V = ValueMap[Op];
22 if (!V && (isa<GlobalValue>(Op) || isa<Constant>(Op)))
23 continue; // Globals and constants don't get relocated
27 std::cerr << "Val = \n" << Op << "Addr = " << (void*)Op;
28 std::cerr << "\nInst = " << I;
31 assert(V && "Referenced value not in value map!");
36 // Clone OldFunc into NewFunc, transforming the old arguments into references to
39 void CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
40 std::map<const Value*, Value*> &ValueMap,
41 std::vector<ReturnInst*> &Returns,
42 const char *NameSuffix) {
43 assert(NameSuffix && "NameSuffix cannot be null!");
46 for (Function::const_aiterator I = OldFunc->abegin(), E = OldFunc->aend();
48 assert(ValueMap.count(I) && "No mapping from source argument specified!");
51 // Loop over all of the basic blocks in the function, cloning them as
52 // appropriate. Note that we save BE this way in order to handle cloning of
53 // recursive functions into themselves.
55 for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
57 const BasicBlock &BB = *BI;
59 // Create a new basic block to copy instructions into!
60 BasicBlock *CBB = new BasicBlock("", NewFunc);
61 if (BB.hasName()) CBB->setName(BB.getName()+NameSuffix);
62 ValueMap[&BB] = CBB; // Add basic block mapping.
64 // Loop over all instructions copying them over...
65 for (BasicBlock::const_iterator II = BB.begin(), IE = BB.end();
67 Instruction *NewInst = II->clone();
69 NewInst->setName(II->getName()+NameSuffix); // Name is not cloned...
70 CBB->getInstList().push_back(NewInst);
71 ValueMap[II] = NewInst; // Add instruction map to value.
74 if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator()))
75 Returns.push_back(RI);
78 // Loop over all of the instructions in the function, fixing up operand
79 // references as we go. This uses ValueMap to do all the hard work.
81 for (Function::const_iterator BB = OldFunc->begin(), BE = OldFunc->end();
83 BasicBlock *NBB = cast<BasicBlock>(ValueMap[BB]);
85 // Loop over all instructions, fixing each one as we find it...
86 for (BasicBlock::iterator II = NBB->begin(); II != NBB->end(); ++II)
87 RemapInstruction(II, ValueMap);
91 /// CloneFunction - Return a copy of the specified function, but without
92 /// embedding the function into another module. Also, any references specified
93 /// in the ValueMap are changed to refer to their mapped value instead of the
94 /// original one. If any of the arguments to the function are in the ValueMap,
95 /// the arguments are deleted from the resultant function. The ValueMap is
96 /// updated to include mappings from all of the instructions and basicblocks in
97 /// the function from their old to new values.
99 Function *CloneFunction(const Function *F,
100 std::map<const Value*, Value*> &ValueMap) {
101 std::vector<const Type*> ArgTypes;
103 // The user might be deleting arguments to the function by specifying them in
104 // the ValueMap. If so, we need to not add the arguments to the arg ty vector
106 for (Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
107 if (ValueMap.count(I) == 0) // Haven't mapped the argument to anything yet?
108 ArgTypes.push_back(I->getType());
110 // Create a new function type...
111 FunctionType *FTy = FunctionType::get(F->getFunctionType()->getReturnType(),
112 ArgTypes, F->getFunctionType()->isVarArg());
114 // Create the new function...
115 Function *NewF = new Function(FTy, F->hasInternalLinkage(), F->getName());
117 // Loop over the arguments, copying the names of the mapped arguments over...
118 Function::aiterator DestI = NewF->abegin();
119 for (Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
120 if (ValueMap.count(I)) { // Is this argument preserved?
121 DestI->setName(I->getName()); // Copy the name over...
122 ValueMap[I] = DestI; // Add mapping to ValueMap
125 std::vector<ReturnInst*> Returns; // Ignore returns cloned...
126 CloneFunctionInto(NewF, F, ValueMap, Returns);