TargetData.h revision 2e07494170d5f56805b7a6c1b70808fc2a157052
1//===-- llvm/Target/TargetData.h - Data size & alignment info ---*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 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. 13// 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. 17// 18//===----------------------------------------------------------------------===// 19 20#ifndef LLVM_TARGET_TARGETDATA_H 21#define LLVM_TARGET_TARGETDATA_H 22 23#include "llvm/Pass.h" 24#include "llvm/ADT/SmallVector.h" 25 26namespace llvm { 27 28class Value; 29class Type; 30class IntegerType; 31class StructType; 32class StructLayout; 33class GlobalVariable; 34class LLVMContext; 35 36/// Enum used to categorize the alignment types stored by TargetAlignElem 37enum AlignTypeEnum { 38 INTEGER_ALIGN = 'i', ///< Integer type alignment 39 VECTOR_ALIGN = 'v', ///< Vector type alignment 40 FLOAT_ALIGN = 'f', ///< Floating point type alignment 41 AGGREGATE_ALIGN = 'a', ///< Aggregate alignment 42 STACK_ALIGN = 's' ///< Stack objects alignment 43}; 44/// Target alignment element. 45/// 46/// Stores the alignment data associated with a given alignment type (pointer, 47/// integer, vector, float) and type bit width. 48/// 49/// @note The unusual order of elements in the structure attempts to reduce 50/// padding and make the structure slightly more cache friendly. 51struct TargetAlignElem { 52 AlignTypeEnum AlignType : 8; //< Alignment type (AlignTypeEnum) 53 unsigned char ABIAlign; //< ABI alignment for this type/bitw 54 unsigned char PrefAlign; //< Pref. alignment for this type/bitw 55 uint32_t TypeBitWidth; //< Type bit width 56 57 /// Initializer 58 static TargetAlignElem get(AlignTypeEnum align_type, unsigned char abi_align, 59 unsigned char pref_align, uint32_t bit_width); 60 /// Equality predicate 61 bool operator==(const TargetAlignElem &rhs) const; 62 /// output stream operator 63 std::ostream &dump(std::ostream &os) const; 64}; 65 66class TargetData : public ImmutablePass { 67private: 68 bool LittleEndian; ///< Defaults to false 69 unsigned char PointerMemSize; ///< Pointer size in bytes 70 unsigned char PointerABIAlign; ///< Pointer ABI alignment 71 unsigned char PointerPrefAlign; ///< Pointer preferred alignment 72 73 /// Alignments- Where the primitive type alignment data is stored. 74 /// 75 /// @sa init(). 76 /// @note Could support multiple size pointer alignments, e.g., 32-bit 77 /// pointers vs. 64-bit pointers by extending TargetAlignment, but for now, 78 /// we don't. 79 SmallVector<TargetAlignElem, 16> Alignments; 80 81 82 83 /*! 84 This member is a signal that a requested alignment type and bit width were 85 not found in the SmallVector. 86 */ 87 static const TargetAlignElem InvalidAlignmentElem; 88 89 // Opaque pointer for the StructType -> StructLayout map. 90 mutable void *LayoutMap; 91 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, 96 bool ABIAlign, const Type *Ty) const; 97 //! Internal helper method that returns requested alignment for type. 98 unsigned char getAlignment(const Type *Ty, bool abi_or_pref) const; 99 100 /// Valid alignment predicate. 101 /// 102 /// Predicate that tests a TargetAlignElem reference returned by get() against 103 /// InvalidAlignmentElem. 104 bool validAlignment(const TargetAlignElem &align) const { 105 return &align != &InvalidAlignmentElem; 106 } 107 108public: 109 /// Default ctor. 110 /// 111 /// @note This has to exist, because this is a pass, but it should never be 112 /// used. 113 TargetData(); 114 115 /// Constructs a TargetData from a specification string. See init(). 116 explicit TargetData(StringRef TargetDescription) 117 : ImmutablePass(&ID) { 118 init(TargetDescription); 119 } 120 121 /// Initialize target data from properties stored in the module. 122 explicit TargetData(const Module *M); 123 124 TargetData(const TargetData &TD) : 125 ImmutablePass(&ID), 126 LittleEndian(TD.isLittleEndian()), 127 PointerMemSize(TD.PointerMemSize), 128 PointerABIAlign(TD.PointerABIAlign), 129 PointerPrefAlign(TD.PointerPrefAlign), 130 Alignments(TD.Alignments), 131 LayoutMap(0) 132 { } 133 134 ~TargetData(); // Not virtual, do not subclass this class 135 136 //! Parse a target data layout string and initialize TargetData alignments. 137 void init(StringRef TargetDescription); 138 139 /// Target endianness... 140 bool isLittleEndian() const { return LittleEndian; } 141 bool isBigEndian() const { return !LittleEndian; } 142 143 /// getStringRepresentation - Return the string representation of the 144 /// TargetData. This representation is in the same format accepted by the 145 /// string constructor above. 146 std::string getStringRepresentation() const; 147 /// Target pointer alignment 148 unsigned char getPointerABIAlignment() const { return PointerABIAlign; } 149 /// Return target's alignment for stack-based pointers 150 unsigned char getPointerPrefAlignment() const { return PointerPrefAlign; } 151 /// Target pointer size 152 unsigned char getPointerSize() const { return PointerMemSize; } 153 /// Target pointer size, in bits 154 unsigned char getPointerSizeInBits() const { return 8*PointerMemSize; } 155 156 /// Size examples: 157 /// 158 /// Type SizeInBits StoreSizeInBits AllocSizeInBits[*] 159 /// ---- ---------- --------------- --------------- 160 /// i1 1 8 8 161 /// i8 8 8 8 162 /// i19 19 24 32 163 /// i32 32 32 32 164 /// i100 100 104 128 165 /// i128 128 128 128 166 /// Float 32 32 32 167 /// Double 64 64 64 168 /// X86_FP80 80 80 96 169 /// 170 /// [*] The alloc size depends on the alignment, and thus on the target. 171 /// These values are for x86-32 linux. 172 173 /// getTypeSizeInBits - Return the number of bits necessary to hold the 174 /// specified type. For example, returns 36 for i36 and 80 for x86_fp80. 175 uint64_t getTypeSizeInBits(const Type* Ty) const; 176 177 /// getTypeStoreSize - Return the maximum number of bytes that may be 178 /// overwritten by storing the specified type. For example, returns 5 179 /// for i36 and 10 for x86_fp80. 180 uint64_t getTypeStoreSize(const Type *Ty) const { 181 return (getTypeSizeInBits(Ty)+7)/8; 182 } 183 184 /// getTypeStoreSizeInBits - Return the maximum number of bits that may be 185 /// overwritten by storing the specified type; always a multiple of 8. For 186 /// example, returns 40 for i36 and 80 for x86_fp80. 187 uint64_t getTypeStoreSizeInBits(const Type *Ty) const { 188 return 8*getTypeStoreSize(Ty); 189 } 190 191 /// getTypeAllocSize - Return the offset in bytes between successive objects 192 /// of the specified type, including alignment padding. This is the amount 193 /// that alloca reserves for this type. For example, returns 12 or 16 for 194 /// x86_fp80, depending on alignment. 195 uint64_t getTypeAllocSize(const Type* Ty) const { 196 // Round up to the next alignment boundary. 197 return RoundUpAlignment(getTypeStoreSize(Ty), getABITypeAlignment(Ty)); 198 } 199 200 /// getTypeAllocSizeInBits - Return the offset in bits between successive 201 /// objects of the specified type, including alignment padding; always a 202 /// multiple of 8. This is the amount that alloca reserves for this type. 203 /// For example, returns 96 or 128 for x86_fp80, depending on alignment. 204 uint64_t getTypeAllocSizeInBits(const Type* Ty) const { 205 return 8*getTypeAllocSize(Ty); 206 } 207 208 /// getABITypeAlignment - Return the minimum ABI-required alignment for the 209 /// specified type. 210 unsigned char getABITypeAlignment(const Type *Ty) const; 211 212 /// getCallFrameTypeAlignment - Return the minimum ABI-required alignment 213 /// for the specified type when it is part of a call frame. 214 unsigned char getCallFrameTypeAlignment(const Type *Ty) const; 215 216 217 /// getPrefTypeAlignment - Return the preferred stack/global alignment for 218 /// the specified type. This is always at least as good as the ABI alignment. 219 unsigned char getPrefTypeAlignment(const Type *Ty) const; 220 221 /// getPreferredTypeAlignmentShift - Return the preferred alignment for the 222 /// specified type, returned as log2 of the value (a shift amount). 223 /// 224 unsigned char getPreferredTypeAlignmentShift(const Type *Ty) const; 225 226 /// getIntPtrType - Return an unsigned integer type that is the same size or 227 /// greater to the host pointer size. 228 /// 229 const IntegerType *getIntPtrType(LLVMContext &C) const; 230 231 /// getIndexedOffset - return the offset from the beginning of the type for 232 /// the specified indices. This is used to implement getelementptr. 233 /// 234 uint64_t getIndexedOffset(const Type *Ty, 235 Value* const* Indices, unsigned NumIndices) const; 236 237 /// getStructLayout - Return a StructLayout object, indicating the alignment 238 /// of the struct, its size, and the offsets of its fields. Note that this 239 /// information is lazily cached. 240 const StructLayout *getStructLayout(const StructType *Ty) const; 241 242 /// InvalidateStructLayoutInfo - TargetData speculatively caches StructLayout 243 /// objects. If a TargetData object is alive when types are being refined and 244 /// removed, this method must be called whenever a StructType is removed to 245 /// avoid a dangling pointer in this cache. 246 void InvalidateStructLayoutInfo(const StructType *Ty) const; 247 248 /// getPreferredAlignment - Return the preferred alignment of the specified 249 /// global. This includes an explicitly requested alignment (if the global 250 /// has one). 251 unsigned getPreferredAlignment(const GlobalVariable *GV) const; 252 253 /// getPreferredAlignmentLog - Return the preferred alignment of the 254 /// specified global, returned in log form. This includes an explicitly 255 /// requested alignment (if the global has one). 256 unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const; 257 258 /// RoundUpAlignment - Round the specified value up to the next alignment 259 /// boundary specified by Alignment. For example, 7 rounded up to an 260 /// alignment boundary of 4 is 8. 8 rounded up to the alignment boundary of 4 261 /// is 8 because it is already aligned. 262 template <typename UIntTy> 263 static UIntTy RoundUpAlignment(UIntTy Val, unsigned Alignment) { 264 assert((Alignment & (Alignment-1)) == 0 && "Alignment must be power of 2!"); 265 return (Val + (Alignment-1)) & ~UIntTy(Alignment-1); 266 } 267 268 static char ID; // Pass identification, replacement for typeid 269}; 270 271/// StructLayout - used to lazily calculate structure layout information for a 272/// target machine, based on the TargetData structure. 273/// 274class StructLayout { 275 uint64_t StructSize; 276 unsigned StructAlignment; 277 unsigned NumElements; 278 uint64_t MemberOffsets[1]; // variable sized array! 279public: 280 281 uint64_t getSizeInBytes() const { 282 return StructSize; 283 } 284 285 uint64_t getSizeInBits() const { 286 return 8*StructSize; 287 } 288 289 unsigned getAlignment() const { 290 return StructAlignment; 291 } 292 293 /// getElementContainingOffset - Given a valid byte offset into the structure, 294 /// return the structure index that contains it. 295 /// 296 unsigned getElementContainingOffset(uint64_t Offset) const; 297 298 uint64_t getElementOffset(unsigned Idx) const { 299 assert(Idx < NumElements && "Invalid element idx!"); 300 return MemberOffsets[Idx]; 301 } 302 303 uint64_t getElementOffsetInBits(unsigned Idx) const { 304 return getElementOffset(Idx)*8; 305 } 306 307private: 308 friend class TargetData; // Only TargetData can create this class 309 StructLayout(const StructType *ST, const TargetData &TD); 310}; 311 312} // End llvm namespace 313 314#endif 315