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