LLVM API Documentation

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/Constants.h"
00018 #include "llvm/DerivedTypes.h"
00019 #include "llvm/Instructions.h"
00020 #include "llvm/Function.h"
00021 #include "ValueMapper.h"
00022 using namespace llvm;
00023 
00024 // CloneBasicBlock - See comments in Cloning.h
00025 BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB,
00026                                   std::map<const Value*, Value*> &ValueMap,
00027                                   const char *NameSuffix, Function *F,
00028                                   ClonedCodeInfo *CodeInfo) {
00029   BasicBlock *NewBB = new BasicBlock("", F);
00030   if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix);
00031 
00032   bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false;
00033   
00034   // Loop over all instructions, and copy them over.
00035   for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end();
00036        II != IE; ++II) {
00037     Instruction *NewInst = II->clone();
00038     if (II->hasName())
00039       NewInst->setName(II->getName()+NameSuffix);
00040     NewBB->getInstList().push_back(NewInst);
00041     ValueMap[II] = NewInst;                // Add instruction map to value.
00042     
00043     hasCalls |= isa<CallInst>(II);
00044     if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) {
00045       if (isa<ConstantInt>(AI->getArraySize()))
00046         hasStaticAllocas = true;
00047       else
00048         hasDynamicAllocas = true;
00049     }
00050   }
00051   
00052   if (CodeInfo) {
00053     CodeInfo->ContainsCalls          |= hasCalls;
00054     CodeInfo->ContainsUnwinds        |= isa<UnwindInst>(BB->getTerminator());
00055     CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas;
00056     CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas && 
00057                                         BB != &BB->getParent()->front();
00058   }
00059   return NewBB;
00060 }
00061 
00062 // Clone OldFunc into NewFunc, transforming the old arguments into references to
00063 // ArgMap values.
00064 //
00065 void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
00066                              std::map<const Value*, Value*> &ValueMap,
00067                              std::vector<ReturnInst*> &Returns,
00068                              const char *NameSuffix, ClonedCodeInfo *CodeInfo) {
00069   assert(NameSuffix && "NameSuffix cannot be null!");
00070 
00071 #ifndef NDEBUG
00072   for (Function::const_arg_iterator I = OldFunc->arg_begin(), 
00073        E = OldFunc->arg_end(); I != E; ++I)
00074     assert(ValueMap.count(I) && "No mapping from source argument specified!");
00075 #endif
00076 
00077   // Loop over all of the basic blocks in the function, cloning them as
00078   // appropriate.  Note that we save BE this way in order to handle cloning of
00079   // recursive functions into themselves.
00080   //
00081   for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
00082        BI != BE; ++BI) {
00083     const BasicBlock &BB = *BI;
00084 
00085     // Create a new basic block and copy instructions into it!
00086     BasicBlock *CBB = CloneBasicBlock(&BB, ValueMap, NameSuffix, NewFunc,
00087                                       CodeInfo);
00088     ValueMap[&BB] = CBB;                       // Add basic block mapping.
00089 
00090     if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator()))
00091       Returns.push_back(RI);
00092   }
00093 
00094   // Loop over all of the instructions in the function, fixing up operand
00095   // references as we go.  This uses ValueMap to do all the hard work.
00096   //
00097   for (Function::iterator BB = cast<BasicBlock>(ValueMap[OldFunc->begin()]),
00098          BE = NewFunc->end(); BB != BE; ++BB)
00099     // Loop over all instructions, fixing each one as we find it...
00100     for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ++II)
00101       RemapInstruction(II, ValueMap);
00102 }
00103 
00104 /// CloneFunction - Return a copy of the specified function, but without
00105 /// embedding the function into another module.  Also, any references specified
00106 /// in the ValueMap are changed to refer to their mapped value instead of the
00107 /// original one.  If any of the arguments to the function are in the ValueMap,
00108 /// the arguments are deleted from the resultant function.  The ValueMap is
00109 /// updated to include mappings from all of the instructions and basicblocks in
00110 /// the function from their old to new values.
00111 ///
00112 Function *llvm::CloneFunction(const Function *F,
00113                               std::map<const Value*, Value*> &ValueMap,
00114                               ClonedCodeInfo *CodeInfo) {
00115   std::vector<const Type*> ArgTypes;
00116 
00117   // The user might be deleting arguments to the function by specifying them in
00118   // the ValueMap.  If so, we need to not add the arguments to the arg ty vector
00119   //
00120   for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
00121        I != E; ++I)
00122     if (ValueMap.count(I) == 0)  // Haven't mapped the argument to anything yet?
00123       ArgTypes.push_back(I->getType());
00124 
00125   // Create a new function type...
00126   FunctionType *FTy = FunctionType::get(F->getFunctionType()->getReturnType(),
00127                                     ArgTypes, F->getFunctionType()->isVarArg());
00128 
00129   // Create the new function...
00130   Function *NewF = new Function(FTy, F->getLinkage(), F->getName());
00131 
00132   // Loop over the arguments, copying the names of the mapped arguments over...
00133   Function::arg_iterator DestI = NewF->arg_begin();
00134   for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
00135        I != E; ++I)
00136     if (ValueMap.count(I) == 0) {   // Is this argument preserved?
00137       DestI->setName(I->getName()); // Copy the name over...
00138       ValueMap[I] = DestI++;        // Add mapping to ValueMap
00139     }
00140 
00141   std::vector<ReturnInst*> Returns;  // Ignore returns cloned...
00142   CloneFunctionInto(NewF, F, ValueMap, Returns, "", CodeInfo);
00143   return NewF;
00144 }
00145