blob: ac9770a15120d929be94d72462acd0d00aef5441 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- llvm/DataLayout.h - Data size & alignment info -----------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
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.
38using LLVMTargetDataRef = struct LLVMOpaqueTargetData *;
39
40namespace llvm {
41
42class GlobalVariable;
43class LLVMContext;
44class Module;
45class StructLayout;
46class Triple;
47class Value;
48
49/// Enum used to categorize the alignment types stored by LayoutAlignElem
50enum 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
Andrew Scullcdfcccc2018-10-05 20:58:37 +010063/// Layout alignment element.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010064///
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.
70struct LayoutAlignElem {
Andrew Scullcdfcccc2018-10-05 20:58:37 +010071 /// Alignment type from \c AlignTypeEnum
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010072 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
Andrew Scullcdfcccc2018-10-05 20:58:37 +010083/// Layout pointer alignment element.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010084///
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.
89struct 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
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100104/// A parsed version of the target data layout string in and methods for
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100105/// 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.
110class DataLayout {
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100111public:
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 };
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100118private:
119 /// Defaults to false.
120 bool BigEndian;
121
122 unsigned AllocaAddrSpace;
123 unsigned StackNaturalAlign;
124 unsigned ProgramAddrSpace;
125
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100126 unsigned FunctionPtrAlign;
127 FunctionPtrAlignType TheFunctionPtrAlignType;
128
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100129 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
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100141 /// Primitive type alignment data. This is sorted by type and bit
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100142 /// width during construction.
143 using AlignmentsTy = SmallVector<LayoutAlignElem, 16>;
144 AlignmentsTy Alignments;
145
146 AlignmentsTy::const_iterator
147 findAlignmentLowerBound(AlignTypeEnum AlignType, uint32_t BitWidth) const {
148 return const_cast<DataLayout *>(this)->findAlignmentLowerBound(AlignType,
149 BitWidth);
150 }
151
152 AlignmentsTy::iterator
153 findAlignmentLowerBound(AlignTypeEnum AlignType, uint32_t BitWidth);
154
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100155 /// The string representation used to create this DataLayout
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100156 std::string StringRepresentation;
157
158 using PointersTy = SmallVector<PointerAlignElem, 8>;
159 PointersTy Pointers;
160
161 PointersTy::const_iterator
162 findPointerLowerBound(uint32_t AddressSpace) const {
163 return const_cast<DataLayout *>(this)->findPointerLowerBound(AddressSpace);
164 }
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
193public:
194 /// Constructs a DataLayout from a specification string. See reset().
195 explicit DataLayout(StringRef LayoutDescription) {
196 reset(LayoutDescription);
197 }
198
199 /// Initialize target data from properties stored in the module.
200 explicit DataLayout(const Module *M);
201
202 DataLayout(const DataLayout &DL) { *this = DL; }
203
204 ~DataLayout(); // Not virtual, do not subclass this class
205
206 DataLayout &operator=(const DataLayout &DL) {
207 clear();
208 StringRepresentation = DL.StringRepresentation;
209 BigEndian = DL.isBigEndian();
210 AllocaAddrSpace = DL.AllocaAddrSpace;
211 StackNaturalAlign = DL.StackNaturalAlign;
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100212 FunctionPtrAlign = DL.FunctionPtrAlign;
213 TheFunctionPtrAlignType = DL.TheFunctionPtrAlignType;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100214 ProgramAddrSpace = DL.ProgramAddrSpace;
215 ManglingMode = DL.ManglingMode;
216 LegalIntWidths = DL.LegalIntWidths;
217 Alignments = DL.Alignments;
218 Pointers = DL.Pointers;
219 NonIntegralAddressSpaces = DL.NonIntegralAddressSpaces;
220 return *this;
221 }
222
223 bool operator==(const DataLayout &Other) const;
224 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 bool isLittleEndian() const { return !BigEndian; }
233 bool isBigEndian() const { return BigEndian; }
234
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100235 /// Returns the string representation of the DataLayout.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100236 ///
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 const std::string &getStringRepresentation() const {
241 return StringRepresentation;
242 }
243
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100244 /// Test if the DataLayout was constructed from an empty string.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100245 bool isDefault() const { return StringRepresentation.empty(); }
246
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100247 /// Returns true if the specified type is known to be a native integer
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100248 /// 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 bool isLegalInteger(uint64_t Width) const {
255 for (unsigned LegalIntWidth : LegalIntWidths)
256 if (LegalIntWidth == Width)
257 return true;
258 return false;
259 }
260
261 bool isIllegalInteger(uint64_t Width) const { return !isLegalInteger(Width); }
262
263 /// Returns true if the given alignment exceeds the natural stack alignment.
264 bool exceedsNaturalStackAlignment(unsigned Align) const {
265 return (StackNaturalAlign != 0) && (Align > StackNaturalAlign);
266 }
267
268 unsigned getStackAlignment() const { return StackNaturalAlign; }
269 unsigned getAllocaAddrSpace() const { return AllocaAddrSpace; }
270
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100271 /// Returns the alignment of function pointers, which may or may not be
272 /// related to the alignment of functions.
273 /// \see getFunctionPtrAlignType
274 unsigned getFunctionPtrAlign() const { return FunctionPtrAlign; }
275
276 /// Return the type of function pointer alignment.
277 /// \see getFunctionPtrAlign
278 FunctionPtrAlignType getFunctionPtrAlignType() const {
279 return TheFunctionPtrAlignType;
280 }
281
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100282 unsigned getProgramAddressSpace() const { return ProgramAddrSpace; }
283
284 bool hasMicrosoftFastStdCallMangling() const {
285 return ManglingMode == MM_WinCOFFX86;
286 }
287
288 /// Returns true if symbols with leading question marks should not receive IR
289 /// mangling. True for Windows mangling modes.
290 bool doNotMangleLeadingQuestionMark() const {
291 return ManglingMode == MM_WinCOFF || ManglingMode == MM_WinCOFFX86;
292 }
293
294 bool hasLinkerPrivateGlobalPrefix() const { return ManglingMode == MM_MachO; }
295
296 StringRef getLinkerPrivateGlobalPrefix() const {
297 if (ManglingMode == MM_MachO)
298 return "l";
299 return "";
300 }
301
302 char getGlobalPrefix() const {
303 switch (ManglingMode) {
304 case MM_None:
305 case MM_ELF:
306 case MM_Mips:
307 case MM_WinCOFF:
308 return '\0';
309 case MM_MachO:
310 case MM_WinCOFFX86:
311 return '_';
312 }
313 llvm_unreachable("invalid mangling mode");
314 }
315
316 StringRef getPrivateGlobalPrefix() const {
317 switch (ManglingMode) {
318 case MM_None:
319 return "";
320 case MM_ELF:
321 case MM_WinCOFF:
322 return ".L";
323 case MM_Mips:
324 return "$";
325 case MM_MachO:
326 case MM_WinCOFFX86:
327 return "L";
328 }
329 llvm_unreachable("invalid mangling mode");
330 }
331
332 static const char *getManglingComponent(const Triple &T);
333
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100334 /// Returns true if the specified type fits in a native integer type
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100335 /// 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 bool fitsInLegalInteger(unsigned Width) const {
340 for (unsigned LegalIntWidth : LegalIntWidths)
341 if (Width <= LegalIntWidth)
342 return true;
343 return false;
344 }
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
Andrew Walbran16937d02019-10-22 13:54:20 +0100359 /// Returns the maximum pointer size over all address spaces.
360 unsigned getMaxPointerSize() const;
361
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100362 // 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 ArrayRef<unsigned> getNonIntegralAddressSpaces() const {
368 return NonIntegralAddressSpaces;
369 }
370
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100371 bool isNonIntegralAddressSpace(unsigned AddrSpace) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100372 ArrayRef<unsigned> NonIntegralSpaces = getNonIntegralAddressSpaces();
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100373 return find(NonIntegralSpaces, AddrSpace) != NonIntegralSpaces.end();
374 }
375
376 bool isNonIntegralPointerType(PointerType *PT) const {
377 return isNonIntegralAddressSpace(PT->getAddressSpace());
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100378 }
379
380 bool isNonIntegralPointerType(Type *Ty) const {
381 auto *PTy = dyn_cast<PointerType>(Ty);
382 return PTy && isNonIntegralPointerType(PTy);
383 }
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 unsigned getPointerSizeInBits(unsigned AS = 0) const {
389 return getPointerSize(AS) * 8;
390 }
391
Andrew Walbran16937d02019-10-22 13:54:20 +0100392 /// Returns the maximum pointer size over all address spaces.
393 unsigned getMaxPointerSizeInBits() const {
394 return getMaxPointerSize() * 8;
395 }
396
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100397 /// Size in bits of index used for address calculation in getelementptr.
398 unsigned getIndexSizeInBits(unsigned AS) const {
399 return getIndexSize(AS) * 8;
400 }
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 unsigned getPointerTypeSize(Type *Ty) const {
414 return getPointerTypeSizeInBits(Ty) / 8;
415 }
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
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100434 /// Returns the number of bits necessary to hold the specified type.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100435 ///
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
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100440 /// Returns the maximum number of bytes that may be overwritten by
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100441 /// storing the specified type.
442 ///
443 /// For example, returns 5 for i36 and 10 for x86_fp80.
444 uint64_t getTypeStoreSize(Type *Ty) const {
445 return (getTypeSizeInBits(Ty) + 7) / 8;
446 }
447
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100448 /// Returns the maximum number of bits that may be overwritten by
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100449 /// storing the specified type; always a multiple of 8.
450 ///
451 /// For example, returns 40 for i36 and 80 for x86_fp80.
452 uint64_t getTypeStoreSizeInBits(Type *Ty) const {
453 return 8 * getTypeStoreSize(Ty);
454 }
455
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100456 /// 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 bool typeSizeEqualsStoreSize(Type *Ty) const {
461 return getTypeSizeInBits(Ty) == getTypeStoreSizeInBits(Ty);
462 }
463
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100464 /// Returns the offset in bytes between successive objects of the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100465 /// 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 uint64_t getTypeAllocSize(Type *Ty) const {
470 // Round up to the next alignment boundary.
471 return alignTo(getTypeStoreSize(Ty), getABITypeAlignment(Ty));
472 }
473
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100474 /// Returns the offset in bits between successive objects of the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100475 /// 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 uint64_t getTypeAllocSizeInBits(Type *Ty) const {
480 return 8 * getTypeAllocSize(Ty);
481 }
482
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100483 /// Returns the minimum ABI-required alignment for the specified type.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100484 unsigned getABITypeAlignment(Type *Ty) const;
485
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100486 /// Returns the minimum ABI-required alignment for an integer type of
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100487 /// the specified bitwidth.
488 unsigned getABIIntegerTypeAlignment(unsigned BitWidth) const;
489
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100490 /// Returns the preferred stack/global alignment for the specified
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100491 /// type.
492 ///
493 /// This is always at least as good as the ABI alignment.
494 unsigned getPrefTypeAlignment(Type *Ty) const;
495
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100496 /// Returns the preferred alignment for the specified type, returned as
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100497 /// log2 of the value (a shift amount).
498 unsigned getPreferredTypeAlignmentShift(Type *Ty) const;
499
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100500 /// Returns an integer type with size at least as big as that of a
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100501 /// pointer in the given address space.
502 IntegerType *getIntPtrType(LLVMContext &C, unsigned AddressSpace = 0) const;
503
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100504 /// Returns an integer (vector of integer) type with size at least as
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100505 /// big as that of a pointer of the given pointer (vector of pointer) type.
506 Type *getIntPtrType(Type *) const;
507
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100508 /// Returns the smallest integer type with size at least as big as
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100509 /// Width bits.
510 Type *getSmallestLegalIntType(LLVMContext &C, unsigned Width = 0) const;
511
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100512 /// Returns the largest legal integer type, or null if none are set.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100513 Type *getLargestLegalIntType(LLVMContext &C) const {
514 unsigned LargestSize = getLargestLegalIntTypeSizeInBits();
515 return (LargestSize == 0) ? nullptr : Type::getIntNTy(C, LargestSize);
516 }
517
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100518 /// Returns the size of largest legal integer type size, or 0 if none
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100519 /// are set.
520 unsigned getLargestLegalIntTypeSizeInBits() const;
521
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100522 /// Returns the type of a GEP index.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100523 /// 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
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100527 /// Returns the offset from the beginning of the type for the specified
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100528 /// 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
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100534 /// Returns a StructLayout object, indicating the alignment of the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100535 /// 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
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100540 /// Returns the preferred alignment of the specified global.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100541 ///
542 /// This includes an explicitly requested alignment (if the global has one).
543 unsigned getPreferredAlignment(const GlobalVariable *GV) const;
544
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100545 /// Returns the preferred alignment of the specified global, returned
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100546 /// 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
552inline DataLayout *unwrap(LLVMTargetDataRef P) {
553 return reinterpret_cast<DataLayout *>(P);
554}
555
556inline LLVMTargetDataRef wrap(const DataLayout *P) {
557 return reinterpret_cast<LLVMTargetDataRef>(const_cast<DataLayout *>(P));
558}
559
560/// Used to lazily calculate structure layout information for a target machine,
561/// based on the DataLayout structure.
562class 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
569public:
570 uint64_t getSizeInBytes() const { return StructSize; }
571
572 uint64_t getSizeInBits() const { return 8 * StructSize; }
573
574 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 bool hasPadding() const { return IsPadded; }
579
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100580 /// Given a valid byte offset into the structure, returns the structure
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100581 /// index that contains it.
582 unsigned getElementContainingOffset(uint64_t Offset) const;
583
584 uint64_t getElementOffset(unsigned Idx) const {
585 assert(Idx < NumElements && "Invalid element idx!");
586 return MemberOffsets[Idx];
587 }
588
589 uint64_t getElementOffsetInBits(unsigned Idx) const {
590 return getElementOffset(Idx) * 8;
591 }
592
593private:
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.
601inline uint64_t DataLayout::getTypeSizeInBits(Type *Ty) const {
602 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
603 switch (Ty->getTypeID()) {
604 case Type::LabelTyID:
605 return getPointerSizeInBits(0);
606 case Type::PointerTyID:
607 return getPointerSizeInBits(Ty->getPointerAddressSpace());
608 case Type::ArrayTyID: {
609 ArrayType *ATy = cast<ArrayType>(Ty);
610 return ATy->getNumElements() *
611 getTypeAllocSizeInBits(ATy->getElementType());
612 }
613 case Type::StructTyID:
614 // Get the layout annotation... which is lazily created on demand.
615 return getStructLayout(cast<StructType>(Ty))->getSizeInBits();
616 case Type::IntegerTyID:
617 return Ty->getIntegerBitWidth();
618 case Type::HalfTyID:
619 return 16;
620 case Type::FloatTyID:
621 return 32;
622 case Type::DoubleTyID:
623 case Type::X86_MMXTyID:
624 return 64;
625 case Type::PPC_FP128TyID:
626 case Type::FP128TyID:
627 return 128;
628 // In memory objects this is always aligned to a higher boundary, but
629 // only 80 bits contain information.
630 case Type::X86_FP80TyID:
631 return 80;
632 case Type::VectorTyID: {
633 VectorType *VTy = cast<VectorType>(Ty);
634 return VTy->getNumElements() * getTypeSizeInBits(VTy->getElementType());
635 }
636 default:
637 llvm_unreachable("DataLayout::getTypeSizeInBits(): Unsupported type");
638 }
639}
640
641} // end namespace llvm
642
643#endif // LLVM_IR_DATALAYOUT_H