summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2012-01-24 13:41:11 +0000
committerChris Lattner <sabre@nondot.org>2012-01-24 13:41:11 +0000
commit1ee0ecf84a07693c3a517ba030fac8ac1f9f3fbc (patch)
treece6fb6a15a49539b812ba49d0aec84154fbe1aae
parent53fa1ae51008d22ce5d2aa6fbf22c1afc4ec1401 (diff)
downloadllvm-1ee0ecf84a07693c3a517ba030fac8ac1f9f3fbc.tar.gz
llvm-1ee0ecf84a07693c3a517ba030fac8ac1f9f3fbc.tar.bz2
llvm-1ee0ecf84a07693c3a517ba030fac8ac1f9f3fbc.tar.xz
add more support for ConstantDataSequential
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@148802 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/llvm/Constants.h3
-rw-r--r--lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp21
-rw-r--r--lib/ExecutionEngine/ExecutionEngine.cpp38
-rw-r--r--lib/Linker/LinkModules.cpp39
-rw-r--r--lib/VMCore/Constants.cpp11
5 files changed, 78 insertions, 34 deletions
diff --git a/include/llvm/Constants.h b/include/llvm/Constants.h
index 510847ff23..3dc0ec3de0 100644
--- a/include/llvm/Constants.h
+++ b/include/llvm/Constants.h
@@ -623,6 +623,9 @@ public:
/// getElementType - Return the element type of the array/vector.
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
/// array/vector. The size of the elements is known to be a multiple of one
diff --git a/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
index ee60a9f815..cd9bec11bd 100644
--- a/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
+++ b/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
@@ -1055,6 +1055,23 @@ SDValue SelectionDAGBuilder::getValueImpl(const Value *V) {
return DAG.getMergeValues(&Constants[0], Constants.size(),
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()) {
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
// elements into the Ops array based on what kind of constant it is.
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)
- Ops.push_back(getValue(CP->getOperand(i)));
+ Ops.push_back(getValue(CV->getOperand(i)));
} else {
assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!");
EVT EltVT = TLI.getValueType(VecTy->getElementType());
diff --git a/lib/ExecutionEngine/ExecutionEngine.cpp b/lib/ExecutionEngine/ExecutionEngine.cpp
index 7829a2986b..b9356dcd0a 100644
--- a/lib/ExecutionEngine/ExecutionEngine.cpp
+++ b/lib/ExecutionEngine/ExecutionEngine.cpp
@@ -307,13 +307,12 @@ void ExecutionEngine::runStaticConstructorsDestructors(Module *module,
// Should be an array of '{ i32, void ()* }' structs. The first value is
// the init priority, which we ignore.
- if (isa<ConstantAggregateZero>(GV->getInitializer()))
+ ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
+ if (InitList == 0)
return;
- ConstantArray *InitList = cast<ConstantArray>(GV->getInitializer());
for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
- if (isa<ConstantAggregateZero>(InitList->getOperand(i)))
- continue;
- ConstantStruct *CS = cast<ConstantStruct>(InitList->getOperand(i));
+ ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i));
+ if (CS == 0) continue;
Constant *FP = CS->getOperand(1);
if (FP->isNullValue())
@@ -954,30 +953,47 @@ void ExecutionEngine::LoadValueFromMemory(GenericValue &Result,
void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
DEBUG(dbgs() << "JIT: Initializing " << Addr << " ");
DEBUG(Init->dump());
- if (isa<UndefValue>(Init)) {
+ if (isa<UndefValue>(Init))
return;
- } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) {
+
+ if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) {
unsigned ElementSize =
getTargetData()->getTypeAllocSize(CP->getType()->getElementType());
for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
return;
- } else if (isa<ConstantAggregateZero>(Init)) {
+ }
+
+ if (isa<ConstantAggregateZero>(Init)) {
memset(Addr, 0, (size_t)getTargetData()->getTypeAllocSize(Init->getType()));
return;
- } else if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) {
+ }
+
+ if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) {
unsigned ElementSize =
getTargetData()->getTypeAllocSize(CPA->getType()->getElementType());
for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
return;
- } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) {
+ }
+
+ if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) {
const StructLayout *SL =
getTargetData()->getStructLayout(cast<StructType>(CPS->getType()));
for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i));
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);
StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
return;
diff --git a/lib/Linker/LinkModules.cpp b/lib/Linker/LinkModules.cpp
index 46a76a2599..563aed5daa 100644
--- a/lib/Linker/LinkModules.cpp
+++ b/lib/Linker/LinkModules.cpp
@@ -843,29 +843,32 @@ bool ModuleLinker::linkAliasProto(GlobalAlias *SGA) {
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) {
// Merge the initializer.
SmallVector<Constant*, 16> Elements;
- if (ConstantArray *I = dyn_cast<ConstantArray>(AVI.DstInit)) {
- 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));
- }
+ getArrayElements(AVI.DstInit, Elements);
Constant *SrcInit = MapValue(AVI.SrcInit, ValueMap, RF_None, &TypeMap);
- if (const ConstantArray *I = dyn_cast<ConstantArray>(SrcInit)) {
- 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));
- }
+ getArrayElements(SrcInit, Elements);
+
ArrayType *NewType = cast<ArrayType>(AVI.NewGV->getType()->getElementType());
AVI.NewGV->setInitializer(ConstantArray::get(NewType, Elements));
}
diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp
index 9f1abbd17b..d329c67af0 100644
--- a/lib/VMCore/Constants.cpp
+++ b/lib/VMCore/Constants.cpp
@@ -1995,8 +1995,7 @@ Type *ConstantDataSequential::getElementType() const {
}
StringRef ConstantDataSequential::getRawDataValues() const {
- return StringRef(DataElements,
- getType()->getNumElements()*getElementByteSize());
+ return StringRef(DataElements, getNumElements()*getElementByteSize());
}
/// isElementTypeCompatible - Return true if a ConstantDataSequential can be
@@ -2018,6 +2017,12 @@ bool ConstantDataSequential::isElementTypeCompatible(const Type *Ty) {
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.
uint64_t ConstantDataSequential::getElementByteSize() const {
return getElementType()->getPrimitiveSizeInBits()/8;
@@ -2025,7 +2030,7 @@ uint64_t ConstantDataSequential::getElementByteSize() const {
/// getElementPointer - Return the start of the specified element.
const char *ConstantDataSequential::getElementPointer(unsigned Elt) const {
- assert(Elt < getElementType()->getNumElements() && "Invalid Elt");
+ assert(Elt < getNumElements() && "Invalid Elt");
return DataElements+Elt*getElementByteSize();
}