1*9880d681SAndroid Build Coastguard Worker //===--------- llvm/DataLayout.h - Data size & alignment info ---*- C++ -*-===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file defines layout properties related to datatype size/offset/alignment
11*9880d681SAndroid Build Coastguard Worker // information. It uses lazy annotations to cache information about how
12*9880d681SAndroid Build Coastguard Worker // structure types are laid out and used.
13*9880d681SAndroid Build Coastguard Worker //
14*9880d681SAndroid Build Coastguard Worker // This structure should be created once, filled in if the defaults are not
15*9880d681SAndroid Build Coastguard Worker // correct and then passed around by const&. None of the members functions
16*9880d681SAndroid Build Coastguard Worker // require modification to the object.
17*9880d681SAndroid Build Coastguard Worker //
18*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
19*9880d681SAndroid Build Coastguard Worker
20*9880d681SAndroid Build Coastguard Worker #ifndef LLVM_IR_DATALAYOUT_H
21*9880d681SAndroid Build Coastguard Worker #define LLVM_IR_DATALAYOUT_H
22*9880d681SAndroid Build Coastguard Worker
23*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallVector.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DerivedTypes.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Type.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/Pass.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/DataTypes.h"
28*9880d681SAndroid Build Coastguard Worker
29*9880d681SAndroid Build Coastguard Worker // This needs to be outside of the namespace, to avoid conflict with llvm-c
30*9880d681SAndroid Build Coastguard Worker // decl.
31*9880d681SAndroid Build Coastguard Worker typedef struct LLVMOpaqueTargetData *LLVMTargetDataRef;
32*9880d681SAndroid Build Coastguard Worker
33*9880d681SAndroid Build Coastguard Worker namespace llvm {
34*9880d681SAndroid Build Coastguard Worker
35*9880d681SAndroid Build Coastguard Worker class Value;
36*9880d681SAndroid Build Coastguard Worker class StructType;
37*9880d681SAndroid Build Coastguard Worker class StructLayout;
38*9880d681SAndroid Build Coastguard Worker class Triple;
39*9880d681SAndroid Build Coastguard Worker class GlobalVariable;
40*9880d681SAndroid Build Coastguard Worker class LLVMContext;
41*9880d681SAndroid Build Coastguard Worker template<typename T>
42*9880d681SAndroid Build Coastguard Worker class ArrayRef;
43*9880d681SAndroid Build Coastguard Worker
44*9880d681SAndroid Build Coastguard Worker /// Enum used to categorize the alignment types stored by LayoutAlignElem
45*9880d681SAndroid Build Coastguard Worker enum AlignTypeEnum {
46*9880d681SAndroid Build Coastguard Worker INVALID_ALIGN = 0,
47*9880d681SAndroid Build Coastguard Worker INTEGER_ALIGN = 'i',
48*9880d681SAndroid Build Coastguard Worker VECTOR_ALIGN = 'v',
49*9880d681SAndroid Build Coastguard Worker FLOAT_ALIGN = 'f',
50*9880d681SAndroid Build Coastguard Worker AGGREGATE_ALIGN = 'a'
51*9880d681SAndroid Build Coastguard Worker };
52*9880d681SAndroid Build Coastguard Worker
53*9880d681SAndroid Build Coastguard Worker // FIXME: Currently the DataLayout string carries a "preferred alignment"
54*9880d681SAndroid Build Coastguard Worker // for types. As the DataLayout is module/global, this should likely be
55*9880d681SAndroid Build Coastguard Worker // sunk down to an FTTI element that is queried rather than a global
56*9880d681SAndroid Build Coastguard Worker // preference.
57*9880d681SAndroid Build Coastguard Worker
58*9880d681SAndroid Build Coastguard Worker /// \brief Layout alignment element.
59*9880d681SAndroid Build Coastguard Worker ///
60*9880d681SAndroid Build Coastguard Worker /// Stores the alignment data associated with a given alignment type (integer,
61*9880d681SAndroid Build Coastguard Worker /// vector, float) and type bit width.
62*9880d681SAndroid Build Coastguard Worker ///
63*9880d681SAndroid Build Coastguard Worker /// \note The unusual order of elements in the structure attempts to reduce
64*9880d681SAndroid Build Coastguard Worker /// padding and make the structure slightly more cache friendly.
65*9880d681SAndroid Build Coastguard Worker struct LayoutAlignElem {
66*9880d681SAndroid Build Coastguard Worker /// \brief Alignment type from \c AlignTypeEnum
67*9880d681SAndroid Build Coastguard Worker unsigned AlignType : 8;
68*9880d681SAndroid Build Coastguard Worker unsigned TypeBitWidth : 24;
69*9880d681SAndroid Build Coastguard Worker unsigned ABIAlign : 16;
70*9880d681SAndroid Build Coastguard Worker unsigned PrefAlign : 16;
71*9880d681SAndroid Build Coastguard Worker
72*9880d681SAndroid Build Coastguard Worker static LayoutAlignElem get(AlignTypeEnum align_type, unsigned abi_align,
73*9880d681SAndroid Build Coastguard Worker unsigned pref_align, uint32_t bit_width);
74*9880d681SAndroid Build Coastguard Worker bool operator==(const LayoutAlignElem &rhs) const;
75*9880d681SAndroid Build Coastguard Worker };
76*9880d681SAndroid Build Coastguard Worker
77*9880d681SAndroid Build Coastguard Worker /// \brief Layout pointer alignment element.
78*9880d681SAndroid Build Coastguard Worker ///
79*9880d681SAndroid Build Coastguard Worker /// Stores the alignment data associated with a given pointer and address space.
80*9880d681SAndroid Build Coastguard Worker ///
81*9880d681SAndroid Build Coastguard Worker /// \note The unusual order of elements in the structure attempts to reduce
82*9880d681SAndroid Build Coastguard Worker /// padding and make the structure slightly more cache friendly.
83*9880d681SAndroid Build Coastguard Worker struct PointerAlignElem {
84*9880d681SAndroid Build Coastguard Worker unsigned ABIAlign;
85*9880d681SAndroid Build Coastguard Worker unsigned PrefAlign;
86*9880d681SAndroid Build Coastguard Worker uint32_t TypeByteWidth;
87*9880d681SAndroid Build Coastguard Worker uint32_t AddressSpace;
88*9880d681SAndroid Build Coastguard Worker
89*9880d681SAndroid Build Coastguard Worker /// Initializer
90*9880d681SAndroid Build Coastguard Worker static PointerAlignElem get(uint32_t AddressSpace, unsigned ABIAlign,
91*9880d681SAndroid Build Coastguard Worker unsigned PrefAlign, uint32_t TypeByteWidth);
92*9880d681SAndroid Build Coastguard Worker bool operator==(const PointerAlignElem &rhs) const;
93*9880d681SAndroid Build Coastguard Worker };
94*9880d681SAndroid Build Coastguard Worker
95*9880d681SAndroid Build Coastguard Worker /// \brief A parsed version of the target data layout string in and methods for
96*9880d681SAndroid Build Coastguard Worker /// querying it.
97*9880d681SAndroid Build Coastguard Worker ///
98*9880d681SAndroid Build Coastguard Worker /// The target data layout string is specified *by the target* - a frontend
99*9880d681SAndroid Build Coastguard Worker /// generating LLVM IR is required to generate the right target data for the
100*9880d681SAndroid Build Coastguard Worker /// target being codegen'd to.
101*9880d681SAndroid Build Coastguard Worker class DataLayout {
102*9880d681SAndroid Build Coastguard Worker private:
103*9880d681SAndroid Build Coastguard Worker /// Defaults to false.
104*9880d681SAndroid Build Coastguard Worker bool BigEndian;
105*9880d681SAndroid Build Coastguard Worker
106*9880d681SAndroid Build Coastguard Worker unsigned StackNaturalAlign;
107*9880d681SAndroid Build Coastguard Worker
108*9880d681SAndroid Build Coastguard Worker enum ManglingModeT {
109*9880d681SAndroid Build Coastguard Worker MM_None,
110*9880d681SAndroid Build Coastguard Worker MM_ELF,
111*9880d681SAndroid Build Coastguard Worker MM_MachO,
112*9880d681SAndroid Build Coastguard Worker MM_WinCOFF,
113*9880d681SAndroid Build Coastguard Worker MM_WinCOFFX86,
114*9880d681SAndroid Build Coastguard Worker MM_Mips
115*9880d681SAndroid Build Coastguard Worker };
116*9880d681SAndroid Build Coastguard Worker ManglingModeT ManglingMode;
117*9880d681SAndroid Build Coastguard Worker
118*9880d681SAndroid Build Coastguard Worker SmallVector<unsigned char, 8> LegalIntWidths;
119*9880d681SAndroid Build Coastguard Worker
120*9880d681SAndroid Build Coastguard Worker /// \brief Primitive type alignment data.
121*9880d681SAndroid Build Coastguard Worker SmallVector<LayoutAlignElem, 16> Alignments;
122*9880d681SAndroid Build Coastguard Worker
123*9880d681SAndroid Build Coastguard Worker /// \brief The string representation used to create this DataLayout
124*9880d681SAndroid Build Coastguard Worker std::string StringRepresentation;
125*9880d681SAndroid Build Coastguard Worker
126*9880d681SAndroid Build Coastguard Worker typedef SmallVector<PointerAlignElem, 8> PointersTy;
127*9880d681SAndroid Build Coastguard Worker PointersTy Pointers;
128*9880d681SAndroid Build Coastguard Worker
129*9880d681SAndroid Build Coastguard Worker PointersTy::const_iterator
findPointerLowerBound(uint32_t AddressSpace)130*9880d681SAndroid Build Coastguard Worker findPointerLowerBound(uint32_t AddressSpace) const {
131*9880d681SAndroid Build Coastguard Worker return const_cast<DataLayout *>(this)->findPointerLowerBound(AddressSpace);
132*9880d681SAndroid Build Coastguard Worker }
133*9880d681SAndroid Build Coastguard Worker
134*9880d681SAndroid Build Coastguard Worker PointersTy::iterator findPointerLowerBound(uint32_t AddressSpace);
135*9880d681SAndroid Build Coastguard Worker
136*9880d681SAndroid Build Coastguard Worker /// This member is a signal that a requested alignment type and bit width were
137*9880d681SAndroid Build Coastguard Worker /// not found in the SmallVector.
138*9880d681SAndroid Build Coastguard Worker static const LayoutAlignElem InvalidAlignmentElem;
139*9880d681SAndroid Build Coastguard Worker
140*9880d681SAndroid Build Coastguard Worker /// This member is a signal that a requested pointer type and bit width were
141*9880d681SAndroid Build Coastguard Worker /// not found in the DenseSet.
142*9880d681SAndroid Build Coastguard Worker static const PointerAlignElem InvalidPointerElem;
143*9880d681SAndroid Build Coastguard Worker
144*9880d681SAndroid Build Coastguard Worker // The StructType -> StructLayout map.
145*9880d681SAndroid Build Coastguard Worker mutable void *LayoutMap;
146*9880d681SAndroid Build Coastguard Worker
147*9880d681SAndroid Build Coastguard Worker void setAlignment(AlignTypeEnum align_type, unsigned abi_align,
148*9880d681SAndroid Build Coastguard Worker unsigned pref_align, uint32_t bit_width);
149*9880d681SAndroid Build Coastguard Worker unsigned getAlignmentInfo(AlignTypeEnum align_type, uint32_t bit_width,
150*9880d681SAndroid Build Coastguard Worker bool ABIAlign, Type *Ty) const;
151*9880d681SAndroid Build Coastguard Worker void setPointerAlignment(uint32_t AddrSpace, unsigned ABIAlign,
152*9880d681SAndroid Build Coastguard Worker unsigned PrefAlign, uint32_t TypeByteWidth);
153*9880d681SAndroid Build Coastguard Worker
154*9880d681SAndroid Build Coastguard Worker /// Internal helper method that returns requested alignment for type.
155*9880d681SAndroid Build Coastguard Worker unsigned getAlignment(Type *Ty, bool abi_or_pref) const;
156*9880d681SAndroid Build Coastguard Worker
157*9880d681SAndroid Build Coastguard Worker /// \brief Valid alignment predicate.
158*9880d681SAndroid Build Coastguard Worker ///
159*9880d681SAndroid Build Coastguard Worker /// Predicate that tests a LayoutAlignElem reference returned by get() against
160*9880d681SAndroid Build Coastguard Worker /// InvalidAlignmentElem.
validAlignment(const LayoutAlignElem & align)161*9880d681SAndroid Build Coastguard Worker bool validAlignment(const LayoutAlignElem &align) const {
162*9880d681SAndroid Build Coastguard Worker return &align != &InvalidAlignmentElem;
163*9880d681SAndroid Build Coastguard Worker }
164*9880d681SAndroid Build Coastguard Worker
165*9880d681SAndroid Build Coastguard Worker /// \brief Valid pointer predicate.
166*9880d681SAndroid Build Coastguard Worker ///
167*9880d681SAndroid Build Coastguard Worker /// Predicate that tests a PointerAlignElem reference returned by get()
168*9880d681SAndroid Build Coastguard Worker /// against \c InvalidPointerElem.
validPointer(const PointerAlignElem & align)169*9880d681SAndroid Build Coastguard Worker bool validPointer(const PointerAlignElem &align) const {
170*9880d681SAndroid Build Coastguard Worker return &align != &InvalidPointerElem;
171*9880d681SAndroid Build Coastguard Worker }
172*9880d681SAndroid Build Coastguard Worker
173*9880d681SAndroid Build Coastguard Worker /// Parses a target data specification string. Assert if the string is
174*9880d681SAndroid Build Coastguard Worker /// malformed.
175*9880d681SAndroid Build Coastguard Worker void parseSpecifier(StringRef LayoutDescription);
176*9880d681SAndroid Build Coastguard Worker
177*9880d681SAndroid Build Coastguard Worker // Free all internal data structures.
178*9880d681SAndroid Build Coastguard Worker void clear();
179*9880d681SAndroid Build Coastguard Worker
180*9880d681SAndroid Build Coastguard Worker public:
181*9880d681SAndroid Build Coastguard Worker /// Constructs a DataLayout from a specification string. See reset().
DataLayout(StringRef LayoutDescription)182*9880d681SAndroid Build Coastguard Worker explicit DataLayout(StringRef LayoutDescription) : LayoutMap(nullptr) {
183*9880d681SAndroid Build Coastguard Worker reset(LayoutDescription);
184*9880d681SAndroid Build Coastguard Worker }
185*9880d681SAndroid Build Coastguard Worker
186*9880d681SAndroid Build Coastguard Worker /// Initialize target data from properties stored in the module.
187*9880d681SAndroid Build Coastguard Worker explicit DataLayout(const Module *M);
188*9880d681SAndroid Build Coastguard Worker
189*9880d681SAndroid Build Coastguard Worker void init(const Module *M);
190*9880d681SAndroid Build Coastguard Worker
DataLayout(const DataLayout & DL)191*9880d681SAndroid Build Coastguard Worker DataLayout(const DataLayout &DL) : LayoutMap(nullptr) { *this = DL; }
192*9880d681SAndroid Build Coastguard Worker
193*9880d681SAndroid Build Coastguard Worker DataLayout &operator=(const DataLayout &DL) {
194*9880d681SAndroid Build Coastguard Worker clear();
195*9880d681SAndroid Build Coastguard Worker StringRepresentation = DL.StringRepresentation;
196*9880d681SAndroid Build Coastguard Worker BigEndian = DL.isBigEndian();
197*9880d681SAndroid Build Coastguard Worker StackNaturalAlign = DL.StackNaturalAlign;
198*9880d681SAndroid Build Coastguard Worker ManglingMode = DL.ManglingMode;
199*9880d681SAndroid Build Coastguard Worker LegalIntWidths = DL.LegalIntWidths;
200*9880d681SAndroid Build Coastguard Worker Alignments = DL.Alignments;
201*9880d681SAndroid Build Coastguard Worker Pointers = DL.Pointers;
202*9880d681SAndroid Build Coastguard Worker return *this;
203*9880d681SAndroid Build Coastguard Worker }
204*9880d681SAndroid Build Coastguard Worker
205*9880d681SAndroid Build Coastguard Worker bool operator==(const DataLayout &Other) const;
206*9880d681SAndroid Build Coastguard Worker bool operator!=(const DataLayout &Other) const { return !(*this == Other); }
207*9880d681SAndroid Build Coastguard Worker
208*9880d681SAndroid Build Coastguard Worker ~DataLayout(); // Not virtual, do not subclass this class
209*9880d681SAndroid Build Coastguard Worker
210*9880d681SAndroid Build Coastguard Worker /// Parse a data layout string (with fallback to default values).
211*9880d681SAndroid Build Coastguard Worker void reset(StringRef LayoutDescription);
212*9880d681SAndroid Build Coastguard Worker
213*9880d681SAndroid Build Coastguard Worker /// Layout endianness...
isLittleEndian()214*9880d681SAndroid Build Coastguard Worker bool isLittleEndian() const { return !BigEndian; }
isBigEndian()215*9880d681SAndroid Build Coastguard Worker bool isBigEndian() const { return BigEndian; }
216*9880d681SAndroid Build Coastguard Worker
217*9880d681SAndroid Build Coastguard Worker /// \brief Returns the string representation of the DataLayout.
218*9880d681SAndroid Build Coastguard Worker ///
219*9880d681SAndroid Build Coastguard Worker /// This representation is in the same format accepted by the string
220*9880d681SAndroid Build Coastguard Worker /// constructor above. This should not be used to compare two DataLayout as
221*9880d681SAndroid Build Coastguard Worker /// different string can represent the same layout.
getStringRepresentation()222*9880d681SAndroid Build Coastguard Worker const std::string &getStringRepresentation() const {
223*9880d681SAndroid Build Coastguard Worker return StringRepresentation;
224*9880d681SAndroid Build Coastguard Worker }
225*9880d681SAndroid Build Coastguard Worker
226*9880d681SAndroid Build Coastguard Worker /// \brief Test if the DataLayout was constructed from an empty string.
isDefault()227*9880d681SAndroid Build Coastguard Worker bool isDefault() const { return StringRepresentation.empty(); }
228*9880d681SAndroid Build Coastguard Worker
229*9880d681SAndroid Build Coastguard Worker /// \brief Returns true if the specified type is known to be a native integer
230*9880d681SAndroid Build Coastguard Worker /// type supported by the CPU.
231*9880d681SAndroid Build Coastguard Worker ///
232*9880d681SAndroid Build Coastguard Worker /// For example, i64 is not native on most 32-bit CPUs and i37 is not native
233*9880d681SAndroid Build Coastguard Worker /// on any known one. This returns false if the integer width is not legal.
234*9880d681SAndroid Build Coastguard Worker ///
235*9880d681SAndroid Build Coastguard Worker /// The width is specified in bits.
isLegalInteger(uint64_t Width)236*9880d681SAndroid Build Coastguard Worker bool isLegalInteger(uint64_t Width) const {
237*9880d681SAndroid Build Coastguard Worker for (unsigned LegalIntWidth : LegalIntWidths)
238*9880d681SAndroid Build Coastguard Worker if (LegalIntWidth == Width)
239*9880d681SAndroid Build Coastguard Worker return true;
240*9880d681SAndroid Build Coastguard Worker return false;
241*9880d681SAndroid Build Coastguard Worker }
242*9880d681SAndroid Build Coastguard Worker
isIllegalInteger(uint64_t Width)243*9880d681SAndroid Build Coastguard Worker bool isIllegalInteger(uint64_t Width) const { return !isLegalInteger(Width); }
244*9880d681SAndroid Build Coastguard Worker
245*9880d681SAndroid Build Coastguard Worker /// Returns true if the given alignment exceeds the natural stack alignment.
exceedsNaturalStackAlignment(unsigned Align)246*9880d681SAndroid Build Coastguard Worker bool exceedsNaturalStackAlignment(unsigned Align) const {
247*9880d681SAndroid Build Coastguard Worker return (StackNaturalAlign != 0) && (Align > StackNaturalAlign);
248*9880d681SAndroid Build Coastguard Worker }
249*9880d681SAndroid Build Coastguard Worker
getStackAlignment()250*9880d681SAndroid Build Coastguard Worker unsigned getStackAlignment() const { return StackNaturalAlign; }
251*9880d681SAndroid Build Coastguard Worker
hasMicrosoftFastStdCallMangling()252*9880d681SAndroid Build Coastguard Worker bool hasMicrosoftFastStdCallMangling() const {
253*9880d681SAndroid Build Coastguard Worker return ManglingMode == MM_WinCOFFX86;
254*9880d681SAndroid Build Coastguard Worker }
255*9880d681SAndroid Build Coastguard Worker
hasLinkerPrivateGlobalPrefix()256*9880d681SAndroid Build Coastguard Worker bool hasLinkerPrivateGlobalPrefix() const { return ManglingMode == MM_MachO; }
257*9880d681SAndroid Build Coastguard Worker
getLinkerPrivateGlobalPrefix()258*9880d681SAndroid Build Coastguard Worker const char *getLinkerPrivateGlobalPrefix() const {
259*9880d681SAndroid Build Coastguard Worker if (ManglingMode == MM_MachO)
260*9880d681SAndroid Build Coastguard Worker return "l";
261*9880d681SAndroid Build Coastguard Worker return "";
262*9880d681SAndroid Build Coastguard Worker }
263*9880d681SAndroid Build Coastguard Worker
getGlobalPrefix()264*9880d681SAndroid Build Coastguard Worker char getGlobalPrefix() const {
265*9880d681SAndroid Build Coastguard Worker switch (ManglingMode) {
266*9880d681SAndroid Build Coastguard Worker case MM_None:
267*9880d681SAndroid Build Coastguard Worker case MM_ELF:
268*9880d681SAndroid Build Coastguard Worker case MM_Mips:
269*9880d681SAndroid Build Coastguard Worker case MM_WinCOFF:
270*9880d681SAndroid Build Coastguard Worker return '\0';
271*9880d681SAndroid Build Coastguard Worker case MM_MachO:
272*9880d681SAndroid Build Coastguard Worker case MM_WinCOFFX86:
273*9880d681SAndroid Build Coastguard Worker return '_';
274*9880d681SAndroid Build Coastguard Worker }
275*9880d681SAndroid Build Coastguard Worker llvm_unreachable("invalid mangling mode");
276*9880d681SAndroid Build Coastguard Worker }
277*9880d681SAndroid Build Coastguard Worker
getPrivateGlobalPrefix()278*9880d681SAndroid Build Coastguard Worker const char *getPrivateGlobalPrefix() const {
279*9880d681SAndroid Build Coastguard Worker switch (ManglingMode) {
280*9880d681SAndroid Build Coastguard Worker case MM_None:
281*9880d681SAndroid Build Coastguard Worker return "";
282*9880d681SAndroid Build Coastguard Worker case MM_ELF:
283*9880d681SAndroid Build Coastguard Worker return ".L";
284*9880d681SAndroid Build Coastguard Worker case MM_Mips:
285*9880d681SAndroid Build Coastguard Worker return "$";
286*9880d681SAndroid Build Coastguard Worker case MM_MachO:
287*9880d681SAndroid Build Coastguard Worker case MM_WinCOFF:
288*9880d681SAndroid Build Coastguard Worker case MM_WinCOFFX86:
289*9880d681SAndroid Build Coastguard Worker return "L";
290*9880d681SAndroid Build Coastguard Worker }
291*9880d681SAndroid Build Coastguard Worker llvm_unreachable("invalid mangling mode");
292*9880d681SAndroid Build Coastguard Worker }
293*9880d681SAndroid Build Coastguard Worker
294*9880d681SAndroid Build Coastguard Worker static const char *getManglingComponent(const Triple &T);
295*9880d681SAndroid Build Coastguard Worker
296*9880d681SAndroid Build Coastguard Worker /// \brief Returns true if the specified type fits in a native integer type
297*9880d681SAndroid Build Coastguard Worker /// supported by the CPU.
298*9880d681SAndroid Build Coastguard Worker ///
299*9880d681SAndroid Build Coastguard Worker /// For example, if the CPU only supports i32 as a native integer type, then
300*9880d681SAndroid Build Coastguard Worker /// i27 fits in a legal integer type but i45 does not.
fitsInLegalInteger(unsigned Width)301*9880d681SAndroid Build Coastguard Worker bool fitsInLegalInteger(unsigned Width) const {
302*9880d681SAndroid Build Coastguard Worker for (unsigned LegalIntWidth : LegalIntWidths)
303*9880d681SAndroid Build Coastguard Worker if (Width <= LegalIntWidth)
304*9880d681SAndroid Build Coastguard Worker return true;
305*9880d681SAndroid Build Coastguard Worker return false;
306*9880d681SAndroid Build Coastguard Worker }
307*9880d681SAndroid Build Coastguard Worker
308*9880d681SAndroid Build Coastguard Worker /// Layout pointer alignment
309*9880d681SAndroid Build Coastguard Worker /// FIXME: The defaults need to be removed once all of
310*9880d681SAndroid Build Coastguard Worker /// the backends/clients are updated.
311*9880d681SAndroid Build Coastguard Worker unsigned getPointerABIAlignment(unsigned AS = 0) const;
312*9880d681SAndroid Build Coastguard Worker
313*9880d681SAndroid Build Coastguard Worker /// Return target's alignment for stack-based pointers
314*9880d681SAndroid Build Coastguard Worker /// FIXME: The defaults need to be removed once all of
315*9880d681SAndroid Build Coastguard Worker /// the backends/clients are updated.
316*9880d681SAndroid Build Coastguard Worker unsigned getPointerPrefAlignment(unsigned AS = 0) const;
317*9880d681SAndroid Build Coastguard Worker
318*9880d681SAndroid Build Coastguard Worker /// Layout pointer size
319*9880d681SAndroid Build Coastguard Worker /// FIXME: The defaults need to be removed once all of
320*9880d681SAndroid Build Coastguard Worker /// the backends/clients are updated.
321*9880d681SAndroid Build Coastguard Worker unsigned getPointerSize(unsigned AS = 0) const;
322*9880d681SAndroid Build Coastguard Worker
323*9880d681SAndroid Build Coastguard Worker /// Layout pointer size, in bits
324*9880d681SAndroid Build Coastguard Worker /// FIXME: The defaults need to be removed once all of
325*9880d681SAndroid Build Coastguard Worker /// the backends/clients are updated.
326*9880d681SAndroid Build Coastguard Worker unsigned getPointerSizeInBits(unsigned AS = 0) const {
327*9880d681SAndroid Build Coastguard Worker return getPointerSize(AS) * 8;
328*9880d681SAndroid Build Coastguard Worker }
329*9880d681SAndroid Build Coastguard Worker
330*9880d681SAndroid Build Coastguard Worker /// Layout pointer size, in bits, based on the type. If this function is
331*9880d681SAndroid Build Coastguard Worker /// called with a pointer type, then the type size of the pointer is returned.
332*9880d681SAndroid Build Coastguard Worker /// If this function is called with a vector of pointers, then the type size
333*9880d681SAndroid Build Coastguard Worker /// of the pointer is returned. This should only be called with a pointer or
334*9880d681SAndroid Build Coastguard Worker /// vector of pointers.
335*9880d681SAndroid Build Coastguard Worker unsigned getPointerTypeSizeInBits(Type *) const;
336*9880d681SAndroid Build Coastguard Worker
getPointerTypeSize(Type * Ty)337*9880d681SAndroid Build Coastguard Worker unsigned getPointerTypeSize(Type *Ty) const {
338*9880d681SAndroid Build Coastguard Worker return getPointerTypeSizeInBits(Ty) / 8;
339*9880d681SAndroid Build Coastguard Worker }
340*9880d681SAndroid Build Coastguard Worker
341*9880d681SAndroid Build Coastguard Worker /// Size examples:
342*9880d681SAndroid Build Coastguard Worker ///
343*9880d681SAndroid Build Coastguard Worker /// Type SizeInBits StoreSizeInBits AllocSizeInBits[*]
344*9880d681SAndroid Build Coastguard Worker /// ---- ---------- --------------- ---------------
345*9880d681SAndroid Build Coastguard Worker /// i1 1 8 8
346*9880d681SAndroid Build Coastguard Worker /// i8 8 8 8
347*9880d681SAndroid Build Coastguard Worker /// i19 19 24 32
348*9880d681SAndroid Build Coastguard Worker /// i32 32 32 32
349*9880d681SAndroid Build Coastguard Worker /// i100 100 104 128
350*9880d681SAndroid Build Coastguard Worker /// i128 128 128 128
351*9880d681SAndroid Build Coastguard Worker /// Float 32 32 32
352*9880d681SAndroid Build Coastguard Worker /// Double 64 64 64
353*9880d681SAndroid Build Coastguard Worker /// X86_FP80 80 80 96
354*9880d681SAndroid Build Coastguard Worker ///
355*9880d681SAndroid Build Coastguard Worker /// [*] The alloc size depends on the alignment, and thus on the target.
356*9880d681SAndroid Build Coastguard Worker /// These values are for x86-32 linux.
357*9880d681SAndroid Build Coastguard Worker
358*9880d681SAndroid Build Coastguard Worker /// \brief Returns the number of bits necessary to hold the specified type.
359*9880d681SAndroid Build Coastguard Worker ///
360*9880d681SAndroid Build Coastguard Worker /// For example, returns 36 for i36 and 80 for x86_fp80. The type passed must
361*9880d681SAndroid Build Coastguard Worker /// have a size (Type::isSized() must return true).
362*9880d681SAndroid Build Coastguard Worker uint64_t getTypeSizeInBits(Type *Ty) const;
363*9880d681SAndroid Build Coastguard Worker
364*9880d681SAndroid Build Coastguard Worker /// \brief Returns the maximum number of bytes that may be overwritten by
365*9880d681SAndroid Build Coastguard Worker /// storing the specified type.
366*9880d681SAndroid Build Coastguard Worker ///
367*9880d681SAndroid Build Coastguard Worker /// For example, returns 5 for i36 and 10 for x86_fp80.
getTypeStoreSize(Type * Ty)368*9880d681SAndroid Build Coastguard Worker uint64_t getTypeStoreSize(Type *Ty) const {
369*9880d681SAndroid Build Coastguard Worker return (getTypeSizeInBits(Ty) + 7) / 8;
370*9880d681SAndroid Build Coastguard Worker }
371*9880d681SAndroid Build Coastguard Worker
372*9880d681SAndroid Build Coastguard Worker /// \brief Returns the maximum number of bits that may be overwritten by
373*9880d681SAndroid Build Coastguard Worker /// storing the specified type; always a multiple of 8.
374*9880d681SAndroid Build Coastguard Worker ///
375*9880d681SAndroid Build Coastguard Worker /// For example, returns 40 for i36 and 80 for x86_fp80.
getTypeStoreSizeInBits(Type * Ty)376*9880d681SAndroid Build Coastguard Worker uint64_t getTypeStoreSizeInBits(Type *Ty) const {
377*9880d681SAndroid Build Coastguard Worker return 8 * getTypeStoreSize(Ty);
378*9880d681SAndroid Build Coastguard Worker }
379*9880d681SAndroid Build Coastguard Worker
380*9880d681SAndroid Build Coastguard Worker /// \brief Returns the offset in bytes between successive objects of the
381*9880d681SAndroid Build Coastguard Worker /// specified type, including alignment padding.
382*9880d681SAndroid Build Coastguard Worker ///
383*9880d681SAndroid Build Coastguard Worker /// This is the amount that alloca reserves for this type. For example,
384*9880d681SAndroid Build Coastguard Worker /// returns 12 or 16 for x86_fp80, depending on alignment.
getTypeAllocSize(Type * Ty)385*9880d681SAndroid Build Coastguard Worker uint64_t getTypeAllocSize(Type *Ty) const {
386*9880d681SAndroid Build Coastguard Worker // Round up to the next alignment boundary.
387*9880d681SAndroid Build Coastguard Worker return alignTo(getTypeStoreSize(Ty), getABITypeAlignment(Ty));
388*9880d681SAndroid Build Coastguard Worker }
389*9880d681SAndroid Build Coastguard Worker
390*9880d681SAndroid Build Coastguard Worker /// \brief Returns the offset in bits between successive objects of the
391*9880d681SAndroid Build Coastguard Worker /// specified type, including alignment padding; always a multiple of 8.
392*9880d681SAndroid Build Coastguard Worker ///
393*9880d681SAndroid Build Coastguard Worker /// This is the amount that alloca reserves for this type. For example,
394*9880d681SAndroid Build Coastguard Worker /// returns 96 or 128 for x86_fp80, depending on alignment.
getTypeAllocSizeInBits(Type * Ty)395*9880d681SAndroid Build Coastguard Worker uint64_t getTypeAllocSizeInBits(Type *Ty) const {
396*9880d681SAndroid Build Coastguard Worker return 8 * getTypeAllocSize(Ty);
397*9880d681SAndroid Build Coastguard Worker }
398*9880d681SAndroid Build Coastguard Worker
399*9880d681SAndroid Build Coastguard Worker /// \brief Returns the minimum ABI-required alignment for the specified type.
400*9880d681SAndroid Build Coastguard Worker unsigned getABITypeAlignment(Type *Ty) const;
401*9880d681SAndroid Build Coastguard Worker
402*9880d681SAndroid Build Coastguard Worker /// \brief Returns the minimum ABI-required alignment for an integer type of
403*9880d681SAndroid Build Coastguard Worker /// the specified bitwidth.
404*9880d681SAndroid Build Coastguard Worker unsigned getABIIntegerTypeAlignment(unsigned BitWidth) const;
405*9880d681SAndroid Build Coastguard Worker
406*9880d681SAndroid Build Coastguard Worker /// \brief Returns the preferred stack/global alignment for the specified
407*9880d681SAndroid Build Coastguard Worker /// type.
408*9880d681SAndroid Build Coastguard Worker ///
409*9880d681SAndroid Build Coastguard Worker /// This is always at least as good as the ABI alignment.
410*9880d681SAndroid Build Coastguard Worker unsigned getPrefTypeAlignment(Type *Ty) const;
411*9880d681SAndroid Build Coastguard Worker
412*9880d681SAndroid Build Coastguard Worker /// \brief Returns the preferred alignment for the specified type, returned as
413*9880d681SAndroid Build Coastguard Worker /// log2 of the value (a shift amount).
414*9880d681SAndroid Build Coastguard Worker unsigned getPreferredTypeAlignmentShift(Type *Ty) const;
415*9880d681SAndroid Build Coastguard Worker
416*9880d681SAndroid Build Coastguard Worker /// \brief Returns an integer type with size at least as big as that of a
417*9880d681SAndroid Build Coastguard Worker /// pointer in the given address space.
418*9880d681SAndroid Build Coastguard Worker IntegerType *getIntPtrType(LLVMContext &C, unsigned AddressSpace = 0) const;
419*9880d681SAndroid Build Coastguard Worker
420*9880d681SAndroid Build Coastguard Worker /// \brief Returns an integer (vector of integer) type with size at least as
421*9880d681SAndroid Build Coastguard Worker /// big as that of a pointer of the given pointer (vector of pointer) type.
422*9880d681SAndroid Build Coastguard Worker Type *getIntPtrType(Type *) const;
423*9880d681SAndroid Build Coastguard Worker
424*9880d681SAndroid Build Coastguard Worker /// \brief Returns the smallest integer type with size at least as big as
425*9880d681SAndroid Build Coastguard Worker /// Width bits.
426*9880d681SAndroid Build Coastguard Worker Type *getSmallestLegalIntType(LLVMContext &C, unsigned Width = 0) const;
427*9880d681SAndroid Build Coastguard Worker
428*9880d681SAndroid Build Coastguard Worker /// \brief Returns the largest legal integer type, or null if none are set.
getLargestLegalIntType(LLVMContext & C)429*9880d681SAndroid Build Coastguard Worker Type *getLargestLegalIntType(LLVMContext &C) const {
430*9880d681SAndroid Build Coastguard Worker unsigned LargestSize = getLargestLegalIntTypeSizeInBits();
431*9880d681SAndroid Build Coastguard Worker return (LargestSize == 0) ? nullptr : Type::getIntNTy(C, LargestSize);
432*9880d681SAndroid Build Coastguard Worker }
433*9880d681SAndroid Build Coastguard Worker
434*9880d681SAndroid Build Coastguard Worker /// \brief Returns the size of largest legal integer type size, or 0 if none
435*9880d681SAndroid Build Coastguard Worker /// are set.
436*9880d681SAndroid Build Coastguard Worker unsigned getLargestLegalIntTypeSizeInBits() const;
437*9880d681SAndroid Build Coastguard Worker
438*9880d681SAndroid Build Coastguard Worker /// \brief Returns the offset from the beginning of the type for the specified
439*9880d681SAndroid Build Coastguard Worker /// indices.
440*9880d681SAndroid Build Coastguard Worker ///
441*9880d681SAndroid Build Coastguard Worker /// Note that this takes the element type, not the pointer type.
442*9880d681SAndroid Build Coastguard Worker /// This is used to implement getelementptr.
443*9880d681SAndroid Build Coastguard Worker int64_t getIndexedOffsetInType(Type *ElemTy, ArrayRef<Value *> Indices) const;
444*9880d681SAndroid Build Coastguard Worker
445*9880d681SAndroid Build Coastguard Worker /// \brief Returns a StructLayout object, indicating the alignment of the
446*9880d681SAndroid Build Coastguard Worker /// struct, its size, and the offsets of its fields.
447*9880d681SAndroid Build Coastguard Worker ///
448*9880d681SAndroid Build Coastguard Worker /// Note that this information is lazily cached.
449*9880d681SAndroid Build Coastguard Worker const StructLayout *getStructLayout(StructType *Ty) const;
450*9880d681SAndroid Build Coastguard Worker
451*9880d681SAndroid Build Coastguard Worker /// \brief Returns the preferred alignment of the specified global.
452*9880d681SAndroid Build Coastguard Worker ///
453*9880d681SAndroid Build Coastguard Worker /// This includes an explicitly requested alignment (if the global has one).
454*9880d681SAndroid Build Coastguard Worker unsigned getPreferredAlignment(const GlobalVariable *GV) const;
455*9880d681SAndroid Build Coastguard Worker
456*9880d681SAndroid Build Coastguard Worker /// \brief Returns the preferred alignment of the specified global, returned
457*9880d681SAndroid Build Coastguard Worker /// in log form.
458*9880d681SAndroid Build Coastguard Worker ///
459*9880d681SAndroid Build Coastguard Worker /// This includes an explicitly requested alignment (if the global has one).
460*9880d681SAndroid Build Coastguard Worker unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const;
461*9880d681SAndroid Build Coastguard Worker };
462*9880d681SAndroid Build Coastguard Worker
unwrap(LLVMTargetDataRef P)463*9880d681SAndroid Build Coastguard Worker inline DataLayout *unwrap(LLVMTargetDataRef P) {
464*9880d681SAndroid Build Coastguard Worker return reinterpret_cast<DataLayout *>(P);
465*9880d681SAndroid Build Coastguard Worker }
466*9880d681SAndroid Build Coastguard Worker
wrap(const DataLayout * P)467*9880d681SAndroid Build Coastguard Worker inline LLVMTargetDataRef wrap(const DataLayout *P) {
468*9880d681SAndroid Build Coastguard Worker return reinterpret_cast<LLVMTargetDataRef>(const_cast<DataLayout *>(P));
469*9880d681SAndroid Build Coastguard Worker }
470*9880d681SAndroid Build Coastguard Worker
471*9880d681SAndroid Build Coastguard Worker /// Used to lazily calculate structure layout information for a target machine,
472*9880d681SAndroid Build Coastguard Worker /// based on the DataLayout structure.
473*9880d681SAndroid Build Coastguard Worker class StructLayout {
474*9880d681SAndroid Build Coastguard Worker uint64_t StructSize;
475*9880d681SAndroid Build Coastguard Worker unsigned StructAlignment;
476*9880d681SAndroid Build Coastguard Worker unsigned IsPadded : 1;
477*9880d681SAndroid Build Coastguard Worker unsigned NumElements : 31;
478*9880d681SAndroid Build Coastguard Worker uint64_t MemberOffsets[1]; // variable sized array!
479*9880d681SAndroid Build Coastguard Worker public:
getSizeInBytes()480*9880d681SAndroid Build Coastguard Worker uint64_t getSizeInBytes() const { return StructSize; }
481*9880d681SAndroid Build Coastguard Worker
getSizeInBits()482*9880d681SAndroid Build Coastguard Worker uint64_t getSizeInBits() const { return 8 * StructSize; }
483*9880d681SAndroid Build Coastguard Worker
getAlignment()484*9880d681SAndroid Build Coastguard Worker unsigned getAlignment() const { return StructAlignment; }
485*9880d681SAndroid Build Coastguard Worker
486*9880d681SAndroid Build Coastguard Worker /// Returns whether the struct has padding or not between its fields.
487*9880d681SAndroid Build Coastguard Worker /// NB: Padding in nested element is not taken into account.
hasPadding()488*9880d681SAndroid Build Coastguard Worker bool hasPadding() const { return IsPadded; }
489*9880d681SAndroid Build Coastguard Worker
490*9880d681SAndroid Build Coastguard Worker /// \brief Given a valid byte offset into the structure, returns the structure
491*9880d681SAndroid Build Coastguard Worker /// index that contains it.
492*9880d681SAndroid Build Coastguard Worker unsigned getElementContainingOffset(uint64_t Offset) const;
493*9880d681SAndroid Build Coastguard Worker
getElementOffset(unsigned Idx)494*9880d681SAndroid Build Coastguard Worker uint64_t getElementOffset(unsigned Idx) const {
495*9880d681SAndroid Build Coastguard Worker assert(Idx < NumElements && "Invalid element idx!");
496*9880d681SAndroid Build Coastguard Worker return MemberOffsets[Idx];
497*9880d681SAndroid Build Coastguard Worker }
498*9880d681SAndroid Build Coastguard Worker
getElementOffsetInBits(unsigned Idx)499*9880d681SAndroid Build Coastguard Worker uint64_t getElementOffsetInBits(unsigned Idx) const {
500*9880d681SAndroid Build Coastguard Worker return getElementOffset(Idx) * 8;
501*9880d681SAndroid Build Coastguard Worker }
502*9880d681SAndroid Build Coastguard Worker
503*9880d681SAndroid Build Coastguard Worker private:
504*9880d681SAndroid Build Coastguard Worker friend class DataLayout; // Only DataLayout can create this class
505*9880d681SAndroid Build Coastguard Worker StructLayout(StructType *ST, const DataLayout &DL);
506*9880d681SAndroid Build Coastguard Worker };
507*9880d681SAndroid Build Coastguard Worker
508*9880d681SAndroid Build Coastguard Worker // The implementation of this method is provided inline as it is particularly
509*9880d681SAndroid Build Coastguard Worker // well suited to constant folding when called on a specific Type subclass.
getTypeSizeInBits(Type * Ty)510*9880d681SAndroid Build Coastguard Worker inline uint64_t DataLayout::getTypeSizeInBits(Type *Ty) const {
511*9880d681SAndroid Build Coastguard Worker assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
512*9880d681SAndroid Build Coastguard Worker switch (Ty->getTypeID()) {
513*9880d681SAndroid Build Coastguard Worker case Type::LabelTyID:
514*9880d681SAndroid Build Coastguard Worker return getPointerSizeInBits(0);
515*9880d681SAndroid Build Coastguard Worker case Type::PointerTyID:
516*9880d681SAndroid Build Coastguard Worker return getPointerSizeInBits(Ty->getPointerAddressSpace());
517*9880d681SAndroid Build Coastguard Worker case Type::ArrayTyID: {
518*9880d681SAndroid Build Coastguard Worker ArrayType *ATy = cast<ArrayType>(Ty);
519*9880d681SAndroid Build Coastguard Worker return ATy->getNumElements() *
520*9880d681SAndroid Build Coastguard Worker getTypeAllocSizeInBits(ATy->getElementType());
521*9880d681SAndroid Build Coastguard Worker }
522*9880d681SAndroid Build Coastguard Worker case Type::StructTyID:
523*9880d681SAndroid Build Coastguard Worker // Get the layout annotation... which is lazily created on demand.
524*9880d681SAndroid Build Coastguard Worker return getStructLayout(cast<StructType>(Ty))->getSizeInBits();
525*9880d681SAndroid Build Coastguard Worker case Type::IntegerTyID:
526*9880d681SAndroid Build Coastguard Worker return Ty->getIntegerBitWidth();
527*9880d681SAndroid Build Coastguard Worker case Type::HalfTyID:
528*9880d681SAndroid Build Coastguard Worker return 16;
529*9880d681SAndroid Build Coastguard Worker case Type::FloatTyID:
530*9880d681SAndroid Build Coastguard Worker return 32;
531*9880d681SAndroid Build Coastguard Worker case Type::DoubleTyID:
532*9880d681SAndroid Build Coastguard Worker case Type::X86_MMXTyID:
533*9880d681SAndroid Build Coastguard Worker return 64;
534*9880d681SAndroid Build Coastguard Worker case Type::PPC_FP128TyID:
535*9880d681SAndroid Build Coastguard Worker case Type::FP128TyID:
536*9880d681SAndroid Build Coastguard Worker return 128;
537*9880d681SAndroid Build Coastguard Worker // In memory objects this is always aligned to a higher boundary, but
538*9880d681SAndroid Build Coastguard Worker // only 80 bits contain information.
539*9880d681SAndroid Build Coastguard Worker case Type::X86_FP80TyID:
540*9880d681SAndroid Build Coastguard Worker return 80;
541*9880d681SAndroid Build Coastguard Worker case Type::VectorTyID: {
542*9880d681SAndroid Build Coastguard Worker VectorType *VTy = cast<VectorType>(Ty);
543*9880d681SAndroid Build Coastguard Worker return VTy->getNumElements() * getTypeSizeInBits(VTy->getElementType());
544*9880d681SAndroid Build Coastguard Worker }
545*9880d681SAndroid Build Coastguard Worker default:
546*9880d681SAndroid Build Coastguard Worker llvm_unreachable("DataLayout::getTypeSizeInBits(): Unsupported type");
547*9880d681SAndroid Build Coastguard Worker }
548*9880d681SAndroid Build Coastguard Worker }
549*9880d681SAndroid Build Coastguard Worker
550*9880d681SAndroid Build Coastguard Worker } // End llvm namespace
551*9880d681SAndroid Build Coastguard Worker
552*9880d681SAndroid Build Coastguard Worker #endif
553