2002-03-30 03:03:54 +08:00
|
|
|
|
2002-11-20 04:59:41 +08:00
|
|
|
|
|
|
|
|
2002-03-30 03:03:54 +08:00
|
|
|
// FIXME: document
|
|
|
|
|
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-03-30 03:03:54 +08:00
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include <map>
|
|
|
|
|
2002-04-27 15:27:19 +08:00
|
|
|
// FIXME: This should be merged with FunctionInlining
|
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);
|
|
|
|
Value *V = ValueMap[Op];
|
|
|
|
if (!V && (isa<GlobalValue>(Op) || isa<Constant>(Op)))
|
|
|
|
continue; // Globals and constants don't get relocated
|
|
|
|
|
|
|
|
#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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clone OldFunc into NewFunc, transforming the old arguments into references to
|
|
|
|
// ArgMap values.
|
|
|
|
//
|
|
|
|
void CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
|
2002-11-20 05:54:07 +08:00
|
|
|
const std::vector<Value*> &ArgMap,
|
|
|
|
std::vector<ReturnInst*> &Returns,
|
|
|
|
const char *NameSuffix) {
|
|
|
|
assert(NameSuffix && "NameSuffix cannot be null!");
|
2002-06-26 00:12:52 +08:00
|
|
|
assert(OldFunc->asize() == ArgMap.size() &&
|
2002-03-30 03:03:54 +08:00
|
|
|
"Improper number of argument values to map specified!");
|
|
|
|
|
|
|
|
// Keep a mapping between the original function's values and the new
|
|
|
|
// duplicated code's values. This includes all of: Function arguments,
|
|
|
|
// instruction values, constant pool entries, and basic blocks.
|
|
|
|
//
|
|
|
|
std::map<const Value *, Value*> ValueMap;
|
|
|
|
|
|
|
|
// Add all of the function arguments to the mapping...
|
2002-06-26 00:12:52 +08:00
|
|
|
unsigned i = 0;
|
|
|
|
for (Function::const_aiterator I = OldFunc->abegin(), E = OldFunc->aend();
|
|
|
|
I != E; ++I, ++i)
|
|
|
|
ValueMap[I] = ArgMap[i];
|
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
|
|
|
|
|
|
|
// Create a new basic block to copy instructions into!
|
2002-11-20 05:54:07 +08:00
|
|
|
BasicBlock *CBB = new BasicBlock("", NewFunc);
|
|
|
|
if (BB.hasName()) CBB->setName(BB.getName()+NameSuffix);
|
2002-06-26 00:12:52 +08:00
|
|
|
ValueMap[&BB] = CBB; // Add basic block mapping.
|
2002-03-30 03:03:54 +08:00
|
|
|
|
|
|
|
// Loop over all instructions copying them over...
|
2002-06-26 00:12:52 +08:00
|
|
|
for (BasicBlock::const_iterator II = BB.begin(), IE = BB.end();
|
2002-03-30 03:03:54 +08:00
|
|
|
II != IE; ++II) {
|
2002-06-26 00:12:52 +08:00
|
|
|
Instruction *NewInst = II->clone();
|
2002-11-20 05:54:07 +08:00
|
|
|
if (II->hasName())
|
|
|
|
NewInst->setName(II->getName()+NameSuffix); // Name is not cloned...
|
2002-03-30 03:03:54 +08:00
|
|
|
CBB->getInstList().push_back(NewInst);
|
2002-06-26 00:12:52 +08:00
|
|
|
ValueMap[II] = NewInst; // Add instruction map to value.
|
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
|
|
|
}
|
|
|
|
}
|