Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 1 | //===-- TargetData.cpp - Data size & alignment routines --------------------==// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file defines target properties related to datatype size/offset/alignment |
| 11 | // information. It uses lazy annotations to cache information about how |
| 12 | // structure types are laid out and used. |
| 13 | // |
| 14 | // This structure should be created once, filled in if the defaults are not |
| 15 | // correct and then passed around by const&. None of the members functions |
| 16 | // require modification to the object. |
| 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
Vikram S. Adve | 0799fc4 | 2001-09-18 12:58:33 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 53a0c38 | 2003-04-24 19:09:05 +0000 | [diff] [blame] | 21 | #include "llvm/Module.h" |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 22 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 31bcdb8 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 23 | #include "llvm/Constants.h" |
Chris Lattner | f045328 | 2003-12-22 05:01:15 +0000 | [diff] [blame^] | 24 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 25 | |
Misha Brukman | 5560c9d | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 26 | // Handle the Pass registration stuff necessary to use TargetData's. |
Chris Lattner | aa31ad0 | 2002-09-25 23:46:55 +0000 | [diff] [blame] | 27 | namespace { |
| 28 | // Register the default SparcV9 implementation... |
| 29 | RegisterPass<TargetData> X("targetdata", "Target Data Layout"); |
| 30 | } |
| 31 | |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 32 | static inline void getTypeInfo(const Type *Ty, const TargetData *TD, |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 33 | uint64_t &Size, unsigned char &Alignment); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 34 | |
| 35 | //===----------------------------------------------------------------------===// |
| 36 | // Support for StructLayout Annotation |
| 37 | //===----------------------------------------------------------------------===// |
| 38 | |
| 39 | StructLayout::StructLayout(const StructType *ST, const TargetData &TD) |
| 40 | : Annotation(TD.getStructLayoutAID()) { |
| 41 | StructAlignment = 0; |
| 42 | StructSize = 0; |
| 43 | |
| 44 | // Loop over each of the elements, placing them in memory... |
| 45 | for (StructType::ElementTypes::const_iterator |
| 46 | TI = ST->getElementTypes().begin(), |
| 47 | TE = ST->getElementTypes().end(); TI != TE; ++TI) { |
| 48 | const Type *Ty = *TI; |
| 49 | unsigned char A; |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 50 | unsigned TyAlign; |
| 51 | uint64_t TySize; |
| 52 | getTypeInfo(Ty, &TD, TySize, A); |
| 53 | TyAlign = A; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 54 | |
Misha Brukman | 5560c9d | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 55 | // Add padding if necessary to make the data element aligned properly... |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 56 | if (StructSize % TyAlign != 0) |
| 57 | StructSize = (StructSize/TyAlign + 1) * TyAlign; // Add padding... |
| 58 | |
| 59 | // Keep track of maximum alignment constraint |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 60 | StructAlignment = std::max(TyAlign, StructAlignment); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 61 | |
| 62 | MemberOffsets.push_back(StructSize); |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 63 | StructSize += TySize; // Consume space for this data item |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Chris Lattner | 4e840d4 | 2003-05-21 18:08:44 +0000 | [diff] [blame] | 66 | // Empty structures have alignment of 1 byte. |
| 67 | if (StructAlignment == 0) StructAlignment = 1; |
| 68 | |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 69 | // Add padding to the end of the struct so that it could be put in an array |
| 70 | // and all array elements would be aligned correctly. |
| 71 | if (StructSize % StructAlignment != 0) |
| 72 | StructSize = (StructSize/StructAlignment + 1) * StructAlignment; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Chris Lattner | 97b7311 | 2001-09-07 16:40:04 +0000 | [diff] [blame] | 75 | Annotation *TargetData::TypeAnFactory(AnnotationID AID, const Annotable *T, |
| 76 | void *D) { |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 77 | const TargetData &TD = *(const TargetData*)D; |
| 78 | assert(AID == TD.AID && "Target data annotation ID mismatch!"); |
Chris Lattner | 949a362 | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 79 | const Type *Ty = cast<Type>((const Value *)T); |
Chris Lattner | 9b62503 | 2002-05-06 16:15:30 +0000 | [diff] [blame] | 80 | assert(isa<StructType>(Ty) && |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 81 | "Can only create StructLayout annotation on structs!"); |
Chris Lattner | 949a362 | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 82 | return new StructLayout(cast<StructType>(Ty), TD); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | //===----------------------------------------------------------------------===// |
| 86 | // TargetData Class Implementation |
| 87 | //===----------------------------------------------------------------------===// |
| 88 | |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 89 | TargetData::TargetData(const std::string &TargetName, |
Chris Lattner | 10daaa1 | 2003-04-26 20:11:09 +0000 | [diff] [blame] | 90 | bool isLittleEndian, unsigned char PtrSize, |
Chris Lattner | 85131c8 | 2002-10-14 22:41:13 +0000 | [diff] [blame] | 91 | unsigned char PtrAl, unsigned char DoubleAl, |
| 92 | unsigned char FloatAl, unsigned char LongAl, |
| 93 | unsigned char IntAl, unsigned char ShortAl, |
| 94 | unsigned char ByteAl) |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 95 | : AID(AnnotationManager::getID("TargetData::" + TargetName)) { |
| 96 | AnnotationManager::registerAnnotationFactory(AID, TypeAnFactory, this); |
| 97 | |
Chris Lattner | 46326d9 | 2003-04-25 02:50:45 +0000 | [diff] [blame] | 98 | // If this assert triggers, a pass "required" TargetData information, but the |
| 99 | // top level tool did not provide once for it. We do not want to default |
| 100 | // construct, or else we might end up using a bad endianness or pointer size! |
| 101 | // |
| 102 | assert(!TargetName.empty() && |
| 103 | "ERROR: Tool did not specify a target data to use!"); |
| 104 | |
Chris Lattner | 85131c8 | 2002-10-14 22:41:13 +0000 | [diff] [blame] | 105 | LittleEndian = isLittleEndian; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 106 | PointerSize = PtrSize; |
| 107 | PointerAlignment = PtrAl; |
| 108 | DoubleAlignment = DoubleAl; |
Chris Lattner | dd7253c | 2003-04-25 06:06:43 +0000 | [diff] [blame] | 109 | assert(DoubleAlignment == PtrAl && |
| 110 | "Double alignment and pointer alignment agree for now!"); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 111 | FloatAlignment = FloatAl; |
| 112 | LongAlignment = LongAl; |
| 113 | IntAlignment = IntAl; |
| 114 | ShortAlignment = ShortAl; |
| 115 | ByteAlignment = ByteAl; |
| 116 | } |
| 117 | |
Chris Lattner | 53a0c38 | 2003-04-24 19:09:05 +0000 | [diff] [blame] | 118 | TargetData::TargetData(const std::string &ToolName, const Module *M) |
| 119 | : AID(AnnotationManager::getID("TargetData::" + ToolName)) { |
| 120 | AnnotationManager::registerAnnotationFactory(AID, TypeAnFactory, this); |
| 121 | |
Chris Lattner | 030574f | 2003-08-24 13:49:22 +0000 | [diff] [blame] | 122 | LittleEndian = M->getEndianness() != Module::BigEndian; |
| 123 | PointerSize = M->getPointerSize() != Module::Pointer64 ? 4 : 8; |
Chris Lattner | 53a0c38 | 2003-04-24 19:09:05 +0000 | [diff] [blame] | 124 | PointerAlignment = PointerSize; |
Chris Lattner | dd7253c | 2003-04-25 06:06:43 +0000 | [diff] [blame] | 125 | DoubleAlignment = PointerSize; |
Chris Lattner | 53a0c38 | 2003-04-24 19:09:05 +0000 | [diff] [blame] | 126 | FloatAlignment = 4; |
| 127 | LongAlignment = 8; |
| 128 | IntAlignment = 4; |
| 129 | ShortAlignment = 2; |
| 130 | ByteAlignment = 1; |
| 131 | } |
| 132 | |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 133 | TargetData::~TargetData() { |
| 134 | AnnotationManager::registerAnnotationFactory(AID, 0); // Deregister factory |
| 135 | } |
| 136 | |
| 137 | static inline void getTypeInfo(const Type *Ty, const TargetData *TD, |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 138 | uint64_t &Size, unsigned char &Alignment) { |
Chris Lattner | f59ce92 | 2001-12-13 00:46:11 +0000 | [diff] [blame] | 139 | assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!"); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 140 | switch (Ty->getPrimitiveID()) { |
| 141 | case Type::VoidTyID: |
| 142 | case Type::BoolTyID: |
| 143 | case Type::UByteTyID: |
| 144 | case Type::SByteTyID: Size = 1; Alignment = TD->getByteAlignment(); return; |
| 145 | case Type::UShortTyID: |
| 146 | case Type::ShortTyID: Size = 2; Alignment = TD->getShortAlignment(); return; |
| 147 | case Type::UIntTyID: |
| 148 | case Type::IntTyID: Size = 4; Alignment = TD->getIntAlignment(); return; |
| 149 | case Type::ULongTyID: |
| 150 | case Type::LongTyID: Size = 8; Alignment = TD->getLongAlignment(); return; |
| 151 | case Type::FloatTyID: Size = 4; Alignment = TD->getFloatAlignment(); return; |
| 152 | case Type::DoubleTyID: Size = 8; Alignment = TD->getDoubleAlignment(); return; |
| 153 | case Type::LabelTyID: |
| 154 | case Type::PointerTyID: |
| 155 | Size = TD->getPointerSize(); Alignment = TD->getPointerAlignment(); |
| 156 | return; |
| 157 | case Type::ArrayTyID: { |
| 158 | const ArrayType *ATy = (const ArrayType *)Ty; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 159 | getTypeInfo(ATy->getElementType(), TD, Size, Alignment); |
| 160 | Size *= ATy->getNumElements(); |
| 161 | return; |
| 162 | } |
| 163 | case Type::StructTyID: { |
| 164 | // Get the layout annotation... which is lazily created on demand. |
| 165 | const StructLayout *Layout = TD->getStructLayout((const StructType*)Ty); |
| 166 | Size = Layout->StructSize; Alignment = Layout->StructAlignment; |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | case Type::TypeTyID: |
| 171 | default: |
| 172 | assert(0 && "Bad type for getTypeInfo!!!"); |
| 173 | return; |
| 174 | } |
| 175 | } |
| 176 | |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 177 | uint64_t TargetData::getTypeSize(const Type *Ty) const { |
| 178 | uint64_t Size; |
| 179 | unsigned char Align; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 180 | getTypeInfo(Ty, this, Size, Align); |
| 181 | return Size; |
| 182 | } |
| 183 | |
| 184 | unsigned char TargetData::getTypeAlignment(const Type *Ty) const { |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 185 | uint64_t Size; |
| 186 | unsigned char Align; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 187 | getTypeInfo(Ty, this, Size, Align); |
| 188 | return Align; |
| 189 | } |
| 190 | |
Chris Lattner | f045328 | 2003-12-22 05:01:15 +0000 | [diff] [blame^] | 191 | /// getIntPtrType - Return an unsigned integer type that is the same size or |
| 192 | /// greater to the host pointer size. |
| 193 | const Type *TargetData::getIntPtrType() const { |
| 194 | switch (getPointerSize()) { |
| 195 | default: assert(0 && "Unknown pointer size!"); |
| 196 | case 2: return Type::UShortTy; |
| 197 | case 4: return Type::UIntTy; |
| 198 | case 8: return Type::ULongTy; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 203 | uint64_t TargetData::getIndexedOffset(const Type *ptrTy, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 204 | const std::vector<Value*> &Idx) const { |
Vikram S. Adve | ed0030e | 2002-08-04 20:52:39 +0000 | [diff] [blame] | 205 | const Type *Ty = ptrTy; |
| 206 | assert(isa<PointerType>(Ty) && "Illegal argument for getIndexedOffset()"); |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 207 | uint64_t Result = 0; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 208 | |
Chris Lattner | 3cac88a | 2002-09-11 01:21:33 +0000 | [diff] [blame] | 209 | for (unsigned CurIDX = 0; CurIDX != Idx.size(); ++CurIDX) { |
| 210 | if (Idx[CurIDX]->getType() == Type::LongTy) { |
Vikram S. Adve | ca710e9 | 2002-08-13 18:17:56 +0000 | [diff] [blame] | 211 | // Update Ty to refer to current element |
| 212 | Ty = cast<SequentialType>(Ty)->getElementType(); |
| 213 | |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 214 | // Get the array index and the size of each array element. |
Chris Lattner | 3cac88a | 2002-09-11 01:21:33 +0000 | [diff] [blame] | 215 | int64_t arrayIdx = cast<ConstantSInt>(Idx[CurIDX])->getValue(); |
Chris Lattner | 94a5118 | 2003-06-04 02:35:35 +0000 | [diff] [blame] | 216 | Result += arrayIdx * (int64_t)getTypeSize(Ty); |
Chris Lattner | 4954f04 | 2003-06-02 05:21:06 +0000 | [diff] [blame] | 217 | } else { |
| 218 | const StructType *STy = cast<StructType>(Ty); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 219 | assert(Idx[CurIDX]->getType() == Type::UByteTy && "Illegal struct idx"); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 220 | unsigned FieldNo = cast<ConstantUInt>(Idx[CurIDX])->getValue(); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 221 | |
| 222 | // Get structure layout information... |
| 223 | const StructLayout *Layout = getStructLayout(STy); |
| 224 | |
| 225 | // Add in the offset, as calculated by the structure layout info... |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 226 | assert(FieldNo < Layout->MemberOffsets.size() &&"FieldNo out of range!"); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 227 | Result += Layout->MemberOffsets[FieldNo]; |
Vikram S. Adve | ed0030e | 2002-08-04 20:52:39 +0000 | [diff] [blame] | 228 | |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 229 | // Update Ty to refer to current element |
| 230 | Ty = STy->getElementTypes()[FieldNo]; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
| 234 | return Result; |
| 235 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 236 | |