1 //===-- llvm/Target/TargetData.h - Data size & alignment info ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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/Support/ErrorHandling.h"
26 #include "llvm/ADT/SmallVector.h"
38 /// Enum used to categorize the alignment types stored by TargetAlignElem
40 INTEGER_ALIGN = 'i', ///< Integer type alignment
41 VECTOR_ALIGN = 'v', ///< Vector type alignment
42 FLOAT_ALIGN = 'f', ///< Floating point type alignment
43 AGGREGATE_ALIGN = 'a', ///< Aggregate alignment
44 STACK_ALIGN = 's' ///< Stack objects alignment
46 /// Target alignment element.
48 /// Stores the alignment data associated with a given alignment type (pointer,
49 /// integer, vector, float) and type bit width.
51 /// @note The unusual order of elements in the structure attempts to reduce
52 /// padding and make the structure slightly more cache friendly.
53 struct TargetAlignElem {
54 AlignTypeEnum AlignType : 8; //< Alignment type (AlignTypeEnum)
55 unsigned char ABIAlign; //< ABI alignment for this type/bitw
56 unsigned char PrefAlign; //< Pref. alignment for this type/bitw
57 uint32_t TypeBitWidth; //< Type bit width
60 static TargetAlignElem get(AlignTypeEnum align_type, unsigned char abi_align,
61 unsigned char pref_align, uint32_t bit_width);
62 /// Equality predicate
63 bool operator==(const TargetAlignElem &rhs) const;
64 /// output stream operator
65 std::ostream &dump(std::ostream &os) const;
68 class TargetData : public ImmutablePass {
70 bool LittleEndian; ///< Defaults to false
71 unsigned char PointerMemSize; ///< Pointer size in bytes
72 unsigned char PointerABIAlign; ///< Pointer ABI alignment
73 unsigned char PointerPrefAlign; ///< Pointer preferred alignment
75 //! Where the primitive type alignment data is stored.
78 @note Could support multiple size pointer alignments, e.g., 32-bit pointers
79 vs. 64-bit pointers by extending TargetAlignment, but for now, we don't.
81 SmallVector<TargetAlignElem, 16> Alignments;
82 //! Alignment iterator shorthand
83 typedef SmallVector<TargetAlignElem, 16>::iterator align_iterator;
84 //! Constant alignment iterator shorthand
85 typedef SmallVector<TargetAlignElem, 16>::const_iterator align_const_iterator;
86 //! Invalid alignment.
88 This member is a signal that a requested alignment type and bit width were
89 not found in the SmallVector.
91 static const TargetAlignElem InvalidAlignmentElem;
93 //! Set/initialize target alignments
94 void setAlignment(AlignTypeEnum align_type, unsigned char abi_align,
95 unsigned char pref_align, uint32_t bit_width);
96 unsigned getAlignmentInfo(AlignTypeEnum align_type, uint32_t bit_width,
97 bool ABIAlign, const Type *Ty) const;
98 //! Internal helper method that returns requested alignment for type.
99 unsigned char getAlignment(const Type *Ty, bool abi_or_pref) const;
101 /// Valid alignment predicate.
103 /// Predicate that tests a TargetAlignElem reference returned by get() against
104 /// InvalidAlignmentElem.
105 inline bool validAlignment(const TargetAlignElem &align) const {
106 return (&align != &InvalidAlignmentElem);
112 /// @note This has to exist, because this is a pass, but it should never be
114 TargetData() : ImmutablePass(&ID) {
115 llvm_report_error("Bad TargetData ctor used. "
116 "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(&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) :
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 /// getStringRepresentation - Return the string representation of the
147 /// TargetData. This representation is in the same format accepted by the
148 /// string constructor above.
149 std::string getStringRepresentation() const;
150 /// Target pointer alignment
151 unsigned char getPointerABIAlignment() const { return PointerABIAlign; }
152 /// Return target's alignment for stack-based pointers
153 unsigned char getPointerPrefAlignment() const { return PointerPrefAlign; }
154 /// Target pointer size
155 unsigned char getPointerSize() const { return PointerMemSize; }
156 /// Target pointer size, in bits
157 unsigned char getPointerSizeInBits() const { return 8*PointerMemSize; }
161 /// Type SizeInBits StoreSizeInBits AllocSizeInBits[*]
162 /// ---- ---------- --------------- ---------------
171 /// X86_FP80 80 80 96
173 /// [*] The alloc size depends on the alignment, and thus on the target.
174 /// These values are for x86-32 linux.
176 /// getTypeSizeInBits - Return the number of bits necessary to hold the
177 /// specified type. For example, returns 36 for i36 and 80 for x86_fp80.
178 uint64_t getTypeSizeInBits(const Type* Ty) const;
180 /// getTypeStoreSize - Return the maximum number of bytes that may be
181 /// overwritten by storing the specified type. For example, returns 5
182 /// for i36 and 10 for x86_fp80.
183 uint64_t getTypeStoreSize(const Type *Ty) const {
184 return (getTypeSizeInBits(Ty)+7)/8;
187 /// getTypeStoreSizeInBits - Return the maximum number of bits that may be
188 /// overwritten by storing the specified type; always a multiple of 8. For
189 /// example, returns 40 for i36 and 80 for x86_fp80.
190 uint64_t getTypeStoreSizeInBits(const Type *Ty) const {
191 return 8*getTypeStoreSize(Ty);
194 /// getTypeAllocSize - Return the offset in bytes between successive objects
195 /// of the specified type, including alignment padding. This is the amount
196 /// that alloca reserves for this type. For example, returns 12 or 16 for
197 /// x86_fp80, depending on alignment.
198 uint64_t getTypeAllocSize(const Type* Ty) const {
199 // Round up to the next alignment boundary.
200 return RoundUpAlignment(getTypeStoreSize(Ty), getABITypeAlignment(Ty));
203 /// getTypeAllocSizeInBits - Return the offset in bits between successive
204 /// objects of the specified type, including alignment padding; always a
205 /// multiple of 8. This is the amount that alloca reserves for this type.
206 /// For example, returns 96 or 128 for x86_fp80, depending on alignment.
207 uint64_t getTypeAllocSizeInBits(const Type* Ty) const {
208 return 8*getTypeAllocSize(Ty);
211 /// getABITypeAlignment - Return the minimum ABI-required alignment for the
213 unsigned char getABITypeAlignment(const Type *Ty) const;
215 /// getCallFrameTypeAlignment - Return the minimum ABI-required alignment
216 /// for the specified type when it is part of a call frame.
217 unsigned char getCallFrameTypeAlignment(const Type *Ty) const;
220 /// getPrefTypeAlignment - Return the preferred stack/global alignment for
221 /// the specified type. This is always at least as good as the ABI alignment.
222 unsigned char getPrefTypeAlignment(const Type *Ty) const;
224 /// getPreferredTypeAlignmentShift - Return the preferred alignment for the
225 /// specified type, returned as log2 of the value (a shift amount).
227 unsigned char getPreferredTypeAlignmentShift(const Type *Ty) const;
229 /// getIntPtrType - Return an unsigned integer type that is the same size or
230 /// greater to the host pointer size.
232 const IntegerType *getIntPtrType() const;
234 /// getIndexedOffset - return the offset from the beginning of the type for
235 /// the specified indices. This is used to implement getelementptr.
237 uint64_t getIndexedOffset(const Type *Ty,
238 Value* const* Indices, unsigned NumIndices) const;
240 /// getStructLayout - Return a StructLayout object, indicating the alignment
241 /// of the struct, its size, and the offsets of its fields. Note that this
242 /// information is lazily cached.
243 const StructLayout *getStructLayout(const StructType *Ty) const;
245 /// InvalidateStructLayoutInfo - TargetData speculatively caches StructLayout
246 /// objects. If a TargetData object is alive when types are being refined and
247 /// removed, this method must be called whenever a StructType is removed to
248 /// avoid a dangling pointer in this cache.
249 void InvalidateStructLayoutInfo(const StructType *Ty) const;
251 /// getPreferredAlignment - Return the preferred alignment of the specified
252 /// global. This includes an explicitly requested alignment (if the global
254 unsigned getPreferredAlignment(const GlobalVariable *GV) const;
256 /// getPreferredAlignmentLog - Return the preferred alignment of the
257 /// specified global, returned in log form. This includes an explicitly
258 /// requested alignment (if the global has one).
259 unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const;
261 /// RoundUpAlignment - Round the specified value up to the next alignment
262 /// boundary specified by Alignment. For example, 7 rounded up to an
263 /// alignment boundary of 4 is 8. 8 rounded up to the alignment boundary of 4
264 /// is 8 because it is already aligned.
265 template <typename UIntTy>
266 static UIntTy RoundUpAlignment(UIntTy Val, unsigned Alignment) {
267 assert((Alignment & (Alignment-1)) == 0 && "Alignment must be power of 2!");
268 return (Val + (Alignment-1)) & ~UIntTy(Alignment-1);
271 static char ID; // Pass identification, replacement for typeid
274 /// StructLayout - used to lazily calculate structure layout information for a
275 /// target machine, based on the TargetData structure.
279 unsigned StructAlignment;
280 unsigned NumElements;
281 uint64_t MemberOffsets[1]; // variable sized array!
284 uint64_t getSizeInBytes() const {
288 uint64_t getSizeInBits() const {
292 unsigned getAlignment() const {
293 return StructAlignment;
296 /// getElementContainingOffset - Given a valid byte offset into the structure,
297 /// return the structure index that contains it.
299 unsigned getElementContainingOffset(uint64_t Offset) const;
301 uint64_t getElementOffset(unsigned Idx) const {
302 assert(Idx < NumElements && "Invalid element idx!");
303 return MemberOffsets[Idx];
306 uint64_t getElementOffsetInBits(unsigned Idx) const {
307 return getElementOffset(Idx)*8;
311 friend class TargetData; // Only TargetData can create this class
312 StructLayout(const StructType *ST, const TargetData &TD);
315 } // End llvm namespace