Delete dead line
[oota-llvm.git] / lib / Transforms / Utils / CloneFunction.cpp
1 //===- CloneFunction.cpp - Clone a function into another function ---------===//
2 // 
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 //===----------------------------------------------------------------------===//
9 //
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 //===----------------------------------------------------------------------===//
15
16 #include "llvm/Transforms/Utils/Cloning.h"
17 #include "llvm/iTerminators.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/Function.h"
20 #include "ValueMapper.h"
21
22 namespace llvm {
23
24 // RemapInstruction - Convert the instruction operands from referencing the 
25 // current values into those specified by ValueMap.
26 //
27 static 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);
31     Value *V = MapValue(Op, ValueMap);
32 #ifndef NDEBUG
33     if (!V) {
34       std::cerr << "Val = \n" << Op << "Addr = " << (void*)Op;
35       std::cerr << "\nInst = " << I;
36     }
37 #endif
38     assert(V && "Referenced value not in value map!");
39     I->setOperand(op, V);
40   }
41 }
42
43 // CloneBasicBlock - See comments in Cloning.h
44 BasicBlock *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
62 // Clone OldFunc into NewFunc, transforming the old arguments into references to
63 // ArgMap values.
64 //
65 void CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
66                        std::map<const Value*, Value*> &ValueMap,
67                        std::vector<ReturnInst*> &Returns,
68                        const char *NameSuffix) {
69   assert(NameSuffix && "NameSuffix cannot be null!");
70   
71 #ifndef NDEBUG
72   for (Function::const_aiterator I = OldFunc->abegin(), E = OldFunc->aend();
73        I != E; ++I)
74     assert(ValueMap.count(I) && "No mapping from source argument specified!");
75 #endif
76
77   // Loop over all of the basic blocks in the function, cloning them as
78   // appropriate.  Note that we save BE this way in order to handle cloning of
79   // recursive functions into themselves.
80   //
81   for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
82        BI != BE; ++BI) {
83     const BasicBlock &BB = *BI;
84     
85     // Create a new basic block and copy instructions into it!
86     BasicBlock *CBB = CloneBasicBlock(&BB, ValueMap, NameSuffix);
87     NewFunc->getBasicBlockList().push_back(CBB);
88     ValueMap[&BB] = CBB;                       // Add basic block mapping.
89
90     if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator()))
91       Returns.push_back(RI);
92   }
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   //
97   for (Function::const_iterator BB = OldFunc->begin(), BE = OldFunc->end();
98        BB != BE; ++BB) {
99     BasicBlock *NBB = cast<BasicBlock>(ValueMap[BB]);
100     
101     // Loop over all instructions, fixing each one as we find it...
102     for (BasicBlock::iterator II = NBB->begin(); II != NBB->end(); ++II)
103       RemapInstruction(II, ValueMap);
104   }
105 }
106
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 ///
115 Function *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...
131   Function *NewF = new Function(FTy, F->getLinkage(), F->getName());
132   
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)
136     if (ValueMap.count(I) == 0) {   // Is this argument preserved?
137       DestI->setName(I->getName()); // Copy the name over...
138       ValueMap[I] = DestI++;        // Add mapping to ValueMap
139     }
140
141   std::vector<ReturnInst*> Returns;  // Ignore returns cloned...
142   CloneFunctionInto(NewF, F, ValueMap, Returns);
143   return NewF;                    
144 }
145
146 } // End llvm namespace