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/Instructions.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/Function.h"
20 #include "ValueMapper.h"
23 // CloneBasicBlock - See comments in Cloning.h
24 BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB,
25 std::map<const Value*, Value*> &ValueMap,
26 const char *NameSuffix, Function *F) {
27 BasicBlock *NewBB = new BasicBlock("", F);
28 if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix);
30 // Loop over all instructions copying them over...
31 for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end();
33 Instruction *NewInst = II->clone();
35 NewInst->setName(II->getName()+NameSuffix);
36 NewBB->getInstList().push_back(NewInst);
37 ValueMap[II] = NewInst; // Add instruction map to value.
42 // Clone OldFunc into NewFunc, transforming the old arguments into references to
45 void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
46 std::map<const Value*, Value*> &ValueMap,
47 std::vector<ReturnInst*> &Returns,
48 const char *NameSuffix) {
49 assert(NameSuffix && "NameSuffix cannot be null!");
52 for (Function::const_aiterator I = OldFunc->abegin(), E = OldFunc->aend();
54 assert(ValueMap.count(I) && "No mapping from source argument specified!");
57 // Loop over all of the basic blocks in the function, cloning them as
58 // appropriate. Note that we save BE this way in order to handle cloning of
59 // recursive functions into themselves.
61 for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
63 const BasicBlock &BB = *BI;
65 // Create a new basic block and copy instructions into it!
66 BasicBlock *CBB = CloneBasicBlock(&BB, ValueMap, NameSuffix, NewFunc);
67 ValueMap[&BB] = CBB; // Add basic block mapping.
69 if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator()))
70 Returns.push_back(RI);
73 // Loop over all of the instructions in the function, fixing up operand
74 // references as we go. This uses ValueMap to do all the hard work.
76 for (Function::iterator BB = cast<BasicBlock>(ValueMap[OldFunc->begin()]),
77 BE = NewFunc->end(); BB != BE; ++BB)
78 // Loop over all instructions, fixing each one as we find it...
79 for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ++II)
80 RemapInstruction(II, ValueMap);
83 /// CloneFunction - Return a copy of the specified function, but without
84 /// embedding the function into another module. Also, any references specified
85 /// in the ValueMap are changed to refer to their mapped value instead of the
86 /// original one. If any of the arguments to the function are in the ValueMap,
87 /// the arguments are deleted from the resultant function. The ValueMap is
88 /// updated to include mappings from all of the instructions and basicblocks in
89 /// the function from their old to new values.
91 Function *llvm::CloneFunction(const Function *F,
92 std::map<const Value*, Value*> &ValueMap) {
93 std::vector<const Type*> ArgTypes;
95 // The user might be deleting arguments to the function by specifying them in
96 // the ValueMap. If so, we need to not add the arguments to the arg ty vector
98 for (Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
99 if (ValueMap.count(I) == 0) // Haven't mapped the argument to anything yet?
100 ArgTypes.push_back(I->getType());
102 // Create a new function type...
103 FunctionType *FTy = FunctionType::get(F->getFunctionType()->getReturnType(),
104 ArgTypes, F->getFunctionType()->isVarArg());
106 // Create the new function...
107 Function *NewF = new Function(FTy, F->getLinkage(), F->getName());
109 // Loop over the arguments, copying the names of the mapped arguments over...
110 Function::aiterator DestI = NewF->abegin();
111 for (Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
112 if (ValueMap.count(I) == 0) { // Is this argument preserved?
113 DestI->setName(I->getName()); // Copy the name over...
114 ValueMap[I] = DestI++; // Add mapping to ValueMap
117 std::vector<ReturnInst*> Returns; // Ignore returns cloned...
118 CloneFunctionInto(NewF, F, ValueMap, Returns);