36b3a7e70144e4bfaa756424bf703e71ee301bda
[oota-llvm.git] / lib / Transforms / Utils / CloneFunction.cpp
1 //===- 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 //===----------------------------------------------------------------------===//
8
9 #include "llvm/Transforms/Utils/Cloning.h"
10 #include "llvm/iTerminators.h"
11 #include "llvm/Function.h"
12 #include <map>
13
14 // RemapInstruction - Convert the instruction operands from referencing the 
15 // current values into those specified by ValueMap.
16 //
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
24
25 #ifndef NDEBUG
26     if (!V) {
27       std::cerr << "Val = \n" << Op << "Addr = " << (void*)Op;
28       std::cerr << "\nInst = " << I;
29     }
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 //
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!");
44   
45 #ifndef NDEBUG
46   for (Function::const_aiterator I = OldFunc->abegin(), E = OldFunc->aend();
47        I != E; ++I)
48     assert(ValueMap.count(I) && "No mapping from source argument specified!");
49 #endif
50
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.
54   //
55   for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
56        BI != BE; ++BI) {
57     const BasicBlock &BB = *BI;
58     
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.
63
64     // Loop over all instructions copying them over...
65     for (BasicBlock::const_iterator II = BB.begin(), IE = BB.end();
66          II != IE; ++II) {
67       Instruction *NewInst = II->clone();
68       if (II->hasName())
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.
72     }
73
74     if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator()))
75       Returns.push_back(RI);
76   }
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   //
81   for (Function::const_iterator BB = OldFunc->begin(), BE = OldFunc->end();
82        BB != BE; ++BB) {
83     BasicBlock *NBB = cast<BasicBlock>(ValueMap[BB]);
84     
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);
88   }
89 }