1 //===-- TargetData.cpp - Data size & alignment routines --------------------==//
3 // This file defines target properties related to datatype size/offset/alignment
4 // information. It uses lazy annotations to cache information about how
5 // structure types are laid out and used.
7 // This structure should be created once, filled in if the defaults are not
8 // correct and then passed around by const&. None of the members functions
9 // require modification to the object.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Target/TargetData.h"
14 #include "llvm/Module.h"
15 #include "llvm/DerivedTypes.h"
16 #include "llvm/Constants.h"
18 // Handle the Pass registration stuff neccesary to use TargetData's.
20 // Register the default SparcV9 implementation...
21 RegisterPass<TargetData> X("targetdata", "Target Data Layout");
25 static inline void getTypeInfo(const Type *Ty, const TargetData *TD,
26 uint64_t &Size, unsigned char &Alignment);
28 //===----------------------------------------------------------------------===//
29 // Support for StructLayout Annotation
30 //===----------------------------------------------------------------------===//
32 StructLayout::StructLayout(const StructType *ST, const TargetData &TD)
33 : Annotation(TD.getStructLayoutAID()) {
37 // Loop over each of the elements, placing them in memory...
38 for (StructType::ElementTypes::const_iterator
39 TI = ST->getElementTypes().begin(),
40 TE = ST->getElementTypes().end(); TI != TE; ++TI) {
45 getTypeInfo(Ty, &TD, TySize, A);
48 // Add padding if neccesary to make the data element aligned properly...
49 if (StructSize % TyAlign != 0)
50 StructSize = (StructSize/TyAlign + 1) * TyAlign; // Add padding...
52 // Keep track of maximum alignment constraint
53 StructAlignment = std::max(TyAlign, StructAlignment);
55 MemberOffsets.push_back(StructSize);
56 StructSize += TySize; // Consume space for this data item
59 // Empty structures have alignment of 1 byte.
60 if (StructAlignment == 0) StructAlignment = 1;
62 // Add padding to the end of the struct so that it could be put in an array
63 // and all array elements would be aligned correctly.
64 if (StructSize % StructAlignment != 0)
65 StructSize = (StructSize/StructAlignment + 1) * StructAlignment;
68 Annotation *TargetData::TypeAnFactory(AnnotationID AID, const Annotable *T,
70 const TargetData &TD = *(const TargetData*)D;
71 assert(AID == TD.AID && "Target data annotation ID mismatch!");
72 const Type *Ty = cast<Type>((const Value *)T);
73 assert(isa<StructType>(Ty) &&
74 "Can only create StructLayout annotation on structs!");
75 return new StructLayout(cast<StructType>(Ty), TD);
78 //===----------------------------------------------------------------------===//
79 // TargetData Class Implementation
80 //===----------------------------------------------------------------------===//
82 TargetData::TargetData(const std::string &TargetName,
83 bool isLittleEndian, unsigned char PtrSize,
84 unsigned char PtrAl, unsigned char DoubleAl,
85 unsigned char FloatAl, unsigned char LongAl,
86 unsigned char IntAl, unsigned char ShortAl,
88 : AID(AnnotationManager::getID("TargetData::" + TargetName)) {
89 AnnotationManager::registerAnnotationFactory(AID, TypeAnFactory, this);
91 // If this assert triggers, a pass "required" TargetData information, but the
92 // top level tool did not provide once for it. We do not want to default
93 // construct, or else we might end up using a bad endianness or pointer size!
95 assert(!TargetName.empty() &&
96 "ERROR: Tool did not specify a target data to use!");
98 LittleEndian = isLittleEndian;
99 PointerSize = PtrSize;
100 PointerAlignment = PtrAl;
101 DoubleAlignment = DoubleAl;
102 assert(DoubleAlignment == PtrAl &&
103 "Double alignment and pointer alignment agree for now!");
104 FloatAlignment = FloatAl;
105 LongAlignment = LongAl;
106 IntAlignment = IntAl;
107 ShortAlignment = ShortAl;
108 ByteAlignment = ByteAl;
111 TargetData::TargetData(const std::string &ToolName, const Module *M)
112 : AID(AnnotationManager::getID("TargetData::" + ToolName)) {
113 AnnotationManager::registerAnnotationFactory(AID, TypeAnFactory, this);
115 LittleEndian = M->isLittleEndian();
116 PointerSize = M->has32BitPointers() ? 4 : 8;
117 PointerAlignment = PointerSize;
118 DoubleAlignment = PointerSize;
126 TargetData::~TargetData() {
127 AnnotationManager::registerAnnotationFactory(AID, 0); // Deregister factory
130 static inline void getTypeInfo(const Type *Ty, const TargetData *TD,
131 uint64_t &Size, unsigned char &Alignment) {
132 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
133 switch (Ty->getPrimitiveID()) {
136 case Type::UByteTyID:
137 case Type::SByteTyID: Size = 1; Alignment = TD->getByteAlignment(); return;
138 case Type::UShortTyID:
139 case Type::ShortTyID: Size = 2; Alignment = TD->getShortAlignment(); return;
141 case Type::IntTyID: Size = 4; Alignment = TD->getIntAlignment(); return;
142 case Type::ULongTyID:
143 case Type::LongTyID: Size = 8; Alignment = TD->getLongAlignment(); return;
144 case Type::FloatTyID: Size = 4; Alignment = TD->getFloatAlignment(); return;
145 case Type::DoubleTyID: Size = 8; Alignment = TD->getDoubleAlignment(); return;
146 case Type::LabelTyID:
147 case Type::PointerTyID:
148 Size = TD->getPointerSize(); Alignment = TD->getPointerAlignment();
150 case Type::ArrayTyID: {
151 const ArrayType *ATy = (const ArrayType *)Ty;
152 getTypeInfo(ATy->getElementType(), TD, Size, Alignment);
153 Size *= ATy->getNumElements();
156 case Type::StructTyID: {
157 // Get the layout annotation... which is lazily created on demand.
158 const StructLayout *Layout = TD->getStructLayout((const StructType*)Ty);
159 Size = Layout->StructSize; Alignment = Layout->StructAlignment;
165 assert(0 && "Bad type for getTypeInfo!!!");
170 uint64_t TargetData::getTypeSize(const Type *Ty) const {
173 getTypeInfo(Ty, this, Size, Align);
177 unsigned char TargetData::getTypeAlignment(const Type *Ty) const {
180 getTypeInfo(Ty, this, Size, Align);
184 uint64_t TargetData::getIndexedOffset(const Type *ptrTy,
185 const std::vector<Value*> &Idx) const {
186 const Type *Ty = ptrTy;
187 assert(isa<PointerType>(Ty) && "Illegal argument for getIndexedOffset()");
190 for (unsigned CurIDX = 0; CurIDX != Idx.size(); ++CurIDX) {
191 if (Idx[CurIDX]->getType() == Type::LongTy) {
192 // Update Ty to refer to current element
193 Ty = cast<SequentialType>(Ty)->getElementType();
195 // Get the array index and the size of each array element.
196 int64_t arrayIdx = cast<ConstantSInt>(Idx[CurIDX])->getValue();
197 Result += arrayIdx * (int64_t)getTypeSize(Ty);
199 const StructType *STy = cast<StructType>(Ty);
200 assert(Idx[CurIDX]->getType() == Type::UByteTy && "Illegal struct idx");
201 unsigned FieldNo = cast<ConstantUInt>(Idx[CurIDX])->getValue();
203 // Get structure layout information...
204 const StructLayout *Layout = getStructLayout(STy);
206 // Add in the offset, as calculated by the structure layout info...
207 assert(FieldNo < Layout->MemberOffsets.size() &&"FieldNo out of range!");
208 Result += Layout->MemberOffsets[FieldNo];
210 // Update Ty to refer to current element
211 Ty = STy->getElementTypes()[FieldNo];