Micah Villmow | 99b1148 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 1 | //===-- DataLayout.cpp - Data size & alignment routines --------------------==// |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Micah Villmow | 99b1148 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 10 | // This file defines layout properties related to datatype size/offset/alignment |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 11 | // information. |
| 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 | |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 19 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/DenseMap.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Constants.h" |
| 22 | #include "llvm/IR/DerivedTypes.h" |
| 23 | #include "llvm/IR/Module.h" |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 25 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
| 26 | #include "llvm/Support/ManagedStatic.h" |
| 27 | #include "llvm/Support/MathExtras.h" |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Mutex.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 29 | #include "llvm/Support/raw_ostream.h" |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 30 | #include <algorithm> |
| 31 | #include <cstdlib> |
| 32 | using namespace llvm; |
| 33 | |
Micah Villmow | 99b1148 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 34 | // Handle the Pass registration stuff necessary to use DataLayout's. |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 35 | |
| 36 | // Register the default SparcV9 implementation... |
Micah Villmow | 99b1148 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 37 | INITIALIZE_PASS(DataLayout, "datalayout", "Data Layout", false, true) |
| 38 | char DataLayout::ID = 0; |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 39 | |
| 40 | //===----------------------------------------------------------------------===// |
| 41 | // Support for StructLayout |
| 42 | //===----------------------------------------------------------------------===// |
| 43 | |
Micah Villmow | 99b1148 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 44 | StructLayout::StructLayout(StructType *ST, const DataLayout &TD) { |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 45 | assert(!ST->isOpaque() && "Cannot get layout of opaque structs"); |
| 46 | StructAlignment = 0; |
| 47 | StructSize = 0; |
| 48 | NumElements = ST->getNumElements(); |
| 49 | |
| 50 | // Loop over each of the elements, placing them in memory. |
| 51 | for (unsigned i = 0, e = NumElements; i != e; ++i) { |
| 52 | Type *Ty = ST->getElementType(i); |
| 53 | unsigned TyAlign = ST->isPacked() ? 1 : TD.getABITypeAlignment(Ty); |
| 54 | |
| 55 | // Add padding if necessary to align the data element properly. |
| 56 | if ((StructSize & (TyAlign-1)) != 0) |
Micah Villmow | 99b1148 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 57 | StructSize = DataLayout::RoundUpAlignment(StructSize, TyAlign); |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 58 | |
| 59 | // Keep track of maximum alignment constraint. |
| 60 | StructAlignment = std::max(TyAlign, StructAlignment); |
| 61 | |
| 62 | MemberOffsets[i] = StructSize; |
| 63 | StructSize += TD.getTypeAllocSize(Ty); // Consume space for this data item |
| 64 | } |
| 65 | |
| 66 | // Empty structures have alignment of 1 byte. |
| 67 | if (StructAlignment == 0) StructAlignment = 1; |
| 68 | |
| 69 | // Add padding to the end of the struct so that it could be put in an array |
| 70 | // and all array elements would be aligned correctly. |
| 71 | if ((StructSize & (StructAlignment-1)) != 0) |
Micah Villmow | 99b1148 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 72 | StructSize = DataLayout::RoundUpAlignment(StructSize, StructAlignment); |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | |
| 76 | /// getElementContainingOffset - Given a valid offset into the structure, |
| 77 | /// return the structure index that contains it. |
| 78 | unsigned StructLayout::getElementContainingOffset(uint64_t Offset) const { |
| 79 | const uint64_t *SI = |
| 80 | std::upper_bound(&MemberOffsets[0], &MemberOffsets[NumElements], Offset); |
| 81 | assert(SI != &MemberOffsets[0] && "Offset not in structure type!"); |
| 82 | --SI; |
| 83 | assert(*SI <= Offset && "upper_bound didn't work"); |
| 84 | assert((SI == &MemberOffsets[0] || *(SI-1) <= Offset) && |
| 85 | (SI+1 == &MemberOffsets[NumElements] || *(SI+1) > Offset) && |
| 86 | "Upper bound didn't work!"); |
| 87 | |
| 88 | // Multiple fields can have the same offset if any of them are zero sized. |
| 89 | // For example, in { i32, [0 x i32], i32 }, searching for offset 4 will stop |
| 90 | // at the i32 element, because it is the last element at that offset. This is |
| 91 | // the right one to return, because anything after it will have a higher |
| 92 | // offset, implying that this element is non-empty. |
| 93 | return SI-&MemberOffsets[0]; |
| 94 | } |
| 95 | |
| 96 | //===----------------------------------------------------------------------===// |
Micah Villmow | 99b1148 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 97 | // LayoutAlignElem, LayoutAlign support |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 98 | //===----------------------------------------------------------------------===// |
| 99 | |
Micah Villmow | 99b1148 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 100 | LayoutAlignElem |
| 101 | LayoutAlignElem::get(AlignTypeEnum align_type, unsigned abi_align, |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 102 | unsigned pref_align, uint32_t bit_width) { |
| 103 | assert(abi_align <= pref_align && "Preferred alignment worse than ABI!"); |
Micah Villmow | 99b1148 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 104 | LayoutAlignElem retval; |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 105 | retval.AlignType = align_type; |
| 106 | retval.ABIAlign = abi_align; |
| 107 | retval.PrefAlign = pref_align; |
| 108 | retval.TypeBitWidth = bit_width; |
| 109 | return retval; |
| 110 | } |
| 111 | |
| 112 | bool |
Micah Villmow | 99b1148 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 113 | LayoutAlignElem::operator==(const LayoutAlignElem &rhs) const { |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 114 | return (AlignType == rhs.AlignType |
| 115 | && ABIAlign == rhs.ABIAlign |
| 116 | && PrefAlign == rhs.PrefAlign |
| 117 | && TypeBitWidth == rhs.TypeBitWidth); |
| 118 | } |
| 119 | |
Micah Villmow | 99b1148 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 120 | const LayoutAlignElem |
Richard Smith | fe4ef2c | 2012-12-20 04:02:58 +0000 | [diff] [blame] | 121 | DataLayout::InvalidAlignmentElem = LayoutAlignElem::get(INVALID_ALIGN, 0, 0, 0); |
Micah Villmow | 7d66146 | 2012-10-09 16:06:12 +0000 | [diff] [blame] | 122 | |
| 123 | //===----------------------------------------------------------------------===// |
| 124 | // PointerAlignElem, PointerAlign support |
| 125 | //===----------------------------------------------------------------------===// |
| 126 | |
| 127 | PointerAlignElem |
| 128 | PointerAlignElem::get(uint32_t addr_space, unsigned abi_align, |
Eli Bendersky | f578c89 | 2013-01-30 19:24:23 +0000 | [diff] [blame] | 129 | unsigned pref_align, uint32_t bit_width) { |
Micah Villmow | 7d66146 | 2012-10-09 16:06:12 +0000 | [diff] [blame] | 130 | assert(abi_align <= pref_align && "Preferred alignment worse than ABI!"); |
| 131 | PointerAlignElem retval; |
| 132 | retval.AddressSpace = addr_space; |
| 133 | retval.ABIAlign = abi_align; |
| 134 | retval.PrefAlign = pref_align; |
| 135 | retval.TypeBitWidth = bit_width; |
| 136 | return retval; |
| 137 | } |
| 138 | |
| 139 | bool |
| 140 | PointerAlignElem::operator==(const PointerAlignElem &rhs) const { |
| 141 | return (ABIAlign == rhs.ABIAlign |
| 142 | && AddressSpace == rhs.AddressSpace |
| 143 | && PrefAlign == rhs.PrefAlign |
| 144 | && TypeBitWidth == rhs.TypeBitWidth); |
| 145 | } |
| 146 | |
| 147 | const PointerAlignElem |
| 148 | DataLayout::InvalidPointerElem = PointerAlignElem::get(~0U, 0U, 0U, 0U); |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 149 | |
| 150 | //===----------------------------------------------------------------------===// |
Micah Villmow | 99b1148 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 151 | // DataLayout Class Implementation |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 152 | //===----------------------------------------------------------------------===// |
| 153 | |
Patrik Hägglund | 1e65676 | 2012-11-14 09:04:56 +0000 | [diff] [blame] | 154 | void DataLayout::init(StringRef Desc) { |
Micah Villmow | 99b1148 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 155 | initializeDataLayoutPass(*PassRegistry::getPassRegistry()); |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 156 | |
| 157 | LayoutMap = 0; |
| 158 | LittleEndian = false; |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 159 | StackNaturalAlign = 0; |
| 160 | |
| 161 | // Default alignments |
| 162 | setAlignment(INTEGER_ALIGN, 1, 1, 1); // i1 |
| 163 | setAlignment(INTEGER_ALIGN, 1, 1, 8); // i8 |
| 164 | setAlignment(INTEGER_ALIGN, 2, 2, 16); // i16 |
| 165 | setAlignment(INTEGER_ALIGN, 4, 4, 32); // i32 |
| 166 | setAlignment(INTEGER_ALIGN, 4, 8, 64); // i64 |
| 167 | setAlignment(FLOAT_ALIGN, 2, 2, 16); // half |
| 168 | setAlignment(FLOAT_ALIGN, 4, 4, 32); // float |
| 169 | setAlignment(FLOAT_ALIGN, 8, 8, 64); // double |
| 170 | setAlignment(FLOAT_ALIGN, 16, 16, 128); // ppcf128, quad, ... |
| 171 | setAlignment(VECTOR_ALIGN, 8, 8, 64); // v2i32, v1i64, ... |
| 172 | setAlignment(VECTOR_ALIGN, 16, 16, 128); // v16i8, v8i16, v4i32, ... |
| 173 | setAlignment(AGGREGATE_ALIGN, 0, 8, 0); // struct |
Micah Villmow | 7d66146 | 2012-10-09 16:06:12 +0000 | [diff] [blame] | 174 | setPointerAlignment(0, 8, 8, 8); |
Patrik Hägglund | 1e65676 | 2012-11-14 09:04:56 +0000 | [diff] [blame] | 175 | |
Patrik Hagglund | 58b4553 | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 176 | parseSpecifier(Desc); |
| 177 | } |
| 178 | |
| 179 | /// Checked version of split, to ensure mandatory subparts. |
| 180 | static std::pair<StringRef, StringRef> split(StringRef Str, char Separator) { |
| 181 | assert(!Str.empty() && "parse error, string can't be empty here"); |
| 182 | std::pair<StringRef, StringRef> Split = Str.split(Separator); |
| 183 | assert((!Split.second.empty() || Split.first == Str) && |
| 184 | "a trailing separator is not allowed"); |
| 185 | return Split; |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Patrik Hägglund | 37d5be7 | 2012-11-28 12:13:12 +0000 | [diff] [blame] | 188 | /// Get an unsinged integer, including error checks. |
| 189 | static unsigned getInt(StringRef R) { |
Patrik Hägglund | 37d5be7 | 2012-11-28 12:13:12 +0000 | [diff] [blame] | 190 | unsigned Result; |
Patrik Hägglund | dacb9a5 | 2012-11-28 14:32:52 +0000 | [diff] [blame] | 191 | bool error = R.getAsInteger(10, Result); (void)error; |
Patrik Hägglund | 37d5be7 | 2012-11-28 12:13:12 +0000 | [diff] [blame] | 192 | assert(!error && "not a number, or does not fit in an unsigned int"); |
| 193 | return Result; |
| 194 | } |
| 195 | |
Patrik Hagglund | 58b4553 | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 196 | /// Convert bits into bytes. Assert if not a byte width multiple. |
| 197 | static unsigned inBytes(unsigned Bits) { |
| 198 | assert(Bits % 8 == 0 && "number of bits must be a byte width multiple"); |
| 199 | return Bits / 8; |
| 200 | } |
| 201 | |
| 202 | void DataLayout::parseSpecifier(StringRef Desc) { |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 203 | |
| 204 | while (!Desc.empty()) { |
Patrik Hagglund | 58b4553 | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 205 | |
| 206 | // Split at '-'. |
| 207 | std::pair<StringRef, StringRef> Split = split(Desc, '-'); |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 208 | Desc = Split.second; |
| 209 | |
Patrik Hagglund | 58b4553 | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 210 | // Split at ':'. |
| 211 | Split = split(Split.first, ':'); |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 212 | |
Patrik Hagglund | 58b4553 | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 213 | // Aliases used below. |
| 214 | StringRef &Tok = Split.first; // Current token. |
| 215 | StringRef &Rest = Split.second; // The rest of the string. |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 216 | |
Patrik Hagglund | 58b4553 | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 217 | char Specifier = Tok.front(); |
| 218 | Tok = Tok.substr(1); |
| 219 | |
| 220 | switch (Specifier) { |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 221 | case 'E': |
Patrik Hägglund | 1e65676 | 2012-11-14 09:04:56 +0000 | [diff] [blame] | 222 | LittleEndian = false; |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 223 | break; |
| 224 | case 'e': |
Patrik Hägglund | 1e65676 | 2012-11-14 09:04:56 +0000 | [diff] [blame] | 225 | LittleEndian = true; |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 226 | break; |
| 227 | case 'p': { |
Patrik Hagglund | 58b4553 | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 228 | // Address space. |
| 229 | unsigned AddrSpace = Tok.empty() ? 0 : getInt(Tok); |
| 230 | assert(AddrSpace < 1 << 24 && |
| 231 | "Invalid address space, must be a 24bit integer"); |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 232 | |
Patrik Hagglund | 58b4553 | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 233 | // Size. |
| 234 | Split = split(Rest, ':'); |
| 235 | unsigned PointerMemSize = inBytes(getInt(Tok)); |
| 236 | |
| 237 | // ABI alignment. |
| 238 | Split = split(Rest, ':'); |
| 239 | unsigned PointerABIAlign = inBytes(getInt(Tok)); |
| 240 | |
| 241 | // Preferred alignment. |
| 242 | unsigned PointerPrefAlign = PointerABIAlign; |
| 243 | if (!Rest.empty()) { |
| 244 | Split = split(Rest, ':'); |
| 245 | PointerPrefAlign = inBytes(getInt(Tok)); |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 246 | } |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 247 | |
Patrik Hagglund | 58b4553 | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 248 | setPointerAlignment(AddrSpace, PointerABIAlign, PointerPrefAlign, |
| 249 | PointerMemSize); |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 250 | break; |
| 251 | } |
| 252 | case 'i': |
| 253 | case 'v': |
| 254 | case 'f': |
| 255 | case 'a': |
| 256 | case 's': { |
| 257 | AlignTypeEnum AlignType; |
Patrik Hagglund | 58b4553 | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 258 | switch (Specifier) { |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 259 | default: |
| 260 | case 'i': AlignType = INTEGER_ALIGN; break; |
| 261 | case 'v': AlignType = VECTOR_ALIGN; break; |
| 262 | case 'f': AlignType = FLOAT_ALIGN; break; |
| 263 | case 'a': AlignType = AGGREGATE_ALIGN; break; |
| 264 | case 's': AlignType = STACK_ALIGN; break; |
| 265 | } |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 266 | |
Patrik Hagglund | 58b4553 | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 267 | // Bit size. |
| 268 | unsigned Size = Tok.empty() ? 0 : getInt(Tok); |
| 269 | |
| 270 | // ABI alignment. |
| 271 | Split = split(Rest, ':'); |
| 272 | unsigned ABIAlign = inBytes(getInt(Tok)); |
| 273 | |
| 274 | // Preferred alignment. |
| 275 | unsigned PrefAlign = ABIAlign; |
| 276 | if (!Rest.empty()) { |
| 277 | Split = split(Rest, ':'); |
| 278 | PrefAlign = inBytes(getInt(Tok)); |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 279 | } |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 280 | |
Patrik Hägglund | 1e65676 | 2012-11-14 09:04:56 +0000 | [diff] [blame] | 281 | setAlignment(AlignType, ABIAlign, PrefAlign, Size); |
Micah Villmow | 99b1148 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 282 | |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 283 | break; |
| 284 | } |
| 285 | case 'n': // Native integer types. |
Patrik Hagglund | 58b4553 | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 286 | for (;;) { |
| 287 | unsigned Width = getInt(Tok); |
| 288 | assert(Width != 0 && "width must be non-zero"); |
Patrik Hägglund | 37d5be7 | 2012-11-28 12:13:12 +0000 | [diff] [blame] | 289 | LegalIntWidths.push_back(Width); |
Patrik Hagglund | 58b4553 | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 290 | if (Rest.empty()) |
| 291 | break; |
| 292 | Split = split(Rest, ':'); |
| 293 | } |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 294 | break; |
| 295 | case 'S': { // Stack natural alignment. |
Patrik Hagglund | 58b4553 | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 296 | StackNaturalAlign = inBytes(getInt(Tok)); |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 297 | break; |
| 298 | } |
| 299 | default: |
Patrik Hagglund | 58b4553 | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 300 | llvm_unreachable("Unknown specifier in datalayout string"); |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 301 | break; |
| 302 | } |
| 303 | } |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | /// Default ctor. |
| 307 | /// |
| 308 | /// @note This has to exist, because this is a pass, but it should never be |
| 309 | /// used. |
Micah Villmow | 99b1148 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 310 | DataLayout::DataLayout() : ImmutablePass(ID) { |
| 311 | report_fatal_error("Bad DataLayout ctor used. " |
Eli Bendersky | f578c89 | 2013-01-30 19:24:23 +0000 | [diff] [blame] | 312 | "Tool did not specify a DataLayout to use?"); |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Micah Villmow | 99b1148 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 315 | DataLayout::DataLayout(const Module *M) |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 316 | : ImmutablePass(ID) { |
Patrik Hägglund | 1e65676 | 2012-11-14 09:04:56 +0000 | [diff] [blame] | 317 | init(M->getDataLayout()); |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | void |
Micah Villmow | 99b1148 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 321 | DataLayout::setAlignment(AlignTypeEnum align_type, unsigned abi_align, |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 322 | unsigned pref_align, uint32_t bit_width) { |
| 323 | assert(abi_align <= pref_align && "Preferred alignment worse than ABI!"); |
| 324 | assert(pref_align < (1 << 16) && "Alignment doesn't fit in bitfield"); |
| 325 | assert(bit_width < (1 << 24) && "Bit width doesn't fit in bitfield"); |
| 326 | for (unsigned i = 0, e = Alignments.size(); i != e; ++i) { |
Micah Villmow | bf07a51 | 2012-10-05 17:02:14 +0000 | [diff] [blame] | 327 | if (Alignments[i].AlignType == (unsigned)align_type && |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 328 | Alignments[i].TypeBitWidth == bit_width) { |
| 329 | // Update the abi, preferred alignments. |
| 330 | Alignments[i].ABIAlign = abi_align; |
| 331 | Alignments[i].PrefAlign = pref_align; |
| 332 | return; |
| 333 | } |
| 334 | } |
| 335 | |
Micah Villmow | 99b1148 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 336 | Alignments.push_back(LayoutAlignElem::get(align_type, abi_align, |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 337 | pref_align, bit_width)); |
| 338 | } |
| 339 | |
Micah Villmow | 7d66146 | 2012-10-09 16:06:12 +0000 | [diff] [blame] | 340 | void |
| 341 | DataLayout::setPointerAlignment(uint32_t addr_space, unsigned abi_align, |
| 342 | unsigned pref_align, uint32_t bit_width) { |
| 343 | assert(abi_align <= pref_align && "Preferred alignment worse than ABI!"); |
| 344 | DenseMap<unsigned,PointerAlignElem>::iterator val = Pointers.find(addr_space); |
| 345 | if (val == Pointers.end()) { |
| 346 | Pointers[addr_space] = PointerAlignElem::get(addr_space, |
| 347 | abi_align, pref_align, bit_width); |
| 348 | } else { |
| 349 | val->second.ABIAlign = abi_align; |
| 350 | val->second.PrefAlign = pref_align; |
| 351 | val->second.TypeBitWidth = bit_width; |
| 352 | } |
| 353 | } |
| 354 | |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 355 | /// getAlignmentInfo - Return the alignment (either ABI if ABIInfo = true or |
Micah Villmow | 99b1148 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 356 | /// preferred if ABIInfo = false) the layout wants for the specified datatype. |
| 357 | unsigned DataLayout::getAlignmentInfo(AlignTypeEnum AlignType, |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 358 | uint32_t BitWidth, bool ABIInfo, |
| 359 | Type *Ty) const { |
| 360 | // Check to see if we have an exact match and remember the best match we see. |
| 361 | int BestMatchIdx = -1; |
| 362 | int LargestInt = -1; |
| 363 | for (unsigned i = 0, e = Alignments.size(); i != e; ++i) { |
Micah Villmow | bf07a51 | 2012-10-05 17:02:14 +0000 | [diff] [blame] | 364 | if (Alignments[i].AlignType == (unsigned)AlignType && |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 365 | Alignments[i].TypeBitWidth == BitWidth) |
| 366 | return ABIInfo ? Alignments[i].ABIAlign : Alignments[i].PrefAlign; |
| 367 | |
| 368 | // The best match so far depends on what we're looking for. |
| 369 | if (AlignType == INTEGER_ALIGN && |
| 370 | Alignments[i].AlignType == INTEGER_ALIGN) { |
| 371 | // The "best match" for integers is the smallest size that is larger than |
| 372 | // the BitWidth requested. |
| 373 | if (Alignments[i].TypeBitWidth > BitWidth && (BestMatchIdx == -1 || |
Eli Bendersky | f578c89 | 2013-01-30 19:24:23 +0000 | [diff] [blame] | 374 | Alignments[i].TypeBitWidth < Alignments[BestMatchIdx].TypeBitWidth)) |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 375 | BestMatchIdx = i; |
| 376 | // However, if there isn't one that's larger, then we must use the |
| 377 | // largest one we have (see below) |
| 378 | if (LargestInt == -1 || |
| 379 | Alignments[i].TypeBitWidth > Alignments[LargestInt].TypeBitWidth) |
| 380 | LargestInt = i; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | // Okay, we didn't find an exact solution. Fall back here depending on what |
| 385 | // is being looked for. |
| 386 | if (BestMatchIdx == -1) { |
| 387 | // If we didn't find an integer alignment, fall back on most conservative. |
| 388 | if (AlignType == INTEGER_ALIGN) { |
| 389 | BestMatchIdx = LargestInt; |
| 390 | } else { |
| 391 | assert(AlignType == VECTOR_ALIGN && "Unknown alignment type!"); |
| 392 | |
| 393 | // By default, use natural alignment for vector types. This is consistent |
| 394 | // with what clang and llvm-gcc do. |
| 395 | unsigned Align = getTypeAllocSize(cast<VectorType>(Ty)->getElementType()); |
| 396 | Align *= cast<VectorType>(Ty)->getNumElements(); |
| 397 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 398 | // This happens for non-power-of-2 length vectors. |
| 399 | if (Align & (Align-1)) |
| 400 | Align = NextPowerOf2(Align); |
| 401 | return Align; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | // Since we got a "best match" index, just return it. |
| 406 | return ABIInfo ? Alignments[BestMatchIdx].ABIAlign |
| 407 | : Alignments[BestMatchIdx].PrefAlign; |
| 408 | } |
| 409 | |
| 410 | namespace { |
| 411 | |
| 412 | class StructLayoutMap { |
| 413 | typedef DenseMap<StructType*, StructLayout*> LayoutInfoTy; |
| 414 | LayoutInfoTy LayoutInfo; |
| 415 | |
| 416 | public: |
| 417 | virtual ~StructLayoutMap() { |
| 418 | // Remove any layouts. |
| 419 | for (LayoutInfoTy::iterator I = LayoutInfo.begin(), E = LayoutInfo.end(); |
| 420 | I != E; ++I) { |
| 421 | StructLayout *Value = I->second; |
| 422 | Value->~StructLayout(); |
| 423 | free(Value); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | StructLayout *&operator[](StructType *STy) { |
| 428 | return LayoutInfo[STy]; |
| 429 | } |
| 430 | |
| 431 | // for debugging... |
| 432 | virtual void dump() const {} |
| 433 | }; |
| 434 | |
| 435 | } // end anonymous namespace |
| 436 | |
Micah Villmow | 99b1148 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 437 | DataLayout::~DataLayout() { |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 438 | delete static_cast<StructLayoutMap*>(LayoutMap); |
| 439 | } |
| 440 | |
Pete Cooper | 8f8af52 | 2013-03-12 17:37:31 +0000 | [diff] [blame] | 441 | bool DataLayout::doFinalization(Module &M) { |
| 442 | delete static_cast<StructLayoutMap*>(LayoutMap); |
| 443 | LayoutMap = 0; |
| 444 | return false; |
| 445 | } |
| 446 | |
Micah Villmow | 99b1148 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 447 | const StructLayout *DataLayout::getStructLayout(StructType *Ty) const { |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 448 | if (!LayoutMap) |
| 449 | LayoutMap = new StructLayoutMap(); |
| 450 | |
| 451 | StructLayoutMap *STM = static_cast<StructLayoutMap*>(LayoutMap); |
| 452 | StructLayout *&SL = (*STM)[Ty]; |
| 453 | if (SL) return SL; |
| 454 | |
| 455 | // Otherwise, create the struct layout. Because it is variable length, we |
| 456 | // malloc it, then use placement new. |
| 457 | int NumElts = Ty->getNumElements(); |
| 458 | StructLayout *L = |
| 459 | (StructLayout *)malloc(sizeof(StructLayout)+(NumElts-1) * sizeof(uint64_t)); |
| 460 | |
| 461 | // Set SL before calling StructLayout's ctor. The ctor could cause other |
| 462 | // entries to be added to TheMap, invalidating our reference. |
| 463 | SL = L; |
| 464 | |
| 465 | new (L) StructLayout(Ty, *this); |
| 466 | |
| 467 | return L; |
| 468 | } |
| 469 | |
Micah Villmow | 99b1148 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 470 | std::string DataLayout::getStringRepresentation() const { |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 471 | std::string Result; |
| 472 | raw_string_ostream OS(Result); |
| 473 | |
Micah Villmow | 7d66146 | 2012-10-09 16:06:12 +0000 | [diff] [blame] | 474 | OS << (LittleEndian ? "e" : "E"); |
| 475 | SmallVector<unsigned, 8> addrSpaces; |
| 476 | // Lets get all of the known address spaces and sort them |
| 477 | // into increasing order so that we can emit the string |
| 478 | // in a cleaner format. |
| 479 | for (DenseMap<unsigned, PointerAlignElem>::const_iterator |
| 480 | pib = Pointers.begin(), pie = Pointers.end(); |
| 481 | pib != pie; ++pib) { |
| 482 | addrSpaces.push_back(pib->first); |
| 483 | } |
| 484 | std::sort(addrSpaces.begin(), addrSpaces.end()); |
| 485 | for (SmallVector<unsigned, 8>::iterator asb = addrSpaces.begin(), |
| 486 | ase = addrSpaces.end(); asb != ase; ++asb) { |
| 487 | const PointerAlignElem &PI = Pointers.find(*asb)->second; |
| 488 | OS << "-p"; |
| 489 | if (PI.AddressSpace) { |
| 490 | OS << PI.AddressSpace; |
| 491 | } |
| 492 | OS << ":" << PI.TypeBitWidth*8 << ':' << PI.ABIAlign*8 |
| 493 | << ':' << PI.PrefAlign*8; |
| 494 | } |
| 495 | OS << "-S" << StackNaturalAlign*8; |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 496 | |
| 497 | for (unsigned i = 0, e = Alignments.size(); i != e; ++i) { |
Micah Villmow | 99b1148 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 498 | const LayoutAlignElem &AI = Alignments[i]; |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 499 | OS << '-' << (char)AI.AlignType << AI.TypeBitWidth << ':' |
| 500 | << AI.ABIAlign*8 << ':' << AI.PrefAlign*8; |
| 501 | } |
| 502 | |
| 503 | if (!LegalIntWidths.empty()) { |
| 504 | OS << "-n" << (unsigned)LegalIntWidths[0]; |
| 505 | |
| 506 | for (unsigned i = 1, e = LegalIntWidths.size(); i != e; ++i) |
| 507 | OS << ':' << (unsigned)LegalIntWidths[i]; |
| 508 | } |
| 509 | return OS.str(); |
| 510 | } |
| 511 | |
| 512 | |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 513 | /*! |
| 514 | \param abi_or_pref Flag that determines which alignment is returned. true |
| 515 | returns the ABI alignment, false returns the preferred alignment. |
| 516 | \param Ty The underlying type for which alignment is determined. |
| 517 | |
| 518 | Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref |
| 519 | == false) for the requested type \a Ty. |
| 520 | */ |
Micah Villmow | 99b1148 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 521 | unsigned DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const { |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 522 | int AlignType = -1; |
| 523 | |
| 524 | assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!"); |
| 525 | switch (Ty->getTypeID()) { |
| 526 | // Early escape for the non-numeric types. |
| 527 | case Type::LabelTyID: |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 528 | return (abi_or_pref |
Micah Villmow | 7d66146 | 2012-10-09 16:06:12 +0000 | [diff] [blame] | 529 | ? getPointerABIAlignment(0) |
| 530 | : getPointerPrefAlignment(0)); |
| 531 | case Type::PointerTyID: { |
| 532 | unsigned AS = dyn_cast<PointerType>(Ty)->getAddressSpace(); |
| 533 | return (abi_or_pref |
| 534 | ? getPointerABIAlignment(AS) |
| 535 | : getPointerPrefAlignment(AS)); |
| 536 | } |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 537 | case Type::ArrayTyID: |
| 538 | return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref); |
| 539 | |
| 540 | case Type::StructTyID: { |
| 541 | // Packed structure types always have an ABI alignment of one. |
| 542 | if (cast<StructType>(Ty)->isPacked() && abi_or_pref) |
| 543 | return 1; |
| 544 | |
| 545 | // Get the layout annotation... which is lazily created on demand. |
| 546 | const StructLayout *Layout = getStructLayout(cast<StructType>(Ty)); |
| 547 | unsigned Align = getAlignmentInfo(AGGREGATE_ALIGN, 0, abi_or_pref, Ty); |
| 548 | return std::max(Align, Layout->getAlignment()); |
| 549 | } |
| 550 | case Type::IntegerTyID: |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 551 | AlignType = INTEGER_ALIGN; |
| 552 | break; |
| 553 | case Type::HalfTyID: |
| 554 | case Type::FloatTyID: |
| 555 | case Type::DoubleTyID: |
| 556 | // PPC_FP128TyID and FP128TyID have different data contents, but the |
| 557 | // same size and alignment, so they look the same here. |
| 558 | case Type::PPC_FP128TyID: |
| 559 | case Type::FP128TyID: |
| 560 | case Type::X86_FP80TyID: |
| 561 | AlignType = FLOAT_ALIGN; |
| 562 | break; |
| 563 | case Type::X86_MMXTyID: |
| 564 | case Type::VectorTyID: |
| 565 | AlignType = VECTOR_ALIGN; |
| 566 | break; |
| 567 | default: |
| 568 | llvm_unreachable("Bad type for getAlignment!!!"); |
| 569 | } |
| 570 | |
| 571 | return getAlignmentInfo((AlignTypeEnum)AlignType, getTypeSizeInBits(Ty), |
| 572 | abi_or_pref, Ty); |
| 573 | } |
| 574 | |
Micah Villmow | 99b1148 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 575 | unsigned DataLayout::getABITypeAlignment(Type *Ty) const { |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 576 | return getAlignment(Ty, true); |
| 577 | } |
| 578 | |
| 579 | /// getABIIntegerTypeAlignment - Return the minimum ABI-required alignment for |
| 580 | /// an integer type of the specified bitwidth. |
Micah Villmow | 99b1148 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 581 | unsigned DataLayout::getABIIntegerTypeAlignment(unsigned BitWidth) const { |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 582 | return getAlignmentInfo(INTEGER_ALIGN, BitWidth, true, 0); |
| 583 | } |
| 584 | |
| 585 | |
Micah Villmow | 99b1148 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 586 | unsigned DataLayout::getCallFrameTypeAlignment(Type *Ty) const { |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 587 | for (unsigned i = 0, e = Alignments.size(); i != e; ++i) |
| 588 | if (Alignments[i].AlignType == STACK_ALIGN) |
| 589 | return Alignments[i].ABIAlign; |
| 590 | |
| 591 | return getABITypeAlignment(Ty); |
| 592 | } |
| 593 | |
Micah Villmow | 99b1148 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 594 | unsigned DataLayout::getPrefTypeAlignment(Type *Ty) const { |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 595 | return getAlignment(Ty, false); |
| 596 | } |
| 597 | |
Micah Villmow | 99b1148 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 598 | unsigned DataLayout::getPreferredTypeAlignmentShift(Type *Ty) const { |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 599 | unsigned Align = getPrefTypeAlignment(Ty); |
| 600 | assert(!(Align & (Align-1)) && "Alignment is not a power of two!"); |
| 601 | return Log2_32(Align); |
| 602 | } |
| 603 | |
Duncan Sands | 7ed4f94 | 2012-10-29 17:31:46 +0000 | [diff] [blame] | 604 | /// getIntPtrType - Return an integer type with size at least as big as that |
| 605 | /// of a pointer in the given address space. |
Micah Villmow | 7d66146 | 2012-10-09 16:06:12 +0000 | [diff] [blame] | 606 | IntegerType *DataLayout::getIntPtrType(LLVMContext &C, |
| 607 | unsigned AddressSpace) const { |
| 608 | return IntegerType::get(C, getPointerSizeInBits(AddressSpace)); |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Duncan Sands | 7ed4f94 | 2012-10-29 17:31:46 +0000 | [diff] [blame] | 611 | /// getIntPtrType - Return an integer (vector of integer) type with size at |
| 612 | /// least as big as that of a pointer of the given pointer (vector of pointer) |
| 613 | /// type. |
| 614 | Type *DataLayout::getIntPtrType(Type *Ty) const { |
Chandler Carruth | ece6c6b | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 615 | assert(Ty->isPtrOrPtrVectorTy() && |
| 616 | "Expected a pointer or pointer vector type."); |
Chandler Carruth | ece6c6b | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 617 | unsigned NumBits = getTypeSizeInBits(Ty->getScalarType()); |
Duncan Sands | 7ed4f94 | 2012-10-29 17:31:46 +0000 | [diff] [blame] | 618 | IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits); |
| 619 | if (VectorType *VecTy = dyn_cast<VectorType>(Ty)) |
| 620 | return VectorType::get(IntTy, VecTy->getNumElements()); |
| 621 | return IntTy; |
Micah Villmow | aa76e9e | 2012-10-24 15:52:52 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Arnaud A. de Grandmaison | 2be921a | 2013-03-22 08:25:01 +0000 | [diff] [blame] | 624 | Type *DataLayout::getSmallestLegalIntType(LLVMContext &C, unsigned Width) const { |
| 625 | for (unsigned i = 0, e = (unsigned)LegalIntWidths.size(); i != e; ++i) |
| 626 | if (Width <= LegalIntWidths[i]) |
| 627 | return Type::getIntNTy(C, LegalIntWidths[i]); |
| 628 | return 0; |
| 629 | } |
| 630 | |
Micah Villmow | 99b1148 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 631 | uint64_t DataLayout::getIndexedOffset(Type *ptrTy, |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 632 | ArrayRef<Value *> Indices) const { |
| 633 | Type *Ty = ptrTy; |
| 634 | assert(Ty->isPointerTy() && "Illegal argument for getIndexedOffset()"); |
| 635 | uint64_t Result = 0; |
| 636 | |
| 637 | generic_gep_type_iterator<Value* const*> |
| 638 | TI = gep_type_begin(ptrTy, Indices); |
| 639 | for (unsigned CurIDX = 0, EndIDX = Indices.size(); CurIDX != EndIDX; |
| 640 | ++CurIDX, ++TI) { |
| 641 | if (StructType *STy = dyn_cast<StructType>(*TI)) { |
| 642 | assert(Indices[CurIDX]->getType() == |
| 643 | Type::getInt32Ty(ptrTy->getContext()) && |
| 644 | "Illegal struct idx"); |
| 645 | unsigned FieldNo = cast<ConstantInt>(Indices[CurIDX])->getZExtValue(); |
| 646 | |
| 647 | // Get structure layout information... |
| 648 | const StructLayout *Layout = getStructLayout(STy); |
| 649 | |
| 650 | // Add in the offset, as calculated by the structure layout info... |
| 651 | Result += Layout->getElementOffset(FieldNo); |
| 652 | |
| 653 | // Update Ty to refer to current element |
| 654 | Ty = STy->getElementType(FieldNo); |
| 655 | } else { |
| 656 | // Update Ty to refer to current element |
| 657 | Ty = cast<SequentialType>(Ty)->getElementType(); |
| 658 | |
| 659 | // Get the array index and the size of each array element. |
| 660 | if (int64_t arrayIdx = cast<ConstantInt>(Indices[CurIDX])->getSExtValue()) |
| 661 | Result += (uint64_t)arrayIdx * getTypeAllocSize(Ty); |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | return Result; |
| 666 | } |
| 667 | |
| 668 | /// getPreferredAlignment - Return the preferred alignment of the specified |
| 669 | /// global. This includes an explicitly requested alignment (if the global |
| 670 | /// has one). |
Micah Villmow | 99b1148 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 671 | unsigned DataLayout::getPreferredAlignment(const GlobalVariable *GV) const { |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 672 | Type *ElemType = GV->getType()->getElementType(); |
| 673 | unsigned Alignment = getPrefTypeAlignment(ElemType); |
| 674 | unsigned GVAlignment = GV->getAlignment(); |
| 675 | if (GVAlignment >= Alignment) { |
| 676 | Alignment = GVAlignment; |
| 677 | } else if (GVAlignment != 0) { |
| 678 | Alignment = std::max(GVAlignment, getABITypeAlignment(ElemType)); |
| 679 | } |
| 680 | |
| 681 | if (GV->hasInitializer() && GVAlignment == 0) { |
| 682 | if (Alignment < 16) { |
| 683 | // If the global is not external, see if it is large. If so, give it a |
| 684 | // larger alignment. |
| 685 | if (getTypeSizeInBits(ElemType) > 128) |
| 686 | Alignment = 16; // 16-byte alignment. |
| 687 | } |
| 688 | } |
| 689 | return Alignment; |
| 690 | } |
| 691 | |
| 692 | /// getPreferredAlignmentLog - Return the preferred alignment of the |
| 693 | /// specified global, returned in log form. This includes an explicitly |
| 694 | /// requested alignment (if the global has one). |
Micah Villmow | 99b1148 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 695 | unsigned DataLayout::getPreferredAlignmentLog(const GlobalVariable *GV) const { |
Micah Villmow | e18c2ae | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 696 | return Log2_32(getPreferredAlignment(GV)); |
| 697 | } |