/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/IR/DataLayout.h
Line | Count | Source (jump to first uncovered line) |
1 | | //===- llvm/DataLayout.h - Data size & alignment info -----------*- C++ -*-===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This file defines layout properties related to datatype size/offset/alignment |
10 | | // information. It uses lazy annotations to cache information about how |
11 | | // structure types are laid out and used. |
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 | | |
19 | | #ifndef LLVM_IR_DATALAYOUT_H |
20 | | #define LLVM_IR_DATALAYOUT_H |
21 | | |
22 | | #include "llvm/ADT/ArrayRef.h" |
23 | | #include "llvm/ADT/STLExtras.h" |
24 | | #include "llvm/ADT/SmallVector.h" |
25 | | #include "llvm/ADT/StringRef.h" |
26 | | #include "llvm/IR/DerivedTypes.h" |
27 | | #include "llvm/IR/Type.h" |
28 | | #include "llvm/Pass.h" |
29 | | #include "llvm/Support/Casting.h" |
30 | | #include "llvm/Support/ErrorHandling.h" |
31 | | #include "llvm/Support/MathExtras.h" |
32 | | #include <cassert> |
33 | | #include <cstdint> |
34 | | #include <string> |
35 | | |
36 | | // This needs to be outside of the namespace, to avoid conflict with llvm-c |
37 | | // decl. |
38 | | using LLVMTargetDataRef = struct LLVMOpaqueTargetData *; |
39 | | |
40 | | namespace llvm { |
41 | | |
42 | | class GlobalVariable; |
43 | | class LLVMContext; |
44 | | class Module; |
45 | | class StructLayout; |
46 | | class Triple; |
47 | | class Value; |
48 | | |
49 | | /// Enum used to categorize the alignment types stored by LayoutAlignElem |
50 | | enum AlignTypeEnum { |
51 | | INVALID_ALIGN = 0, |
52 | | INTEGER_ALIGN = 'i', |
53 | | VECTOR_ALIGN = 'v', |
54 | | FLOAT_ALIGN = 'f', |
55 | | AGGREGATE_ALIGN = 'a' |
56 | | }; |
57 | | |
58 | | // FIXME: Currently the DataLayout string carries a "preferred alignment" |
59 | | // for types. As the DataLayout is module/global, this should likely be |
60 | | // sunk down to an FTTI element that is queried rather than a global |
61 | | // preference. |
62 | | |
63 | | /// Layout alignment element. |
64 | | /// |
65 | | /// Stores the alignment data associated with a given alignment type (integer, |
66 | | /// vector, float) and type bit width. |
67 | | /// |
68 | | /// \note The unusual order of elements in the structure attempts to reduce |
69 | | /// padding and make the structure slightly more cache friendly. |
70 | | struct LayoutAlignElem { |
71 | | /// Alignment type from \c AlignTypeEnum |
72 | | unsigned AlignType : 8; |
73 | | unsigned TypeBitWidth : 24; |
74 | | unsigned ABIAlign : 16; |
75 | | unsigned PrefAlign : 16; |
76 | | |
77 | | static LayoutAlignElem get(AlignTypeEnum align_type, unsigned abi_align, |
78 | | unsigned pref_align, uint32_t bit_width); |
79 | | |
80 | | bool operator==(const LayoutAlignElem &rhs) const; |
81 | | }; |
82 | | |
83 | | /// Layout pointer alignment element. |
84 | | /// |
85 | | /// Stores the alignment data associated with a given pointer and address space. |
86 | | /// |
87 | | /// \note The unusual order of elements in the structure attempts to reduce |
88 | | /// padding and make the structure slightly more cache friendly. |
89 | | struct PointerAlignElem { |
90 | | unsigned ABIAlign; |
91 | | unsigned PrefAlign; |
92 | | uint32_t TypeByteWidth; |
93 | | uint32_t AddressSpace; |
94 | | uint32_t IndexWidth; |
95 | | |
96 | | /// Initializer |
97 | | static PointerAlignElem get(uint32_t AddressSpace, unsigned ABIAlign, |
98 | | unsigned PrefAlign, uint32_t TypeByteWidth, |
99 | | uint32_t IndexWidth); |
100 | | |
101 | | bool operator==(const PointerAlignElem &rhs) const; |
102 | | }; |
103 | | |
104 | | /// A parsed version of the target data layout string in and methods for |
105 | | /// querying it. |
106 | | /// |
107 | | /// The target data layout string is specified *by the target* - a frontend |
108 | | /// generating LLVM IR is required to generate the right target data for the |
109 | | /// target being codegen'd to. |
110 | | class DataLayout { |
111 | | public: |
112 | | enum class FunctionPtrAlignType { |
113 | | /// The function pointer alignment is independent of the function alignment. |
114 | | Independent, |
115 | | /// The function pointer alignment is a multiple of the function alignment. |
116 | | MultipleOfFunctionAlign, |
117 | | }; |
118 | | private: |
119 | | /// Defaults to false. |
120 | | bool BigEndian; |
121 | | |
122 | | unsigned AllocaAddrSpace; |
123 | | unsigned StackNaturalAlign; |
124 | | unsigned ProgramAddrSpace; |
125 | | |
126 | | unsigned FunctionPtrAlign; |
127 | | FunctionPtrAlignType TheFunctionPtrAlignType; |
128 | | |
129 | | enum ManglingModeT { |
130 | | MM_None, |
131 | | MM_ELF, |
132 | | MM_MachO, |
133 | | MM_WinCOFF, |
134 | | MM_WinCOFFX86, |
135 | | MM_Mips |
136 | | }; |
137 | | ManglingModeT ManglingMode; |
138 | | |
139 | | SmallVector<unsigned char, 8> LegalIntWidths; |
140 | | |
141 | | /// Primitive type alignment data. This is sorted by type and bit |
142 | | /// width during construction. |
143 | | using AlignmentsTy = SmallVector<LayoutAlignElem, 16>; |
144 | | AlignmentsTy Alignments; |
145 | | |
146 | | AlignmentsTy::const_iterator |
147 | 304M | findAlignmentLowerBound(AlignTypeEnum AlignType, uint32_t BitWidth) const { |
148 | 304M | return const_cast<DataLayout *>(this)->findAlignmentLowerBound(AlignType, |
149 | 304M | BitWidth); |
150 | 304M | } |
151 | | |
152 | | AlignmentsTy::iterator |
153 | | findAlignmentLowerBound(AlignTypeEnum AlignType, uint32_t BitWidth); |
154 | | |
155 | | /// The string representation used to create this DataLayout |
156 | | std::string StringRepresentation; |
157 | | |
158 | | using PointersTy = SmallVector<PointerAlignElem, 8>; |
159 | | PointersTy Pointers; |
160 | | |
161 | | PointersTy::const_iterator |
162 | 697M | findPointerLowerBound(uint32_t AddressSpace) const { |
163 | 697M | return const_cast<DataLayout *>(this)->findPointerLowerBound(AddressSpace); |
164 | 697M | } |
165 | | |
166 | | PointersTy::iterator findPointerLowerBound(uint32_t AddressSpace); |
167 | | |
168 | | // The StructType -> StructLayout map. |
169 | | mutable void *LayoutMap = nullptr; |
170 | | |
171 | | /// Pointers in these address spaces are non-integral, and don't have a |
172 | | /// well-defined bitwise representation. |
173 | | SmallVector<unsigned, 8> NonIntegralAddressSpaces; |
174 | | |
175 | | void setAlignment(AlignTypeEnum align_type, unsigned abi_align, |
176 | | unsigned pref_align, uint32_t bit_width); |
177 | | unsigned getAlignmentInfo(AlignTypeEnum align_type, uint32_t bit_width, |
178 | | bool ABIAlign, Type *Ty) const; |
179 | | void setPointerAlignment(uint32_t AddrSpace, unsigned ABIAlign, |
180 | | unsigned PrefAlign, uint32_t TypeByteWidth, |
181 | | uint32_t IndexWidth); |
182 | | |
183 | | /// Internal helper method that returns requested alignment for type. |
184 | | unsigned getAlignment(Type *Ty, bool abi_or_pref) const; |
185 | | |
186 | | /// Parses a target data specification string. Assert if the string is |
187 | | /// malformed. |
188 | | void parseSpecifier(StringRef LayoutDescription); |
189 | | |
190 | | // Free all internal data structures. |
191 | | void clear(); |
192 | | |
193 | | public: |
194 | | /// Constructs a DataLayout from a specification string. See reset(). |
195 | 199k | explicit DataLayout(StringRef LayoutDescription) { |
196 | 199k | reset(LayoutDescription); |
197 | 199k | } |
198 | | |
199 | | /// Initialize target data from properties stored in the module. |
200 | | explicit DataLayout(const Module *M); |
201 | | |
202 | 1.62M | DataLayout(const DataLayout &DL) { *this = DL; } |
203 | | |
204 | | ~DataLayout(); // Not virtual, do not subclass this class |
205 | | |
206 | 1.70M | DataLayout &operator=(const DataLayout &DL) { |
207 | 1.70M | clear(); |
208 | 1.70M | StringRepresentation = DL.StringRepresentation; |
209 | 1.70M | BigEndian = DL.isBigEndian(); |
210 | 1.70M | AllocaAddrSpace = DL.AllocaAddrSpace; |
211 | 1.70M | StackNaturalAlign = DL.StackNaturalAlign; |
212 | 1.70M | FunctionPtrAlign = DL.FunctionPtrAlign; |
213 | 1.70M | TheFunctionPtrAlignType = DL.TheFunctionPtrAlignType; |
214 | 1.70M | ProgramAddrSpace = DL.ProgramAddrSpace; |
215 | 1.70M | ManglingMode = DL.ManglingMode; |
216 | 1.70M | LegalIntWidths = DL.LegalIntWidths; |
217 | 1.70M | Alignments = DL.Alignments; |
218 | 1.70M | Pointers = DL.Pointers; |
219 | 1.70M | NonIntegralAddressSpaces = DL.NonIntegralAddressSpaces; |
220 | 1.70M | return *this; |
221 | 1.70M | } |
222 | | |
223 | | bool operator==(const DataLayout &Other) const; |
224 | 1.09k | bool operator!=(const DataLayout &Other) const { return !(*this == Other); } |
225 | | |
226 | | void init(const Module *M); |
227 | | |
228 | | /// Parse a data layout string (with fallback to default values). |
229 | | void reset(StringRef LayoutDescription); |
230 | | |
231 | | /// Layout endianness... |
232 | 11.5M | bool isLittleEndian() const { return !BigEndian; } |
233 | 6.41M | bool isBigEndian() const { return BigEndian; } |
234 | | |
235 | | /// Returns the string representation of the DataLayout. |
236 | | /// |
237 | | /// This representation is in the same format accepted by the string |
238 | | /// constructor above. This should not be used to compare two DataLayout as |
239 | | /// different string can represent the same layout. |
240 | 72.6k | const std::string &getStringRepresentation() const { |
241 | 72.6k | return StringRepresentation; |
242 | 72.6k | } |
243 | | |
244 | | /// Test if the DataLayout was constructed from an empty string. |
245 | 1.47k | bool isDefault() const { return StringRepresentation.empty(); } |
246 | | |
247 | | /// Returns true if the specified type is known to be a native integer |
248 | | /// type supported by the CPU. |
249 | | /// |
250 | | /// For example, i64 is not native on most 32-bit CPUs and i37 is not native |
251 | | /// on any known one. This returns false if the integer width is not legal. |
252 | | /// |
253 | | /// The width is specified in bits. |
254 | 29.5M | bool isLegalInteger(uint64_t Width) const { |
255 | 29.5M | for (unsigned LegalIntWidth : LegalIntWidths) |
256 | 52.9M | if (LegalIntWidth == Width) |
257 | 26.6M | return true; |
258 | 29.5M | return false2.87M ; |
259 | 29.5M | } |
260 | | |
261 | 1.39k | bool isIllegalInteger(uint64_t Width) const { return !isLegalInteger(Width); } |
262 | | |
263 | | /// Returns true if the given alignment exceeds the natural stack alignment. |
264 | 4.67k | bool exceedsNaturalStackAlignment(unsigned Align) const { |
265 | 4.67k | return (StackNaturalAlign != 0) && (Align > StackNaturalAlign)4.64k ; |
266 | 4.67k | } |
267 | | |
268 | 8.17k | unsigned getStackAlignment() const { return StackNaturalAlign; } |
269 | 5.04M | unsigned getAllocaAddrSpace() const { return AllocaAddrSpace; } |
270 | | |
271 | | /// Returns the alignment of function pointers, which may or may not be |
272 | | /// related to the alignment of functions. |
273 | | /// \see getFunctionPtrAlignType |
274 | 2.67k | unsigned getFunctionPtrAlign() const { return FunctionPtrAlign; } |
275 | | |
276 | | /// Return the type of function pointer alignment. |
277 | | /// \see getFunctionPtrAlign |
278 | 2.67k | FunctionPtrAlignType getFunctionPtrAlignType() const { |
279 | 2.67k | return TheFunctionPtrAlignType; |
280 | 2.67k | } |
281 | | |
282 | 2.86M | unsigned getProgramAddressSpace() const { return ProgramAddrSpace; } |
283 | | |
284 | 4.27M | bool hasMicrosoftFastStdCallMangling() const { |
285 | 4.27M | return ManglingMode == MM_WinCOFFX86; |
286 | 4.27M | } |
287 | | |
288 | | /// Returns true if symbols with leading question marks should not receive IR |
289 | | /// mangling. True for Windows mangling modes. |
290 | 8.60M | bool doNotMangleLeadingQuestionMark() const { |
291 | 8.60M | return ManglingMode == MM_WinCOFF || ManglingMode == MM_WinCOFFX868.59M ; |
292 | 8.60M | } |
293 | | |
294 | 126 | bool hasLinkerPrivateGlobalPrefix() const { return ManglingMode == MM_MachO; } |
295 | | |
296 | 1.62M | StringRef getLinkerPrivateGlobalPrefix() const { |
297 | 1.62M | if (ManglingMode == MM_MachO) |
298 | 1.62M | return "l"; |
299 | 944 | return ""; |
300 | 944 | } |
301 | | |
302 | 4.39M | char getGlobalPrefix() const { |
303 | 4.39M | switch (ManglingMode) { |
304 | 4.39M | case MM_None: |
305 | 362k | case MM_ELF: |
306 | 362k | case MM_Mips: |
307 | 362k | case MM_WinCOFF: |
308 | 362k | return '\0'; |
309 | 4.03M | case MM_MachO: |
310 | 4.03M | case MM_WinCOFFX86: |
311 | 4.03M | return '_'; |
312 | 0 | } |
313 | 0 | llvm_unreachable("invalid mangling mode"); |
314 | 0 | } |
315 | | |
316 | 371k | StringRef getPrivateGlobalPrefix() const { |
317 | 371k | switch (ManglingMode) { |
318 | 371k | case MM_None: |
319 | 47 | return ""; |
320 | 371k | case MM_ELF: |
321 | 84.0k | case MM_WinCOFF: |
322 | 84.0k | return ".L"; |
323 | 84.0k | case MM_Mips: |
324 | 1.69k | return "$"; |
325 | 285k | case MM_MachO: |
326 | 285k | case MM_WinCOFFX86: |
327 | 285k | return "L"; |
328 | 0 | } |
329 | 0 | llvm_unreachable("invalid mangling mode"); |
330 | 0 | } |
331 | | |
332 | | static const char *getManglingComponent(const Triple &T); |
333 | | |
334 | | /// Returns true if the specified type fits in a native integer type |
335 | | /// supported by the CPU. |
336 | | /// |
337 | | /// For example, if the CPU only supports i32 as a native integer type, then |
338 | | /// i27 fits in a legal integer type but i45 does not. |
339 | 272k | bool fitsInLegalInteger(unsigned Width) const { |
340 | 272k | for (unsigned LegalIntWidth : LegalIntWidths) |
341 | 306k | if (Width <= LegalIntWidth) |
342 | 271k | return true; |
343 | 272k | return false1.12k ; |
344 | 272k | } |
345 | | |
346 | | /// Layout pointer alignment |
347 | | unsigned getPointerABIAlignment(unsigned AS) const; |
348 | | |
349 | | /// Return target's alignment for stack-based pointers |
350 | | /// FIXME: The defaults need to be removed once all of |
351 | | /// the backends/clients are updated. |
352 | | unsigned getPointerPrefAlignment(unsigned AS = 0) const; |
353 | | |
354 | | /// Layout pointer size |
355 | | /// FIXME: The defaults need to be removed once all of |
356 | | /// the backends/clients are updated. |
357 | | unsigned getPointerSize(unsigned AS = 0) const; |
358 | | |
359 | | /// Returns the maximum pointer size over all address spaces. |
360 | | unsigned getMaxPointerSize() const; |
361 | | |
362 | | // Index size used for address calculation. |
363 | | unsigned getIndexSize(unsigned AS) const; |
364 | | |
365 | | /// Return the address spaces containing non-integral pointers. Pointers in |
366 | | /// this address space don't have a well-defined bitwise representation. |
367 | 8.62M | ArrayRef<unsigned> getNonIntegralAddressSpaces() const { |
368 | 8.62M | return NonIntegralAddressSpaces; |
369 | 8.62M | } |
370 | | |
371 | 8.62M | bool isNonIntegralAddressSpace(unsigned AddrSpace) const { |
372 | 8.62M | ArrayRef<unsigned> NonIntegralSpaces = getNonIntegralAddressSpaces(); |
373 | 8.62M | return find(NonIntegralSpaces, AddrSpace) != NonIntegralSpaces.end(); |
374 | 8.62M | } |
375 | | |
376 | 8.62M | bool isNonIntegralPointerType(PointerType *PT) const { |
377 | 8.62M | return isNonIntegralAddressSpace(PT->getAddressSpace()); |
378 | 8.62M | } |
379 | | |
380 | 10.6M | bool isNonIntegralPointerType(Type *Ty) const { |
381 | 10.6M | auto *PTy = dyn_cast<PointerType>(Ty); |
382 | 10.6M | return PTy && isNonIntegralPointerType(PTy)8.55M ; |
383 | 10.6M | } |
384 | | |
385 | | /// Layout pointer size, in bits |
386 | | /// FIXME: The defaults need to be removed once all of |
387 | | /// the backends/clients are updated. |
388 | 242M | unsigned getPointerSizeInBits(unsigned AS = 0) const { |
389 | 242M | return getPointerSize(AS) * 8; |
390 | 242M | } |
391 | | |
392 | | /// Returns the maximum pointer size over all address spaces. |
393 | 109M | unsigned getMaxPointerSizeInBits() const { |
394 | 109M | return getMaxPointerSize() * 8; |
395 | 109M | } |
396 | | |
397 | | /// Size in bits of index used for address calculation in getelementptr. |
398 | 412M | unsigned getIndexSizeInBits(unsigned AS) const { |
399 | 412M | return getIndexSize(AS) * 8; |
400 | 412M | } |
401 | | |
402 | | /// Layout pointer size, in bits, based on the type. If this function is |
403 | | /// called with a pointer type, then the type size of the pointer is returned. |
404 | | /// If this function is called with a vector of pointers, then the type size |
405 | | /// of the pointer is returned. This should only be called with a pointer or |
406 | | /// vector of pointers. |
407 | | unsigned getPointerTypeSizeInBits(Type *) const; |
408 | | |
409 | | /// Layout size of the index used in GEP calculation. |
410 | | /// The function should be called with pointer or vector of pointers type. |
411 | | unsigned getIndexTypeSizeInBits(Type *Ty) const; |
412 | | |
413 | 17.3k | unsigned getPointerTypeSize(Type *Ty) const { |
414 | 17.3k | return getPointerTypeSizeInBits(Ty) / 8; |
415 | 17.3k | } |
416 | | |
417 | | /// Size examples: |
418 | | /// |
419 | | /// Type SizeInBits StoreSizeInBits AllocSizeInBits[*] |
420 | | /// ---- ---------- --------------- --------------- |
421 | | /// i1 1 8 8 |
422 | | /// i8 8 8 8 |
423 | | /// i19 19 24 32 |
424 | | /// i32 32 32 32 |
425 | | /// i100 100 104 128 |
426 | | /// i128 128 128 128 |
427 | | /// Float 32 32 32 |
428 | | /// Double 64 64 64 |
429 | | /// X86_FP80 80 80 96 |
430 | | /// |
431 | | /// [*] The alloc size depends on the alignment, and thus on the target. |
432 | | /// These values are for x86-32 linux. |
433 | | |
434 | | /// Returns the number of bits necessary to hold the specified type. |
435 | | /// |
436 | | /// For example, returns 36 for i36 and 80 for x86_fp80. The type passed must |
437 | | /// have a size (Type::isSized() must return true). |
438 | | uint64_t getTypeSizeInBits(Type *Ty) const; |
439 | | |
440 | | /// Returns the maximum number of bytes that may be overwritten by |
441 | | /// storing the specified type. |
442 | | /// |
443 | | /// For example, returns 5 for i36 and 10 for x86_fp80. |
444 | 436M | uint64_t getTypeStoreSize(Type *Ty) const { |
445 | 436M | return (getTypeSizeInBits(Ty) + 7) / 8; |
446 | 436M | } |
447 | | |
448 | | /// Returns the maximum number of bits that may be overwritten by |
449 | | /// storing the specified type; always a multiple of 8. |
450 | | /// |
451 | | /// For example, returns 40 for i36 and 80 for x86_fp80. |
452 | 27.8M | uint64_t getTypeStoreSizeInBits(Type *Ty) const { |
453 | 27.8M | return 8 * getTypeStoreSize(Ty); |
454 | 27.8M | } |
455 | | |
456 | | /// Returns true if no extra padding bits are needed when storing the |
457 | | /// specified type. |
458 | | /// |
459 | | /// For example, returns false for i19 that has a 24-bit store size. |
460 | 15.6M | bool typeSizeEqualsStoreSize(Type *Ty) const { |
461 | 15.6M | return getTypeSizeInBits(Ty) == getTypeStoreSizeInBits(Ty); |
462 | 15.6M | } |
463 | | |
464 | | /// Returns the offset in bytes between successive objects of the |
465 | | /// specified type, including alignment padding. |
466 | | /// |
467 | | /// This is the amount that alloca reserves for this type. For example, |
468 | | /// returns 12 or 16 for x86_fp80, depending on alignment. |
469 | 310M | uint64_t getTypeAllocSize(Type *Ty) const { |
470 | 310M | // Round up to the next alignment boundary. |
471 | 310M | return alignTo(getTypeStoreSize(Ty), getABITypeAlignment(Ty)); |
472 | 310M | } |
473 | | |
474 | | /// Returns the offset in bits between successive objects of the |
475 | | /// specified type, including alignment padding; always a multiple of 8. |
476 | | /// |
477 | | /// This is the amount that alloca reserves for this type. For example, |
478 | | /// returns 96 or 128 for x86_fp80, depending on alignment. |
479 | 36.4M | uint64_t getTypeAllocSizeInBits(Type *Ty) const { |
480 | 36.4M | return 8 * getTypeAllocSize(Ty); |
481 | 36.4M | } |
482 | | |
483 | | /// Returns the minimum ABI-required alignment for the specified type. |
484 | | unsigned getABITypeAlignment(Type *Ty) const; |
485 | | |
486 | | /// Returns the minimum ABI-required alignment for an integer type of |
487 | | /// the specified bitwidth. |
488 | | unsigned getABIIntegerTypeAlignment(unsigned BitWidth) const; |
489 | | |
490 | | /// Returns the preferred stack/global alignment for the specified |
491 | | /// type. |
492 | | /// |
493 | | /// This is always at least as good as the ABI alignment. |
494 | | unsigned getPrefTypeAlignment(Type *Ty) const; |
495 | | |
496 | | /// Returns the preferred alignment for the specified type, returned as |
497 | | /// log2 of the value (a shift amount). |
498 | | unsigned getPreferredTypeAlignmentShift(Type *Ty) const; |
499 | | |
500 | | /// Returns an integer type with size at least as big as that of a |
501 | | /// pointer in the given address space. |
502 | | IntegerType *getIntPtrType(LLVMContext &C, unsigned AddressSpace = 0) const; |
503 | | |
504 | | /// Returns an integer (vector of integer) type with size at least as |
505 | | /// big as that of a pointer of the given pointer (vector of pointer) type. |
506 | | Type *getIntPtrType(Type *) const; |
507 | | |
508 | | /// Returns the smallest integer type with size at least as big as |
509 | | /// Width bits. |
510 | | Type *getSmallestLegalIntType(LLVMContext &C, unsigned Width = 0) const; |
511 | | |
512 | | /// Returns the largest legal integer type, or null if none are set. |
513 | 0 | Type *getLargestLegalIntType(LLVMContext &C) const { |
514 | 0 | unsigned LargestSize = getLargestLegalIntTypeSizeInBits(); |
515 | 0 | return (LargestSize == 0) ? nullptr : Type::getIntNTy(C, LargestSize); |
516 | 0 | } |
517 | | |
518 | | /// Returns the size of largest legal integer type size, or 0 if none |
519 | | /// are set. |
520 | | unsigned getLargestLegalIntTypeSizeInBits() const; |
521 | | |
522 | | /// Returns the type of a GEP index. |
523 | | /// If it was not specified explicitly, it will be the integer type of the |
524 | | /// pointer width - IntPtrType. |
525 | | Type *getIndexType(Type *PtrTy) const; |
526 | | |
527 | | /// Returns the offset from the beginning of the type for the specified |
528 | | /// indices. |
529 | | /// |
530 | | /// Note that this takes the element type, not the pointer type. |
531 | | /// This is used to implement getelementptr. |
532 | | int64_t getIndexedOffsetInType(Type *ElemTy, ArrayRef<Value *> Indices) const; |
533 | | |
534 | | /// Returns a StructLayout object, indicating the alignment of the |
535 | | /// struct, its size, and the offsets of its fields. |
536 | | /// |
537 | | /// Note that this information is lazily cached. |
538 | | const StructLayout *getStructLayout(StructType *Ty) const; |
539 | | |
540 | | /// Returns the preferred alignment of the specified global. |
541 | | /// |
542 | | /// This includes an explicitly requested alignment (if the global has one). |
543 | | unsigned getPreferredAlignment(const GlobalVariable *GV) const; |
544 | | |
545 | | /// Returns the preferred alignment of the specified global, returned |
546 | | /// in log form. |
547 | | /// |
548 | | /// This includes an explicitly requested alignment (if the global has one). |
549 | | unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const; |
550 | | }; |
551 | | |
552 | 5 | inline DataLayout *unwrap(LLVMTargetDataRef P) { |
553 | 5 | return reinterpret_cast<DataLayout *>(P); |
554 | 5 | } |
555 | | |
556 | 5 | inline LLVMTargetDataRef wrap(const DataLayout *P) { |
557 | 5 | return reinterpret_cast<LLVMTargetDataRef>(const_cast<DataLayout *>(P)); |
558 | 5 | } |
559 | | |
560 | | /// Used to lazily calculate structure layout information for a target machine, |
561 | | /// based on the DataLayout structure. |
562 | | class StructLayout { |
563 | | uint64_t StructSize; |
564 | | unsigned StructAlignment; |
565 | | unsigned IsPadded : 1; |
566 | | unsigned NumElements : 31; |
567 | | uint64_t MemberOffsets[1]; // variable sized array! |
568 | | |
569 | | public: |
570 | 8.50M | uint64_t getSizeInBytes() const { return StructSize; } |
571 | | |
572 | 93.6M | uint64_t getSizeInBits() const { return 8 * StructSize; } |
573 | | |
574 | 80.3M | unsigned getAlignment() const { return StructAlignment; } |
575 | | |
576 | | /// Returns whether the struct has padding or not between its fields. |
577 | | /// NB: Padding in nested element is not taken into account. |
578 | 50 | bool hasPadding() const { return IsPadded; } |
579 | | |
580 | | /// Given a valid byte offset into the structure, returns the structure |
581 | | /// index that contains it. |
582 | | unsigned getElementContainingOffset(uint64_t Offset) const; |
583 | | |
584 | 108M | uint64_t getElementOffset(unsigned Idx) const { |
585 | 108M | assert(Idx < NumElements && "Invalid element idx!"); |
586 | 108M | return MemberOffsets[Idx]; |
587 | 108M | } |
588 | | |
589 | 623 | uint64_t getElementOffsetInBits(unsigned Idx) const { |
590 | 623 | return getElementOffset(Idx) * 8; |
591 | 623 | } |
592 | | |
593 | | private: |
594 | | friend class DataLayout; // Only DataLayout can create this class |
595 | | |
596 | | StructLayout(StructType *ST, const DataLayout &DL); |
597 | | }; |
598 | | |
599 | | // The implementation of this method is provided inline as it is particularly |
600 | | // well suited to constant folding when called on a specific Type subclass. |
601 | 1.66G | inline uint64_t DataLayout::getTypeSizeInBits(Type *Ty) const { |
602 | 1.66G | assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!"); |
603 | 1.66G | switch (Ty->getTypeID()) { |
604 | 1.66G | case Type::LabelTyID: |
605 | 0 | return getPointerSizeInBits(0); |
606 | 1.66G | case Type::PointerTyID: |
607 | 95.1M | return getPointerSizeInBits(Ty->getPointerAddressSpace()); |
608 | 1.66G | case Type::ArrayTyID: { |
609 | 36.1M | ArrayType *ATy = cast<ArrayType>(Ty); |
610 | 36.1M | return ATy->getNumElements() * |
611 | 36.1M | getTypeAllocSizeInBits(ATy->getElementType()); |
612 | 1.66G | } |
613 | 1.66G | case Type::StructTyID: |
614 | 93.6M | // Get the layout annotation... which is lazily created on demand. |
615 | 93.6M | return getStructLayout(cast<StructType>(Ty))->getSizeInBits(); |
616 | 1.66G | case Type::IntegerTyID: |
617 | 1.25G | return Ty->getIntegerBitWidth(); |
618 | 1.66G | case Type::HalfTyID: |
619 | 102k | return 16; |
620 | 1.66G | case Type::FloatTyID: |
621 | 41.0M | return 32; |
622 | 1.66G | case Type::DoubleTyID: |
623 | 100M | case Type::X86_MMXTyID: |
624 | 100M | return 64; |
625 | 100M | case Type::PPC_FP128TyID: |
626 | 41.4k | case Type::FP128TyID: |
627 | 41.4k | return 128; |
628 | 41.4k | // In memory objects this is always aligned to a higher boundary, but |
629 | 41.4k | // only 80 bits contain information. |
630 | 46.7k | case Type::X86_FP80TyID: |
631 | 46.7k | return 80; |
632 | 47.6M | case Type::VectorTyID: { |
633 | 47.6M | VectorType *VTy = cast<VectorType>(Ty); |
634 | 47.6M | return VTy->getNumElements() * getTypeSizeInBits(VTy->getElementType()); |
635 | 41.4k | } |
636 | 41.4k | default: |
637 | 0 | llvm_unreachable("DataLayout::getTypeSizeInBits(): Unsupported type"); |
638 | 1.66G | } |
639 | 1.66G | } |
640 | | |
641 | | } // end namespace llvm |
642 | | |
643 | | #endif // LLVM_IR_DATALAYOUT_H |