Allow the code cloning interfaces to capture some important info about the

code being cloned if the client wants.

llvm-svn: 25281
This commit is contained in:
Chris Lattner 2006-01-13 18:39:17 +00:00
parent 257492c0ab
commit edad1288fd
2 changed files with 74 additions and 17 deletions

View File

@ -37,6 +37,31 @@ class Trace;
/// ///
Module *CloneModule(const Module *M); Module *CloneModule(const Module *M);
/// ClonedCodeInfo - This struct can be used to capture information about code
/// being cloned, while it is being cloned.
struct ClonedCodeInfo {
/// ContainsCalls - This is set to true if the cloned code contains a normal
/// call instruction.
bool ContainsCalls;
/// ContainsUnwinds - This is set to true if the cloned code contains an
/// unwind instruction.
bool ContainsUnwinds;
/// ContainsDynamicAllocas - This is set to true if the cloned code contains
/// a 'dynamic' alloca. Dynamic allocas are allocas that are either not in
/// the entry block or they are in the entry block but are not a constant
/// size.
bool ContainsDynamicAllocas;
ClonedCodeInfo() {
ContainsCalls = false;
ContainsUnwinds = false;
ContainsDynamicAllocas = false;
}
};
/// CloneBasicBlock - Return a copy of the specified basic block, but without /// CloneBasicBlock - Return a copy of the specified basic block, but without
/// embedding the block into a particular function. The block returned is an /// embedding the block into a particular function. The block returned is an
/// exact copy of the specified basic block, without any remapping having been /// exact copy of the specified basic block, without any remapping having been
@ -61,9 +86,14 @@ Module *CloneModule(const Module *M);
/// If you would like the basic block to be auto-inserted into the end of a /// If you would like the basic block to be auto-inserted into the end of a
/// function, you can specify it as the optional fourth parameter. /// function, you can specify it as the optional fourth parameter.
/// ///
/// If you would like to collect additional information about the cloned
/// function, you can specify a ClonedCodeInfo object with the optional fifth
/// parameter.
///
BasicBlock *CloneBasicBlock(const BasicBlock *BB, BasicBlock *CloneBasicBlock(const BasicBlock *BB,
std::map<const Value*, Value*> &ValueMap, std::map<const Value*, Value*> &ValueMap,
const char *NameSuffix = "", Function *F = 0); const char *NameSuffix = "", Function *F = 0,
ClonedCodeInfo *CodeInfo = 0);
/// CloneFunction - Return a copy of the specified function, but without /// CloneFunction - Return a copy of the specified function, but without
@ -72,16 +102,18 @@ BasicBlock *CloneBasicBlock(const BasicBlock *BB,
/// original one. If any of the arguments to the function are in the ValueMap, /// 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 /// the arguments are deleted from the resultant function. The ValueMap is
/// updated to include mappings from all of the instructions and basicblocks in /// updated to include mappings from all of the instructions and basicblocks in
/// the function from their old to new values. /// the function from their old to new values. The final argument captures
/// information about the cloned code if non-null.
/// ///
Function *CloneFunction(const Function *F, Function *CloneFunction(const Function *F,
std::map<const Value*, Value*> &ValueMap); std::map<const Value*, Value*> &ValueMap,
ClonedCodeInfo *CodeInfo = 0);
/// CloneFunction - Version of the function that doesn't need the ValueMap. /// CloneFunction - Version of the function that doesn't need the ValueMap.
/// ///
inline Function *CloneFunction(const Function *F) { inline Function *CloneFunction(const Function *F, ClonedCodeInfo *CodeInfo = 0){
std::map<const Value*, Value*> ValueMap; std::map<const Value*, Value*> ValueMap;
return CloneFunction(F, ValueMap); return CloneFunction(F, ValueMap, CodeInfo);
} }
/// Clone OldFunc into NewFunc, transforming the old arguments into references /// Clone OldFunc into NewFunc, transforming the old arguments into references
@ -93,7 +125,8 @@ inline Function *CloneFunction(const Function *F) {
void CloneFunctionInto(Function *NewFunc, const Function *OldFunc, void CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
std::map<const Value*, Value*> &ValueMap, std::map<const Value*, Value*> &ValueMap,
std::vector<ReturnInst*> &Returns, std::vector<ReturnInst*> &Returns,
const char *NameSuffix = ""); const char *NameSuffix = "",
ClonedCodeInfo *CodeInfo = 0);
/// CloneTraceInto - Clone T into NewFunc. Original<->clone mapping is /// CloneTraceInto - Clone T into NewFunc. Original<->clone mapping is

View File

@ -14,8 +14,9 @@
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "llvm/Transforms/Utils/Cloning.h" #include "llvm/Transforms/Utils/Cloning.h"
#include "llvm/Instructions.h" #include "llvm/Constants.h"
#include "llvm/DerivedTypes.h" #include "llvm/DerivedTypes.h"
#include "llvm/Instructions.h"
#include "llvm/Function.h" #include "llvm/Function.h"
#include "ValueMapper.h" #include "ValueMapper.h"
using namespace llvm; using namespace llvm;
@ -23,11 +24,14 @@ using namespace llvm;
// CloneBasicBlock - See comments in Cloning.h // CloneBasicBlock - See comments in Cloning.h
BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB, BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB,
std::map<const Value*, Value*> &ValueMap, std::map<const Value*, Value*> &ValueMap,
const char *NameSuffix, Function *F) { const char *NameSuffix, Function *F,
ClonedCodeInfo *CodeInfo) {
BasicBlock *NewBB = new BasicBlock("", F); BasicBlock *NewBB = new BasicBlock("", F);
if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix); if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix);
// Loop over all instructions copying them over... bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false;
// Loop over all instructions, and copy them over.
for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end();
II != IE; ++II) { II != IE; ++II) {
Instruction *NewInst = II->clone(); Instruction *NewInst = II->clone();
@ -35,6 +39,22 @@ BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB,
NewInst->setName(II->getName()+NameSuffix); NewInst->setName(II->getName()+NameSuffix);
NewBB->getInstList().push_back(NewInst); NewBB->getInstList().push_back(NewInst);
ValueMap[II] = NewInst; // Add instruction map to value. ValueMap[II] = NewInst; // Add instruction map to value.
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();
} }
return NewBB; return NewBB;
} }
@ -45,12 +65,12 @@ BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB,
void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc, void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
std::map<const Value*, Value*> &ValueMap, std::map<const Value*, Value*> &ValueMap,
std::vector<ReturnInst*> &Returns, std::vector<ReturnInst*> &Returns,
const char *NameSuffix) { const char *NameSuffix, ClonedCodeInfo *CodeInfo) {
assert(NameSuffix && "NameSuffix cannot be null!"); assert(NameSuffix && "NameSuffix cannot be null!");
#ifndef NDEBUG #ifndef NDEBUG
for (Function::const_arg_iterator I = OldFunc->arg_begin(), E = OldFunc->arg_end(); for (Function::const_arg_iterator I = OldFunc->arg_begin(),
I != E; ++I) E = OldFunc->arg_end(); I != E; ++I)
assert(ValueMap.count(I) && "No mapping from source argument specified!"); assert(ValueMap.count(I) && "No mapping from source argument specified!");
#endif #endif
@ -63,7 +83,8 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
const BasicBlock &BB = *BI; const BasicBlock &BB = *BI;
// Create a new basic block and copy instructions into it! // Create a new basic block and copy instructions into it!
BasicBlock *CBB = CloneBasicBlock(&BB, ValueMap, NameSuffix, NewFunc); BasicBlock *CBB = CloneBasicBlock(&BB, ValueMap, NameSuffix, NewFunc,
CodeInfo);
ValueMap[&BB] = CBB; // Add basic block mapping. ValueMap[&BB] = CBB; // Add basic block mapping.
if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator())) if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator()))
@ -89,13 +110,15 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
/// the function from their old to new values. /// the function from their old to new values.
/// ///
Function *llvm::CloneFunction(const Function *F, Function *llvm::CloneFunction(const Function *F,
std::map<const Value*, Value*> &ValueMap) { std::map<const Value*, Value*> &ValueMap,
ClonedCodeInfo *CodeInfo) {
std::vector<const Type*> ArgTypes; std::vector<const Type*> ArgTypes;
// The user might be deleting arguments to the function by specifying them in // 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 // the ValueMap. If so, we need to not add the arguments to the arg ty vector
// //
for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I) for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
I != E; ++I)
if (ValueMap.count(I) == 0) // Haven't mapped the argument to anything yet? if (ValueMap.count(I) == 0) // Haven't mapped the argument to anything yet?
ArgTypes.push_back(I->getType()); ArgTypes.push_back(I->getType());
@ -108,14 +131,15 @@ Function *llvm::CloneFunction(const Function *F,
// Loop over the arguments, copying the names of the mapped arguments over... // Loop over the arguments, copying the names of the mapped arguments over...
Function::arg_iterator DestI = NewF->arg_begin(); Function::arg_iterator DestI = NewF->arg_begin();
for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I) for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
I != E; ++I)
if (ValueMap.count(I) == 0) { // Is this argument preserved? if (ValueMap.count(I) == 0) { // Is this argument preserved?
DestI->setName(I->getName()); // Copy the name over... DestI->setName(I->getName()); // Copy the name over...
ValueMap[I] = DestI++; // Add mapping to ValueMap ValueMap[I] = DestI++; // Add mapping to ValueMap
} }
std::vector<ReturnInst*> Returns; // Ignore returns cloned... std::vector<ReturnInst*> Returns; // Ignore returns cloned...
CloneFunctionInto(NewF, F, ValueMap, Returns); CloneFunctionInto(NewF, F, ValueMap, Returns, "", CodeInfo);
return NewF; return NewF;
} }