blob: 17ad8c5175d3fa2b1e80fa01ad99582bba53aea2 [file] [log] [blame]
Chris Lattner8bce9882002-11-19 22:04:49 +00001//===- CloneFunction.cpp - Clone a function into another function ---------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
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 Lattnerb1120052002-11-19 21:54:07 +000017#include "llvm/iTerminators.h"
Chris Lattnerfb311d22002-11-19 23:12:22 +000018#include "llvm/DerivedTypes.h"
Chris Lattner16bfdb52002-03-29 19:03:54 +000019#include "llvm/Function.h"
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000020#include "ValueMapper.h"
Chris Lattnerdf3c3422004-01-09 06:12:26 +000021using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000022
Chris Lattner16bfdb52002-03-29 19:03:54 +000023// RemapInstruction - Convert the instruction operands from referencing the
24// current values into those specified by ValueMap.
25//
26static 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);
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000030 Value *V = MapValue(Op, ValueMap);
Chris Lattner16bfdb52002-03-29 19:03:54 +000031#ifndef NDEBUG
32 if (!V) {
Anand Shukla2ac04a02002-06-25 21:18:19 +000033 std::cerr << "Val = \n" << Op << "Addr = " << (void*)Op;
34 std::cerr << "\nInst = " << I;
Chris Lattner16bfdb52002-03-29 19:03:54 +000035 }
36#endif
37 assert(V && "Referenced value not in value map!");
38 I->setOperand(op, V);
39 }
40}
41
Chris Lattnere9f42322003-04-18 03:50:09 +000042// CloneBasicBlock - See comments in Cloning.h
Chris Lattnerdf3c3422004-01-09 06:12:26 +000043BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB,
44 std::map<const Value*, Value*> &ValueMap,
45 const char *NameSuffix) {
Chris Lattnere9f42322003-04-18 03:50:09 +000046 BasicBlock *NewBB = new BasicBlock("");
47 if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix);
48
49 // Loop over all instructions copying them over...
50 for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end();
51 II != IE; ++II) {
52 Instruction *NewInst = II->clone();
53 if (II->hasName())
54 NewInst->setName(II->getName()+NameSuffix);
55 NewBB->getInstList().push_back(NewInst);
56 ValueMap[II] = NewInst; // Add instruction map to value.
57 }
58 return NewBB;
59}
60
Chris Lattner16bfdb52002-03-29 19:03:54 +000061// Clone OldFunc into NewFunc, transforming the old arguments into references to
62// ArgMap values.
63//
Chris Lattnerdf3c3422004-01-09 06:12:26 +000064void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
65 std::map<const Value*, Value*> &ValueMap,
66 std::vector<ReturnInst*> &Returns,
67 const char *NameSuffix) {
Chris Lattnerb1120052002-11-19 21:54:07 +000068 assert(NameSuffix && "NameSuffix cannot be null!");
Chris Lattner16bfdb52002-03-29 19:03:54 +000069
Chris Lattnerc3626182002-11-19 22:54:01 +000070#ifndef NDEBUG
Chris Lattnerfda72b12002-06-25 16:12:52 +000071 for (Function::const_aiterator I = OldFunc->abegin(), E = OldFunc->aend();
Chris Lattnerc3626182002-11-19 22:54:01 +000072 I != E; ++I)
73 assert(ValueMap.count(I) && "No mapping from source argument specified!");
74#endif
Chris Lattner16bfdb52002-03-29 19:03:54 +000075
76 // Loop over all of the basic blocks in the function, cloning them as
Chris Lattnerb1120052002-11-19 21:54:07 +000077 // appropriate. Note that we save BE this way in order to handle cloning of
78 // recursive functions into themselves.
Chris Lattner16bfdb52002-03-29 19:03:54 +000079 //
80 for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
81 BI != BE; ++BI) {
Chris Lattnerfda72b12002-06-25 16:12:52 +000082 const BasicBlock &BB = *BI;
Chris Lattner16bfdb52002-03-29 19:03:54 +000083
Chris Lattnere9f42322003-04-18 03:50:09 +000084 // Create a new basic block and copy instructions into it!
85 BasicBlock *CBB = CloneBasicBlock(&BB, ValueMap, NameSuffix);
86 NewFunc->getBasicBlockList().push_back(CBB);
Chris Lattnerfda72b12002-06-25 16:12:52 +000087 ValueMap[&BB] = CBB; // Add basic block mapping.
Chris Lattner16bfdb52002-03-29 19:03:54 +000088
Chris Lattnerb1120052002-11-19 21:54:07 +000089 if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator()))
90 Returns.push_back(RI);
Chris Lattner16bfdb52002-03-29 19:03:54 +000091 }
92
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.
95 //
Chris Lattnerfda72b12002-06-25 16:12:52 +000096 for (Function::const_iterator BB = OldFunc->begin(), BE = OldFunc->end();
97 BB != BE; ++BB) {
Chris Lattner16bfdb52002-03-29 19:03:54 +000098 BasicBlock *NBB = cast<BasicBlock>(ValueMap[BB]);
99
100 // Loop over all instructions, fixing each one as we find it...
Chris Lattnerfda72b12002-06-25 16:12:52 +0000101 for (BasicBlock::iterator II = NBB->begin(); II != NBB->end(); ++II)
102 RemapInstruction(II, ValueMap);
Chris Lattner16bfdb52002-03-29 19:03:54 +0000103 }
104}
Chris Lattnerfb311d22002-11-19 23:12:22 +0000105
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.
113///
Chris Lattnerdf3c3422004-01-09 06:12:26 +0000114Function *llvm::CloneFunction(const Function *F,
115 std::map<const Value*, Value*> &ValueMap) {
Chris Lattnerfb311d22002-11-19 23:12:22 +0000116 std::vector<const Type*> ArgTypes;
117
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
120 //
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());
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());
Chris Lattnerfb311d22002-11-19 23:12:22 +0000131
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)
Chris Lattner7c6d9d9e2002-11-20 18:32:31 +0000135 if (ValueMap.count(I) == 0) { // Is this argument preserved?
Chris Lattnerfb311d22002-11-19 23:12:22 +0000136 DestI->setName(I->getName()); // Copy the name over...
Chris Lattner7c6d9d9e2002-11-20 18:32:31 +0000137 ValueMap[I] = DestI++; // Add mapping to ValueMap
Chris Lattnerfb311d22002-11-19 23:12:22 +0000138 }
139
140 std::vector<ReturnInst*> Returns; // Ignore returns cloned...
141 CloneFunctionInto(NewF, F, ValueMap, Returns);
142 return NewF;
143}
Brian Gaeke960707c2003-11-11 22:41:34 +0000144