1 //===-- llvm/Target/TargetData.h - Data size & alignment info ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines target properties related to datatype size/offset/alignment
11 // information. It uses lazy annotations to cache information about how
12 // structure types are laid out and used.
14 // This structure should be created once, filled in if the defaults are not
15 // correct and then passed around by const&. None of the members functions
16 // require modification to the object.
18 //===----------------------------------------------------------------------===//
20 #ifndef LLVM_TARGET_TARGETDATA_H
21 #define LLVM_TARGET_TARGETDATA_H
23 #include "llvm/Pass.h"
24 #include "llvm/Support/DataTypes.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include "llvm/Config/config.h"
37 /// Enum used to categorize the alignment types stored by TargetAlignElem
39 INTEGER_ALIGN = 'i', ///< Integer type alignment
40 VECTOR_ALIGN = 'v', ///< Vector type alignment
41 FLOAT_ALIGN = 'f', ///< Floating point type alignment
42 AGGREGATE_ALIGN = 'a', ///< Aggregate alignment
43 STACK_ALIGN = 's' ///< Stack objects alignment
45 /// Target alignment element.
47 /// Stores the alignment data associated with a given alignment type (pointer,
48 /// integer, vector, float) and type bit width.
50 /// @note The unusual order of elements in the structure attempts to reduce
51 /// padding and make the structure slightly more cache friendly.
52 struct TargetAlignElem {
53 AlignTypeEnum AlignType : 8; //< Alignment type (AlignTypeEnum)
54 unsigned char ABIAlign; //< ABI alignment for this type/bitw
55 unsigned char PrefAlign; //< Pref. alignment for this type/bitw
56 uint32_t TypeBitWidth; //< Type bit width
59 static TargetAlignElem get(AlignTypeEnum align_type, unsigned char abi_align,
60 unsigned char pref_align, uint32_t bit_width);
61 /// Equality predicate
62 bool operator==(const TargetAlignElem &rhs) const;
63 /// output stream operator
64 std::ostream &dump(std::ostream &os) const;
67 class TargetData : public ImmutablePass {
69 bool LittleEndian; ///< Defaults to false
70 unsigned char PointerMemSize; ///< Pointer size in bytes
71 unsigned char PointerABIAlign; ///< Pointer ABI alignment
72 unsigned char PointerPrefAlign; ///< Pointer preferred alignment
74 //! Where the primitive type alignment data is stored.
77 @note Could support multiple size pointer alignments, e.g., 32-bit pointers
78 vs. 64-bit pointers by extending TargetAlignment, but for now, we don't.
80 SmallVector<TargetAlignElem, 16> Alignments;
81 //! Alignment iterator shorthand
82 typedef SmallVector<TargetAlignElem, 16>::iterator align_iterator;
83 //! Constant alignment iterator shorthand
84 typedef SmallVector<TargetAlignElem, 16>::const_iterator align_const_iterator;
85 //! Invalid alignment.
87 This member is a signal that a requested alignment type and bit width were
88 not found in the SmallVector.
90 static const TargetAlignElem InvalidAlignmentElem;
92 //! Set/initialize target alignments
93 void setAlignment(AlignTypeEnum align_type, unsigned char abi_align,
94 unsigned char pref_align, uint32_t bit_width);
95 unsigned getAlignmentInfo(AlignTypeEnum align_type, uint32_t bit_width,
97 //! Internal helper method that returns requested alignment for type.
98 unsigned char getAlignment(const Type *Ty, bool abi_or_pref) const;
100 /// Valid alignment predicate.
102 /// Predicate that tests a TargetAlignElem reference returned by get() against
103 /// InvalidAlignmentElem.
104 inline bool validAlignment(const TargetAlignElem &align) const {
105 return (&align != &InvalidAlignmentElem);
111 /// @note This has to exist, because this is a pass, but it should never be
113 TargetData() : ImmutablePass(intptr_t(&ID)) {
114 assert(0 && "ERROR: Bad TargetData ctor used. "
115 "Tool did not specify a TargetData to use?");
119 /// Constructs a TargetData from a specification string. See init().
120 explicit TargetData(const std::string &TargetDescription)
121 : ImmutablePass(intptr_t(&ID)) {
122 init(TargetDescription);
125 /// Initialize target data from properties stored in the module.
126 explicit TargetData(const Module *M);
128 TargetData(const TargetData &TD) :
129 ImmutablePass(intptr_t(&ID)),
130 LittleEndian(TD.isLittleEndian()),
131 PointerMemSize(TD.PointerMemSize),
132 PointerABIAlign(TD.PointerABIAlign),
133 PointerPrefAlign(TD.PointerPrefAlign),
134 Alignments(TD.Alignments)
137 ~TargetData(); // Not virtual, do not subclass this class
139 //! Parse a target data layout string and initialize TargetData alignments.
140 void init(const std::string &TargetDescription);
142 /// Target endianness...
143 bool isLittleEndian() const { return LittleEndian; }
144 bool isBigEndian() const { return !LittleEndian; }
146 /// Host endianness...
147 bool hostIsLittleEndian() const {
154 bool hostIsBigEndian() const { return !hostIsLittleEndian(); }
156 /// getStringRepresentation - Return the string representation of the
157 /// TargetData. This representation is in the same format accepted by the
158 /// string constructor above.
159 std::string getStringRepresentation() const;
160 /// Target pointer alignment
161 unsigned char getPointerABIAlignment() const { return PointerABIAlign; }
162 /// Return target's alignment for stack-based pointers
163 unsigned char getPointerPrefAlignment() const { return PointerPrefAlign; }
164 /// Target pointer size
165 unsigned char getPointerSize() const { return PointerMemSize; }
166 /// Target pointer size, in bits
167 unsigned char getPointerSizeInBits() const { return 8*PointerMemSize; }
169 /// getTypeSizeInBits - Return the number of bits necessary to hold the
170 /// specified type. For example, returns 36 for i36 and 80 for x86_fp80.
171 uint64_t getTypeSizeInBits(const Type* Ty) const;
173 /// getTypeStoreSize - Return the maximum number of bytes that may be
174 /// overwritten by storing the specified type. For example, returns 5
175 /// for i36 and 10 for x86_fp80.
176 uint64_t getTypeStoreSize(const Type *Ty) const {
177 return (getTypeSizeInBits(Ty)+7)/8;
180 /// getTypeStoreSizeInBits - Return the maximum number of bits that may be
181 /// overwritten by storing the specified type; always a multiple of 8. For
182 /// example, returns 40 for i36 and 80 for x86_fp80.
183 uint64_t getTypeStoreSizeInBits(const Type *Ty) const {
184 return 8*getTypeStoreSize(Ty);
187 /// getABITypeSize - Return the offset in bytes between successive objects
188 /// of the specified type, including alignment padding. This is the amount
189 /// that alloca reserves for this type. For example, returns 12 or 16 for
190 /// x86_fp80, depending on alignment.
191 uint64_t getABITypeSize(const Type* Ty) const {
192 unsigned char Align = getABITypeAlignment(Ty);
193 return (getTypeStoreSize(Ty) + Align - 1)/Align*Align;
196 /// getABITypeSizeInBits - Return the offset in bits between successive
197 /// objects of the specified type, including alignment padding; always a
198 /// multiple of 8. This is the amount that alloca reserves for this type.
199 /// For example, returns 96 or 128 for x86_fp80, depending on alignment.
200 uint64_t getABITypeSizeInBits(const Type* Ty) const {
201 return 8*getABITypeSize(Ty);
204 /// getABITypeAlignment - Return the minimum ABI-required alignment for the
206 unsigned char getABITypeAlignment(const Type *Ty) const;
208 /// getCallFrameTypeAlignment - Return the minimum ABI-required alignment
209 /// for the specified type when it is part of a call frame.
210 unsigned char getCallFrameTypeAlignment(const Type *Ty) const;
213 /// getPrefTypeAlignment - Return the preferred stack/global alignment for
214 /// the specified type.
215 unsigned char getPrefTypeAlignment(const Type *Ty) const;
217 /// getPreferredTypeAlignmentShift - Return the preferred alignment for the
218 /// specified type, returned as log2 of the value (a shift amount).
220 unsigned char getPreferredTypeAlignmentShift(const Type *Ty) const;
222 /// getIntPtrType - Return an unsigned integer type that is the same size or
223 /// greater to the host pointer size.
225 const Type *getIntPtrType() const;
227 /// getIndexedOffset - return the offset from the beginning of the type for the
228 /// specified indices. This is used to implement getelementptr.
230 uint64_t getIndexedOffset(const Type *Ty,
231 Value* const* Indices, unsigned NumIndices) const;
233 /// getStructLayout - Return a StructLayout object, indicating the alignment
234 /// of the struct, its size, and the offsets of its fields. Note that this
235 /// information is lazily cached.
236 const StructLayout *getStructLayout(const StructType *Ty) const;
238 /// InvalidateStructLayoutInfo - TargetData speculatively caches StructLayout
239 /// objects. If a TargetData object is alive when types are being refined and
240 /// removed, this method must be called whenever a StructType is removed to
241 /// avoid a dangling pointer in this cache.
242 void InvalidateStructLayoutInfo(const StructType *Ty) const;
244 /// getPreferredAlignmentLog - Return the preferred alignment of the
245 /// specified global, returned in log form. This includes an explicitly
246 /// requested alignment (if the global has one).
247 unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const;
249 static char ID; // Pass identification, replacement for typeid
252 /// StructLayout - used to lazily calculate structure layout information for a
253 /// target machine, based on the TargetData structure.
257 unsigned StructAlignment;
258 unsigned NumElements;
259 uint64_t MemberOffsets[1]; // variable sized array!
262 uint64_t getSizeInBytes() const {
266 uint64_t getSizeInBits() const {
270 unsigned getAlignment() const {
271 return StructAlignment;
274 /// getElementContainingOffset - Given a valid offset into the structure,
275 /// return the structure index that contains it.
277 unsigned getElementContainingOffset(uint64_t Offset) const;
279 uint64_t getElementOffset(unsigned Idx) const {
280 assert(Idx < NumElements && "Invalid element idx!");
281 return MemberOffsets[Idx];
284 uint64_t getElementOffsetInBits(unsigned Idx) const {
285 return getElementOffset(Idx)*8;
289 friend class TargetData; // Only TargetData can create this class
290 StructLayout(const StructType *ST, const TargetData &TD);
293 } // End llvm namespace