154a082b3aa77c06b6f4bcf1da402c3de2d51e5b
[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/DerivedTypes.h"
12 #include "llvm/Function.h"
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 }
90
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 ///
99 Function *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)
120     if (ValueMap.count(I)) {        // Is this argument preserved?
121       DestI->setName(I->getName()); // Copy the name over...
122       ValueMap[I] = DestI;          // Add mapping to ValueMap
123     }
124
125   std::vector<ReturnInst*> Returns;  // Ignore returns cloned...
126   CloneFunctionInto(NewF, F, ValueMap, Returns);
127   return NewF;                    
128 }