1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2025-01-31 12:41:49 +01:00

add more support for ConstantDataSequential

llvm-svn: 148802
This commit is contained in:
Chris Lattner 2012-01-24 13:41:11 +00:00
parent a0474f4619
commit 9713727d0b
5 changed files with 78 additions and 34 deletions

View File

@ -623,6 +623,9 @@ public:
/// getElementType - Return the element type of the array/vector. /// getElementType - Return the element type of the array/vector.
Type *getElementType() const; Type *getElementType() const;
/// getNumElements - Return the number of elements in the array or vector.
unsigned getNumElements() const;
/// getElementByteSize - Return the size (in bytes) of each element in the /// getElementByteSize - Return the size (in bytes) of each element in the
/// array/vector. The size of the elements is known to be a multiple of one /// array/vector. The size of the elements is known to be a multiple of one

View File

@ -1055,6 +1055,23 @@ SDValue SelectionDAGBuilder::getValueImpl(const Value *V) {
return DAG.getMergeValues(&Constants[0], Constants.size(), return DAG.getMergeValues(&Constants[0], Constants.size(),
getCurDebugLoc()); getCurDebugLoc());
} }
if (const ConstantDataSequential *CDS =
dyn_cast<ConstantDataSequential>(C)) {
SmallVector<SDValue, 4> Ops;
for (unsigned i = 0, e = CDS->getType()->getNumElements(); i != e; ++i) {
SDNode *Val = getValue(CDS->getElementAsConstant(i)).getNode();
// Add each leaf value from the operand to the Constants list
// to form a flattened list of all the values.
for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
Ops.push_back(SDValue(Val, i));
}
if (isa<ArrayType>(CDS->getType()))
return DAG.getMergeValues(&Ops[0], Ops.size(), getCurDebugLoc());
return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
VT, &Ops[0], Ops.size());
}
if (C->getType()->isStructTy() || C->getType()->isArrayTy()) { if (C->getType()->isStructTy() || C->getType()->isArrayTy()) {
assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) && assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
@ -1089,9 +1106,9 @@ SDValue SelectionDAGBuilder::getValueImpl(const Value *V) {
// Now that we know the number and type of the elements, get that number of // Now that we know the number and type of the elements, get that number of
// elements into the Ops array based on what kind of constant it is. // elements into the Ops array based on what kind of constant it is.
SmallVector<SDValue, 16> Ops; SmallVector<SDValue, 16> Ops;
if (const ConstantVector *CP = dyn_cast<ConstantVector>(C)) { if (const ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
for (unsigned i = 0; i != NumElements; ++i) for (unsigned i = 0; i != NumElements; ++i)
Ops.push_back(getValue(CP->getOperand(i))); Ops.push_back(getValue(CV->getOperand(i)));
} else { } else {
assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!"); assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!");
EVT EltVT = TLI.getValueType(VecTy->getElementType()); EVT EltVT = TLI.getValueType(VecTy->getElementType());

View File

@ -307,13 +307,12 @@ void ExecutionEngine::runStaticConstructorsDestructors(Module *module,
// Should be an array of '{ i32, void ()* }' structs. The first value is // Should be an array of '{ i32, void ()* }' structs. The first value is
// the init priority, which we ignore. // the init priority, which we ignore.
if (isa<ConstantAggregateZero>(GV->getInitializer())) ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
if (InitList == 0)
return; return;
ConstantArray *InitList = cast<ConstantArray>(GV->getInitializer());
for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) { for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
if (isa<ConstantAggregateZero>(InitList->getOperand(i))) ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i));
continue; if (CS == 0) continue;
ConstantStruct *CS = cast<ConstantStruct>(InitList->getOperand(i));
Constant *FP = CS->getOperand(1); Constant *FP = CS->getOperand(1);
if (FP->isNullValue()) if (FP->isNullValue())
@ -954,30 +953,47 @@ void ExecutionEngine::LoadValueFromMemory(GenericValue &Result,
void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) { void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
DEBUG(dbgs() << "JIT: Initializing " << Addr << " "); DEBUG(dbgs() << "JIT: Initializing " << Addr << " ");
DEBUG(Init->dump()); DEBUG(Init->dump());
if (isa<UndefValue>(Init)) { if (isa<UndefValue>(Init))
return; return;
} else if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) {
if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) {
unsigned ElementSize = unsigned ElementSize =
getTargetData()->getTypeAllocSize(CP->getType()->getElementType()); getTargetData()->getTypeAllocSize(CP->getType()->getElementType());
for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize); InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
return; return;
} else if (isa<ConstantAggregateZero>(Init)) { }
if (isa<ConstantAggregateZero>(Init)) {
memset(Addr, 0, (size_t)getTargetData()->getTypeAllocSize(Init->getType())); memset(Addr, 0, (size_t)getTargetData()->getTypeAllocSize(Init->getType()));
return; return;
} else if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) { }
if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) {
unsigned ElementSize = unsigned ElementSize =
getTargetData()->getTypeAllocSize(CPA->getType()->getElementType()); getTargetData()->getTypeAllocSize(CPA->getType()->getElementType());
for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i) for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize); InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
return; return;
} else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) { }
if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) {
const StructLayout *SL = const StructLayout *SL =
getTargetData()->getStructLayout(cast<StructType>(CPS->getType())); getTargetData()->getStructLayout(cast<StructType>(CPS->getType()));
for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i) for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i)); InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i));
return; return;
} else if (Init->getType()->isFirstClassType()) { }
if (const ConstantDataSequential *CDS =
dyn_cast<ConstantDataSequential>(Init)) {
// CDS is already laid out in host memory order.
StringRef Data = CDS->getRawDataValues();
memcpy(Addr, Data.data(), Data.size());
return;
}
if (Init->getType()->isFirstClassType()) {
GenericValue Val = getConstantValue(Init); GenericValue Val = getConstantValue(Init);
StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType()); StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
return; return;

View File

@ -843,29 +843,32 @@ bool ModuleLinker::linkAliasProto(GlobalAlias *SGA) {
return false; return false;
} }
static void getArrayElements(Constant *C, SmallVectorImpl<Constant*> &Dest) {
if (ConstantArray *I = dyn_cast<ConstantArray>(C)) {
for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
Dest.push_back(I->getOperand(i));
return;
}
if (ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(C)) {
for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
Dest.push_back(CDS->getElementAsConstant(i));
return;
}
ConstantAggregateZero *CAZ = cast<ConstantAggregateZero>(C);
Dest.append(cast<ArrayType>(C->getType())->getNumElements(),
CAZ->getSequentialElement());
}
void ModuleLinker::linkAppendingVarInit(const AppendingVarInfo &AVI) { void ModuleLinker::linkAppendingVarInit(const AppendingVarInfo &AVI) {
// Merge the initializer. // Merge the initializer.
SmallVector<Constant*, 16> Elements; SmallVector<Constant*, 16> Elements;
if (ConstantArray *I = dyn_cast<ConstantArray>(AVI.DstInit)) { getArrayElements(AVI.DstInit, Elements);
for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
Elements.push_back(I->getOperand(i));
} else {
assert(isa<ConstantAggregateZero>(AVI.DstInit));
ArrayType *DstAT = cast<ArrayType>(AVI.DstInit->getType());
Type *EltTy = DstAT->getElementType();
Elements.append(DstAT->getNumElements(), Constant::getNullValue(EltTy));
}
Constant *SrcInit = MapValue(AVI.SrcInit, ValueMap, RF_None, &TypeMap); Constant *SrcInit = MapValue(AVI.SrcInit, ValueMap, RF_None, &TypeMap);
if (const ConstantArray *I = dyn_cast<ConstantArray>(SrcInit)) { getArrayElements(SrcInit, Elements);
for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
Elements.push_back(I->getOperand(i));
} else {
assert(isa<ConstantAggregateZero>(SrcInit));
ArrayType *SrcAT = cast<ArrayType>(SrcInit->getType());
Type *EltTy = SrcAT->getElementType();
Elements.append(SrcAT->getNumElements(), Constant::getNullValue(EltTy));
}
ArrayType *NewType = cast<ArrayType>(AVI.NewGV->getType()->getElementType()); ArrayType *NewType = cast<ArrayType>(AVI.NewGV->getType()->getElementType());
AVI.NewGV->setInitializer(ConstantArray::get(NewType, Elements)); AVI.NewGV->setInitializer(ConstantArray::get(NewType, Elements));
} }

View File

@ -1995,8 +1995,7 @@ Type *ConstantDataSequential::getElementType() const {
} }
StringRef ConstantDataSequential::getRawDataValues() const { StringRef ConstantDataSequential::getRawDataValues() const {
return StringRef(DataElements, return StringRef(DataElements, getNumElements()*getElementByteSize());
getType()->getNumElements()*getElementByteSize());
} }
/// isElementTypeCompatible - Return true if a ConstantDataSequential can be /// isElementTypeCompatible - Return true if a ConstantDataSequential can be
@ -2018,6 +2017,12 @@ bool ConstantDataSequential::isElementTypeCompatible(const Type *Ty) {
return false; return false;
} }
/// getNumElements - Return the number of elements in the array or vector.
unsigned ConstantDataSequential::getNumElements() const {
return getType()->getNumElements();
}
/// getElementByteSize - Return the size in bytes of the elements in the data. /// getElementByteSize - Return the size in bytes of the elements in the data.
uint64_t ConstantDataSequential::getElementByteSize() const { uint64_t ConstantDataSequential::getElementByteSize() const {
return getElementType()->getPrimitiveSizeInBits()/8; return getElementType()->getPrimitiveSizeInBits()/8;
@ -2025,7 +2030,7 @@ uint64_t ConstantDataSequential::getElementByteSize() const {
/// getElementPointer - Return the start of the specified element. /// getElementPointer - Return the start of the specified element.
const char *ConstantDataSequential::getElementPointer(unsigned Elt) const { const char *ConstantDataSequential::getElementPointer(unsigned Elt) const {
assert(Elt < getElementType()->getNumElements() && "Invalid Elt"); assert(Elt < getNumElements() && "Invalid Elt");
return DataElements+Elt*getElementByteSize(); return DataElements+Elt*getElementByteSize();
} }