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"
39 /// Enum used to categorize the alignment types stored by TargetAlignElem
41 INTEGER_ALIGN = 'i', ///< Integer type alignment
42 VECTOR_ALIGN = 'v', ///< Vector type alignment
43 FLOAT_ALIGN = 'f', ///< Floating point type alignment
44 AGGREGATE_ALIGN = 'a', ///< Aggregate alignment
45 STACK_ALIGN = 's' ///< Stack objects alignment
47 /// Target alignment element.
49 /// Stores the alignment data associated with a given alignment type (pointer,
50 /// integer, vector, float) and type bit width.
52 /// @note The unusual order of elements in the structure attempts to reduce
53 /// padding and make the structure slightly more cache friendly.
54 struct TargetAlignElem {
55 AlignTypeEnum AlignType : 8; //< Alignment type (AlignTypeEnum)
56 unsigned char ABIAlign; //< ABI alignment for this type/bitw
57 unsigned char PrefAlign; //< Pref. alignment for this type/bitw
58 uint32_t TypeBitWidth; //< Type bit width
61 static TargetAlignElem get(AlignTypeEnum align_type, unsigned char abi_align,
62 unsigned char pref_align, uint32_t bit_width);
63 /// Equality predicate
64 bool operator==(const TargetAlignElem &rhs) const;
65 /// output stream operator
66 std::ostream &dump(std::ostream &os) const;
69 class TargetData : public ImmutablePass {
71 bool LittleEndian; ///< Defaults to false
72 unsigned char PointerMemSize; ///< Pointer size in bytes
73 unsigned char PointerABIAlign; ///< Pointer ABI alignment
74 unsigned char PointerPrefAlign; ///< Pointer preferred alignment
76 //! Where the primitive type alignment data is stored.
79 @note Could support multiple size pointer alignments, e.g., 32-bit pointers
80 vs. 64-bit pointers by extending TargetAlignment, but for now, we don't.
82 SmallVector<TargetAlignElem, 16> Alignments;
83 //! Alignment iterator shorthand
84 typedef SmallVector<TargetAlignElem, 16>::iterator align_iterator;
85 //! Constant alignment iterator shorthand
86 typedef SmallVector<TargetAlignElem, 16>::const_iterator align_const_iterator;
87 //! Invalid alignment.
89 This member is a signal that a requested alignment type and bit width were
90 not found in the SmallVector.
92 static const TargetAlignElem InvalidAlignmentElem;
94 /// Opaque pointer for the StructType -> StructLayout map
97 //! Set/initialize target alignments
98 void setAlignment(AlignTypeEnum align_type, unsigned char abi_align,
99 unsigned char pref_align, uint32_t bit_width);
100 unsigned getAlignmentInfo(AlignTypeEnum align_type, uint32_t bit_width,
101 bool ABIAlign, const Type *Ty) const;
102 //! Internal helper method that returns requested alignment for type.
103 unsigned char getAlignment(const Type *Ty, bool abi_or_pref) const;
105 /// Valid alignment predicate.
107 /// Predicate that tests a TargetAlignElem reference returned by get() against
108 /// InvalidAlignmentElem.
109 inline bool validAlignment(const TargetAlignElem &align) const {
110 return (&align != &InvalidAlignmentElem);
114 void operator=(const TargetData&);
119 /// @note This has to exist, because this is a pass, but it should never be
121 TargetData() : ImmutablePass(&ID) {
122 llvm_report_error("Bad TargetData ctor used. "
123 "Tool did not specify a TargetData to use?");
126 /// Constructs a TargetData from a specification string. See init().
127 explicit TargetData(const std::string &TargetDescription);
129 /// Initialize target data from properties stored in the module.
130 explicit TargetData(const Module *M);
131 TargetData(const TargetData &TD);
133 ~TargetData(); // Not virtual, do not subclass this class
135 //! Parse a target data layout string and initialize TargetData alignments.
136 void init(const std::string &TargetDescription);
138 /// Target endianness...
139 bool isLittleEndian() const { return LittleEndian; }
140 bool isBigEndian() const { return !LittleEndian; }
142 /// getStringRepresentation - Return the string representation of the
143 /// TargetData. This representation is in the same format accepted by the
144 /// string constructor above.
145 std::string getStringRepresentation() const;
146 /// Target pointer alignment
147 unsigned char getPointerABIAlignment() const { return PointerABIAlign; }
148 /// Return target's alignment for stack-based pointers
149 unsigned char getPointerPrefAlignment() const { return PointerPrefAlign; }
150 /// Target pointer size
151 unsigned char getPointerSize() const { return PointerMemSize; }
152 /// Target pointer size, in bits
153 unsigned char getPointerSizeInBits() const { return 8*PointerMemSize; }
157 /// Type SizeInBits StoreSizeInBits AllocSizeInBits[*]
158 /// ---- ---------- --------------- ---------------
167 /// X86_FP80 80 80 96
169 /// [*] The alloc size depends on the alignment, and thus on the target.
170 /// These values are for x86-32 linux.
172 /// getTypeSizeInBits - Return the number of bits necessary to hold the
173 /// specified type. For example, returns 36 for i36 and 80 for x86_fp80.
174 uint64_t getTypeSizeInBits(const Type* Ty) const;
176 /// getTypeStoreSize - Return the maximum number of bytes that may be
177 /// overwritten by storing the specified type. For example, returns 5
178 /// for i36 and 10 for x86_fp80.
179 uint64_t getTypeStoreSize(const Type *Ty) const {
180 return (getTypeSizeInBits(Ty)+7)/8;
183 /// getTypeStoreSizeInBits - Return the maximum number of bits that may be
184 /// overwritten by storing the specified type; always a multiple of 8. For
185 /// example, returns 40 for i36 and 80 for x86_fp80.
186 uint64_t getTypeStoreSizeInBits(const Type *Ty) const {
187 return 8*getTypeStoreSize(Ty);
190 /// getTypeAllocSize - Return the offset in bytes between successive objects
191 /// of the specified type, including alignment padding. This is the amount
192 /// that alloca reserves for this type. For example, returns 12 or 16 for
193 /// x86_fp80, depending on alignment.
194 uint64_t getTypeAllocSize(const Type* Ty) const {
195 // Round up to the next alignment boundary.
196 return RoundUpAlignment(getTypeStoreSize(Ty), getABITypeAlignment(Ty));
199 /// getTypeAllocSizeInBits - Return the offset in bits between successive
200 /// objects of the specified type, including alignment padding; always a
201 /// multiple of 8. This is the amount that alloca reserves for this type.
202 /// For example, returns 96 or 128 for x86_fp80, depending on alignment.
203 uint64_t getTypeAllocSizeInBits(const Type* Ty) const {
204 return 8*getTypeAllocSize(Ty);
207 /// getABITypeAlignment - Return the minimum ABI-required alignment for the
209 unsigned char getABITypeAlignment(const Type *Ty) const;
211 /// getCallFrameTypeAlignment - Return the minimum ABI-required alignment
212 /// for the specified type when it is part of a call frame.
213 unsigned char getCallFrameTypeAlignment(const Type *Ty) const;
216 /// getPrefTypeAlignment - Return the preferred stack/global alignment for
217 /// the specified type. This is always at least as good as the ABI alignment.
218 unsigned char getPrefTypeAlignment(const Type *Ty) const;
220 /// getPreferredTypeAlignmentShift - Return the preferred alignment for the
221 /// specified type, returned as log2 of the value (a shift amount).
223 unsigned char getPreferredTypeAlignmentShift(const Type *Ty) const;
225 /// getIntPtrType - Return an unsigned integer type that is the same size or
226 /// greater to the host pointer size.
228 const IntegerType *getIntPtrType(LLVMContext &C) const;
230 /// getIndexedOffset - return the offset from the beginning of the type for
231 /// the specified indices. This is used to implement getelementptr.
233 uint64_t getIndexedOffset(const Type *Ty,
234 Value* const* Indices, unsigned NumIndices) const;
236 /// getStructLayout - Return a StructLayout object, indicating the alignment
237 /// of the struct, its size, and the offsets of its fields. Note that this
238 /// information is lazily cached.
239 const StructLayout *getStructLayout(const StructType *Ty) const;
241 /// InvalidateStructLayoutInfo - TargetData speculatively caches StructLayout
242 /// objects. If a TargetData object is alive when types are being refined and
243 /// removed, this method must be called whenever a StructType is removed to
244 /// avoid a dangling pointer in this cache.
245 void InvalidateStructLayoutInfo(const StructType *Ty) const;
247 /// getPreferredAlignment - Return the preferred alignment of the specified
248 /// global. This includes an explicitly requested alignment (if the global
250 unsigned getPreferredAlignment(const GlobalVariable *GV) const;
252 /// getPreferredAlignmentLog - Return the preferred alignment of the
253 /// specified global, returned in log form. This includes an explicitly
254 /// requested alignment (if the global has one).
255 unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const;
257 /// RoundUpAlignment - Round the specified value up to the next alignment
258 /// boundary specified by Alignment. For example, 7 rounded up to an
259 /// alignment boundary of 4 is 8. 8 rounded up to the alignment boundary of 4
260 /// is 8 because it is already aligned.
261 template <typename UIntTy>
262 static UIntTy RoundUpAlignment(UIntTy Val, unsigned Alignment) {
263 assert((Alignment & (Alignment-1)) == 0 && "Alignment must be power of 2!");
264 return (Val + (Alignment-1)) & ~UIntTy(Alignment-1);
267 static char ID; // Pass identification, replacement for typeid
270 /// StructLayout - used to lazily calculate structure layout information for a
271 /// target machine, based on the TargetData structure.
275 unsigned StructAlignment;
276 unsigned NumElements;
277 uint64_t MemberOffsets[1]; // variable sized array!
280 uint64_t getSizeInBytes() const {
284 uint64_t getSizeInBits() const {
288 unsigned getAlignment() const {
289 return StructAlignment;
292 /// getElementContainingOffset - Given a valid byte offset into the structure,
293 /// return the structure index that contains it.
295 unsigned getElementContainingOffset(uint64_t Offset) const;
297 uint64_t getElementOffset(unsigned Idx) const {
298 assert(Idx < NumElements && "Invalid element idx!");
299 return MemberOffsets[Idx];
302 uint64_t getElementOffsetInBits(unsigned Idx) const {
303 return getElementOffset(Idx)*8;
307 friend class TargetData; // Only TargetData can create this class
308 StructLayout(const StructType *ST, const TargetData &TD);
311 } // End llvm namespace