TargetData.h revision 4e9f579028d06d92fa6d39a25c818a61d2384544
1//===-- llvm/Target/TargetData.h - Data size & alignment info ---*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 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. 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/Support/DataTypes.h" 25 26namespace llvm { 27 28class Value; 29class Type; 30class StructType; 31class StructLayout; 32class GlobalVariable; 33 34class TargetData : public ImmutablePass { 35 bool LittleEndian; // Defaults to false 36 37 // ABI alignments 38 unsigned char BoolABIAlignment; // Defaults to 1 byte 39 unsigned char ByteABIAlignment; // Defaults to 1 byte 40 unsigned char ShortABIAlignment; // Defaults to 2 bytes 41 unsigned char IntABIAlignment; // Defaults to 4 bytes 42 unsigned char LongABIAlignment; // Defaults to 8 bytes 43 unsigned char FloatABIAlignment; // Defaults to 4 bytes 44 unsigned char DoubleABIAlignment; // Defaults to 8 bytes 45 unsigned char PointerMemSize; // Defaults to 8 bytes 46 unsigned char PointerABIAlignment; // Defaults to 8 bytes 47 48 // Preferred stack/global type alignments 49 unsigned char BoolPrefAlignment; // Defaults to BoolABIAlignment 50 unsigned char BytePrefAlignment; // Defaults to ByteABIAlignment 51 unsigned char ShortPrefAlignment; // Defaults to ShortABIAlignment 52 unsigned char IntPrefAlignment; // Defaults to IntABIAlignment 53 unsigned char LongPrefAlignment; // Defaults to LongABIAlignment 54 unsigned char FloatPrefAlignment; // Defaults to FloatABIAlignment 55 unsigned char DoublePrefAlignment; // Defaults to DoubleABIAlignment 56 unsigned char PointerPrefAlignment; // Defaults to PointerABIAlignment 57 unsigned char AggMinPrefAlignment; // Defaults to 0 bytes 58 59public: 60 /// Default ctor - This has to exist, because this is a pass, but it should 61 /// never be used. 62 TargetData() { 63 assert(0 && "ERROR: Bad TargetData ctor used. " 64 "Tool did not specify a TargetData to use?"); 65 abort(); 66 } 67 68 /// Constructs a TargetData from a string of the following format: 69 /// "E-p:64:64-d:64-f:32-l:64-i:32-s:16-b:8-B:8" 70 /// The above string is considered the default, and any values not specified 71 /// in the string will be assumed to be as above, with the caveat that unspecified 72 /// values are always assumed to be smaller than the size of a pointer. 73 TargetData(const std::string &TargetDescription) { 74 init(TargetDescription); 75 } 76 77 /// Initialize target data from properties stored in the module. 78 TargetData(const Module *M); 79 80 TargetData(const TargetData &TD) : 81 ImmutablePass(), 82 LittleEndian(TD.isLittleEndian()), 83 BoolABIAlignment(TD.getBoolABIAlignment()), 84 ByteABIAlignment(TD.getByteABIAlignment()), 85 ShortABIAlignment(TD.getShortABIAlignment()), 86 IntABIAlignment(TD.getIntABIAlignment()), 87 LongABIAlignment(TD.getLongABIAlignment()), 88 FloatABIAlignment(TD.getFloatABIAlignment()), 89 DoubleABIAlignment(TD.getDoubleABIAlignment()), 90 PointerMemSize(TD.getPointerSize()), 91 PointerABIAlignment(TD.getPointerABIAlignment()), 92 BoolPrefAlignment(TD.getBoolPrefAlignment()), 93 BytePrefAlignment(TD.getBytePrefAlignment()), 94 ShortPrefAlignment(TD.getShortPrefAlignment()), 95 IntPrefAlignment(TD.getIntPrefAlignment()), 96 LongPrefAlignment(TD.getLongPrefAlignment()), 97 FloatPrefAlignment(TD.getFloatPrefAlignment()), 98 DoublePrefAlignment(TD.getDoublePrefAlignment()), 99 PointerPrefAlignment(TD.getPointerPrefAlignment()), 100 AggMinPrefAlignment(TD.getAggMinPrefAlignment()) { 101 } 102 103 ~TargetData(); // Not virtual, do not subclass this class 104 105 /// Parse a target data layout string and initialize TargetData members. 106 /// 107 /// Parse a target data layout string, initializing the various TargetData 108 /// members along the way. A TargetData specification string looks like 109 /// "E-p:64:64-d:64-f:32-l:64-i:32-s:16-b:8-B:8" and specifies the 110 /// target's endianess, the ABI alignments of various data types and 111 /// the size of pointers. 112 /// 113 /// "-" is used as a separator and ":" separates a token from its argument. 114 /// 115 /// Alignment is indicated in bits and internally converted to the 116 /// appropriate number of bytes. 117 /// 118 /// The preferred stack/global alignment specifications (":[prefalign]") are 119 /// optional and default to the ABI alignment. 120 /// 121 /// Valid tokens: 122 /// <br> 123 /// <em>E</em> specifies big endian architecture (1234) [default]<br> 124 /// <em>e</em> specifies little endian architecture (4321) <br> 125 /// <em>p:[ptr size]:[ptr align]</em> specifies pointer size and alignment 126 /// [default = 64:64] <br> 127 /// <em>d:[align]:[prefalign]</em> specifies double floating 128 /// point alignment [default = 64] <br> 129 /// <em>f:[align]:[prefalign]</em> specifies single floating 130 /// point alignment [default = 32] <br> 131 /// <em>l:[align]:[prefalign]:[globalign[</em> specifies long integer 132 /// alignment [default = 64] <br> 133 /// <em>i:[align]:[prefalign]</em> specifies integer alignment 134 /// [default = 32] <br> 135 /// <em>s:[align]:[prefalign]</em> specifies short integer 136 /// alignment [default = 16] <br> 137 /// <em>b:[align]:[prefalign]</em> specifies byte data type 138 /// alignment [default = 8] <br> 139 /// <em>B:[align]:[prefalign]</em> specifies boolean data type 140 /// alignment [default = 8] <br> 141 /// <em>A:[prefalign]</em> specifies an aggregates' minimum alignment 142 /// on the stack and when emitted as a global. The default minimum aggregate 143 /// alignment defaults to 0, which causes the aggregate's "natural" internal 144 /// alignment calculated by llvm to be preferred. 145 /// 146 /// All other token types are silently ignored. 147 void init(const std::string &TargetDescription); 148 149 150 /// Target endianness... 151 bool isLittleEndian() const { return LittleEndian; } 152 bool isBigEndian() const { return !LittleEndian; } 153 154 /// Target boolean alignment 155 unsigned char getBoolABIAlignment() const { return BoolABIAlignment; } 156 /// Target byte alignment 157 unsigned char getByteABIAlignment() const { return ByteABIAlignment; } 158 /// Target short alignment 159 unsigned char getShortABIAlignment() const { return ShortABIAlignment; } 160 /// Target integer alignment 161 unsigned char getIntABIAlignment() const { return IntABIAlignment; } 162 /// Target long alignment 163 unsigned char getLongABIAlignment() const { return LongABIAlignment; } 164 /// Target single precision float alignment 165 unsigned char getFloatABIAlignment() const { return FloatABIAlignment; } 166 /// Target double precision float alignment 167 unsigned char getDoubleABIAlignment() const { return DoubleABIAlignment; } 168 /// Target pointer alignment 169 unsigned char getPointerABIAlignment() const { return PointerABIAlignment; } 170 /// Target pointer size 171 unsigned char getPointerSize() const { return PointerMemSize; } 172 /// Target pointer size, in bits 173 unsigned char getPointerSizeInBits() const { return 8*PointerMemSize; } 174 175 /// Return target's alignment for booleans on stack 176 unsigned char getBoolPrefAlignment() const { 177 return BoolPrefAlignment; 178 } 179 /// Return target's alignment for integers on stack 180 unsigned char getBytePrefAlignment() const { 181 return BytePrefAlignment; 182 } 183 /// Return target's alignment for shorts on stack 184 unsigned char getShortPrefAlignment() const { 185 return ShortPrefAlignment; 186 } 187 /// Return target's alignment for integers on stack 188 unsigned char getIntPrefAlignment() const { 189 return IntPrefAlignment; 190 } 191 /// Return target's alignment for longs on stack 192 unsigned char getLongPrefAlignment() const { 193 return LongPrefAlignment; 194 } 195 /// Return target's alignment for single precision floats on stack 196 unsigned char getFloatPrefAlignment() const { 197 return FloatPrefAlignment; 198 } 199 /// Return target's alignment for double preceision floats on stack 200 unsigned char getDoublePrefAlignment() const { 201 return DoublePrefAlignment; 202 } 203 /// Return target's alignment for stack-based pointers 204 unsigned char getPointerPrefAlignment() const { 205 return PointerPrefAlignment; 206 } 207 /// Return target's alignment for stack-based structures 208 unsigned char getAggMinPrefAlignment() const { 209 return AggMinPrefAlignment; 210 } 211 212 /// getStringRepresentation - Return the string representation of the 213 /// TargetData. This representation is in the same format accepted by the 214 /// string constructor above. 215 std::string getStringRepresentation() const; 216 217 /// getTypeSize - Return the number of bytes necessary to hold the specified 218 /// type. 219 /// 220 uint64_t getTypeSize(const Type *Ty) const; 221 222 /// getTypeSizeInBits - Return the number of bytes necessary to hold the 223 /// specified type. 224 uint64_t getTypeSizeInBits(const Type* Ty) const; 225 226 /// getTypeAlignmentABI - Return the minimum ABI-required alignment for the 227 /// specified type. 228 unsigned char getTypeAlignmentABI(const Type *Ty) const; 229 230 /// getTypeAlignmentPref - Return the preferred stack/global alignment for 231 /// the specified type. 232 unsigned char getTypeAlignmentPref(const Type *Ty) const; 233 234 /// getPreferredTypeAlignmentShift - Return the preferred alignment for the 235 /// specified type, returned as log2 of the value (a shift amount). 236 /// 237 unsigned char getPreferredTypeAlignmentShift(const Type *Ty) const; 238 239 /// getIntPtrType - Return an unsigned integer type that is the same size or 240 /// greater to the host pointer size. 241 /// 242 const Type *getIntPtrType() const; 243 244 /// getIndexOffset - return the offset from the beginning of the type for the 245 /// specified indices. This is used to implement getelementptr. 246 /// 247 uint64_t getIndexedOffset(const Type *Ty, 248 Value* const* Indices, unsigned NumIndices) const; 249 250 /// getStructLayout - Return a StructLayout object, indicating the alignment 251 /// of the struct, its size, and the offsets of its fields. Note that this 252 /// information is lazily cached. 253 const StructLayout *getStructLayout(const StructType *Ty) const; 254 255 /// InvalidateStructLayoutInfo - TargetData speculatively caches StructLayout 256 /// objects. If a TargetData object is alive when types are being refined and 257 /// removed, this method must be called whenever a StructType is removed to 258 /// avoid a dangling pointer in this cache. 259 void InvalidateStructLayoutInfo(const StructType *Ty) const; 260 261 /// getPreferredAlignmentLog - Return the preferred alignment of the 262 /// specified global, returned in log form. This includes an explicitly 263 /// requested alignment (if the global has one). 264 unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const; 265}; 266 267/// StructLayout - used to lazily calculate structure layout information for a 268/// target machine, based on the TargetData structure. 269/// 270class StructLayout { 271 uint64_t StructSize; 272 unsigned StructAlignment; 273 unsigned NumElements; 274 uint64_t MemberOffsets[1]; // variable sized array! 275public: 276 277 uint64_t getSizeInBytes() const { 278 return StructSize; 279 } 280 281 unsigned getAlignment() const { 282 return StructAlignment; 283 } 284 285 /// getElementContainingOffset - Given a valid offset into the structure, 286 /// return the structure index that contains it. 287 /// 288 unsigned getElementContainingOffset(uint64_t Offset) const; 289 290 uint64_t getElementOffset(unsigned Idx) const { 291 assert(Idx < NumElements && "Invalid element idx!"); 292 return MemberOffsets[Idx]; 293 } 294 295private: 296 friend class TargetData; // Only TargetData can create this class 297 StructLayout(const StructType *ST, const TargetData &TD); 298}; 299 300} // End llvm namespace 301 302#endif 303