blob: d8aa9aebe43bcafd66385f7974c55a8336778bad [file] [log] [blame]
Chris Lattner6c2e2e52002-11-19 22:04:49 +00001//===- CloneFunction.cpp - Clone a function into another function ---------===//
John Criswellb576c942003-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 Lattner6c2e2e52002-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 Lattnerfa703a42002-03-29 19:03:54 +000015
Chris Lattner309f1932002-11-19 20:59:41 +000016#include "llvm/Transforms/Utils/Cloning.h"
Chris Lattnerdcd80402002-11-19 21:54:07 +000017#include "llvm/iTerminators.h"
Chris Lattner5a8932f2002-11-19 23:12:22 +000018#include "llvm/DerivedTypes.h"
Chris Lattnerfa703a42002-03-29 19:03:54 +000019#include "llvm/Function.h"
Chris Lattner51cbcbf2002-11-20 20:47:41 +000020#include "ValueMapper.h"
Chris Lattnerfa703a42002-03-29 19:03:54 +000021
Brian Gaeked0fde302003-11-11 22:41:34 +000022namespace llvm {
23
Chris Lattnerfa703a42002-03-29 19:03:54 +000024// RemapInstruction - Convert the instruction operands from referencing the
25// current values into those specified by ValueMap.
26//
27static inline void RemapInstruction(Instruction *I,
28 std::map<const Value *, Value*> &ValueMap) {
29 for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) {
30 const Value *Op = I->getOperand(op);
Chris Lattner51cbcbf2002-11-20 20:47:41 +000031 Value *V = MapValue(Op, ValueMap);
Chris Lattnerfa703a42002-03-29 19:03:54 +000032#ifndef NDEBUG
33 if (!V) {
Anand Shukla3b5eabb2002-06-25 21:18:19 +000034 std::cerr << "Val = \n" << Op << "Addr = " << (void*)Op;
35 std::cerr << "\nInst = " << I;
Chris Lattnerfa703a42002-03-29 19:03:54 +000036 }
37#endif
38 assert(V && "Referenced value not in value map!");
39 I->setOperand(op, V);
40 }
41}
42
Chris Lattner17d145d2003-04-18 03:50:09 +000043// CloneBasicBlock - See comments in Cloning.h
44BasicBlock *CloneBasicBlock(const BasicBlock *BB,
45 std::map<const Value*, Value*> &ValueMap,
46 const char *NameSuffix) {
47 BasicBlock *NewBB = new BasicBlock("");
48 if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix);
49
50 // Loop over all instructions copying them over...
51 for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end();
52 II != IE; ++II) {
53 Instruction *NewInst = II->clone();
54 if (II->hasName())
55 NewInst->setName(II->getName()+NameSuffix);
56 NewBB->getInstList().push_back(NewInst);
57 ValueMap[II] = NewInst; // Add instruction map to value.
58 }
59 return NewBB;
60}
61
Chris Lattnerfa703a42002-03-29 19:03:54 +000062// Clone OldFunc into NewFunc, transforming the old arguments into references to
63// ArgMap values.
64//
65void CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
Chris Lattnerd1801552002-11-19 22:54:01 +000066 std::map<const Value*, Value*> &ValueMap,
Chris Lattnerdcd80402002-11-19 21:54:07 +000067 std::vector<ReturnInst*> &Returns,
68 const char *NameSuffix) {
69 assert(NameSuffix && "NameSuffix cannot be null!");
Chris Lattnerfa703a42002-03-29 19:03:54 +000070
Chris Lattnerd1801552002-11-19 22:54:01 +000071#ifndef NDEBUG
Chris Lattner18961502002-06-25 16:12:52 +000072 for (Function::const_aiterator I = OldFunc->abegin(), E = OldFunc->aend();
Chris Lattnerd1801552002-11-19 22:54:01 +000073 I != E; ++I)
74 assert(ValueMap.count(I) && "No mapping from source argument specified!");
75#endif
Chris Lattnerfa703a42002-03-29 19:03:54 +000076
77 // Loop over all of the basic blocks in the function, cloning them as
Chris Lattnerdcd80402002-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 Lattnerfa703a42002-03-29 19:03:54 +000080 //
81 for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
82 BI != BE; ++BI) {
Chris Lattner18961502002-06-25 16:12:52 +000083 const BasicBlock &BB = *BI;
Chris Lattnerfa703a42002-03-29 19:03:54 +000084
Chris Lattner17d145d2003-04-18 03:50:09 +000085 // Create a new basic block and copy instructions into it!
86 BasicBlock *CBB = CloneBasicBlock(&BB, ValueMap, NameSuffix);
87 NewFunc->getBasicBlockList().push_back(CBB);
Chris Lattner18961502002-06-25 16:12:52 +000088 ValueMap[&BB] = CBB; // Add basic block mapping.
Chris Lattnerfa703a42002-03-29 19:03:54 +000089
Chris Lattnerdcd80402002-11-19 21:54:07 +000090 if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator()))
91 Returns.push_back(RI);
Chris Lattnerfa703a42002-03-29 19:03:54 +000092 }
93
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.
96 //
Chris Lattner18961502002-06-25 16:12:52 +000097 for (Function::const_iterator BB = OldFunc->begin(), BE = OldFunc->end();
98 BB != BE; ++BB) {
Chris Lattnerfa703a42002-03-29 19:03:54 +000099 BasicBlock *NBB = cast<BasicBlock>(ValueMap[BB]);
100
101 // Loop over all instructions, fixing each one as we find it...
Chris Lattner18961502002-06-25 16:12:52 +0000102 for (BasicBlock::iterator II = NBB->begin(); II != NBB->end(); ++II)
103 RemapInstruction(II, ValueMap);
Chris Lattnerfa703a42002-03-29 19:03:54 +0000104 }
105}
Chris Lattner5a8932f2002-11-19 23:12:22 +0000106
107/// CloneFunction - Return a copy of the specified function, but without
108/// embedding the function into another module. Also, any references specified
109/// in the ValueMap are changed to refer to their mapped value instead of the
110/// original one. If any of the arguments to the function are in the ValueMap,
111/// the arguments are deleted from the resultant function. The ValueMap is
112/// updated to include mappings from all of the instructions and basicblocks in
113/// the function from their old to new values.
114///
115Function *CloneFunction(const Function *F,
116 std::map<const Value*, Value*> &ValueMap) {
117 std::vector<const Type*> ArgTypes;
118
119 // The user might be deleting arguments to the function by specifying them in
120 // the ValueMap. If so, we need to not add the arguments to the arg ty vector
121 //
122 for (Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
123 if (ValueMap.count(I) == 0) // Haven't mapped the argument to anything yet?
124 ArgTypes.push_back(I->getType());
125
126 // Create a new function type...
127 FunctionType *FTy = FunctionType::get(F->getFunctionType()->getReturnType(),
128 ArgTypes, F->getFunctionType()->isVarArg());
129
130 // Create the new function...
Chris Lattner4ad02e72003-04-16 20:28:45 +0000131 Function *NewF = new Function(FTy, F->getLinkage(), F->getName());
Chris Lattner5a8932f2002-11-19 23:12:22 +0000132
133 // Loop over the arguments, copying the names of the mapped arguments over...
134 Function::aiterator DestI = NewF->abegin();
135 for (Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
Chris Lattnerc09aab02002-11-20 18:32:31 +0000136 if (ValueMap.count(I) == 0) { // Is this argument preserved?
Chris Lattner5a8932f2002-11-19 23:12:22 +0000137 DestI->setName(I->getName()); // Copy the name over...
Chris Lattnerc09aab02002-11-20 18:32:31 +0000138 ValueMap[I] = DestI++; // Add mapping to ValueMap
Chris Lattner5a8932f2002-11-19 23:12:22 +0000139 }
140
141 std::vector<ReturnInst*> Returns; // Ignore returns cloned...
142 CloneFunctionInto(NewF, F, ValueMap, Returns);
143 return NewF;
144}
Brian Gaeked0fde302003-11-11 22:41:34 +0000145
146} // End llvm namespace