Change TargetData::getIndexedOffset interface to not require indices
[oota-llvm.git] / include / llvm / Target / TargetData.h
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 #include <vector>
26 #include <string>
27
28 namespace llvm {
29
30 class Value;
31 class Type;
32 class StructType;
33 class StructLayout;
34 class GlobalVariable;
35
36 class TargetData : public ImmutablePass {
37   bool          LittleEndian;          // Defaults to false
38
39   // ABI alignments
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
49
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
60
61 public:
62   /// Default ctor - This has to exist, because this is a pass, but it should
63   /// never be used.
64   TargetData() {
65     assert(0 && "ERROR: Bad TargetData ctor used.  "
66            "Tool did not specify a TargetData to use?");
67     abort();
68   }
69     
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);
77   }
78
79   /// Initialize target data from properties stored in the module.
80   TargetData(const Module *M);
81
82   TargetData(const TargetData &TD) : 
83     ImmutablePass(),
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()) {
103   }
104
105   ~TargetData();  // Not virtual, do not subclass this class
106
107   /// Parse a target data layout string and initialize TargetData members.
108   ///
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.
114   ///
115   /// "-" is used as a separator and ":" separates a token from its argument.
116   ///
117   /// Alignment is indicated in bits and internally converted to the
118   /// appropriate number of bytes.
119   ///
120   /// The preferred stack/global alignment specifications (":[prefalign]") are
121   /// optional and default to the ABI alignment.
122   ///
123   /// Valid tokens:
124   /// <br>
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.
147   ///
148   /// All other token types are silently ignored.
149   void init(const std::string &TargetDescription);
150   
151   
152   /// Target endianness...
153   bool          isLittleEndian()       const { return     LittleEndian; }
154   bool          isBigEndian()          const { return    !LittleEndian; }
155
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; }
176
177   /// Return target's alignment for booleans on stack
178   unsigned char getBoolPrefAlignment() const {
179     return BoolPrefAlignment;
180   }
181   /// Return target's alignment for integers on stack
182   unsigned char getBytePrefAlignment() const {
183     return BytePrefAlignment;
184   }
185   /// Return target's alignment for shorts on stack
186   unsigned char getShortPrefAlignment() const {
187     return ShortPrefAlignment;
188   }
189   /// Return target's alignment for integers on stack
190   unsigned char getIntPrefAlignment()     const {
191     return IntPrefAlignment;
192   }
193   /// Return target's alignment for longs on stack
194   unsigned char getLongPrefAlignment() const {
195     return LongPrefAlignment;
196   }
197   /// Return target's alignment for single precision floats on stack
198   unsigned char getFloatPrefAlignment() const {
199     return FloatPrefAlignment;
200   }
201   /// Return target's alignment for double preceision floats on stack
202   unsigned char getDoublePrefAlignment()  const {
203     return DoublePrefAlignment;
204   }
205   /// Return target's alignment for stack-based pointers
206   unsigned char getPointerPrefAlignment() const {
207     return PointerPrefAlignment;
208   }
209   /// Return target's alignment for stack-based structures
210   unsigned char getAggMinPrefAlignment() const {
211     return AggMinPrefAlignment;
212   }
213
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;
218
219   /// getTypeSize - Return the number of bytes necessary to hold the specified
220   /// type.
221   ///
222   uint64_t getTypeSize(const Type *Ty) const;
223
224   /// getTypeSizeInBits - Return the number of bytes necessary to hold the
225   /// specified type.
226   uint64_t getTypeSizeInBits(const Type* Ty) const;
227
228   /// getTypeAlignmentABI - Return the minimum ABI-required alignment for the
229   /// specified type.
230   unsigned char getTypeAlignmentABI(const Type *Ty) const;
231
232   /// getTypeAlignmentPref - Return the preferred stack/global alignment for
233   /// the specified type.
234   unsigned char getTypeAlignmentPref(const Type *Ty) const;
235
236   /// getPreferredTypeAlignmentShift - Return the preferred alignment for the
237   /// specified type, returned as log2 of the value (a shift amount).
238   ///
239   unsigned char getPreferredTypeAlignmentShift(const Type *Ty) const;
240
241   /// getIntPtrType - Return an unsigned integer type that is the same size or
242   /// greater to the host pointer size.
243   ///
244   const Type *getIntPtrType() const;
245
246   /// getIndexOffset - return the offset from the beginning of the type for the
247   /// specified indices.  This is used to implement getelementptr.
248   ///
249   uint64_t getIndexedOffset(const Type *Ty,
250                             Value* const* Indices, unsigned NumIndices) const;
251   
252   uint64_t getIndexedOffset(const Type *Ty,
253                             const std::vector<Value*> &Indices) const {
254     return getIndexedOffset(Ty, &Indices[0], Indices.size());
255   }
256
257   /// getStructLayout - Return a StructLayout object, indicating the alignment
258   /// of the struct, its size, and the offsets of its fields.  Note that this
259   /// information is lazily cached.
260   const StructLayout *getStructLayout(const StructType *Ty) const;
261   
262   /// InvalidateStructLayoutInfo - TargetData speculatively caches StructLayout
263   /// objects.  If a TargetData object is alive when types are being refined and
264   /// removed, this method must be called whenever a StructType is removed to
265   /// avoid a dangling pointer in this cache.
266   void InvalidateStructLayoutInfo(const StructType *Ty) const;
267
268   /// getPreferredAlignmentLog - Return the preferred alignment of the
269   /// specified global, returned in log form.  This includes an explicitly
270   /// requested alignment (if the global has one).
271   unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const;
272 };
273
274 /// StructLayout - used to lazily calculate structure layout information for a
275 /// target machine, based on the TargetData structure.
276 ///
277 class StructLayout {
278 public:
279   std::vector<uint64_t> MemberOffsets;
280   uint64_t StructSize;
281   unsigned StructAlignment;
282
283   /// getElementContainingOffset - Given a valid offset into the structure,
284   /// return the structure index that contains it.
285   ///
286   unsigned getElementContainingOffset(uint64_t Offset) const;
287
288 private:
289   friend class TargetData;   // Only TargetData can create this class
290   StructLayout(const StructType *ST, const TargetData &TD);
291 };
292
293 } // End llvm namespace
294
295 #endif