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