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/Constants.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/Function.h"
21 #include "ValueMapper.h"
24 // CloneBasicBlock - See comments in Cloning.h
25 BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB,
26 std::map<const Value*, Value*> &ValueMap,
27 const char *NameSuffix, Function *F,
28 ClonedCodeInfo *CodeInfo) {
29 BasicBlock *NewBB = new BasicBlock("", F);
30 if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix);
32 bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false;
34 // Loop over all instructions, and copy them over.
35 for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end();
37 Instruction *NewInst = II->clone();
39 NewInst->setName(II->getName()+NameSuffix);
40 NewBB->getInstList().push_back(NewInst);
41 ValueMap[II] = NewInst; // Add instruction map to value.
43 hasCalls |= isa<CallInst>(II);
44 if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) {
45 if (isa<ConstantInt>(AI->getArraySize()))
46 hasStaticAllocas = true;
48 hasDynamicAllocas = true;
53 CodeInfo->ContainsCalls |= hasCalls;
54 CodeInfo->ContainsUnwinds |= isa<UnwindInst>(BB->getTerminator());
55 CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas;
56 CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas &&
57 BB != &BB->getParent()->front();
62 // Clone OldFunc into NewFunc, transforming the old arguments into references to
65 void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
66 std::map<const Value*, Value*> &ValueMap,
67 std::vector<ReturnInst*> &Returns,
68 const char *NameSuffix, ClonedCodeInfo *CodeInfo) {
69 assert(NameSuffix && "NameSuffix cannot be null!");
72 for (Function::const_arg_iterator I = OldFunc->arg_begin(),
73 E = OldFunc->arg_end(); I != E; ++I)
74 assert(ValueMap.count(I) && "No mapping from source argument specified!");
77 // Loop over all of the basic blocks in the function, cloning them as
78 // appropriate. Note that we save BE this way in order to handle cloning of
79 // recursive functions into themselves.
81 for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
83 const BasicBlock &BB = *BI;
85 // Create a new basic block and copy instructions into it!
86 BasicBlock *CBB = CloneBasicBlock(&BB, ValueMap, NameSuffix, NewFunc,
88 ValueMap[&BB] = CBB; // Add basic block mapping.
90 if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator()))
91 Returns.push_back(RI);
94 // Loop over all of the instructions in the function, fixing up operand
95 // references as we go. This uses ValueMap to do all the hard work.
97 for (Function::iterator BB = cast<BasicBlock>(ValueMap[OldFunc->begin()]),
98 BE = NewFunc->end(); BB != BE; ++BB)
99 // Loop over all instructions, fixing each one as we find it...
100 for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ++II)
101 RemapInstruction(II, ValueMap);
104 /// CloneFunction - Return a copy of the specified function, but without
105 /// embedding the function into another module. Also, any references specified
106 /// in the ValueMap are changed to refer to their mapped value instead of the
107 /// original one. If any of the arguments to the function are in the ValueMap,
108 /// the arguments are deleted from the resultant function. The ValueMap is
109 /// updated to include mappings from all of the instructions and basicblocks in
110 /// the function from their old to new values.
112 Function *llvm::CloneFunction(const Function *F,
113 std::map<const Value*, Value*> &ValueMap,
114 ClonedCodeInfo *CodeInfo) {
115 std::vector<const Type*> ArgTypes;
117 // The user might be deleting arguments to the function by specifying them in
118 // the ValueMap. If so, we need to not add the arguments to the arg ty vector
120 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
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::arg_iterator DestI = NewF->arg_begin();
134 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
136 if (ValueMap.count(I) == 0) { // Is this argument preserved?
137 DestI->setName(I->getName()); // Copy the name over...
138 ValueMap[I] = DestI++; // Add mapping to ValueMap
141 std::vector<ReturnInst*> Returns; // Ignore returns cloned...
142 CloneFunctionInto(NewF, F, ValueMap, Returns, "", CodeInfo);