Privatize StructLayout::MemberOffsets, adding an accessor

llvm-svn: 34156
This commit is contained in:
Chris Lattner 2007-02-10 19:55:17 +00:00
parent 2230c96657
commit c473d8e431
10 changed files with 20 additions and 14 deletions

View File

@ -275,16 +275,21 @@ public:
/// target machine, based on the TargetData structure.
///
class StructLayout {
public:
std::vector<uint64_t> MemberOffsets;
uint64_t StructSize;
public:
unsigned StructAlignment;
uint64_t StructSize;
/// getElementContainingOffset - Given a valid offset into the structure,
/// return the structure index that contains it.
///
unsigned getElementContainingOffset(uint64_t Offset) const;
uint64_t getElementOffset(unsigned Idx) const {
assert(Idx < MemberOffsets.size() && "Invalid element idx!");
return MemberOffsets[Idx];
}
private:
friend class TargetData; // Only TargetData can create this class
StructLayout(const StructType *ST, const TargetData &TD);

View File

@ -70,7 +70,7 @@ static bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV,
if (const StructType *ST = dyn_cast<StructType>(*GTI)) {
// N = N + Offset
Offset += TD.getStructLayout(ST)->MemberOffsets[CI->getZExtValue()];
Offset += TD.getStructLayout(ST)->getElementOffset(CI->getZExtValue());
} else {
const SequentialType *ST = cast<SequentialType>(*GTI);
Offset += TD.getTypeSize(ST->getElementType())*CI->getSExtValue();

View File

@ -739,8 +739,8 @@ void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
// Check if padding is needed and insert one or more 0s.
uint64_t fieldSize = TD->getTypeSize(field->getType());
uint64_t padSize = ((i == e-1? cvsLayout->StructSize
: cvsLayout->MemberOffsets[i+1])
- cvsLayout->MemberOffsets[i]) - fieldSize;
: cvsLayout->getElementOffset(i+1))
- cvsLayout->getElementOffset(i)) - fieldSize;
sizeSoFar += fieldSize + padSize;
// Now print the actual field value

View File

@ -878,7 +878,8 @@ void MachOWriter::InitMem(const Constant *C, void *Addr, intptr_t Offset,
const StructLayout *SL =
TD->getStructLayout(cast<StructType>(CPS->getType()));
for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
WorkList.push_back(CPair(CPS->getOperand(i), PA+SL->MemberOffsets[i]));
WorkList.push_back(CPair(CPS->getOperand(i),
PA+SL->getElementOffset(i)));
} else {
cerr << "Bad Type: " << *PC->getType() << "\n";
assert(0 && "Unknown constant type to initialize memory with!");

View File

@ -1678,7 +1678,7 @@ void SelectionDAGLowering::visitGetElementPtr(User &I) {
unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
if (Field) {
// N = N + Offset
uint64_t Offset = TD->getStructLayout(StTy)->MemberOffsets[Field];
uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
N = DAG.getNode(ISD::ADD, N.getValueType(), N,
getIntPtrConstant(Offset));
}
@ -3702,7 +3702,7 @@ static bool OptimizeGEPExpression(GetElementPtrInst *GEPI,
if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
if (Field)
ConstantOffset += TD->getStructLayout(StTy)->MemberOffsets[Field];
ConstantOffset += TD->getStructLayout(StTy)->getElementOffset(Field);
Ty = StTy->getElementType(Field);
} else {
Ty = cast<SequentialType>(Ty)->getElementType();

View File

@ -716,7 +716,7 @@ void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
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->MemberOffsets[i]);
InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i));
return;
}

View File

@ -1082,7 +1082,7 @@ GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
unsigned Index = unsigned(CPU->getZExtValue());
Total += (PointerTy)SLO->MemberOffsets[Index];
Total += (PointerTy)SLO->getElementOffset(Index);
} else {
const SequentialType *ST = cast<SequentialType>(*I);
// Get the index number for the array... which must be long type...

View File

@ -466,8 +466,7 @@ uint64_t TargetData::getIndexedOffset(const Type *ptrTy, Value* const* Indices,
const StructLayout *Layout = getStructLayout(STy);
// Add in the offset, as calculated by the structure layout info...
assert(FieldNo < Layout->MemberOffsets.size() &&"FieldNo out of range!");
Result += Layout->MemberOffsets[FieldNo];
Result += Layout->getElementOffset(FieldNo);
// Update Ty to refer to current element
Ty = STy->getElementType(FieldNo);

View File

@ -267,7 +267,7 @@ SCEVHandle LoopStrengthReduce::GetExpressionSCEV(Instruction *Exp, Loop *L) {
if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
const StructLayout *SL = TD->getStructLayout(STy);
unsigned Idx = cast<ConstantInt>(GEP->getOperand(i))->getZExtValue();
uint64_t Offset = SL->MemberOffsets[Idx];
uint64_t Offset = SL->getElementOffset(Idx);
GEPVal = SCEVAddExpr::get(GEPVal,
SCEVUnknown::getIntegerSCEV(Offset, UIntPtrTy));
} else {

View File

@ -801,7 +801,8 @@ void SROA::ConvertUsesToScalar(Value *Ptr, AllocaInst *NewAI, unsigned Offset) {
else
NewOffset += AggSizeInBits-ElSizeBits*(Idx+1);
} else if (const StructType *STy = dyn_cast<StructType>(AggTy)) {
unsigned EltBitOffset = TD.getStructLayout(STy)->MemberOffsets[Idx]*8;
unsigned EltBitOffset =
TD.getStructLayout(STy)->getElementOffset(Idx)*8;
if (TD.isLittleEndian() || isVectorInsert)
NewOffset += EltBitOffset;