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