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