2002-11-19 22:04:49 +00:00
|
|
|
//===- CloneFunction.cpp - Clone a function into another function ---------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-11-19 22:04:49 +00:00
|
|
|
//
|
|
|
|
// This file implements the CloneFunctionInto interface, which is used as the
|
|
|
|
// low-level function cloner. This is used by the CloneFunction and function
|
|
|
|
// inliner to do the dirty work of copying the body of a function around.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2002-03-29 19:03:54 +00:00
|
|
|
|
2002-11-19 20:59:41 +00:00
|
|
|
#include "llvm/Transforms/Utils/Cloning.h"
|
2006-01-13 18:39:17 +00:00
|
|
|
#include "llvm/Constants.h"
|
2002-11-19 23:12:22 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2006-01-13 18:39:17 +00:00
|
|
|
#include "llvm/Instructions.h"
|
2002-03-29 19:03:54 +00:00
|
|
|
#include "llvm/Function.h"
|
2002-11-20 20:47:41 +00:00
|
|
|
#include "ValueMapper.h"
|
2004-01-09 06:12:26 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2003-04-18 03:50:09 +00:00
|
|
|
// CloneBasicBlock - See comments in Cloning.h
|
2004-01-09 06:12:26 +00:00
|
|
|
BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB,
|
|
|
|
std::map<const Value*, Value*> &ValueMap,
|
2006-01-13 18:39:17 +00:00
|
|
|
const char *NameSuffix, Function *F,
|
|
|
|
ClonedCodeInfo *CodeInfo) {
|
2004-02-04 01:19:43 +00:00
|
|
|
BasicBlock *NewBB = new BasicBlock("", F);
|
2003-04-18 03:50:09 +00:00
|
|
|
if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix);
|
|
|
|
|
2006-01-13 18:39:17 +00:00
|
|
|
bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false;
|
|
|
|
|
|
|
|
// Loop over all instructions, and copy them over.
|
2003-04-18 03:50:09 +00:00
|
|
|
for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end();
|
|
|
|
II != IE; ++II) {
|
|
|
|
Instruction *NewInst = II->clone();
|
|
|
|
if (II->hasName())
|
|
|
|
NewInst->setName(II->getName()+NameSuffix);
|
|
|
|
NewBB->getInstList().push_back(NewInst);
|
|
|
|
ValueMap[II] = NewInst; // Add instruction map to value.
|
2006-01-13 18:39:17 +00:00
|
|
|
|
|
|
|
hasCalls |= isa<CallInst>(II);
|
|
|
|
if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) {
|
|
|
|
if (isa<ConstantInt>(AI->getArraySize()))
|
|
|
|
hasStaticAllocas = true;
|
|
|
|
else
|
|
|
|
hasDynamicAllocas = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CodeInfo) {
|
|
|
|
CodeInfo->ContainsCalls |= hasCalls;
|
|
|
|
CodeInfo->ContainsUnwinds |= isa<UnwindInst>(BB->getTerminator());
|
|
|
|
CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas;
|
|
|
|
CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas &&
|
|
|
|
BB != &BB->getParent()->front();
|
2003-04-18 03:50:09 +00:00
|
|
|
}
|
|
|
|
return NewBB;
|
|
|
|
}
|
|
|
|
|
2002-03-29 19:03:54 +00:00
|
|
|
// Clone OldFunc into NewFunc, transforming the old arguments into references to
|
|
|
|
// ArgMap values.
|
|
|
|
//
|
2004-01-09 06:12:26 +00:00
|
|
|
void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
|
|
|
|
std::map<const Value*, Value*> &ValueMap,
|
|
|
|
std::vector<ReturnInst*> &Returns,
|
2006-01-13 18:39:17 +00:00
|
|
|
const char *NameSuffix, ClonedCodeInfo *CodeInfo) {
|
2002-11-19 21:54:07 +00:00
|
|
|
assert(NameSuffix && "NameSuffix cannot be null!");
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2002-11-19 22:54:01 +00:00
|
|
|
#ifndef NDEBUG
|
2006-01-13 18:39:17 +00:00
|
|
|
for (Function::const_arg_iterator I = OldFunc->arg_begin(),
|
|
|
|
E = OldFunc->arg_end(); I != E; ++I)
|
2002-11-19 22:54:01 +00:00
|
|
|
assert(ValueMap.count(I) && "No mapping from source argument specified!");
|
|
|
|
#endif
|
2002-03-29 19:03:54 +00:00
|
|
|
|
|
|
|
// Loop over all of the basic blocks in the function, cloning them as
|
2002-11-19 21:54:07 +00:00
|
|
|
// appropriate. Note that we save BE this way in order to handle cloning of
|
|
|
|
// recursive functions into themselves.
|
2002-03-29 19:03:54 +00:00
|
|
|
//
|
|
|
|
for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
|
|
|
|
BI != BE; ++BI) {
|
2002-06-25 16:12:52 +00:00
|
|
|
const BasicBlock &BB = *BI;
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2003-04-18 03:50:09 +00:00
|
|
|
// Create a new basic block and copy instructions into it!
|
2006-01-13 18:39:17 +00:00
|
|
|
BasicBlock *CBB = CloneBasicBlock(&BB, ValueMap, NameSuffix, NewFunc,
|
|
|
|
CodeInfo);
|
2002-06-25 16:12:52 +00:00
|
|
|
ValueMap[&BB] = CBB; // Add basic block mapping.
|
2002-03-29 19:03:54 +00:00
|
|
|
|
2002-11-19 21:54:07 +00:00
|
|
|
if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator()))
|
|
|
|
Returns.push_back(RI);
|
2002-03-29 19:03:54 +00:00
|
|
|
}
|
|
|
|
|
2005-04-21 23:48:37 +00:00
|
|
|
// Loop over all of the instructions in the function, fixing up operand
|
2002-03-29 19:03:54 +00:00
|
|
|
// references as we go. This uses ValueMap to do all the hard work.
|
|
|
|
//
|
2004-02-04 21:44:26 +00:00
|
|
|
for (Function::iterator BB = cast<BasicBlock>(ValueMap[OldFunc->begin()]),
|
|
|
|
BE = NewFunc->end(); BB != BE; ++BB)
|
2002-03-29 19:03:54 +00:00
|
|
|
// Loop over all instructions, fixing each one as we find it...
|
2004-02-04 21:44:26 +00:00
|
|
|
for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ++II)
|
2002-06-25 16:12:52 +00:00
|
|
|
RemapInstruction(II, ValueMap);
|
2002-03-29 19:03:54 +00:00
|
|
|
}
|
2002-11-19 23:12:22 +00:00
|
|
|
|
|
|
|
/// CloneFunction - Return a copy of the specified function, but without
|
|
|
|
/// embedding the function into another module. Also, any references specified
|
|
|
|
/// in the ValueMap are changed to refer to their mapped value instead of the
|
|
|
|
/// original one. If any of the arguments to the function are in the ValueMap,
|
|
|
|
/// the arguments are deleted from the resultant function. The ValueMap is
|
|
|
|
/// updated to include mappings from all of the instructions and basicblocks in
|
|
|
|
/// the function from their old to new values.
|
|
|
|
///
|
2004-01-09 06:12:26 +00:00
|
|
|
Function *llvm::CloneFunction(const Function *F,
|
2006-01-13 18:39:17 +00:00
|
|
|
std::map<const Value*, Value*> &ValueMap,
|
|
|
|
ClonedCodeInfo *CodeInfo) {
|
2002-11-19 23:12:22 +00:00
|
|
|
std::vector<const Type*> ArgTypes;
|
|
|
|
|
|
|
|
// The user might be deleting arguments to the function by specifying them in
|
|
|
|
// the ValueMap. If so, we need to not add the arguments to the arg ty vector
|
|
|
|
//
|
2006-01-13 18:39:17 +00:00
|
|
|
for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
|
|
|
|
I != E; ++I)
|
2002-11-19 23:12:22 +00:00
|
|
|
if (ValueMap.count(I) == 0) // Haven't mapped the argument to anything yet?
|
|
|
|
ArgTypes.push_back(I->getType());
|
|
|
|
|
|
|
|
// Create a new function type...
|
|
|
|
FunctionType *FTy = FunctionType::get(F->getFunctionType()->getReturnType(),
|
|
|
|
ArgTypes, F->getFunctionType()->isVarArg());
|
|
|
|
|
|
|
|
// Create the new function...
|
2003-04-16 20:28:45 +00:00
|
|
|
Function *NewF = new Function(FTy, F->getLinkage(), F->getName());
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2002-11-19 23:12:22 +00:00
|
|
|
// Loop over the arguments, copying the names of the mapped arguments over...
|
2005-03-15 04:54:21 +00:00
|
|
|
Function::arg_iterator DestI = NewF->arg_begin();
|
2006-01-13 18:39:17 +00:00
|
|
|
for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
|
|
|
|
I != E; ++I)
|
2002-11-20 18:32:31 +00:00
|
|
|
if (ValueMap.count(I) == 0) { // Is this argument preserved?
|
2002-11-19 23:12:22 +00:00
|
|
|
DestI->setName(I->getName()); // Copy the name over...
|
2002-11-20 18:32:31 +00:00
|
|
|
ValueMap[I] = DestI++; // Add mapping to ValueMap
|
2002-11-19 23:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<ReturnInst*> Returns; // Ignore returns cloned...
|
2006-01-13 18:39:17 +00:00
|
|
|
CloneFunctionInto(NewF, F, ValueMap, Returns, "", CodeInfo);
|
2005-04-21 23:48:37 +00:00
|
|
|
return NewF;
|
2002-11-19 23:12:22 +00:00
|
|
|
}
|
2003-11-11 22:41:34 +00:00
|
|
|
|