1 //===- CloneFunction.cpp - Clone a function into another function ---------===//
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.
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Transforms/Utils/Cloning.h"
10 #include "llvm/iTerminators.h"
11 #include "llvm/Function.h"
14 // RemapInstruction - Convert the instruction operands from referencing the
15 // current values into those specified by ValueMap.
17 static 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
27 std::cerr << "Val = \n" << Op << "Addr = " << (void*)Op;
28 std::cerr << "\nInst = " << I;
31 assert(V && "Referenced value not in value map!");
36 // Clone OldFunc into NewFunc, transforming the old arguments into references to
39 void CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
40 std::map<const Value*, Value*> &ValueMap,
41 std::vector<ReturnInst*> &Returns,
42 const char *NameSuffix) {
43 assert(NameSuffix && "NameSuffix cannot be null!");
46 for (Function::const_aiterator I = OldFunc->abegin(), E = OldFunc->aend();
48 assert(ValueMap.count(I) && "No mapping from source argument specified!");
51 // Loop over all of the basic blocks in the function, cloning them as
52 // appropriate. Note that we save BE this way in order to handle cloning of
53 // recursive functions into themselves.
55 for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
57 const BasicBlock &BB = *BI;
59 // Create a new basic block to copy instructions into!
60 BasicBlock *CBB = new BasicBlock("", NewFunc);
61 if (BB.hasName()) CBB->setName(BB.getName()+NameSuffix);
62 ValueMap[&BB] = CBB; // Add basic block mapping.
64 // Loop over all instructions copying them over...
65 for (BasicBlock::const_iterator II = BB.begin(), IE = BB.end();
67 Instruction *NewInst = II->clone();
69 NewInst->setName(II->getName()+NameSuffix); // Name is not cloned...
70 CBB->getInstList().push_back(NewInst);
71 ValueMap[II] = NewInst; // Add instruction map to value.
74 if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator()))
75 Returns.push_back(RI);
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.
81 for (Function::const_iterator BB = OldFunc->begin(), BE = OldFunc->end();
83 BasicBlock *NBB = cast<BasicBlock>(ValueMap[BB]);
85 // Loop over all instructions, fixing each one as we find it...
86 for (BasicBlock::iterator II = NBB->begin(); II != NBB->end(); ++II)
87 RemapInstruction(II, ValueMap);