2002-11-20 06:04:49 +08:00
|
|
|
//===- CloneFunction.cpp - Clone a function into another function ---------===//
|
|
|
|
//
|
|
|
|
// 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-30 03:03:54 +08:00
|
|
|
|
2002-11-20 04:59:41 +08:00
|
|
|
#include "llvm/Transforms/Utils/Cloning.h"
|
2002-11-20 05:54:07 +08:00
|
|
|
#include "llvm/iTerminators.h"
|
2002-11-20 07:12:22 +08:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2002-03-30 03:03:54 +08:00
|
|
|
#include "llvm/Function.h"
|
2002-11-21 04:47:41 +08:00
|
|
|
#include "ValueMapper.h"
|
2002-03-30 03:03:54 +08:00
|
|
|
|
|
|
|
// RemapInstruction - Convert the instruction operands from referencing the
|
|
|
|
// current values into those specified by ValueMap.
|
|
|
|
//
|
|
|
|
static inline void RemapInstruction(Instruction *I,
|
|
|
|
std::map<const Value *, Value*> &ValueMap) {
|
|
|
|
for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) {
|
|
|
|
const Value *Op = I->getOperand(op);
|
2002-11-21 04:47:41 +08:00
|
|
|
Value *V = MapValue(Op, ValueMap);
|
2002-03-30 03:03:54 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
if (!V) {
|
2002-06-26 05:18:19 +08:00
|
|
|
std::cerr << "Val = \n" << Op << "Addr = " << (void*)Op;
|
|
|
|
std::cerr << "\nInst = " << I;
|
2002-03-30 03:03:54 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
assert(V && "Referenced value not in value map!");
|
|
|
|
I->setOperand(op, V);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-04-18 11:50:09 +08:00
|
|
|
// CloneBasicBlock - See comments in Cloning.h
|
|
|
|
BasicBlock *CloneBasicBlock(const BasicBlock *BB,
|
|
|
|
std::map<const Value*, Value*> &ValueMap,
|
|
|
|
const char *NameSuffix) {
|
|
|
|
BasicBlock *NewBB = new BasicBlock("");
|
|
|
|
if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix);
|
|
|
|
|
|
|
|
// Loop over all instructions copying them over...
|
|
|
|
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.
|
|
|
|
}
|
|
|
|
return NewBB;
|
|
|
|
}
|
|
|
|
|
2002-03-30 03:03:54 +08:00
|
|
|
// Clone OldFunc into NewFunc, transforming the old arguments into references to
|
|
|
|
// ArgMap values.
|
|
|
|
//
|
|
|
|
void CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
|
2002-11-20 06:54:01 +08:00
|
|
|
std::map<const Value*, Value*> &ValueMap,
|
2002-11-20 05:54:07 +08:00
|
|
|
std::vector<ReturnInst*> &Returns,
|
|
|
|
const char *NameSuffix) {
|
|
|
|
assert(NameSuffix && "NameSuffix cannot be null!");
|
2002-03-30 03:03:54 +08:00
|
|
|
|
2002-11-20 06:54:01 +08:00
|
|
|
#ifndef NDEBUG
|
2002-06-26 00:12:52 +08:00
|
|
|
for (Function::const_aiterator I = OldFunc->abegin(), E = OldFunc->aend();
|
2002-11-20 06:54:01 +08:00
|
|
|
I != E; ++I)
|
|
|
|
assert(ValueMap.count(I) && "No mapping from source argument specified!");
|
|
|
|
#endif
|
2002-03-30 03:03:54 +08:00
|
|
|
|
|
|
|
// Loop over all of the basic blocks in the function, cloning them as
|
2002-11-20 05:54:07 +08:00
|
|
|
// appropriate. Note that we save BE this way in order to handle cloning of
|
|
|
|
// recursive functions into themselves.
|
2002-03-30 03:03:54 +08:00
|
|
|
//
|
|
|
|
for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
|
|
|
|
BI != BE; ++BI) {
|
2002-06-26 00:12:52 +08:00
|
|
|
const BasicBlock &BB = *BI;
|
2002-03-30 03:03:54 +08:00
|
|
|
|
2003-04-18 11:50:09 +08:00
|
|
|
// Create a new basic block and copy instructions into it!
|
|
|
|
BasicBlock *CBB = CloneBasicBlock(&BB, ValueMap, NameSuffix);
|
|
|
|
NewFunc->getBasicBlockList().push_back(CBB);
|
2002-06-26 00:12:52 +08:00
|
|
|
ValueMap[&BB] = CBB; // Add basic block mapping.
|
2002-03-30 03:03:54 +08:00
|
|
|
|
2002-11-20 05:54:07 +08:00
|
|
|
if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator()))
|
|
|
|
Returns.push_back(RI);
|
2002-03-30 03:03:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Loop over all of the instructions in the function, fixing up operand
|
|
|
|
// references as we go. This uses ValueMap to do all the hard work.
|
|
|
|
//
|
2002-06-26 00:12:52 +08:00
|
|
|
for (Function::const_iterator BB = OldFunc->begin(), BE = OldFunc->end();
|
|
|
|
BB != BE; ++BB) {
|
2002-03-30 03:03:54 +08:00
|
|
|
BasicBlock *NBB = cast<BasicBlock>(ValueMap[BB]);
|
|
|
|
|
|
|
|
// Loop over all instructions, fixing each one as we find it...
|
2002-06-26 00:12:52 +08:00
|
|
|
for (BasicBlock::iterator II = NBB->begin(); II != NBB->end(); ++II)
|
|
|
|
RemapInstruction(II, ValueMap);
|
2002-03-30 03:03:54 +08:00
|
|
|
}
|
|
|
|
}
|
2002-11-20 07:12:22 +08: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.
|
|
|
|
///
|
|
|
|
Function *CloneFunction(const Function *F,
|
|
|
|
std::map<const Value*, Value*> &ValueMap) {
|
|
|
|
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
|
|
|
|
//
|
|
|
|
for (Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
|
|
|
|
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-17 04:28:45 +08:00
|
|
|
Function *NewF = new Function(FTy, F->getLinkage(), F->getName());
|
2002-11-20 07:12:22 +08:00
|
|
|
|
|
|
|
// Loop over the arguments, copying the names of the mapped arguments over...
|
|
|
|
Function::aiterator DestI = NewF->abegin();
|
|
|
|
for (Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
|
2002-11-21 02:32:31 +08:00
|
|
|
if (ValueMap.count(I) == 0) { // Is this argument preserved?
|
2002-11-20 07:12:22 +08:00
|
|
|
DestI->setName(I->getName()); // Copy the name over...
|
2002-11-21 02:32:31 +08:00
|
|
|
ValueMap[I] = DestI++; // Add mapping to ValueMap
|
2002-11-20 07:12:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<ReturnInst*> Returns; // Ignore returns cloned...
|
|
|
|
CloneFunctionInto(NewF, F, ValueMap, Returns);
|
|
|
|
return NewF;
|
|
|
|
}
|