CloneFunction.cpp revision 6129af3fb14d1050f9fb4800c787e6f930b910d1
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"
21using namespace llvm;
22
23// CloneBasicBlock - See comments in Cloning.h
24BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB,
25                                  std::map<const Value*, Value*> &ValueMap,
26                                  const char *NameSuffix, Function *F) {
27  BasicBlock *NewBB = new BasicBlock("", F);
28  if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix);
29
30  // Loop over all instructions copying them over...
31  for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end();
32       II != IE; ++II) {
33    Instruction *NewInst = II->clone();
34    if (II->hasName())
35      NewInst->setName(II->getName()+NameSuffix);
36    NewBB->getInstList().push_back(NewInst);
37    ValueMap[II] = NewInst;                // Add instruction map to value.
38  }
39  return NewBB;
40}
41
42// Clone OldFunc into NewFunc, transforming the old arguments into references to
43// ArgMap values.
44//
45void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
46                             std::map<const Value*, Value*> &ValueMap,
47                             std::vector<ReturnInst*> &Returns,
48                             const char *NameSuffix) {
49  assert(NameSuffix && "NameSuffix cannot be null!");
50
51#ifndef NDEBUG
52  for (Function::const_aiterator I = OldFunc->abegin(), E = OldFunc->aend();
53       I != E; ++I)
54    assert(ValueMap.count(I) && "No mapping from source argument specified!");
55#endif
56
57  // Loop over all of the basic blocks in the function, cloning them as
58  // appropriate.  Note that we save BE this way in order to handle cloning of
59  // recursive functions into themselves.
60  //
61  for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
62       BI != BE; ++BI) {
63    const BasicBlock &BB = *BI;
64
65    // Create a new basic block and copy instructions into it!
66    BasicBlock *CBB = CloneBasicBlock(&BB, ValueMap, NameSuffix, NewFunc);
67    ValueMap[&BB] = CBB;                       // Add basic block mapping.
68
69    if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator()))
70      Returns.push_back(RI);
71  }
72
73  // Loop over all of the instructions in the function, fixing up operand
74  // references as we go.  This uses ValueMap to do all the hard work.
75  //
76  for (Function::iterator BB = cast<BasicBlock>(ValueMap[OldFunc->begin()]),
77         BE = NewFunc->end(); BB != BE; ++BB)
78    // Loop over all instructions, fixing each one as we find it...
79    for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ++II)
80      RemapInstruction(II, ValueMap);
81}
82
83/// CloneFunction - Return a copy of the specified function, but without
84/// embedding the function into another module.  Also, any references specified
85/// in the ValueMap are changed to refer to their mapped value instead of the
86/// original one.  If any of the arguments to the function are in the ValueMap,
87/// the arguments are deleted from the resultant function.  The ValueMap is
88/// updated to include mappings from all of the instructions and basicblocks in
89/// the function from their old to new values.
90///
91Function *llvm::CloneFunction(const Function *F,
92                              std::map<const Value*, Value*> &ValueMap) {
93  std::vector<const Type*> ArgTypes;
94
95  // The user might be deleting arguments to the function by specifying them in
96  // the ValueMap.  If so, we need to not add the arguments to the arg ty vector
97  //
98  for (Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
99    if (ValueMap.count(I) == 0)  // Haven't mapped the argument to anything yet?
100      ArgTypes.push_back(I->getType());
101
102  // Create a new function type...
103  FunctionType *FTy = FunctionType::get(F->getFunctionType()->getReturnType(),
104                                    ArgTypes, F->getFunctionType()->isVarArg());
105
106  // Create the new function...
107  Function *NewF = new Function(FTy, F->getLinkage(), F->getName());
108
109  // Loop over the arguments, copying the names of the mapped arguments over...
110  Function::aiterator DestI = NewF->abegin();
111  for (Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
112    if (ValueMap.count(I) == 0) {   // Is this argument preserved?
113      DestI->setName(I->getName()); // Copy the name over...
114      ValueMap[I] = DestI++;        // Add mapping to ValueMap
115    }
116
117  std::vector<ReturnInst*> Returns;  // Ignore returns cloned...
118  CloneFunctionInto(NewF, F, ValueMap, Returns);
119  return NewF;
120}
121
122