blob: eb031613a9359e54903bfafbd404a0e7701993cf [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"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010028#include "llvm/Support/Casting.h"
29#include "llvm/Support/ErrorHandling.h"
30#include "llvm/Support/MathExtras.h"
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020031#include "llvm/Support/Alignment.h"
32#include "llvm/Support/TypeSize.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010033#include <cassert>
34#include <cstdint>
35#include <string>
36
37// This needs to be outside of the namespace, to avoid conflict with llvm-c
38// decl.
39using LLVMTargetDataRef = struct LLVMOpaqueTargetData *;
40
41namespace llvm {
42
43class GlobalVariable;
44class LLVMContext;
45class Module;
46class StructLayout;
47class Triple;
48class Value;
49
50/// Enum used to categorize the alignment types stored by LayoutAlignElem
51enum AlignTypeEnum {
52 INVALID_ALIGN = 0,
53 INTEGER_ALIGN = 'i',
54 VECTOR_ALIGN = 'v',
55 FLOAT_ALIGN = 'f',
56 AGGREGATE_ALIGN = 'a'
57};
58
59// FIXME: Currently the DataLayout string carries a "preferred alignment"
60// for types. As the DataLayout is module/global, this should likely be
61// sunk down to an FTTI element that is queried rather than a global
62// preference.
63
Andrew Scullcdfcccc2018-10-05 20:58:37 +010064/// Layout alignment element.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010065///
66/// Stores the alignment data associated with a given alignment type (integer,
67/// vector, float) and type bit width.
68///
69/// \note The unusual order of elements in the structure attempts to reduce
70/// padding and make the structure slightly more cache friendly.
71struct LayoutAlignElem {
Andrew Scullcdfcccc2018-10-05 20:58:37 +010072 /// Alignment type from \c AlignTypeEnum
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010073 unsigned AlignType : 8;
74 unsigned TypeBitWidth : 24;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020075 Align ABIAlign;
76 Align PrefAlign;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010077
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020078 static LayoutAlignElem get(AlignTypeEnum align_type, Align abi_align,
79 Align pref_align, uint32_t bit_width);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010080
81 bool operator==(const LayoutAlignElem &rhs) const;
82};
83
Andrew Scullcdfcccc2018-10-05 20:58:37 +010084/// Layout pointer alignment element.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010085///
86/// Stores the alignment data associated with a given pointer and address space.
87///
88/// \note The unusual order of elements in the structure attempts to reduce
89/// padding and make the structure slightly more cache friendly.
90struct PointerAlignElem {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020091 Align ABIAlign;
92 Align PrefAlign;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010093 uint32_t TypeByteWidth;
94 uint32_t AddressSpace;
95 uint32_t IndexWidth;
96
97 /// Initializer
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020098 static PointerAlignElem get(uint32_t AddressSpace, Align ABIAlign,
99 Align PrefAlign, uint32_t TypeByteWidth,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100100 uint32_t IndexWidth);
101
102 bool operator==(const PointerAlignElem &rhs) const;
103};
104
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100105/// A parsed version of the target data layout string in and methods for
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100106/// querying it.
107///
108/// The target data layout string is specified *by the target* - a frontend
109/// generating LLVM IR is required to generate the right target data for the
110/// target being codegen'd to.
111class DataLayout {
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100112public:
113 enum class FunctionPtrAlignType {
114 /// The function pointer alignment is independent of the function alignment.
115 Independent,
116 /// The function pointer alignment is a multiple of the function alignment.
117 MultipleOfFunctionAlign,
118 };
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100119private:
120 /// Defaults to false.
121 bool BigEndian;
122
123 unsigned AllocaAddrSpace;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200124 MaybeAlign StackNaturalAlign;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100125 unsigned ProgramAddrSpace;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200126 unsigned DefaultGlobalsAddrSpace;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100127
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200128 MaybeAlign FunctionPtrAlign;
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100129 FunctionPtrAlignType TheFunctionPtrAlignType;
130
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100131 enum ManglingModeT {
132 MM_None,
133 MM_ELF,
134 MM_MachO,
135 MM_WinCOFF,
136 MM_WinCOFFX86,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200137 MM_Mips,
138 MM_XCOFF
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100139 };
140 ManglingModeT ManglingMode;
141
142 SmallVector<unsigned char, 8> LegalIntWidths;
143
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100144 /// Primitive type alignment data. This is sorted by type and bit
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100145 /// width during construction.
146 using AlignmentsTy = SmallVector<LayoutAlignElem, 16>;
147 AlignmentsTy Alignments;
148
149 AlignmentsTy::const_iterator
150 findAlignmentLowerBound(AlignTypeEnum AlignType, uint32_t BitWidth) const {
151 return const_cast<DataLayout *>(this)->findAlignmentLowerBound(AlignType,
152 BitWidth);
153 }
154
155 AlignmentsTy::iterator
156 findAlignmentLowerBound(AlignTypeEnum AlignType, uint32_t BitWidth);
157
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100158 /// The string representation used to create this DataLayout
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100159 std::string StringRepresentation;
160
161 using PointersTy = SmallVector<PointerAlignElem, 8>;
162 PointersTy Pointers;
163
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200164 const PointerAlignElem &getPointerAlignElem(uint32_t AddressSpace) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100165
166 // The StructType -> StructLayout map.
167 mutable void *LayoutMap = nullptr;
168
169 /// Pointers in these address spaces are non-integral, and don't have a
170 /// well-defined bitwise representation.
171 SmallVector<unsigned, 8> NonIntegralAddressSpaces;
172
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200173 /// Attempts to set the alignment of the given type. Returns an error
174 /// description on failure.
175 Error setAlignment(AlignTypeEnum align_type, Align abi_align,
176 Align pref_align, uint32_t bit_width);
177
178 /// Attempts to set the alignment of a pointer in the given address space.
179 /// Returns an error description on failure.
180 Error setPointerAlignment(uint32_t AddrSpace, Align ABIAlign, Align PrefAlign,
181 uint32_t TypeByteWidth, uint32_t IndexWidth);
182
183 /// Internal helper to get alignment for integer of given bitwidth.
184 Align getIntegerAlignment(uint32_t BitWidth, bool abi_or_pref) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100185
186 /// Internal helper method that returns requested alignment for type.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200187 Align getAlignment(Type *Ty, bool abi_or_pref) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100188
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200189 /// Attempts to parse a target data specification string and reports an error
190 /// if the string is malformed.
191 Error parseSpecifier(StringRef Desc);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100192
193 // Free all internal data structures.
194 void clear();
195
196public:
197 /// Constructs a DataLayout from a specification string. See reset().
198 explicit DataLayout(StringRef LayoutDescription) {
199 reset(LayoutDescription);
200 }
201
202 /// Initialize target data from properties stored in the module.
203 explicit DataLayout(const Module *M);
204
205 DataLayout(const DataLayout &DL) { *this = DL; }
206
207 ~DataLayout(); // Not virtual, do not subclass this class
208
209 DataLayout &operator=(const DataLayout &DL) {
210 clear();
211 StringRepresentation = DL.StringRepresentation;
212 BigEndian = DL.isBigEndian();
213 AllocaAddrSpace = DL.AllocaAddrSpace;
214 StackNaturalAlign = DL.StackNaturalAlign;
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100215 FunctionPtrAlign = DL.FunctionPtrAlign;
216 TheFunctionPtrAlignType = DL.TheFunctionPtrAlignType;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100217 ProgramAddrSpace = DL.ProgramAddrSpace;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200218 DefaultGlobalsAddrSpace = DL.DefaultGlobalsAddrSpace;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100219 ManglingMode = DL.ManglingMode;
220 LegalIntWidths = DL.LegalIntWidths;
221 Alignments = DL.Alignments;
222 Pointers = DL.Pointers;
223 NonIntegralAddressSpaces = DL.NonIntegralAddressSpaces;
224 return *this;
225 }
226
227 bool operator==(const DataLayout &Other) const;
228 bool operator!=(const DataLayout &Other) const { return !(*this == Other); }
229
230 void init(const Module *M);
231
232 /// Parse a data layout string (with fallback to default values).
233 void reset(StringRef LayoutDescription);
234
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200235 /// Parse a data layout string and return the layout. Return an error
236 /// description on failure.
237 static Expected<DataLayout> parse(StringRef LayoutDescription);
238
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100239 /// Layout endianness...
240 bool isLittleEndian() const { return !BigEndian; }
241 bool isBigEndian() const { return BigEndian; }
242
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100243 /// Returns the string representation of the DataLayout.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100244 ///
245 /// This representation is in the same format accepted by the string
246 /// constructor above. This should not be used to compare two DataLayout as
247 /// different string can represent the same layout.
248 const std::string &getStringRepresentation() const {
249 return StringRepresentation;
250 }
251
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100252 /// Test if the DataLayout was constructed from an empty string.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100253 bool isDefault() const { return StringRepresentation.empty(); }
254
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100255 /// Returns true if the specified type is known to be a native integer
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100256 /// type supported by the CPU.
257 ///
258 /// For example, i64 is not native on most 32-bit CPUs and i37 is not native
259 /// on any known one. This returns false if the integer width is not legal.
260 ///
261 /// The width is specified in bits.
262 bool isLegalInteger(uint64_t Width) const {
263 for (unsigned LegalIntWidth : LegalIntWidths)
264 if (LegalIntWidth == Width)
265 return true;
266 return false;
267 }
268
269 bool isIllegalInteger(uint64_t Width) const { return !isLegalInteger(Width); }
270
271 /// Returns true if the given alignment exceeds the natural stack alignment.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200272 bool exceedsNaturalStackAlignment(Align Alignment) const {
273 return StackNaturalAlign && (Alignment > *StackNaturalAlign);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100274 }
275
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200276 Align getStackAlignment() const {
277 assert(StackNaturalAlign && "StackNaturalAlign must be defined");
278 return *StackNaturalAlign;
279 }
280
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100281 unsigned getAllocaAddrSpace() const { return AllocaAddrSpace; }
282
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100283 /// Returns the alignment of function pointers, which may or may not be
284 /// related to the alignment of functions.
285 /// \see getFunctionPtrAlignType
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200286 MaybeAlign getFunctionPtrAlign() const { return FunctionPtrAlign; }
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100287
288 /// Return the type of function pointer alignment.
289 /// \see getFunctionPtrAlign
290 FunctionPtrAlignType getFunctionPtrAlignType() const {
291 return TheFunctionPtrAlignType;
292 }
293
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100294 unsigned getProgramAddressSpace() const { return ProgramAddrSpace; }
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200295 unsigned getDefaultGlobalsAddressSpace() const {
296 return DefaultGlobalsAddrSpace;
297 }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100298
299 bool hasMicrosoftFastStdCallMangling() const {
300 return ManglingMode == MM_WinCOFFX86;
301 }
302
303 /// Returns true if symbols with leading question marks should not receive IR
304 /// mangling. True for Windows mangling modes.
305 bool doNotMangleLeadingQuestionMark() const {
306 return ManglingMode == MM_WinCOFF || ManglingMode == MM_WinCOFFX86;
307 }
308
309 bool hasLinkerPrivateGlobalPrefix() const { return ManglingMode == MM_MachO; }
310
311 StringRef getLinkerPrivateGlobalPrefix() const {
312 if (ManglingMode == MM_MachO)
313 return "l";
314 return "";
315 }
316
317 char getGlobalPrefix() const {
318 switch (ManglingMode) {
319 case MM_None:
320 case MM_ELF:
321 case MM_Mips:
322 case MM_WinCOFF:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200323 case MM_XCOFF:
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100324 return '\0';
325 case MM_MachO:
326 case MM_WinCOFFX86:
327 return '_';
328 }
329 llvm_unreachable("invalid mangling mode");
330 }
331
332 StringRef getPrivateGlobalPrefix() const {
333 switch (ManglingMode) {
334 case MM_None:
335 return "";
336 case MM_ELF:
337 case MM_WinCOFF:
338 return ".L";
339 case MM_Mips:
340 return "$";
341 case MM_MachO:
342 case MM_WinCOFFX86:
343 return "L";
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200344 case MM_XCOFF:
345 return "L..";
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100346 }
347 llvm_unreachable("invalid mangling mode");
348 }
349
350 static const char *getManglingComponent(const Triple &T);
351
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100352 /// Returns true if the specified type fits in a native integer type
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100353 /// supported by the CPU.
354 ///
355 /// For example, if the CPU only supports i32 as a native integer type, then
356 /// i27 fits in a legal integer type but i45 does not.
357 bool fitsInLegalInteger(unsigned Width) const {
358 for (unsigned LegalIntWidth : LegalIntWidths)
359 if (Width <= LegalIntWidth)
360 return true;
361 return false;
362 }
363
364 /// Layout pointer alignment
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200365 Align getPointerABIAlignment(unsigned AS) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100366
367 /// Return target's alignment for stack-based pointers
368 /// FIXME: The defaults need to be removed once all of
369 /// the backends/clients are updated.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200370 Align getPointerPrefAlignment(unsigned AS = 0) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100371
372 /// Layout pointer size
373 /// FIXME: The defaults need to be removed once all of
374 /// the backends/clients are updated.
375 unsigned getPointerSize(unsigned AS = 0) const;
376
Andrew Walbran16937d02019-10-22 13:54:20 +0100377 /// Returns the maximum pointer size over all address spaces.
378 unsigned getMaxPointerSize() const;
379
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100380 // Index size used for address calculation.
381 unsigned getIndexSize(unsigned AS) const;
382
383 /// Return the address spaces containing non-integral pointers. Pointers in
384 /// this address space don't have a well-defined bitwise representation.
385 ArrayRef<unsigned> getNonIntegralAddressSpaces() const {
386 return NonIntegralAddressSpaces;
387 }
388
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100389 bool isNonIntegralAddressSpace(unsigned AddrSpace) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100390 ArrayRef<unsigned> NonIntegralSpaces = getNonIntegralAddressSpaces();
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200391 return is_contained(NonIntegralSpaces, AddrSpace);
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100392 }
393
394 bool isNonIntegralPointerType(PointerType *PT) const {
395 return isNonIntegralAddressSpace(PT->getAddressSpace());
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100396 }
397
398 bool isNonIntegralPointerType(Type *Ty) const {
399 auto *PTy = dyn_cast<PointerType>(Ty);
400 return PTy && isNonIntegralPointerType(PTy);
401 }
402
403 /// Layout pointer size, in bits
404 /// FIXME: The defaults need to be removed once all of
405 /// the backends/clients are updated.
406 unsigned getPointerSizeInBits(unsigned AS = 0) const {
407 return getPointerSize(AS) * 8;
408 }
409
Andrew Walbran16937d02019-10-22 13:54:20 +0100410 /// Returns the maximum pointer size over all address spaces.
411 unsigned getMaxPointerSizeInBits() const {
412 return getMaxPointerSize() * 8;
413 }
414
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100415 /// Size in bits of index used for address calculation in getelementptr.
416 unsigned getIndexSizeInBits(unsigned AS) const {
417 return getIndexSize(AS) * 8;
418 }
419
420 /// Layout pointer size, in bits, based on the type. If this function is
421 /// called with a pointer type, then the type size of the pointer is returned.
422 /// If this function is called with a vector of pointers, then the type size
423 /// of the pointer is returned. This should only be called with a pointer or
424 /// vector of pointers.
425 unsigned getPointerTypeSizeInBits(Type *) const;
426
427 /// Layout size of the index used in GEP calculation.
428 /// The function should be called with pointer or vector of pointers type.
429 unsigned getIndexTypeSizeInBits(Type *Ty) const;
430
431 unsigned getPointerTypeSize(Type *Ty) const {
432 return getPointerTypeSizeInBits(Ty) / 8;
433 }
434
435 /// Size examples:
436 ///
437 /// Type SizeInBits StoreSizeInBits AllocSizeInBits[*]
438 /// ---- ---------- --------------- ---------------
439 /// i1 1 8 8
440 /// i8 8 8 8
441 /// i19 19 24 32
442 /// i32 32 32 32
443 /// i100 100 104 128
444 /// i128 128 128 128
445 /// Float 32 32 32
446 /// Double 64 64 64
447 /// X86_FP80 80 80 96
448 ///
449 /// [*] The alloc size depends on the alignment, and thus on the target.
450 /// These values are for x86-32 linux.
451
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100452 /// Returns the number of bits necessary to hold the specified type.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100453 ///
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200454 /// If Ty is a scalable vector type, the scalable property will be set and
455 /// the runtime size will be a positive integer multiple of the base size.
456 ///
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100457 /// For example, returns 36 for i36 and 80 for x86_fp80. The type passed must
458 /// have a size (Type::isSized() must return true).
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200459 TypeSize getTypeSizeInBits(Type *Ty) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100460
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100461 /// Returns the maximum number of bytes that may be overwritten by
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100462 /// storing the specified type.
463 ///
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200464 /// If Ty is a scalable vector type, the scalable property will be set and
465 /// the runtime size will be a positive integer multiple of the base size.
466 ///
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100467 /// For example, returns 5 for i36 and 10 for x86_fp80.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200468 TypeSize getTypeStoreSize(Type *Ty) const {
469 TypeSize BaseSize = getTypeSizeInBits(Ty);
470 return { (BaseSize.getKnownMinSize() + 7) / 8, BaseSize.isScalable() };
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100471 }
472
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100473 /// Returns the maximum number of bits that may be overwritten by
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100474 /// storing the specified type; always a multiple of 8.
475 ///
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200476 /// If Ty is a scalable vector type, the scalable property will be set and
477 /// the runtime size will be a positive integer multiple of the base size.
478 ///
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100479 /// For example, returns 40 for i36 and 80 for x86_fp80.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200480 TypeSize getTypeStoreSizeInBits(Type *Ty) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100481 return 8 * getTypeStoreSize(Ty);
482 }
483
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100484 /// Returns true if no extra padding bits are needed when storing the
485 /// specified type.
486 ///
487 /// For example, returns false for i19 that has a 24-bit store size.
488 bool typeSizeEqualsStoreSize(Type *Ty) const {
489 return getTypeSizeInBits(Ty) == getTypeStoreSizeInBits(Ty);
490 }
491
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100492 /// Returns the offset in bytes between successive objects of the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100493 /// specified type, including alignment padding.
494 ///
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200495 /// If Ty is a scalable vector type, the scalable property will be set and
496 /// the runtime size will be a positive integer multiple of the base size.
497 ///
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100498 /// This is the amount that alloca reserves for this type. For example,
499 /// returns 12 or 16 for x86_fp80, depending on alignment.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200500 TypeSize getTypeAllocSize(Type *Ty) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100501 // Round up to the next alignment boundary.
502 return alignTo(getTypeStoreSize(Ty), getABITypeAlignment(Ty));
503 }
504
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100505 /// Returns the offset in bits between successive objects of the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100506 /// specified type, including alignment padding; always a multiple of 8.
507 ///
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200508 /// If Ty is a scalable vector type, the scalable property will be set and
509 /// the runtime size will be a positive integer multiple of the base size.
510 ///
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100511 /// This is the amount that alloca reserves for this type. For example,
512 /// returns 96 or 128 for x86_fp80, depending on alignment.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200513 TypeSize getTypeAllocSizeInBits(Type *Ty) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100514 return 8 * getTypeAllocSize(Ty);
515 }
516
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100517 /// Returns the minimum ABI-required alignment for the specified type.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200518 /// FIXME: Deprecate this function once migration to Align is over.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100519 unsigned getABITypeAlignment(Type *Ty) const;
520
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200521 /// Returns the minimum ABI-required alignment for the specified type.
522 Align getABITypeAlign(Type *Ty) const;
523
524 /// Helper function to return `Alignment` if it's set or the result of
525 /// `getABITypeAlignment(Ty)`, in any case the result is a valid alignment.
526 inline Align getValueOrABITypeAlignment(MaybeAlign Alignment,
527 Type *Ty) const {
528 return Alignment ? *Alignment : getABITypeAlign(Ty);
529 }
530
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100531 /// Returns the minimum ABI-required alignment for an integer type of
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100532 /// the specified bitwidth.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200533 Align getABIIntegerTypeAlignment(unsigned BitWidth) const {
534 return getIntegerAlignment(BitWidth, /* abi_or_pref */ true);
535 }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100536
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100537 /// Returns the preferred stack/global alignment for the specified
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100538 /// type.
539 ///
540 /// This is always at least as good as the ABI alignment.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200541 /// FIXME: Deprecate this function once migration to Align is over.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100542 unsigned getPrefTypeAlignment(Type *Ty) const;
543
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200544 /// Returns the preferred stack/global alignment for the specified
545 /// type.
546 ///
547 /// This is always at least as good as the ABI alignment.
548 Align getPrefTypeAlign(Type *Ty) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100549
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100550 /// Returns an integer type with size at least as big as that of a
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100551 /// pointer in the given address space.
552 IntegerType *getIntPtrType(LLVMContext &C, unsigned AddressSpace = 0) const;
553
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100554 /// Returns an integer (vector of integer) type with size at least as
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100555 /// big as that of a pointer of the given pointer (vector of pointer) type.
556 Type *getIntPtrType(Type *) const;
557
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100558 /// Returns the smallest integer type with size at least as big as
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100559 /// Width bits.
560 Type *getSmallestLegalIntType(LLVMContext &C, unsigned Width = 0) const;
561
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100562 /// Returns the largest legal integer type, or null if none are set.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100563 Type *getLargestLegalIntType(LLVMContext &C) const {
564 unsigned LargestSize = getLargestLegalIntTypeSizeInBits();
565 return (LargestSize == 0) ? nullptr : Type::getIntNTy(C, LargestSize);
566 }
567
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100568 /// Returns the size of largest legal integer type size, or 0 if none
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100569 /// are set.
570 unsigned getLargestLegalIntTypeSizeInBits() const;
571
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100572 /// Returns the type of a GEP index.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100573 /// If it was not specified explicitly, it will be the integer type of the
574 /// pointer width - IntPtrType.
575 Type *getIndexType(Type *PtrTy) const;
576
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100577 /// Returns the offset from the beginning of the type for the specified
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100578 /// indices.
579 ///
580 /// Note that this takes the element type, not the pointer type.
581 /// This is used to implement getelementptr.
582 int64_t getIndexedOffsetInType(Type *ElemTy, ArrayRef<Value *> Indices) const;
583
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100584 /// Returns a StructLayout object, indicating the alignment of the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100585 /// struct, its size, and the offsets of its fields.
586 ///
587 /// Note that this information is lazily cached.
588 const StructLayout *getStructLayout(StructType *Ty) const;
589
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100590 /// Returns the preferred alignment of the specified global.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100591 ///
592 /// This includes an explicitly requested alignment (if the global has one).
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200593 Align getPreferredAlign(const GlobalVariable *GV) const;
594
595 /// Returns the preferred alignment of the specified global.
596 ///
597 /// This includes an explicitly requested alignment (if the global has one).
598 LLVM_ATTRIBUTE_DEPRECATED(
599 inline unsigned getPreferredAlignment(const GlobalVariable *GV) const,
600 "Use getPreferredAlign instead") {
601 return getPreferredAlign(GV).value();
602 }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100603
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100604 /// Returns the preferred alignment of the specified global, returned
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100605 /// in log form.
606 ///
607 /// This includes an explicitly requested alignment (if the global has one).
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200608 LLVM_ATTRIBUTE_DEPRECATED(
609 inline unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const,
610 "Inline where needed") {
611 return Log2(getPreferredAlign(GV));
612 }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100613};
614
615inline DataLayout *unwrap(LLVMTargetDataRef P) {
616 return reinterpret_cast<DataLayout *>(P);
617}
618
619inline LLVMTargetDataRef wrap(const DataLayout *P) {
620 return reinterpret_cast<LLVMTargetDataRef>(const_cast<DataLayout *>(P));
621}
622
623/// Used to lazily calculate structure layout information for a target machine,
624/// based on the DataLayout structure.
625class StructLayout {
626 uint64_t StructSize;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200627 Align StructAlignment;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100628 unsigned IsPadded : 1;
629 unsigned NumElements : 31;
630 uint64_t MemberOffsets[1]; // variable sized array!
631
632public:
633 uint64_t getSizeInBytes() const { return StructSize; }
634
635 uint64_t getSizeInBits() const { return 8 * StructSize; }
636
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200637 Align getAlignment() const { return StructAlignment; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100638
639 /// Returns whether the struct has padding or not between its fields.
640 /// NB: Padding in nested element is not taken into account.
641 bool hasPadding() const { return IsPadded; }
642
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100643 /// Given a valid byte offset into the structure, returns the structure
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100644 /// index that contains it.
645 unsigned getElementContainingOffset(uint64_t Offset) const;
646
647 uint64_t getElementOffset(unsigned Idx) const {
648 assert(Idx < NumElements && "Invalid element idx!");
649 return MemberOffsets[Idx];
650 }
651
652 uint64_t getElementOffsetInBits(unsigned Idx) const {
653 return getElementOffset(Idx) * 8;
654 }
655
656private:
657 friend class DataLayout; // Only DataLayout can create this class
658
659 StructLayout(StructType *ST, const DataLayout &DL);
660};
661
662// The implementation of this method is provided inline as it is particularly
663// well suited to constant folding when called on a specific Type subclass.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200664inline TypeSize DataLayout::getTypeSizeInBits(Type *Ty) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100665 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
666 switch (Ty->getTypeID()) {
667 case Type::LabelTyID:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200668 return TypeSize::Fixed(getPointerSizeInBits(0));
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100669 case Type::PointerTyID:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200670 return TypeSize::Fixed(getPointerSizeInBits(Ty->getPointerAddressSpace()));
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100671 case Type::ArrayTyID: {
672 ArrayType *ATy = cast<ArrayType>(Ty);
673 return ATy->getNumElements() *
674 getTypeAllocSizeInBits(ATy->getElementType());
675 }
676 case Type::StructTyID:
677 // Get the layout annotation... which is lazily created on demand.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200678 return TypeSize::Fixed(
679 getStructLayout(cast<StructType>(Ty))->getSizeInBits());
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100680 case Type::IntegerTyID:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200681 return TypeSize::Fixed(Ty->getIntegerBitWidth());
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100682 case Type::HalfTyID:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200683 case Type::BFloatTyID:
684 return TypeSize::Fixed(16);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100685 case Type::FloatTyID:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200686 return TypeSize::Fixed(32);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100687 case Type::DoubleTyID:
688 case Type::X86_MMXTyID:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200689 return TypeSize::Fixed(64);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100690 case Type::PPC_FP128TyID:
691 case Type::FP128TyID:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200692 return TypeSize::Fixed(128);
693 case Type::X86_AMXTyID:
694 return TypeSize::Fixed(8192);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100695 // In memory objects this is always aligned to a higher boundary, but
696 // only 80 bits contain information.
697 case Type::X86_FP80TyID:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200698 return TypeSize::Fixed(80);
699 case Type::FixedVectorTyID:
700 case Type::ScalableVectorTyID: {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100701 VectorType *VTy = cast<VectorType>(Ty);
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200702 auto EltCnt = VTy->getElementCount();
703 uint64_t MinBits = EltCnt.getKnownMinValue() *
704 getTypeSizeInBits(VTy->getElementType()).getFixedSize();
705 return TypeSize(MinBits, EltCnt.isScalable());
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100706 }
707 default:
708 llvm_unreachable("DataLayout::getTypeSizeInBits(): Unsupported type");
709 }
710}
711
712} // end namespace llvm
713
714#endif // LLVM_IR_DATALAYOUT_H