From b1919e2f08ecb37140af676fd2916f8d5ed7df3d Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sat, 10 Feb 2007 19:55:17 +0000 Subject: [PATCH] Privatize StructLayout::MemberOffsets, adding an accessor git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@34156 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Target/TargetData.h | 9 +++++++-- lib/Analysis/ConstantFolding.cpp | 2 +- lib/CodeGen/AsmPrinter.cpp | 4 ++-- lib/CodeGen/MachOWriter.cpp | 3 ++- lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp | 4 ++-- lib/ExecutionEngine/ExecutionEngine.cpp | 2 +- lib/ExecutionEngine/Interpreter/Execution.cpp | 2 +- lib/Target/TargetData.cpp | 3 +-- lib/Transforms/Scalar/LoopStrengthReduce.cpp | 2 +- lib/Transforms/Scalar/ScalarReplAggregates.cpp | 3 ++- 10 files changed, 20 insertions(+), 14 deletions(-) diff --git a/include/llvm/Target/TargetData.h b/include/llvm/Target/TargetData.h index d577de7c8a0..516aae700d8 100644 --- a/include/llvm/Target/TargetData.h +++ b/include/llvm/Target/TargetData.h @@ -275,16 +275,21 @@ public: /// target machine, based on the TargetData structure. /// class StructLayout { -public: std::vector 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); diff --git a/lib/Analysis/ConstantFolding.cpp b/lib/Analysis/ConstantFolding.cpp index 25a64ab9a68..1515301a77a 100644 --- a/lib/Analysis/ConstantFolding.cpp +++ b/lib/Analysis/ConstantFolding.cpp @@ -70,7 +70,7 @@ static bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, if (const StructType *ST = dyn_cast(*GTI)) { // N = N + Offset - Offset += TD.getStructLayout(ST)->MemberOffsets[CI->getZExtValue()]; + Offset += TD.getStructLayout(ST)->getElementOffset(CI->getZExtValue()); } else { const SequentialType *ST = cast(*GTI); Offset += TD.getTypeSize(ST->getElementType())*CI->getSExtValue(); diff --git a/lib/CodeGen/AsmPrinter.cpp b/lib/CodeGen/AsmPrinter.cpp index 0394a02aa16..6dac1ea3759 100644 --- a/lib/CodeGen/AsmPrinter.cpp +++ b/lib/CodeGen/AsmPrinter.cpp @@ -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 diff --git a/lib/CodeGen/MachOWriter.cpp b/lib/CodeGen/MachOWriter.cpp index 3beb11a25f2..384dd3ee547 100644 --- a/lib/CodeGen/MachOWriter.cpp +++ b/lib/CodeGen/MachOWriter.cpp @@ -878,7 +878,8 @@ void MachOWriter::InitMem(const Constant *C, void *Addr, intptr_t Offset, const StructLayout *SL = TD->getStructLayout(cast(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!"); diff --git a/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp b/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp index ae7a495bf48..16d25ee2259 100644 --- a/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp +++ b/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp @@ -1678,7 +1678,7 @@ void SelectionDAGLowering::visitGetElementPtr(User &I) { unsigned Field = cast(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(Ty)) { unsigned Field = cast(Idx)->getZExtValue(); if (Field) - ConstantOffset += TD->getStructLayout(StTy)->MemberOffsets[Field]; + ConstantOffset += TD->getStructLayout(StTy)->getElementOffset(Field); Ty = StTy->getElementType(Field); } else { Ty = cast(Ty)->getElementType(); diff --git a/lib/ExecutionEngine/ExecutionEngine.cpp b/lib/ExecutionEngine/ExecutionEngine.cpp index 0fd6d461e1f..6878f89fffb 100644 --- a/lib/ExecutionEngine/ExecutionEngine.cpp +++ b/lib/ExecutionEngine/ExecutionEngine.cpp @@ -716,7 +716,7 @@ void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) { const StructLayout *SL = getTargetData()->getStructLayout(cast(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; } diff --git a/lib/ExecutionEngine/Interpreter/Execution.cpp b/lib/ExecutionEngine/Interpreter/Execution.cpp index fe80dfddd63..936d64f4441 100644 --- a/lib/ExecutionEngine/Interpreter/Execution.cpp +++ b/lib/ExecutionEngine/Interpreter/Execution.cpp @@ -1082,7 +1082,7 @@ GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I, const ConstantInt *CPU = cast(I.getOperand()); unsigned Index = unsigned(CPU->getZExtValue()); - Total += (PointerTy)SLO->MemberOffsets[Index]; + Total += (PointerTy)SLO->getElementOffset(Index); } else { const SequentialType *ST = cast(*I); // Get the index number for the array... which must be long type... diff --git a/lib/Target/TargetData.cpp b/lib/Target/TargetData.cpp index 41288ec6713..1ebe5b5711e 100644 --- a/lib/Target/TargetData.cpp +++ b/lib/Target/TargetData.cpp @@ -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); diff --git a/lib/Transforms/Scalar/LoopStrengthReduce.cpp b/lib/Transforms/Scalar/LoopStrengthReduce.cpp index d841642de05..e97921237fc 100644 --- a/lib/Transforms/Scalar/LoopStrengthReduce.cpp +++ b/lib/Transforms/Scalar/LoopStrengthReduce.cpp @@ -267,7 +267,7 @@ SCEVHandle LoopStrengthReduce::GetExpressionSCEV(Instruction *Exp, Loop *L) { if (const StructType *STy = dyn_cast(*GTI)) { const StructLayout *SL = TD->getStructLayout(STy); unsigned Idx = cast(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 { diff --git a/lib/Transforms/Scalar/ScalarReplAggregates.cpp b/lib/Transforms/Scalar/ScalarReplAggregates.cpp index 6b99bc899bf..016a421945d 100644 --- a/lib/Transforms/Scalar/ScalarReplAggregates.cpp +++ b/lib/Transforms/Scalar/ScalarReplAggregates.cpp @@ -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(AggTy)) { - unsigned EltBitOffset = TD.getStructLayout(STy)->MemberOffsets[Idx]*8; + unsigned EltBitOffset = + TD.getStructLayout(STy)->getElementOffset(Idx)*8; if (TD.isLittleEndian() || isVectorInsert) NewOffset += EltBitOffset;