Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 1 | //===-- TargetData.cpp - Data size & alignment routines --------------------==// |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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. |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 24 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | ec6478b | 2007-02-10 19:43:18 +0000 | [diff] [blame] | 25 | #include "llvm/Support/ManagedStatic.h" |
Chris Lattner | f6ca09a | 2007-02-10 20:26:17 +0000 | [diff] [blame^] | 26 | #include "llvm/ADT/DenseMap.h" |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | e7ea48c | 2005-03-13 19:04:41 +0000 | [diff] [blame] | 28 | #include <algorithm> |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 29 | #include <cstdlib> |
Owen Anderson | 2577c22 | 2006-05-12 07:01:44 +0000 | [diff] [blame] | 30 | #include <sstream> |
Chris Lattner | f045328 | 2003-12-22 05:01:15 +0000 | [diff] [blame] | 31 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 32 | |
Misha Brukman | 5560c9d | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 33 | // Handle the Pass registration stuff necessary to use TargetData's. |
Chris Lattner | aa31ad0 | 2002-09-25 23:46:55 +0000 | [diff] [blame] | 34 | namespace { |
| 35 | // Register the default SparcV9 implementation... |
| 36 | RegisterPass<TargetData> X("targetdata", "Target Data Layout"); |
| 37 | } |
| 38 | |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 39 | static inline void getTypeInfoABI(const Type *Ty, const TargetData *TD, |
| 40 | uint64_t &Size, unsigned char &Alignment); |
| 41 | |
| 42 | static inline void getTypeInfoPref(const Type *Ty, const TargetData *TD, |
| 43 | uint64_t &Size, unsigned char &Alignment); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 44 | |
| 45 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0e7ac16 | 2004-02-26 08:02:17 +0000 | [diff] [blame] | 46 | // Support for StructLayout |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 47 | //===----------------------------------------------------------------------===// |
| 48 | |
Chris Lattner | 0e7ac16 | 2004-02-26 08:02:17 +0000 | [diff] [blame] | 49 | StructLayout::StructLayout(const StructType *ST, const TargetData &TD) { |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 50 | StructAlignment = 0; |
| 51 | StructSize = 0; |
Chris Lattner | 9182e3f | 2007-02-10 20:15:41 +0000 | [diff] [blame] | 52 | NumElements = ST->getNumElements(); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 53 | |
| 54 | // Loop over each of the elements, placing them in memory... |
Chris Lattner | 9182e3f | 2007-02-10 20:15:41 +0000 | [diff] [blame] | 55 | for (unsigned i = 0, e = NumElements; i != e; ++i) { |
| 56 | const Type *Ty = ST->getElementType(i); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 57 | unsigned char A; |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 58 | unsigned TyAlign; |
| 59 | uint64_t TySize; |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 60 | getTypeInfoABI(Ty, &TD, TySize, A); |
Andrew Lenharth | 38ecbf1 | 2006-12-08 18:06:16 +0000 | [diff] [blame] | 61 | TyAlign = ST->isPacked() ? 1 : A; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 62 | |
Misha Brukman | 5560c9d | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 63 | // Add padding if necessary to make the data element aligned properly... |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 64 | if (StructSize % TyAlign != 0) |
| 65 | StructSize = (StructSize/TyAlign + 1) * TyAlign; // Add padding... |
| 66 | |
| 67 | // Keep track of maximum alignment constraint |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 68 | StructAlignment = std::max(TyAlign, StructAlignment); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 69 | |
Chris Lattner | 9182e3f | 2007-02-10 20:15:41 +0000 | [diff] [blame] | 70 | MemberOffsets[i] = StructSize; |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 71 | StructSize += TySize; // Consume space for this data item |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Chris Lattner | 4e840d4 | 2003-05-21 18:08:44 +0000 | [diff] [blame] | 74 | // Empty structures have alignment of 1 byte. |
| 75 | if (StructAlignment == 0) StructAlignment = 1; |
| 76 | |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 77 | // Add padding to the end of the struct so that it could be put in an array |
| 78 | // and all array elements would be aligned correctly. |
| 79 | if (StructSize % StructAlignment != 0) |
| 80 | StructSize = (StructSize/StructAlignment + 1) * StructAlignment; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Chris Lattner | e7ea48c | 2005-03-13 19:04:41 +0000 | [diff] [blame] | 83 | |
| 84 | /// getElementContainingOffset - Given a valid offset into the structure, |
| 85 | /// return the structure index that contains it. |
| 86 | unsigned StructLayout::getElementContainingOffset(uint64_t Offset) const { |
Chris Lattner | 9182e3f | 2007-02-10 20:15:41 +0000 | [diff] [blame] | 87 | const uint64_t *SI = |
| 88 | std::upper_bound(&MemberOffsets[0], &MemberOffsets[NumElements], Offset); |
| 89 | assert(SI != &MemberOffsets[0] && "Offset not in structure type!"); |
Chris Lattner | e7ea48c | 2005-03-13 19:04:41 +0000 | [diff] [blame] | 90 | --SI; |
| 91 | assert(*SI <= Offset && "upper_bound didn't work"); |
Chris Lattner | 9182e3f | 2007-02-10 20:15:41 +0000 | [diff] [blame] | 92 | assert((SI == &MemberOffsets[0] || *(SI-1) < Offset) && |
| 93 | (SI+1 == &MemberOffsets[NumElements] || *(SI+1) > Offset) && |
Chris Lattner | e7ea48c | 2005-03-13 19:04:41 +0000 | [diff] [blame] | 94 | "Upper bound didn't work!"); |
Chris Lattner | 9182e3f | 2007-02-10 20:15:41 +0000 | [diff] [blame] | 95 | return SI-&MemberOffsets[0]; |
Chris Lattner | e7ea48c | 2005-03-13 19:04:41 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 98 | //===----------------------------------------------------------------------===// |
| 99 | // TargetData Class Implementation |
| 100 | //===----------------------------------------------------------------------===// |
| 101 | |
Chris Lattner | acbc07a | 2006-06-16 18:11:26 +0000 | [diff] [blame] | 102 | void TargetData::init(const std::string &TargetDescription) { |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 103 | std::string temp = TargetDescription; |
| 104 | |
| 105 | LittleEndian = false; |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 106 | PointerMemSize = 8; |
| 107 | PointerABIAlignment = 8; |
Owen Anderson | 1027a53 | 2007-01-20 23:07:13 +0000 | [diff] [blame] | 108 | DoubleABIAlignment = 0; |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 109 | FloatABIAlignment = 4; |
Owen Anderson | 1027a53 | 2007-01-20 23:07:13 +0000 | [diff] [blame] | 110 | LongABIAlignment = 0; |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 111 | IntABIAlignment = 4; |
| 112 | ShortABIAlignment = 2; |
| 113 | ByteABIAlignment = 1; |
| 114 | BoolABIAlignment = 1; |
| 115 | BoolPrefAlignment = BoolABIAlignment; |
| 116 | BytePrefAlignment = ByteABIAlignment; |
| 117 | ShortPrefAlignment = ShortABIAlignment; |
| 118 | IntPrefAlignment = IntABIAlignment; |
Owen Anderson | 1027a53 | 2007-01-20 23:07:13 +0000 | [diff] [blame] | 119 | LongPrefAlignment = 8; |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 120 | FloatPrefAlignment = FloatABIAlignment; |
Owen Anderson | 1027a53 | 2007-01-20 23:07:13 +0000 | [diff] [blame] | 121 | DoublePrefAlignment = 8; |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 122 | PointerPrefAlignment = PointerABIAlignment; |
| 123 | AggMinPrefAlignment = 0; |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 124 | |
Owen Anderson | d988b32 | 2006-05-20 00:24:56 +0000 | [diff] [blame] | 125 | while (!temp.empty()) { |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 126 | std::string token = getToken(temp, "-"); |
| 127 | |
Owen Anderson | 84cc6db | 2006-05-17 21:56:02 +0000 | [diff] [blame] | 128 | char signal = getToken(token, ":")[0]; |
| 129 | |
| 130 | switch(signal) { |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 131 | case 'E': |
Owen Anderson | 571a13f | 2006-05-12 06:06:55 +0000 | [diff] [blame] | 132 | LittleEndian = false; |
| 133 | break; |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 134 | case 'e': |
Owen Anderson | 571a13f | 2006-05-12 06:06:55 +0000 | [diff] [blame] | 135 | LittleEndian = true; |
| 136 | break; |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 137 | case 'p': |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 138 | PointerMemSize = atoi(getToken(token,":").c_str()) / 8; |
| 139 | PointerABIAlignment = atoi(getToken(token,":").c_str()) / 8; |
| 140 | PointerPrefAlignment = atoi(getToken(token,":").c_str()) / 8; |
| 141 | if (PointerPrefAlignment == 0) |
| 142 | PointerPrefAlignment = PointerABIAlignment; |
Owen Anderson | 571a13f | 2006-05-12 06:06:55 +0000 | [diff] [blame] | 143 | break; |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 144 | case 'd': |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 145 | DoubleABIAlignment = atoi(getToken(token,":").c_str()) / 8; |
| 146 | DoublePrefAlignment = atoi(getToken(token,":").c_str()) / 8; |
| 147 | if (DoublePrefAlignment == 0) |
| 148 | DoublePrefAlignment = DoubleABIAlignment; |
Owen Anderson | 571a13f | 2006-05-12 06:06:55 +0000 | [diff] [blame] | 149 | break; |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 150 | case 'f': |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 151 | FloatABIAlignment = atoi(getToken(token, ":").c_str()) / 8; |
| 152 | FloatPrefAlignment = atoi(getToken(token,":").c_str()) / 8; |
| 153 | if (FloatPrefAlignment == 0) |
| 154 | FloatPrefAlignment = FloatABIAlignment; |
Owen Anderson | 571a13f | 2006-05-12 06:06:55 +0000 | [diff] [blame] | 155 | break; |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 156 | case 'l': |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 157 | LongABIAlignment = atoi(getToken(token, ":").c_str()) / 8; |
| 158 | LongPrefAlignment = atoi(getToken(token,":").c_str()) / 8; |
| 159 | if (LongPrefAlignment == 0) |
| 160 | LongPrefAlignment = LongABIAlignment; |
Owen Anderson | 571a13f | 2006-05-12 06:06:55 +0000 | [diff] [blame] | 161 | break; |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 162 | case 'i': |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 163 | IntABIAlignment = atoi(getToken(token, ":").c_str()) / 8; |
| 164 | IntPrefAlignment = atoi(getToken(token,":").c_str()) / 8; |
| 165 | if (IntPrefAlignment == 0) |
| 166 | IntPrefAlignment = IntABIAlignment; |
Owen Anderson | 571a13f | 2006-05-12 06:06:55 +0000 | [diff] [blame] | 167 | break; |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 168 | case 's': |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 169 | ShortABIAlignment = atoi(getToken(token, ":").c_str()) / 8; |
| 170 | ShortPrefAlignment = atoi(getToken(token,":").c_str()) / 8; |
| 171 | if (ShortPrefAlignment == 0) |
| 172 | ShortPrefAlignment = ShortABIAlignment; |
Owen Anderson | 571a13f | 2006-05-12 06:06:55 +0000 | [diff] [blame] | 173 | break; |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 174 | case 'b': |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 175 | ByteABIAlignment = atoi(getToken(token, ":").c_str()) / 8; |
| 176 | BytePrefAlignment = atoi(getToken(token,":").c_str()) / 8; |
| 177 | if (BytePrefAlignment == 0) |
| 178 | BytePrefAlignment = ByteABIAlignment; |
Owen Anderson | 571a13f | 2006-05-12 06:06:55 +0000 | [diff] [blame] | 179 | break; |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 180 | case 'B': |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 181 | BoolABIAlignment = atoi(getToken(token, ":").c_str()) / 8; |
| 182 | BoolPrefAlignment = atoi(getToken(token,":").c_str()) / 8; |
| 183 | if (BoolPrefAlignment == 0) |
| 184 | BoolPrefAlignment = BoolABIAlignment; |
| 185 | break; |
| 186 | case 'A': |
| 187 | AggMinPrefAlignment = atoi(getToken(token,":").c_str()) / 8; |
Owen Anderson | 571a13f | 2006-05-12 06:06:55 +0000 | [diff] [blame] | 188 | break; |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 189 | default: |
Owen Anderson | 571a13f | 2006-05-12 06:06:55 +0000 | [diff] [blame] | 190 | break; |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 191 | } |
| 192 | } |
Owen Anderson | 1027a53 | 2007-01-20 23:07:13 +0000 | [diff] [blame] | 193 | |
Reid Spencer | 26f2385 | 2007-01-26 08:11:39 +0000 | [diff] [blame] | 194 | // Unless explicitly specified, the alignments for longs and doubles is |
| 195 | // capped by pointer size. |
Owen Anderson | 1027a53 | 2007-01-20 23:07:13 +0000 | [diff] [blame] | 196 | if (LongABIAlignment == 0) |
| 197 | LongABIAlignment = LongPrefAlignment = PointerMemSize; |
| 198 | if (DoubleABIAlignment == 0) |
| 199 | DoubleABIAlignment = DoublePrefAlignment = PointerMemSize; |
Owen Anderson | 8f60c56 | 2006-05-12 05:49:47 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Chris Lattner | 1790d44 | 2006-06-16 18:22:52 +0000 | [diff] [blame] | 202 | TargetData::TargetData(const Module *M) { |
Reid Spencer | 26f2385 | 2007-01-26 08:11:39 +0000 | [diff] [blame] | 203 | init(M->getDataLayout()); |
Chris Lattner | 53a0c38 | 2003-04-24 19:09:05 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Chris Lattner | ec6478b | 2007-02-10 19:43:18 +0000 | [diff] [blame] | 206 | /// LayoutInfo - The lazy cache of structure layout information maintained by |
Chris Lattner | 9182e3f | 2007-02-10 20:15:41 +0000 | [diff] [blame] | 207 | /// TargetData. Note that the struct types must have been free'd before |
| 208 | /// llvm_shutdown is called (and thus this is deallocated) because all the |
| 209 | /// targets with cached elements should have been destroyed. |
Chris Lattner | 8dff24f | 2006-01-14 00:07:34 +0000 | [diff] [blame] | 210 | /// |
Chris Lattner | ec6478b | 2007-02-10 19:43:18 +0000 | [diff] [blame] | 211 | typedef std::pair<const TargetData*,const StructType*> LayoutKey; |
Chris Lattner | f6ca09a | 2007-02-10 20:26:17 +0000 | [diff] [blame^] | 212 | |
| 213 | struct DenseMapLayoutKeyInfo { |
| 214 | static inline LayoutKey getEmptyKey() { return LayoutKey(0, 0); } |
| 215 | static inline LayoutKey getTombstoneKey() { |
| 216 | return LayoutKey((TargetData*)(intptr_t)-1, 0); |
| 217 | } |
| 218 | static unsigned getHashValue(const LayoutKey &Val) { |
| 219 | return DenseMapKeyInfo<void*>::getHashValue(Val.first) ^ |
| 220 | DenseMapKeyInfo<void*>::getHashValue(Val.second); |
| 221 | } |
| 222 | static bool isPod() { return true; } |
| 223 | }; |
| 224 | |
| 225 | typedef DenseMap<LayoutKey, StructLayout*, DenseMapLayoutKeyInfo> LayoutInfoTy; |
Chris Lattner | a12bd03 | 2007-02-10 20:18:06 +0000 | [diff] [blame] | 226 | static ManagedStatic<LayoutInfoTy> LayoutInfo; |
Chris Lattner | 0e7ac16 | 2004-02-26 08:02:17 +0000 | [diff] [blame] | 227 | |
| 228 | |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 229 | TargetData::~TargetData() { |
Chris Lattner | ec6478b | 2007-02-10 19:43:18 +0000 | [diff] [blame] | 230 | if (LayoutInfo.isConstructed()) { |
Chris Lattner | 0e7ac16 | 2004-02-26 08:02:17 +0000 | [diff] [blame] | 231 | // Remove any layouts for this TD. |
Chris Lattner | a12bd03 | 2007-02-10 20:18:06 +0000 | [diff] [blame] | 232 | LayoutInfoTy &TheMap = *LayoutInfo; |
Chris Lattner | f6ca09a | 2007-02-10 20:26:17 +0000 | [diff] [blame^] | 233 | for (LayoutInfoTy::iterator I = TheMap.begin(), E = TheMap.end(); |
| 234 | I != E; ) { |
| 235 | if (I->first.first == this) { |
| 236 | I->second->~StructLayout(); |
| 237 | free(I->second); |
| 238 | TheMap.erase(I++); |
| 239 | } else { |
| 240 | ++I; |
| 241 | } |
Chris Lattner | 9182e3f | 2007-02-10 20:15:41 +0000 | [diff] [blame] | 242 | } |
Chris Lattner | 0e7ac16 | 2004-02-26 08:02:17 +0000 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
Chris Lattner | 9182e3f | 2007-02-10 20:15:41 +0000 | [diff] [blame] | 246 | const StructLayout *TargetData::getStructLayout(const StructType *Ty) const { |
Chris Lattner | a12bd03 | 2007-02-10 20:18:06 +0000 | [diff] [blame] | 247 | LayoutInfoTy &TheMap = *LayoutInfo; |
Chris Lattner | 9182e3f | 2007-02-10 20:15:41 +0000 | [diff] [blame] | 248 | |
Chris Lattner | f6ca09a | 2007-02-10 20:26:17 +0000 | [diff] [blame^] | 249 | StructLayout *&SL = TheMap[LayoutKey(this, Ty)]; |
| 250 | if (SL) return SL; |
Chris Lattner | 9182e3f | 2007-02-10 20:15:41 +0000 | [diff] [blame] | 251 | |
| 252 | // Otherwise, create the struct layout. Because it is variable length, we |
| 253 | // malloc it, then use placement new. |
| 254 | unsigned NumElts = Ty->getNumElements(); |
| 255 | StructLayout *L = |
| 256 | (StructLayout *)malloc(sizeof(StructLayout)+(NumElts-1)*sizeof(uint64_t)); |
Chris Lattner | f6ca09a | 2007-02-10 20:26:17 +0000 | [diff] [blame^] | 257 | |
| 258 | // Set SL before calling StructLayout's ctor. The ctor could cause other |
| 259 | // entries to be added to TheMap, invalidating our reference. |
| 260 | SL = L; |
| 261 | |
Chris Lattner | 9182e3f | 2007-02-10 20:15:41 +0000 | [diff] [blame] | 262 | new (L) StructLayout(Ty, *this); |
| 263 | |
Chris Lattner | 9182e3f | 2007-02-10 20:15:41 +0000 | [diff] [blame] | 264 | return L; |
| 265 | } |
| 266 | |
| 267 | /// InvalidateStructLayoutInfo - TargetData speculatively caches StructLayout |
| 268 | /// objects. If a TargetData object is alive when types are being refined and |
| 269 | /// removed, this method must be called whenever a StructType is removed to |
| 270 | /// avoid a dangling pointer in this cache. |
| 271 | void TargetData::InvalidateStructLayoutInfo(const StructType *Ty) const { |
| 272 | if (!LayoutInfo.isConstructed()) return; // No cache. |
| 273 | |
Chris Lattner | a12bd03 | 2007-02-10 20:18:06 +0000 | [diff] [blame] | 274 | LayoutInfoTy::iterator I = LayoutInfo->find(LayoutKey(this, Ty)); |
Chris Lattner | 9182e3f | 2007-02-10 20:15:41 +0000 | [diff] [blame] | 275 | if (I != LayoutInfo->end()) { |
| 276 | I->second->~StructLayout(); |
| 277 | free(I->second); |
| 278 | LayoutInfo->erase(I); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | |
Owen Anderson | 2577c22 | 2006-05-12 07:01:44 +0000 | [diff] [blame] | 283 | std::string TargetData::getStringRepresentation() const { |
| 284 | std::stringstream repr; |
| 285 | |
| 286 | if (LittleEndian) |
| 287 | repr << "e"; |
| 288 | else |
| 289 | repr << "E"; |
| 290 | |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 291 | repr << "-p:" << (PointerMemSize * 8) << ":" << (PointerABIAlignment * 8); |
| 292 | repr << "-d:" << (DoubleABIAlignment * 8) << ":" |
| 293 | << (DoublePrefAlignment * 8); |
| 294 | repr << "-f:" << (FloatABIAlignment * 8) << ":" |
| 295 | << (FloatPrefAlignment * 8); |
| 296 | repr << "-l:" << (LongABIAlignment * 8) << ":" |
| 297 | << (LongPrefAlignment * 8); |
| 298 | repr << "-i:" << (IntABIAlignment * 8) << ":" |
| 299 | << (IntPrefAlignment * 8); |
| 300 | repr << "-s:" << (ShortABIAlignment * 8) << ":" |
| 301 | << (ShortPrefAlignment * 8); |
| 302 | repr << "-b:" << (ByteABIAlignment * 8) << ":" |
| 303 | << (BytePrefAlignment * 8); |
| 304 | repr << "-B:" << (BoolABIAlignment * 8) << ":" |
| 305 | << (BoolPrefAlignment * 8); |
| 306 | repr << "-A:" << (AggMinPrefAlignment * 8); |
Owen Anderson | 2577c22 | 2006-05-12 07:01:44 +0000 | [diff] [blame] | 307 | |
| 308 | return repr.str(); |
| 309 | } |
| 310 | |
Chris Lattner | 8dff24f | 2006-01-14 00:07:34 +0000 | [diff] [blame] | 311 | |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 312 | static inline void getTypeInfoABI(const Type *Ty, const TargetData *TD, |
| 313 | uint64_t &Size, unsigned char &Alignment) { |
Chris Lattner | f59ce92 | 2001-12-13 00:46:11 +0000 | [diff] [blame] | 314 | assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!"); |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 315 | switch (Ty->getTypeID()) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 316 | case Type::IntegerTyID: { |
| 317 | unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth(); |
| 318 | if (BitWidth <= 8) { |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 319 | Size = 1; Alignment = TD->getByteABIAlignment(); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 320 | } else if (BitWidth <= 16) { |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 321 | Size = 2; Alignment = TD->getShortABIAlignment(); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 322 | } else if (BitWidth <= 32) { |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 323 | Size = 4; Alignment = TD->getIntABIAlignment(); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 324 | } else if (BitWidth <= 64) { |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 325 | Size = 8; Alignment = TD->getLongABIAlignment(); |
Reid Spencer | d2a988c | 2007-02-05 23:51:43 +0000 | [diff] [blame] | 326 | } else { |
| 327 | Size = ((BitWidth + 7) / 8) & ~1; |
| 328 | Alignment = TD->getLongABIAlignment(); |
| 329 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 330 | return; |
| 331 | } |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 332 | case Type::VoidTyID: Size = 1; Alignment = TD->getByteABIAlignment(); return; |
| 333 | case Type::FloatTyID: Size = 4; Alignment = TD->getFloatABIAlignment(); return; |
| 334 | case Type::DoubleTyID: Size = 8; Alignment = TD->getDoubleABIAlignment(); return; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 335 | case Type::LabelTyID: |
| 336 | case Type::PointerTyID: |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 337 | Size = TD->getPointerSize(); Alignment = TD->getPointerABIAlignment(); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 338 | return; |
| 339 | case Type::ArrayTyID: { |
Chris Lattner | 59b0067 | 2004-07-01 17:32:59 +0000 | [diff] [blame] | 340 | const ArrayType *ATy = cast<ArrayType>(Ty); |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 341 | getTypeInfoABI(ATy->getElementType(), TD, Size, Alignment); |
Brian Gaeke | e0e3589 | 2004-07-02 07:01:31 +0000 | [diff] [blame] | 342 | unsigned AlignedSize = (Size + Alignment - 1)/Alignment*Alignment; |
Chris Lattner | 59b0067 | 2004-07-01 17:32:59 +0000 | [diff] [blame] | 343 | Size = AlignedSize*ATy->getNumElements(); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 344 | return; |
| 345 | } |
Chris Lattner | 527efc6 | 2004-12-01 17:14:28 +0000 | [diff] [blame] | 346 | case Type::PackedTyID: { |
| 347 | const PackedType *PTy = cast<PackedType>(Ty); |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 348 | getTypeInfoABI(PTy->getElementType(), TD, Size, Alignment); |
Chris Lattner | 527efc6 | 2004-12-01 17:14:28 +0000 | [diff] [blame] | 349 | unsigned AlignedSize = (Size + Alignment - 1)/Alignment*Alignment; |
| 350 | Size = AlignedSize*PTy->getNumElements(); |
Evan Cheng | e668bda | 2006-03-31 22:33:42 +0000 | [diff] [blame] | 351 | // FIXME: The alignments of specific packed types are target dependent. |
| 352 | // For now, just set it to be equal to Size. |
Chris Lattner | 0aab36f | 2006-04-03 23:14:49 +0000 | [diff] [blame] | 353 | Alignment = Size; |
Chris Lattner | 527efc6 | 2004-12-01 17:14:28 +0000 | [diff] [blame] | 354 | return; |
| 355 | } |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 356 | case Type::StructTyID: { |
| 357 | // Get the layout annotation... which is lazily created on demand. |
Chris Lattner | 59b0067 | 2004-07-01 17:32:59 +0000 | [diff] [blame] | 358 | const StructLayout *Layout = TD->getStructLayout(cast<StructType>(Ty)); |
Chris Lattner | b0c39a3 | 2007-02-10 19:59:22 +0000 | [diff] [blame] | 359 | Size = Layout->getSizeInBytes(); Alignment = Layout->getAlignment(); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 360 | return; |
| 361 | } |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 362 | |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 363 | default: |
| 364 | assert(0 && "Bad type for getTypeInfo!!!"); |
| 365 | return; |
| 366 | } |
| 367 | } |
| 368 | |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 369 | static inline void getTypeInfoPref(const Type *Ty, const TargetData *TD, |
| 370 | uint64_t &Size, unsigned char &Alignment) { |
| 371 | assert(Ty->isSized() && "Cannot getTypeInfoPref() on a type that is unsized!"); |
| 372 | switch (Ty->getTypeID()) { |
| 373 | case Type::IntegerTyID: { |
| 374 | unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth(); |
| 375 | if (BitWidth <= 8) { |
| 376 | Size = 1; Alignment = TD->getBytePrefAlignment(); |
| 377 | } else if (BitWidth <= 16) { |
| 378 | Size = 2; Alignment = TD->getShortPrefAlignment(); |
| 379 | } else if (BitWidth <= 32) { |
| 380 | Size = 4; Alignment = TD->getIntPrefAlignment(); |
| 381 | } else if (BitWidth <= 64) { |
| 382 | Size = 8; Alignment = TD->getLongPrefAlignment(); |
| 383 | } else |
| 384 | assert(0 && "Integer types > 64 bits not supported."); |
| 385 | return; |
| 386 | } |
| 387 | case Type::VoidTyID: |
| 388 | Size = 1; Alignment = TD->getBytePrefAlignment(); |
| 389 | return; |
| 390 | case Type::FloatTyID: |
| 391 | Size = 4; Alignment = TD->getFloatPrefAlignment(); |
| 392 | return; |
| 393 | case Type::DoubleTyID: |
| 394 | Size = 8; Alignment = TD->getDoublePrefAlignment(); |
| 395 | return; |
| 396 | case Type::LabelTyID: |
| 397 | case Type::PointerTyID: |
| 398 | Size = TD->getPointerSize(); Alignment = TD->getPointerPrefAlignment(); |
| 399 | return; |
| 400 | case Type::ArrayTyID: { |
| 401 | const ArrayType *ATy = cast<ArrayType>(Ty); |
| 402 | getTypeInfoPref(ATy->getElementType(), TD, Size, Alignment); |
| 403 | unsigned AlignedSize = (Size + Alignment - 1)/Alignment*Alignment; |
| 404 | Size = AlignedSize*ATy->getNumElements(); |
| 405 | return; |
| 406 | } |
| 407 | case Type::PackedTyID: { |
| 408 | const PackedType *PTy = cast<PackedType>(Ty); |
| 409 | getTypeInfoPref(PTy->getElementType(), TD, Size, Alignment); |
| 410 | unsigned AlignedSize = (Size + Alignment - 1)/Alignment*Alignment; |
| 411 | Size = AlignedSize*PTy->getNumElements(); |
| 412 | // FIXME: The alignments of specific packed types are target dependent. |
| 413 | // For now, just set it to be equal to Size. |
| 414 | Alignment = Size; |
| 415 | return; |
| 416 | } |
| 417 | case Type::StructTyID: { |
| 418 | // Get the layout annotation... which is lazily created on demand; |
| 419 | // enforce minimum aggregate alignment. |
| 420 | const StructLayout *Layout = TD->getStructLayout(cast<StructType>(Ty)); |
Chris Lattner | b0c39a3 | 2007-02-10 19:59:22 +0000 | [diff] [blame] | 421 | Size = Layout->getSizeInBytes(); |
| 422 | Alignment = std::max(Layout->getAlignment(), |
| 423 | (const unsigned int)TD->getAggMinPrefAlignment()); |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 424 | return; |
| 425 | } |
| 426 | |
| 427 | default: |
| 428 | assert(0 && "Bad type for getTypeInfoPref!!!"); |
| 429 | return; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 434 | uint64_t TargetData::getTypeSize(const Type *Ty) const { |
| 435 | uint64_t Size; |
| 436 | unsigned char Align; |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 437 | getTypeInfoABI(Ty, this, Size, Align); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 438 | return Size; |
| 439 | } |
| 440 | |
Reid Spencer | 7c29243 | 2007-01-20 23:32:04 +0000 | [diff] [blame] | 441 | uint64_t TargetData::getTypeSizeInBits(const Type *Ty) const { |
| 442 | if (Ty->isInteger()) |
| 443 | return cast<IntegerType>(Ty)->getBitWidth(); |
| 444 | |
| 445 | uint64_t Size; |
| 446 | unsigned char Align; |
| 447 | getTypeInfoABI(Ty, this, Size, Align); |
| 448 | return Size * 8; |
| 449 | } |
| 450 | |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 451 | unsigned char TargetData::getTypeAlignmentABI(const Type *Ty) const { |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 452 | uint64_t Size; |
| 453 | unsigned char Align; |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 454 | getTypeInfoABI(Ty, this, Size, Align); |
| 455 | return Align; |
| 456 | } |
| 457 | |
| 458 | unsigned char TargetData::getTypeAlignmentPref(const Type *Ty) const { |
| 459 | uint64_t Size; |
| 460 | unsigned char Align; |
| 461 | getTypeInfoPref(Ty, this, Size, Align); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 462 | return Align; |
| 463 | } |
| 464 | |
Evan Cheng | de268f7 | 2007-01-24 07:03:39 +0000 | [diff] [blame] | 465 | unsigned char TargetData::getPreferredTypeAlignmentShift(const Type *Ty) const { |
Evan Cheng | 778900a | 2007-01-22 23:08:19 +0000 | [diff] [blame] | 466 | unsigned Align = getTypeAlignmentPref(Ty); |
Chris Lattner | d2b0bb4 | 2004-08-17 19:13:00 +0000 | [diff] [blame] | 467 | assert(!(Align & (Align-1)) && "Alignment is not a power of two!"); |
Chris Lattner | 0561b3f | 2005-08-02 19:26:06 +0000 | [diff] [blame] | 468 | return Log2_32(Align); |
Chris Lattner | d2b0bb4 | 2004-08-17 19:13:00 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Chris Lattner | f045328 | 2003-12-22 05:01:15 +0000 | [diff] [blame] | 471 | /// getIntPtrType - Return an unsigned integer type that is the same size or |
| 472 | /// greater to the host pointer size. |
| 473 | const Type *TargetData::getIntPtrType() const { |
| 474 | switch (getPointerSize()) { |
| 475 | default: assert(0 && "Unknown pointer size!"); |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 476 | case 2: return Type::Int16Ty; |
| 477 | case 4: return Type::Int32Ty; |
| 478 | case 8: return Type::Int64Ty; |
Chris Lattner | f045328 | 2003-12-22 05:01:15 +0000 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | |
| 482 | |
Chris Lattner | ddce8d2 | 2007-02-10 19:33:15 +0000 | [diff] [blame] | 483 | uint64_t TargetData::getIndexedOffset(const Type *ptrTy, Value* const* Indices, |
| 484 | unsigned NumIndices) const { |
Vikram S. Adve | ed0030e | 2002-08-04 20:52:39 +0000 | [diff] [blame] | 485 | const Type *Ty = ptrTy; |
| 486 | assert(isa<PointerType>(Ty) && "Illegal argument for getIndexedOffset()"); |
Vikram S. Adve | f66723f | 2002-05-19 15:28:02 +0000 | [diff] [blame] | 487 | uint64_t Result = 0; |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 488 | |
Chris Lattner | ddce8d2 | 2007-02-10 19:33:15 +0000 | [diff] [blame] | 489 | generic_gep_type_iterator<Value* const*> |
| 490 | TI = gep_type_begin(ptrTy, Indices, Indices+NumIndices); |
| 491 | for (unsigned CurIDX = 0; CurIDX != NumIndices; ++CurIDX, ++TI) { |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 492 | if (const StructType *STy = dyn_cast<StructType>(*TI)) { |
Chris Lattner | ddce8d2 | 2007-02-10 19:33:15 +0000 | [diff] [blame] | 493 | assert(Indices[CurIDX]->getType() == Type::Int32Ty &&"Illegal struct idx"); |
| 494 | unsigned FieldNo = cast<ConstantInt>(Indices[CurIDX])->getZExtValue(); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 495 | |
| 496 | // Get structure layout information... |
| 497 | const StructLayout *Layout = getStructLayout(STy); |
| 498 | |
| 499 | // Add in the offset, as calculated by the structure layout info... |
Chris Lattner | b1919e2 | 2007-02-10 19:55:17 +0000 | [diff] [blame] | 500 | Result += Layout->getElementOffset(FieldNo); |
Vikram S. Adve | ed0030e | 2002-08-04 20:52:39 +0000 | [diff] [blame] | 501 | |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 502 | // Update Ty to refer to current element |
Chris Lattner | d21cd80 | 2004-02-09 04:37:31 +0000 | [diff] [blame] | 503 | Ty = STy->getElementType(FieldNo); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 504 | } else { |
| 505 | // Update Ty to refer to current element |
| 506 | Ty = cast<SequentialType>(Ty)->getElementType(); |
| 507 | |
| 508 | // Get the array index and the size of each array element. |
Chris Lattner | ddce8d2 | 2007-02-10 19:33:15 +0000 | [diff] [blame] | 509 | int64_t arrayIdx = cast<ConstantInt>(Indices[CurIDX])->getSExtValue(); |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 510 | Result += arrayIdx * (int64_t)getTypeSize(Ty); |
Chris Lattner | e7fb360 | 2001-08-27 16:00:15 +0000 | [diff] [blame] | 511 | } |
| 512 | } |
| 513 | |
| 514 | return Result; |
| 515 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 516 | |
Devang Patel | f9c197e | 2006-10-24 20:32:14 +0000 | [diff] [blame] | 517 | /// getPreferredAlignmentLog - Return the preferred alignment of the |
| 518 | /// specified global, returned in log form. This includes an explicitly |
| 519 | /// requested alignment (if the global has one). |
| 520 | unsigned TargetData::getPreferredAlignmentLog(const GlobalVariable *GV) const { |
| 521 | const Type *ElemType = GV->getType()->getElementType(); |
Evan Cheng | de268f7 | 2007-01-24 07:03:39 +0000 | [diff] [blame] | 522 | unsigned Alignment = getPreferredTypeAlignmentShift(ElemType); |
Devang Patel | f9c197e | 2006-10-24 20:32:14 +0000 | [diff] [blame] | 523 | if (GV->getAlignment() > (1U << Alignment)) |
| 524 | Alignment = Log2_32(GV->getAlignment()); |
| 525 | |
| 526 | if (GV->hasInitializer()) { |
Devang Patel | f9c197e | 2006-10-24 20:32:14 +0000 | [diff] [blame] | 527 | if (Alignment < 4) { |
| 528 | // If the global is not external, see if it is large. If so, give it a |
| 529 | // larger alignment. |
| 530 | if (getTypeSize(ElemType) > 128) |
| 531 | Alignment = 4; // 16-byte alignment. |
| 532 | } |
| 533 | } |
| 534 | return Alignment; |
| 535 | } |
| 536 | |