mirror of
https://github.com/RPCSX/llvm.git
synced 2025-02-05 11:57:07 +00:00
improve support for uniontype and ConstantUnion, patch by Tim Northover!
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@98656 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
eb2693ebc0
commit
8b3b34f410
@ -293,6 +293,8 @@ void BitcodeReaderValueList::ResolveConstantForwardRefs() {
|
|||||||
} else if (ConstantStruct *UserCS = dyn_cast<ConstantStruct>(UserC)) {
|
} else if (ConstantStruct *UserCS = dyn_cast<ConstantStruct>(UserC)) {
|
||||||
NewC = ConstantStruct::get(Context, &NewOps[0], NewOps.size(),
|
NewC = ConstantStruct::get(Context, &NewOps[0], NewOps.size(),
|
||||||
UserCS->getType()->isPacked());
|
UserCS->getType()->isPacked());
|
||||||
|
} else if (ConstantUnion *UserCU = dyn_cast<ConstantUnion>(UserC)) {
|
||||||
|
NewC = ConstantUnion::get(UserCU->getType(), NewOps[0]);
|
||||||
} else if (isa<ConstantVector>(UserC)) {
|
} else if (isa<ConstantVector>(UserC)) {
|
||||||
NewC = ConstantVector::get(&NewOps[0], NewOps.size());
|
NewC = ConstantVector::get(&NewOps[0], NewOps.size());
|
||||||
} else {
|
} else {
|
||||||
@ -1015,6 +1017,11 @@ bool BitcodeReader::ParseConstants() {
|
|||||||
Elts.push_back(ValueList.getConstantFwdRef(Record[i],
|
Elts.push_back(ValueList.getConstantFwdRef(Record[i],
|
||||||
STy->getElementType(i)));
|
STy->getElementType(i)));
|
||||||
V = ConstantStruct::get(STy, Elts);
|
V = ConstantStruct::get(STy, Elts);
|
||||||
|
} else if (const UnionType *UnTy = dyn_cast<UnionType>(CurTy)) {
|
||||||
|
uint64_t Index = Record[0];
|
||||||
|
Constant *Val = ValueList.getConstantFwdRef(Record[1],
|
||||||
|
UnTy->getElementType(Index));
|
||||||
|
V = ConstantUnion::get(UnTy, Val);
|
||||||
} else if (const ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) {
|
} else if (const ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) {
|
||||||
const Type *EltTy = ATy->getElementType();
|
const Type *EltTy = ATy->getElementType();
|
||||||
for (unsigned i = 0; i != Size; ++i)
|
for (unsigned i = 0; i != Size; ++i)
|
||||||
|
@ -808,11 +808,25 @@ static void WriteConstants(unsigned FirstVal, unsigned LastVal,
|
|||||||
else if (isCStr7)
|
else if (isCStr7)
|
||||||
AbbrevToUse = CString7Abbrev;
|
AbbrevToUse = CString7Abbrev;
|
||||||
} else if (isa<ConstantArray>(C) || isa<ConstantStruct>(V) ||
|
} else if (isa<ConstantArray>(C) || isa<ConstantStruct>(V) ||
|
||||||
isa<ConstantUnion>(C) || isa<ConstantVector>(V)) {
|
isa<ConstantVector>(V)) {
|
||||||
Code = bitc::CST_CODE_AGGREGATE;
|
Code = bitc::CST_CODE_AGGREGATE;
|
||||||
for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
|
for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
|
||||||
Record.push_back(VE.getValueID(C->getOperand(i)));
|
Record.push_back(VE.getValueID(C->getOperand(i)));
|
||||||
AbbrevToUse = AggregateAbbrev;
|
AbbrevToUse = AggregateAbbrev;
|
||||||
|
} else if (isa<ConstantUnion>(C)) {
|
||||||
|
Code = bitc::CST_CODE_AGGREGATE;
|
||||||
|
|
||||||
|
// Unions only have one entry but we must send type along with it.
|
||||||
|
const Type *EntryKind = C->getOperand(0)->getType();
|
||||||
|
|
||||||
|
const UnionType *UnTy = cast<UnionType>(C->getType());
|
||||||
|
int UnionIndex = UnTy->getElementTypeIndex(EntryKind);
|
||||||
|
assert(UnionIndex != -1 && "Constant union contains invalid entry");
|
||||||
|
|
||||||
|
Record.push_back(UnionIndex);
|
||||||
|
Record.push_back(VE.getValueID(C->getOperand(0)));
|
||||||
|
|
||||||
|
AbbrevToUse = AggregateAbbrev;
|
||||||
} else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
|
} else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
|
||||||
switch (CE->getOpcode()) {
|
switch (CE->getOpcode()) {
|
||||||
default:
|
default:
|
||||||
|
@ -1138,6 +1138,21 @@ static void EmitGlobalConstantStruct(const ConstantStruct *CS,
|
|||||||
"Layout of constant struct may be incorrect!");
|
"Layout of constant struct may be incorrect!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void EmitGlobalConstantUnion(const ConstantUnion *CU,
|
||||||
|
unsigned AddrSpace, AsmPrinter &AP) {
|
||||||
|
const TargetData *TD = AP.TM.getTargetData();
|
||||||
|
unsigned Size = TD->getTypeAllocSize(CU->getType());
|
||||||
|
|
||||||
|
const Constant *Contents = CU->getOperand(0);
|
||||||
|
unsigned FilledSize = TD->getTypeAllocSize(Contents->getType());
|
||||||
|
|
||||||
|
// Print the actually filled part
|
||||||
|
AP.EmitGlobalConstant(Contents, AddrSpace);
|
||||||
|
|
||||||
|
// And pad with enough zeroes
|
||||||
|
AP.OutStreamer.EmitZeros(Size-FilledSize, AddrSpace);
|
||||||
|
}
|
||||||
|
|
||||||
static void EmitGlobalConstantFP(const ConstantFP *CFP, unsigned AddrSpace,
|
static void EmitGlobalConstantFP(const ConstantFP *CFP, unsigned AddrSpace,
|
||||||
AsmPrinter &AP) {
|
AsmPrinter &AP) {
|
||||||
// FP Constants are printed as integer constants to avoid losing
|
// FP Constants are printed as integer constants to avoid losing
|
||||||
@ -1258,15 +1273,18 @@ void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) {
|
|||||||
if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
|
if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
|
||||||
return EmitGlobalConstantFP(CFP, AddrSpace, *this);
|
return EmitGlobalConstantFP(CFP, AddrSpace, *this);
|
||||||
|
|
||||||
if (const ConstantVector *V = dyn_cast<ConstantVector>(CV))
|
|
||||||
return EmitGlobalConstantVector(V, AddrSpace, *this);
|
|
||||||
|
|
||||||
if (isa<ConstantPointerNull>(CV)) {
|
if (isa<ConstantPointerNull>(CV)) {
|
||||||
unsigned Size = TM.getTargetData()->getTypeAllocSize(CV->getType());
|
unsigned Size = TM.getTargetData()->getTypeAllocSize(CV->getType());
|
||||||
OutStreamer.EmitIntValue(0, Size, AddrSpace);
|
OutStreamer.EmitIntValue(0, Size, AddrSpace);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (const ConstantUnion *CVU = dyn_cast<ConstantUnion>(CV))
|
||||||
|
return EmitGlobalConstantUnion(CVU, AddrSpace, *this);
|
||||||
|
|
||||||
|
if (const ConstantVector *V = dyn_cast<ConstantVector>(CV))
|
||||||
|
return EmitGlobalConstantVector(V, AddrSpace, *this);
|
||||||
|
|
||||||
// Otherwise, it must be a ConstantExpr. Lower it to an MCExpr, then emit it
|
// Otherwise, it must be a ConstantExpr. Lower it to an MCExpr, then emit it
|
||||||
// thread the streamer with EmitValue.
|
// thread the streamer with EmitValue.
|
||||||
OutStreamer.EmitValue(LowerConstant(CV, *this),
|
OutStreamer.EmitValue(LowerConstant(CV, *this),
|
||||||
|
@ -2592,6 +2592,11 @@ void SelectionDAGBuilder::visitGetElementPtr(User &I) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Ty = StTy->getElementType(Field);
|
Ty = StTy->getElementType(Field);
|
||||||
|
} else if (const UnionType *UnTy = dyn_cast<UnionType>(Ty)) {
|
||||||
|
unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
|
||||||
|
|
||||||
|
// Offset canonically 0 for unions, but type changes
|
||||||
|
Ty = UnTy->getElementType(Field);
|
||||||
} else {
|
} else {
|
||||||
Ty = cast<SequentialType>(Ty)->getElementType();
|
Ty = cast<SequentialType>(Ty)->getElementType();
|
||||||
|
|
||||||
|
@ -460,6 +460,15 @@ uint64_t TargetData::getTypeSizeInBits(const Type *Ty) const {
|
|||||||
case Type::StructTyID:
|
case Type::StructTyID:
|
||||||
// Get the layout annotation... which is lazily created on demand.
|
// Get the layout annotation... which is lazily created on demand.
|
||||||
return getStructLayout(cast<StructType>(Ty))->getSizeInBits();
|
return getStructLayout(cast<StructType>(Ty))->getSizeInBits();
|
||||||
|
case Type::UnionTyID: {
|
||||||
|
const UnionType *UnTy = cast<UnionType>(Ty);
|
||||||
|
uint64_t Size = 0;
|
||||||
|
for (UnionType::element_iterator i = UnTy->element_begin(),
|
||||||
|
e = UnTy->element_end(); i != e; ++i) {
|
||||||
|
Size = std::max(Size, getTypeSizeInBits(*i));
|
||||||
|
}
|
||||||
|
return Size;
|
||||||
|
}
|
||||||
case Type::IntegerTyID:
|
case Type::IntegerTyID:
|
||||||
return cast<IntegerType>(Ty)->getBitWidth();
|
return cast<IntegerType>(Ty)->getBitWidth();
|
||||||
case Type::VoidTyID:
|
case Type::VoidTyID:
|
||||||
@ -516,6 +525,17 @@ unsigned char TargetData::getAlignment(const Type *Ty, bool abi_or_pref) const {
|
|||||||
unsigned Align = getAlignmentInfo(AGGREGATE_ALIGN, 0, abi_or_pref, Ty);
|
unsigned Align = getAlignmentInfo(AGGREGATE_ALIGN, 0, abi_or_pref, Ty);
|
||||||
return std::max(Align, (unsigned)Layout->getAlignment());
|
return std::max(Align, (unsigned)Layout->getAlignment());
|
||||||
}
|
}
|
||||||
|
case Type::UnionTyID: {
|
||||||
|
const UnionType *UnTy = cast<UnionType>(Ty);
|
||||||
|
unsigned Align = 1;
|
||||||
|
|
||||||
|
// Unions need the maximum alignment of all their entries
|
||||||
|
for (UnionType::element_iterator i = UnTy->element_begin(),
|
||||||
|
e = UnTy->element_end(); i != e; ++i) {
|
||||||
|
Align = std::max(Align, (unsigned)getAlignment(*i, abi_or_pref));
|
||||||
|
}
|
||||||
|
return Align;
|
||||||
|
}
|
||||||
case Type::IntegerTyID:
|
case Type::IntegerTyID:
|
||||||
case Type::VoidTyID:
|
case Type::VoidTyID:
|
||||||
AlignType = INTEGER_ALIGN;
|
AlignType = INTEGER_ALIGN;
|
||||||
@ -600,6 +620,11 @@ uint64_t TargetData::getIndexedOffset(const Type *ptrTy, Value* const* Indices,
|
|||||||
|
|
||||||
// Update Ty to refer to current element
|
// Update Ty to refer to current element
|
||||||
Ty = STy->getElementType(FieldNo);
|
Ty = STy->getElementType(FieldNo);
|
||||||
|
} else if (const UnionType *UnTy = dyn_cast<UnionType>(*TI)) {
|
||||||
|
unsigned FieldNo = cast<ConstantInt>(Indices[CurIDX])->getZExtValue();
|
||||||
|
|
||||||
|
// Offset into union is canonically 0, but type changes
|
||||||
|
Ty = UnTy->getElementType(FieldNo);
|
||||||
} else {
|
} else {
|
||||||
// Update Ty to refer to current element
|
// Update Ty to refer to current element
|
||||||
Ty = cast<SequentialType>(Ty)->getElementType();
|
Ty = cast<SequentialType>(Ty)->getElementType();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user