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