blob: f6341fa3cf1d2056f9f15c6180ab7ae96fe9297b [file] [log] [blame]
Chris Lattner8bce9882002-11-19 22:04:49 +00001//===- CloneFunction.cpp - Clone a function into another function ---------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
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.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner8bce9882002-11-19 22:04:49 +00009//
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.
13//
14//===----------------------------------------------------------------------===//
Chris Lattner16bfdb52002-03-29 19:03:54 +000015
Chris Lattner16667512002-11-19 20:59:41 +000016#include "llvm/Transforms/Utils/Cloning.h"
Chris Lattneredad1282006-01-13 18:39:17 +000017#include "llvm/Constants.h"
Chris Lattnerfb311d22002-11-19 23:12:22 +000018#include "llvm/DerivedTypes.h"
Chris Lattneredad1282006-01-13 18:39:17 +000019#include "llvm/Instructions.h"
Chris Lattner16bfdb52002-03-29 19:03:54 +000020#include "llvm/Function.h"
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000021#include "ValueMapper.h"
Chris Lattnerdf3c3422004-01-09 06:12:26 +000022using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000023
Chris Lattnere9f42322003-04-18 03:50:09 +000024// CloneBasicBlock - See comments in Cloning.h
Chris Lattnerdf3c3422004-01-09 06:12:26 +000025BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB,
26 std::map<const Value*, Value*> &ValueMap,
Chris Lattneredad1282006-01-13 18:39:17 +000027 const char *NameSuffix, Function *F,
28 ClonedCodeInfo *CodeInfo) {
Chris Lattnera6578ef32004-02-04 01:19:43 +000029 BasicBlock *NewBB = new BasicBlock("", F);
Chris Lattnere9f42322003-04-18 03:50:09 +000030 if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix);
31
Chris Lattneredad1282006-01-13 18:39:17 +000032 bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false;
33
34 // Loop over all instructions, and copy them over.
Chris Lattnere9f42322003-04-18 03:50:09 +000035 for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end();
36 II != IE; ++II) {
37 Instruction *NewInst = II->clone();
38 if (II->hasName())
39 NewInst->setName(II->getName()+NameSuffix);
40 NewBB->getInstList().push_back(NewInst);
41 ValueMap[II] = NewInst; // Add instruction map to value.
Chris Lattneredad1282006-01-13 18:39:17 +000042
43 hasCalls |= isa<CallInst>(II);
44 if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) {
45 if (isa<ConstantInt>(AI->getArraySize()))
46 hasStaticAllocas = true;
47 else
48 hasDynamicAllocas = true;
49 }
50 }
51
52 if (CodeInfo) {
53 CodeInfo->ContainsCalls |= hasCalls;
54 CodeInfo->ContainsUnwinds |= isa<UnwindInst>(BB->getTerminator());
55 CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas;
56 CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas &&
57 BB != &BB->getParent()->front();
Chris Lattnere9f42322003-04-18 03:50:09 +000058 }
59 return NewBB;
60}
61
Chris Lattner16bfdb52002-03-29 19:03:54 +000062// Clone OldFunc into NewFunc, transforming the old arguments into references to
63// ArgMap values.
64//
Chris Lattnerdf3c3422004-01-09 06:12:26 +000065void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
66 std::map<const Value*, Value*> &ValueMap,
67 std::vector<ReturnInst*> &Returns,
Chris Lattneredad1282006-01-13 18:39:17 +000068 const char *NameSuffix, ClonedCodeInfo *CodeInfo) {
Chris Lattnerb1120052002-11-19 21:54:07 +000069 assert(NameSuffix && "NameSuffix cannot be null!");
Misha Brukmanb1c93172005-04-21 23:48:37 +000070
Chris Lattnerc3626182002-11-19 22:54:01 +000071#ifndef NDEBUG
Chris Lattneredad1282006-01-13 18:39:17 +000072 for (Function::const_arg_iterator I = OldFunc->arg_begin(),
73 E = OldFunc->arg_end(); I != E; ++I)
Chris Lattnerc3626182002-11-19 22:54:01 +000074 assert(ValueMap.count(I) && "No mapping from source argument specified!");
75#endif
Chris Lattner16bfdb52002-03-29 19:03:54 +000076
77 // Loop over all of the basic blocks in the function, cloning them as
Chris Lattnerb1120052002-11-19 21:54:07 +000078 // appropriate. Note that we save BE this way in order to handle cloning of
79 // recursive functions into themselves.
Chris Lattner16bfdb52002-03-29 19:03:54 +000080 //
81 for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
82 BI != BE; ++BI) {
Chris Lattnerfda72b12002-06-25 16:12:52 +000083 const BasicBlock &BB = *BI;
Misha Brukmanb1c93172005-04-21 23:48:37 +000084
Chris Lattnere9f42322003-04-18 03:50:09 +000085 // Create a new basic block and copy instructions into it!
Chris Lattneredad1282006-01-13 18:39:17 +000086 BasicBlock *CBB = CloneBasicBlock(&BB, ValueMap, NameSuffix, NewFunc,
87 CodeInfo);
Chris Lattnerfda72b12002-06-25 16:12:52 +000088 ValueMap[&BB] = CBB; // Add basic block mapping.
Chris Lattner16bfdb52002-03-29 19:03:54 +000089
Chris Lattnerb1120052002-11-19 21:54:07 +000090 if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator()))
91 Returns.push_back(RI);
Chris Lattner16bfdb52002-03-29 19:03:54 +000092 }
93
Misha Brukmanb1c93172005-04-21 23:48:37 +000094 // Loop over all of the instructions in the function, fixing up operand
Chris Lattner16bfdb52002-03-29 19:03:54 +000095 // references as we go. This uses ValueMap to do all the hard work.
96 //
Chris Lattner39ad6f22004-02-04 21:44:26 +000097 for (Function::iterator BB = cast<BasicBlock>(ValueMap[OldFunc->begin()]),
98 BE = NewFunc->end(); BB != BE; ++BB)
Chris Lattner16bfdb52002-03-29 19:03:54 +000099 // Loop over all instructions, fixing each one as we find it...
Chris Lattner39ad6f22004-02-04 21:44:26 +0000100 for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ++II)
Chris Lattnerfda72b12002-06-25 16:12:52 +0000101 RemapInstruction(II, ValueMap);
Chris Lattner16bfdb52002-03-29 19:03:54 +0000102}
Chris Lattnerfb311d22002-11-19 23:12:22 +0000103
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.
111///
Chris Lattnerdf3c3422004-01-09 06:12:26 +0000112Function *llvm::CloneFunction(const Function *F,
Chris Lattneredad1282006-01-13 18:39:17 +0000113 std::map<const Value*, Value*> &ValueMap,
114 ClonedCodeInfo *CodeInfo) {
Chris Lattnerfb311d22002-11-19 23:12:22 +0000115 std::vector<const Type*> ArgTypes;
116
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
119 //
Chris Lattneredad1282006-01-13 18:39:17 +0000120 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
121 I != E; ++I)
Chris Lattnerfb311d22002-11-19 23:12:22 +0000122 if (ValueMap.count(I) == 0) // Haven't mapped the argument to anything yet?
123 ArgTypes.push_back(I->getType());
124
125 // Create a new function type...
126 FunctionType *FTy = FunctionType::get(F->getFunctionType()->getReturnType(),
127 ArgTypes, F->getFunctionType()->isVarArg());
128
129 // Create the new function...
Chris Lattner379a8d22003-04-16 20:28:45 +0000130 Function *NewF = new Function(FTy, F->getLinkage(), F->getName());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000131
Chris Lattnerfb311d22002-11-19 23:12:22 +0000132 // Loop over the arguments, copying the names of the mapped arguments over...
Chris Lattner531f9e92005-03-15 04:54:21 +0000133 Function::arg_iterator DestI = NewF->arg_begin();
Chris Lattneredad1282006-01-13 18:39:17 +0000134 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
135 I != E; ++I)
Chris Lattner7c6d9d9e2002-11-20 18:32:31 +0000136 if (ValueMap.count(I) == 0) { // Is this argument preserved?
Chris Lattnerfb311d22002-11-19 23:12:22 +0000137 DestI->setName(I->getName()); // Copy the name over...
Chris Lattner7c6d9d9e2002-11-20 18:32:31 +0000138 ValueMap[I] = DestI++; // Add mapping to ValueMap
Chris Lattnerfb311d22002-11-19 23:12:22 +0000139 }
140
141 std::vector<ReturnInst*> Returns; // Ignore returns cloned...
Chris Lattneredad1282006-01-13 18:39:17 +0000142 CloneFunctionInto(NewF, F, ValueMap, Returns, "", CodeInfo);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000143 return NewF;
Chris Lattnerfb311d22002-11-19 23:12:22 +0000144}
Brian Gaeke960707c2003-11-11 22:41:34 +0000145