blob: 29c7aaa037427c365b4874f932a4fc13b015524b [file] [log] [blame]
Chris Lattner6c2e2e52002-11-19 22:04:49 +00001//===- CloneFunction.cpp - Clone a function into another function ---------===//
2//
3// This file implements the CloneFunctionInto interface, which is used as the
4// low-level function cloner. This is used by the CloneFunction and function
5// inliner to do the dirty work of copying the body of a function around.
6//
7//===----------------------------------------------------------------------===//
Chris Lattnerfa703a42002-03-29 19:03:54 +00008
Chris Lattner309f1932002-11-19 20:59:41 +00009#include "llvm/Transforms/Utils/Cloning.h"
Chris Lattnerdcd80402002-11-19 21:54:07 +000010#include "llvm/iTerminators.h"
Chris Lattner5a8932f2002-11-19 23:12:22 +000011#include "llvm/DerivedTypes.h"
Chris Lattnerfa703a42002-03-29 19:03:54 +000012#include "llvm/Function.h"
Chris Lattnerfa703a42002-03-29 19:03:54 +000013
Chris Lattnerfa703a42002-03-29 19:03:54 +000014// RemapInstruction - Convert the instruction operands from referencing the
15// current values into those specified by ValueMap.
16//
17static inline void RemapInstruction(Instruction *I,
18 std::map<const Value *, Value*> &ValueMap) {
19 for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) {
20 const Value *Op = I->getOperand(op);
21 Value *V = ValueMap[Op];
22 if (!V && (isa<GlobalValue>(Op) || isa<Constant>(Op)))
23 continue; // Globals and constants don't get relocated
24
25#ifndef NDEBUG
26 if (!V) {
Anand Shukla3b5eabb2002-06-25 21:18:19 +000027 std::cerr << "Val = \n" << Op << "Addr = " << (void*)Op;
28 std::cerr << "\nInst = " << I;
Chris Lattnerfa703a42002-03-29 19:03:54 +000029 }
30#endif
31 assert(V && "Referenced value not in value map!");
32 I->setOperand(op, V);
33 }
34}
35
36// Clone OldFunc into NewFunc, transforming the old arguments into references to
37// ArgMap values.
38//
39void CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
Chris Lattnerd1801552002-11-19 22:54:01 +000040 std::map<const Value*, Value*> &ValueMap,
Chris Lattnerdcd80402002-11-19 21:54:07 +000041 std::vector<ReturnInst*> &Returns,
42 const char *NameSuffix) {
43 assert(NameSuffix && "NameSuffix cannot be null!");
Chris Lattnerfa703a42002-03-29 19:03:54 +000044
Chris Lattnerd1801552002-11-19 22:54:01 +000045#ifndef NDEBUG
Chris Lattner18961502002-06-25 16:12:52 +000046 for (Function::const_aiterator I = OldFunc->abegin(), E = OldFunc->aend();
Chris Lattnerd1801552002-11-19 22:54:01 +000047 I != E; ++I)
48 assert(ValueMap.count(I) && "No mapping from source argument specified!");
49#endif
Chris Lattnerfa703a42002-03-29 19:03:54 +000050
51 // Loop over all of the basic blocks in the function, cloning them as
Chris Lattnerdcd80402002-11-19 21:54:07 +000052 // appropriate. Note that we save BE this way in order to handle cloning of
53 // recursive functions into themselves.
Chris Lattnerfa703a42002-03-29 19:03:54 +000054 //
55 for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
56 BI != BE; ++BI) {
Chris Lattner18961502002-06-25 16:12:52 +000057 const BasicBlock &BB = *BI;
Chris Lattnerfa703a42002-03-29 19:03:54 +000058
59 // Create a new basic block to copy instructions into!
Chris Lattnerdcd80402002-11-19 21:54:07 +000060 BasicBlock *CBB = new BasicBlock("", NewFunc);
61 if (BB.hasName()) CBB->setName(BB.getName()+NameSuffix);
Chris Lattner18961502002-06-25 16:12:52 +000062 ValueMap[&BB] = CBB; // Add basic block mapping.
Chris Lattnerfa703a42002-03-29 19:03:54 +000063
64 // Loop over all instructions copying them over...
Chris Lattner18961502002-06-25 16:12:52 +000065 for (BasicBlock::const_iterator II = BB.begin(), IE = BB.end();
Chris Lattnerfa703a42002-03-29 19:03:54 +000066 II != IE; ++II) {
Chris Lattner18961502002-06-25 16:12:52 +000067 Instruction *NewInst = II->clone();
Chris Lattnerdcd80402002-11-19 21:54:07 +000068 if (II->hasName())
69 NewInst->setName(II->getName()+NameSuffix); // Name is not cloned...
Chris Lattnerfa703a42002-03-29 19:03:54 +000070 CBB->getInstList().push_back(NewInst);
Chris Lattner18961502002-06-25 16:12:52 +000071 ValueMap[II] = NewInst; // Add instruction map to value.
Chris Lattnerfa703a42002-03-29 19:03:54 +000072 }
Chris Lattnerdcd80402002-11-19 21:54:07 +000073
74 if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator()))
75 Returns.push_back(RI);
Chris Lattnerfa703a42002-03-29 19:03:54 +000076 }
77
78 // Loop over all of the instructions in the function, fixing up operand
79 // references as we go. This uses ValueMap to do all the hard work.
80 //
Chris Lattner18961502002-06-25 16:12:52 +000081 for (Function::const_iterator BB = OldFunc->begin(), BE = OldFunc->end();
82 BB != BE; ++BB) {
Chris Lattnerfa703a42002-03-29 19:03:54 +000083 BasicBlock *NBB = cast<BasicBlock>(ValueMap[BB]);
84
85 // Loop over all instructions, fixing each one as we find it...
Chris Lattner18961502002-06-25 16:12:52 +000086 for (BasicBlock::iterator II = NBB->begin(); II != NBB->end(); ++II)
87 RemapInstruction(II, ValueMap);
Chris Lattnerfa703a42002-03-29 19:03:54 +000088 }
89}
Chris Lattner5a8932f2002-11-19 23:12:22 +000090
91/// CloneFunction - Return a copy of the specified function, but without
92/// embedding the function into another module. Also, any references specified
93/// in the ValueMap are changed to refer to their mapped value instead of the
94/// original one. If any of the arguments to the function are in the ValueMap,
95/// the arguments are deleted from the resultant function. The ValueMap is
96/// updated to include mappings from all of the instructions and basicblocks in
97/// the function from their old to new values.
98///
99Function *CloneFunction(const Function *F,
100 std::map<const Value*, Value*> &ValueMap) {
101 std::vector<const Type*> ArgTypes;
102
103 // The user might be deleting arguments to the function by specifying them in
104 // the ValueMap. If so, we need to not add the arguments to the arg ty vector
105 //
106 for (Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
107 if (ValueMap.count(I) == 0) // Haven't mapped the argument to anything yet?
108 ArgTypes.push_back(I->getType());
109
110 // Create a new function type...
111 FunctionType *FTy = FunctionType::get(F->getFunctionType()->getReturnType(),
112 ArgTypes, F->getFunctionType()->isVarArg());
113
114 // Create the new function...
115 Function *NewF = new Function(FTy, F->hasInternalLinkage(), F->getName());
116
117 // Loop over the arguments, copying the names of the mapped arguments over...
118 Function::aiterator DestI = NewF->abegin();
119 for (Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
Chris Lattnerc09aab02002-11-20 18:32:31 +0000120 if (ValueMap.count(I) == 0) { // Is this argument preserved?
Chris Lattner5a8932f2002-11-19 23:12:22 +0000121 DestI->setName(I->getName()); // Copy the name over...
Chris Lattnerc09aab02002-11-20 18:32:31 +0000122 ValueMap[I] = DestI++; // Add mapping to ValueMap
Chris Lattner5a8932f2002-11-19 23:12:22 +0000123 }
124
125 std::vector<ReturnInst*> Returns; // Ignore returns cloned...
126 CloneFunctionInto(NewF, F, ValueMap, Returns);
127 return NewF;
128}