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 |
Chris Lattner | 0e7ac16 | 2004-02-26 08:02:17 +0000 | [diff] [blame] | 11 | // information. |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 12 | // |
| 13 | // This structure should be created once, filled in if the defaults are not |
| 14 | // correct and then passed around by const&. None of the members functions |
| 15 | // require modification to the object. |
| 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Vikram S. Adve | 0799fc4 | 2001-09-18 12:58:33 +0000 | [diff] [blame] | 19 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 53a0c38 | 2003-04-24 19:09:05 +0000 | [diff] [blame] | 20 | #include "llvm/Module.h" |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 21 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 31bcdb8 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 22 | #include "llvm/Constants.h" |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 23 | #include "llvm/Support/GetElementPtrTypeIterator.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 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0e7ac16 | 2004-02-26 08:02:17 +0000 | [diff] [blame] | 36 | // Support for StructLayout |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 37 | //===----------------------------------------------------------------------===// |
| 38 | |
Chris Lattner | 0e7ac16 | 2004-02-26 08:02:17 +0000 | [diff] [blame] | 39 | StructLayout::StructLayout(const StructType *ST, const TargetData &TD) { |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 40 | StructAlignment = 0; |
| 41 | StructSize = 0; |
| 42 | |
| 43 | // Loop over each of the elements, placing them in memory... |
Chris Lattner | d21cd80 | 2004-02-09 04:37:31 +0000 | [diff] [blame] | 44 | for (StructType::element_iterator TI = ST->element_begin(), |
| 45 | TE = ST->element_end(); TI != TE; ++TI) { |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 46 | const Type *Ty = *TI; |
| 47 | unsigned char A; |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 48 | unsigned TyAlign; |
| 49 | uint64_t TySize; |
| 50 | getTypeInfo(Ty, &TD, TySize, A); |
| 51 | TyAlign = A; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 52 | |
Misha Brukman | 5560c9d | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 53 | // Add padding if necessary to make the data element aligned properly... |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 54 | if (StructSize % TyAlign != 0) |
| 55 | StructSize = (StructSize/TyAlign + 1) * TyAlign; // Add padding... |
| 56 | |
| 57 | // Keep track of maximum alignment constraint |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 58 | StructAlignment = std::max(TyAlign, StructAlignment); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 59 | |
| 60 | MemberOffsets.push_back(StructSize); |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 61 | StructSize += TySize; // Consume space for this data item |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Chris Lattner | 4e840d4 | 2003-05-21 18:08:44 +0000 | [diff] [blame] | 64 | // Empty structures have alignment of 1 byte. |
| 65 | if (StructAlignment == 0) StructAlignment = 1; |
| 66 | |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 67 | // Add padding to the end of the struct so that it could be put in an array |
| 68 | // and all array elements would be aligned correctly. |
| 69 | if (StructSize % StructAlignment != 0) |
| 70 | StructSize = (StructSize/StructAlignment + 1) * StructAlignment; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 73 | //===----------------------------------------------------------------------===// |
| 74 | // TargetData Class Implementation |
| 75 | //===----------------------------------------------------------------------===// |
| 76 | |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 77 | TargetData::TargetData(const std::string &TargetName, |
Chris Lattner | 10daaa1 | 2003-04-26 20:11:09 +0000 | [diff] [blame] | 78 | bool isLittleEndian, unsigned char PtrSize, |
Chris Lattner | 85131c8 | 2002-10-14 22:41:13 +0000 | [diff] [blame] | 79 | unsigned char PtrAl, unsigned char DoubleAl, |
| 80 | unsigned char FloatAl, unsigned char LongAl, |
| 81 | unsigned char IntAl, unsigned char ShortAl, |
Chris Lattner | 0e7ac16 | 2004-02-26 08:02:17 +0000 | [diff] [blame] | 82 | unsigned char ByteAl) { |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 83 | |
Chris Lattner | 46326d9 | 2003-04-25 02:50:45 +0000 | [diff] [blame] | 84 | // If this assert triggers, a pass "required" TargetData information, but the |
Brian Gaeke | 8121fcd | 2004-04-14 21:21:56 +0000 | [diff] [blame] | 85 | // top level tool did not provide one for it. We do not want to default |
Chris Lattner | 46326d9 | 2003-04-25 02:50:45 +0000 | [diff] [blame] | 86 | // construct, or else we might end up using a bad endianness or pointer size! |
| 87 | // |
| 88 | assert(!TargetName.empty() && |
| 89 | "ERROR: Tool did not specify a target data to use!"); |
| 90 | |
Chris Lattner | 85131c8 | 2002-10-14 22:41:13 +0000 | [diff] [blame] | 91 | LittleEndian = isLittleEndian; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 92 | PointerSize = PtrSize; |
| 93 | PointerAlignment = PtrAl; |
| 94 | DoubleAlignment = DoubleAl; |
Chris Lattner | dd7253c | 2003-04-25 06:06:43 +0000 | [diff] [blame] | 95 | assert(DoubleAlignment == PtrAl && |
| 96 | "Double alignment and pointer alignment agree for now!"); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 97 | FloatAlignment = FloatAl; |
| 98 | LongAlignment = LongAl; |
| 99 | IntAlignment = IntAl; |
| 100 | ShortAlignment = ShortAl; |
| 101 | ByteAlignment = ByteAl; |
| 102 | } |
| 103 | |
Chris Lattner | 0e7ac16 | 2004-02-26 08:02:17 +0000 | [diff] [blame] | 104 | TargetData::TargetData(const std::string &ToolName, const Module *M) { |
Chris Lattner | 030574f | 2003-08-24 13:49:22 +0000 | [diff] [blame] | 105 | LittleEndian = M->getEndianness() != Module::BigEndian; |
| 106 | PointerSize = M->getPointerSize() != Module::Pointer64 ? 4 : 8; |
Chris Lattner | 53a0c38 | 2003-04-24 19:09:05 +0000 | [diff] [blame] | 107 | PointerAlignment = PointerSize; |
Chris Lattner | dd7253c | 2003-04-25 06:06:43 +0000 | [diff] [blame] | 108 | DoubleAlignment = PointerSize; |
Chris Lattner | 53a0c38 | 2003-04-24 19:09:05 +0000 | [diff] [blame] | 109 | FloatAlignment = 4; |
| 110 | LongAlignment = 8; |
| 111 | IntAlignment = 4; |
| 112 | ShortAlignment = 2; |
| 113 | ByteAlignment = 1; |
| 114 | } |
| 115 | |
Chris Lattner | 0e7ac16 | 2004-02-26 08:02:17 +0000 | [diff] [blame] | 116 | static std::map<std::pair<const TargetData*,const StructType*>, |
| 117 | StructLayout> *Layouts = 0; |
| 118 | |
| 119 | |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 120 | TargetData::~TargetData() { |
Chris Lattner | 0e7ac16 | 2004-02-26 08:02:17 +0000 | [diff] [blame] | 121 | if (Layouts) { |
| 122 | // Remove any layouts for this TD. |
| 123 | std::map<std::pair<const TargetData*, |
| 124 | const StructType*>, StructLayout>::iterator |
| 125 | I = Layouts->lower_bound(std::make_pair(this, (const StructType*)0)); |
| 126 | while (I != Layouts->end() && I->first.first == this) |
| 127 | Layouts->erase(I++); |
| 128 | if (Layouts->empty()) { |
| 129 | delete Layouts; |
| 130 | Layouts = 0; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | const StructLayout *TargetData::getStructLayout(const StructType *Ty) const { |
| 136 | if (Layouts == 0) |
| 137 | Layouts = new std::map<std::pair<const TargetData*,const StructType*>, |
| 138 | StructLayout>(); |
| 139 | std::map<std::pair<const TargetData*,const StructType*>, |
| 140 | StructLayout>::iterator |
| 141 | I = Layouts->lower_bound(std::make_pair(this, Ty)); |
| 142 | if (I != Layouts->end() && I->first.first == this && I->first.second == Ty) |
| 143 | return &I->second; |
| 144 | else { |
| 145 | return &Layouts->insert(I, std::make_pair(std::make_pair(this, Ty), |
| 146 | StructLayout(Ty, *this)))->second; |
| 147 | } |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | static inline void getTypeInfo(const Type *Ty, const TargetData *TD, |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 151 | uint64_t &Size, unsigned char &Alignment) { |
Chris Lattner | f59ce92 | 2001-12-13 00:46:11 +0000 | [diff] [blame] | 152 | assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!"); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 153 | switch (Ty->getPrimitiveID()) { |
| 154 | case Type::VoidTyID: |
| 155 | case Type::BoolTyID: |
| 156 | case Type::UByteTyID: |
| 157 | case Type::SByteTyID: Size = 1; Alignment = TD->getByteAlignment(); return; |
| 158 | case Type::UShortTyID: |
| 159 | case Type::ShortTyID: Size = 2; Alignment = TD->getShortAlignment(); return; |
| 160 | case Type::UIntTyID: |
| 161 | case Type::IntTyID: Size = 4; Alignment = TD->getIntAlignment(); return; |
| 162 | case Type::ULongTyID: |
| 163 | case Type::LongTyID: Size = 8; Alignment = TD->getLongAlignment(); return; |
| 164 | case Type::FloatTyID: Size = 4; Alignment = TD->getFloatAlignment(); return; |
| 165 | case Type::DoubleTyID: Size = 8; Alignment = TD->getDoubleAlignment(); return; |
| 166 | case Type::LabelTyID: |
| 167 | case Type::PointerTyID: |
| 168 | Size = TD->getPointerSize(); Alignment = TD->getPointerAlignment(); |
| 169 | return; |
| 170 | case Type::ArrayTyID: { |
| 171 | const ArrayType *ATy = (const ArrayType *)Ty; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 172 | getTypeInfo(ATy->getElementType(), TD, Size, Alignment); |
| 173 | Size *= ATy->getNumElements(); |
| 174 | return; |
| 175 | } |
| 176 | case Type::StructTyID: { |
| 177 | // Get the layout annotation... which is lazily created on demand. |
| 178 | const StructLayout *Layout = TD->getStructLayout((const StructType*)Ty); |
| 179 | Size = Layout->StructSize; Alignment = Layout->StructAlignment; |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | case Type::TypeTyID: |
| 184 | default: |
| 185 | assert(0 && "Bad type for getTypeInfo!!!"); |
| 186 | return; |
| 187 | } |
| 188 | } |
| 189 | |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 190 | uint64_t TargetData::getTypeSize(const Type *Ty) const { |
| 191 | uint64_t Size; |
| 192 | unsigned char Align; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 193 | getTypeInfo(Ty, this, Size, Align); |
| 194 | return Size; |
| 195 | } |
| 196 | |
| 197 | unsigned char TargetData::getTypeAlignment(const Type *Ty) const { |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 198 | uint64_t Size; |
| 199 | unsigned char Align; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 200 | getTypeInfo(Ty, this, Size, Align); |
| 201 | return Align; |
| 202 | } |
| 203 | |
Chris Lattner | f045328 | 2003-12-22 05:01:15 +0000 | [diff] [blame] | 204 | /// getIntPtrType - Return an unsigned integer type that is the same size or |
| 205 | /// greater to the host pointer size. |
| 206 | const Type *TargetData::getIntPtrType() const { |
| 207 | switch (getPointerSize()) { |
| 208 | default: assert(0 && "Unknown pointer size!"); |
| 209 | case 2: return Type::UShortTy; |
| 210 | case 4: return Type::UIntTy; |
| 211 | case 8: return Type::ULongTy; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 216 | uint64_t TargetData::getIndexedOffset(const Type *ptrTy, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 217 | const std::vector<Value*> &Idx) const { |
Vikram S. Adve | ed0030e | 2002-08-04 20:52:39 +0000 | [diff] [blame] | 218 | const Type *Ty = ptrTy; |
| 219 | assert(isa<PointerType>(Ty) && "Illegal argument for getIndexedOffset()"); |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 220 | uint64_t Result = 0; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 221 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 222 | generic_gep_type_iterator<std::vector<Value*>::const_iterator> |
| 223 | TI = gep_type_begin(ptrTy, Idx.begin(), Idx.end()); |
| 224 | for (unsigned CurIDX = 0; CurIDX != Idx.size(); ++CurIDX, ++TI) { |
| 225 | if (const StructType *STy = dyn_cast<StructType>(*TI)) { |
| 226 | assert(Idx[CurIDX]->getType() == Type::UIntTy && "Illegal struct idx"); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 227 | unsigned FieldNo = cast<ConstantUInt>(Idx[CurIDX])->getValue(); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 228 | |
| 229 | // Get structure layout information... |
| 230 | const StructLayout *Layout = getStructLayout(STy); |
| 231 | |
| 232 | // Add in the offset, as calculated by the structure layout info... |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 233 | assert(FieldNo < Layout->MemberOffsets.size() &&"FieldNo out of range!"); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 234 | Result += Layout->MemberOffsets[FieldNo]; |
Vikram S. Adve | ed0030e | 2002-08-04 20:52:39 +0000 | [diff] [blame] | 235 | |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 236 | // Update Ty to refer to current element |
Chris Lattner | d21cd80 | 2004-02-09 04:37:31 +0000 | [diff] [blame] | 237 | Ty = STy->getElementType(FieldNo); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 238 | } else { |
| 239 | // Update Ty to refer to current element |
| 240 | Ty = cast<SequentialType>(Ty)->getElementType(); |
| 241 | |
| 242 | // Get the array index and the size of each array element. |
| 243 | int64_t arrayIdx = cast<ConstantInt>(Idx[CurIDX])->getRawValue(); |
| 244 | Result += arrayIdx * (int64_t)getTypeSize(Ty); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
| 248 | return Result; |
| 249 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 250 | |