LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

CloneFunction.cpp

Go to the documentation of this file.
00001 //===- CloneFunction.cpp - Clone a function into another function ---------===//
00002 // 
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 // 
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the CloneFunctionInto interface, which is used as the
00011 // low-level function cloner.  This is used by the CloneFunction and function
00012 // inliner to do the dirty work of copying the body of a function around.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #include "llvm/Transforms/Utils/Cloning.h"
00017 #include "llvm/Instructions.h"
00018 #include "llvm/DerivedTypes.h"
00019 #include "llvm/Function.h"
00020 #include "ValueMapper.h"
00021 using namespace llvm;
00022 
00023 // CloneBasicBlock - See comments in Cloning.h
00024 BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB,
00025                                   std::map<const Value*, Value*> &ValueMap,
00026                                   const char *NameSuffix, Function *F) {
00027   BasicBlock *NewBB = new BasicBlock("", F);
00028   if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix);
00029 
00030   // Loop over all instructions copying them over...
00031   for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end();
00032        II != IE; ++II) {
00033     Instruction *NewInst = II->clone();
00034     if (II->hasName())
00035       NewInst->setName(II->getName()+NameSuffix);
00036     NewBB->getInstList().push_back(NewInst);
00037     ValueMap[II] = NewInst;                // Add instruction map to value.
00038   }
00039   return NewBB;
00040 }
00041 
00042 // Clone OldFunc into NewFunc, transforming the old arguments into references to
00043 // ArgMap values.
00044 //
00045 void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
00046                              std::map<const Value*, Value*> &ValueMap,
00047                              std::vector<ReturnInst*> &Returns,
00048                              const char *NameSuffix) {
00049   assert(NameSuffix && "NameSuffix cannot be null!");
00050   
00051 #ifndef NDEBUG
00052   for (Function::const_aiterator I = OldFunc->abegin(), E = OldFunc->aend();
00053        I != E; ++I)
00054     assert(ValueMap.count(I) && "No mapping from source argument specified!");
00055 #endif
00056 
00057   // Loop over all of the basic blocks in the function, cloning them as
00058   // appropriate.  Note that we save BE this way in order to handle cloning of
00059   // recursive functions into themselves.
00060   //
00061   for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
00062        BI != BE; ++BI) {
00063     const BasicBlock &BB = *BI;
00064     
00065     // Create a new basic block and copy instructions into it!
00066     BasicBlock *CBB = CloneBasicBlock(&BB, ValueMap, NameSuffix, NewFunc);
00067     ValueMap[&BB] = CBB;                       // Add basic block mapping.
00068 
00069     if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator()))
00070       Returns.push_back(RI);
00071   }
00072 
00073   // Loop over all of the instructions in the function, fixing up operand 
00074   // references as we go.  This uses ValueMap to do all the hard work.
00075   //
00076   for (Function::iterator BB = cast<BasicBlock>(ValueMap[OldFunc->begin()]),
00077          BE = NewFunc->end(); BB != BE; ++BB)
00078     // Loop over all instructions, fixing each one as we find it...
00079     for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ++II)
00080       RemapInstruction(II, ValueMap);
00081 }
00082 
00083 /// CloneFunction - Return a copy of the specified function, but without
00084 /// embedding the function into another module.  Also, any references specified
00085 /// in the ValueMap are changed to refer to their mapped value instead of the
00086 /// original one.  If any of the arguments to the function are in the ValueMap,
00087 /// the arguments are deleted from the resultant function.  The ValueMap is
00088 /// updated to include mappings from all of the instructions and basicblocks in
00089 /// the function from their old to new values.
00090 ///
00091 Function *llvm::CloneFunction(const Function *F,
00092                               std::map<const Value*, Value*> &ValueMap) {
00093   std::vector<const Type*> ArgTypes;
00094 
00095   // The user might be deleting arguments to the function by specifying them in
00096   // the ValueMap.  If so, we need to not add the arguments to the arg ty vector
00097   //
00098   for (Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
00099     if (ValueMap.count(I) == 0)  // Haven't mapped the argument to anything yet?
00100       ArgTypes.push_back(I->getType());
00101 
00102   // Create a new function type...
00103   FunctionType *FTy = FunctionType::get(F->getFunctionType()->getReturnType(),
00104                                     ArgTypes, F->getFunctionType()->isVarArg());
00105 
00106   // Create the new function...
00107   Function *NewF = new Function(FTy, F->getLinkage(), F->getName());
00108   
00109   // Loop over the arguments, copying the names of the mapped arguments over...
00110   Function::aiterator DestI = NewF->abegin();
00111   for (Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
00112     if (ValueMap.count(I) == 0) {   // Is this argument preserved?
00113       DestI->setName(I->getName()); // Copy the name over...
00114       ValueMap[I] = DestI++;        // Add mapping to ValueMap
00115     }
00116 
00117   std::vector<ReturnInst*> Returns;  // Ignore returns cloned...
00118   CloneFunctionInto(NewF, F, ValueMap, Returns);
00119   return NewF;                    
00120 }
00121