Added separate alias instructions for SSE logical ops that operate on non-packed...
[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/Constants.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/Function.h"
21 #include "ValueMapper.h"
22 using namespace llvm;
23
24 // CloneBasicBlock - See comments in Cloning.h
25 BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB,
26                                   std::map<const Value*, Value*> &ValueMap,
27                                   const char *NameSuffix, Function *F,
28                                   ClonedCodeInfo *CodeInfo) {
29   BasicBlock *NewBB = new BasicBlock("", F);
30   if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix);
31
32   bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false;
33   
34   // Loop over all instructions, and copy them over.
35   for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end();
36        II != IE; ++II) {
37     Instruction *NewInst = II->clone();
38     if (II->hasName())
39       NewInst->setName(II->getName()+NameSuffix);
40     NewBB->getInstList().push_back(NewInst);
41     ValueMap[II] = NewInst;                // Add instruction map to value.
42     
43     hasCalls |= isa<CallInst>(II);
44     if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) {
45       if (isa<ConstantInt>(AI->getArraySize()))
46         hasStaticAllocas = true;
47       else
48         hasDynamicAllocas = true;
49     }
50   }
51   
52   if (CodeInfo) {
53     CodeInfo->ContainsCalls          |= hasCalls;
54     CodeInfo->ContainsUnwinds        |= isa<UnwindInst>(BB->getTerminator());
55     CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas;
56     CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas && 
57                                         BB != &BB->getParent()->front();
58   }
59   return NewBB;
60 }
61
62 // Clone OldFunc into NewFunc, transforming the old arguments into references to
63 // ArgMap values.
64 //
65 void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
66                              std::map<const Value*, Value*> &ValueMap,
67                              std::vector<ReturnInst*> &Returns,
68                              const char *NameSuffix, ClonedCodeInfo *CodeInfo) {
69   assert(NameSuffix && "NameSuffix cannot be null!");
70
71 #ifndef NDEBUG
72   for (Function::const_arg_iterator I = OldFunc->arg_begin(), 
73        E = OldFunc->arg_end(); 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, NewFunc,
87                                       CodeInfo);
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::iterator BB = cast<BasicBlock>(ValueMap[OldFunc->begin()]),
98          BE = NewFunc->end(); BB != BE; ++BB)
99     // Loop over all instructions, fixing each one as we find it...
100     for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ++II)
101       RemapInstruction(II, ValueMap);
102 }
103
104 /// CloneFunction - Return a copy of the specified function, but without
105 /// embedding the function into another module.  Also, any references specified
106 /// in the ValueMap are changed to refer to their mapped value instead of the
107 /// original one.  If any of the arguments to the function are in the ValueMap,
108 /// the arguments are deleted from the resultant function.  The ValueMap is
109 /// updated to include mappings from all of the instructions and basicblocks in
110 /// the function from their old to new values.
111 ///
112 Function *llvm::CloneFunction(const Function *F,
113                               std::map<const Value*, Value*> &ValueMap,
114                               ClonedCodeInfo *CodeInfo) {
115   std::vector<const Type*> ArgTypes;
116
117   // The user might be deleting arguments to the function by specifying them in
118   // the ValueMap.  If so, we need to not add the arguments to the arg ty vector
119   //
120   for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
121        I != E; ++I)
122     if (ValueMap.count(I) == 0)  // Haven't mapped the argument to anything yet?
123       ArgTypes.push_back(I->getType());
124
125   // Create a new function type...
126   FunctionType *FTy = FunctionType::get(F->getFunctionType()->getReturnType(),
127                                     ArgTypes, F->getFunctionType()->isVarArg());
128
129   // Create the new function...
130   Function *NewF = new Function(FTy, F->getLinkage(), F->getName());
131
132   // Loop over the arguments, copying the names of the mapped arguments over...
133   Function::arg_iterator DestI = NewF->arg_begin();
134   for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
135        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, "", CodeInfo);
143   return NewF;
144 }
145