Fix a typo in a comment.
[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 "llvm/ADT/SmallVector.h"
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 /// Enum used to categorize the alignment types stored by TargetAlignElem
37 enum AlignTypeEnum {
38   INTEGER_ALIGN = 'i',               ///< Integer type alignment
39   VECTOR_ALIGN = 'v',                ///< Vector type alignment
40   FLOAT_ALIGN = 'f',                 ///< Floating point type alignment
41   AGGREGATE_ALIGN = 'a'              ///< Aggregate alignment
42 };
43 /// Target alignment element.
44 ///
45 /// Stores the alignment data associated with a given alignment type (pointer,
46 /// integer, packed/vector, float) and type bit width.
47 ///
48 /// @note The unusual order of elements in the structure attempts to reduce
49 /// padding and make the structure slightly more cache friendly.
50 struct TargetAlignElem {
51   AlignTypeEnum       AlignType : 8;  //< Alignment type (AlignTypeEnum)
52   unsigned char       ABIAlign;       //< ABI alignment for this type/bitw
53   unsigned char       PrefAlign;      //< Pref. alignment for this type/bitw
54   uint32_t            TypeBitWidth;   //< Type bit width
55
56   /// Initializer
57   static TargetAlignElem get(AlignTypeEnum align_type, unsigned char abi_align,
58                              unsigned char pref_align, uint32_t bit_width);
59   /// Equality predicate
60   bool operator==(const TargetAlignElem &rhs) const;
61   /// output stream operator
62   std::ostream &dump(std::ostream &os) const;
63 };
64
65 class TargetData : public ImmutablePass {
66 private:
67   bool          LittleEndian;          ///< Defaults to false
68   unsigned char PointerMemSize;        ///< Pointer size in bytes
69   unsigned char PointerABIAlign;       ///< Pointer ABI alignment
70   unsigned char PointerPrefAlign;      ///< Pointer preferred alignment
71
72   //! Where the primitive type alignment data is stored.
73   /*!
74    @sa init().
75    @note Could support multiple size pointer alignments, e.g., 32-bit pointers
76    vs. 64-bit pointers by extending TargetAlignment, but for now, we don't.
77    */
78   SmallVector<TargetAlignElem, 16> Alignments;
79   //! Alignment iterator shorthand
80   typedef SmallVector<TargetAlignElem, 16>::iterator align_iterator;
81   //! Constant alignment iterator shorthand
82   typedef SmallVector<TargetAlignElem, 16>::const_iterator align_const_iterator;
83   //! Invalid alignment.
84   /*!
85     This member is a signal that a requested alignment type and bit width were
86     not found in the SmallVector.
87    */
88   static const TargetAlignElem InvalidAlignmentElem;
89
90   //! Set/initialize target alignments
91   void setAlignment(AlignTypeEnum align_type, unsigned char abi_align,
92                     unsigned char pref_align, uint32_t bit_width);
93   unsigned getAlignmentInfo(AlignTypeEnum align_type, uint32_t bit_width,
94                             bool ABIAlign) const;
95   //! Internal helper method that returns requested alignment for type.
96   unsigned char getAlignment(const Type *Ty, bool abi_or_pref) const;
97
98   /// Valid alignment predicate.
99   ///
100   /// Predicate that tests a TargetAlignElem reference returned by get() against
101   /// InvalidAlignmentElem.
102   inline bool validAlignment(const TargetAlignElem &align) const {
103     return (&align != &InvalidAlignmentElem);
104   }
105
106 public:
107   /// Default ctor.
108   ///
109   /// @note This has to exist, because this is a pass, but it should never be
110   /// used.
111   TargetData() {
112     assert(0 && "ERROR: Bad TargetData ctor used.  "
113            "Tool did not specify a TargetData to use?");
114     abort();
115   }
116     
117   /// Constructs a TargetData from a specification string. See init().
118   TargetData(const std::string &TargetDescription) {
119     init(TargetDescription);
120   }
121
122   /// Initialize target data from properties stored in the module.
123   TargetData(const Module *M);
124
125   TargetData(const TargetData &TD) : 
126     ImmutablePass(),
127     LittleEndian(TD.isLittleEndian()),
128     PointerMemSize(TD.PointerMemSize),
129     PointerABIAlign(TD.PointerABIAlign),
130     PointerPrefAlign(TD.PointerPrefAlign),
131     Alignments(TD.Alignments)
132   { }
133
134   ~TargetData();  // Not virtual, do not subclass this class
135
136   //! Parse a target data layout string and initialize TargetData alignments.
137   void init(const std::string &TargetDescription);
138   
139   /// Target endianness...
140   bool          isLittleEndian()       const { return     LittleEndian; }
141   bool          isBigEndian()          const { return    !LittleEndian; }
142
143   /// getStringRepresentation - Return the string representation of the
144   /// TargetData.  This representation is in the same format accepted by the
145   /// string constructor above.
146   std::string getStringRepresentation() const;
147   /// Target pointer alignment
148   unsigned char getPointerABIAlignment() const { return PointerABIAlign; }
149   /// Return target's alignment for stack-based pointers
150   unsigned char getPointerPrefAlignment() const { return PointerPrefAlign; }
151   /// Target pointer size
152   unsigned char getPointerSize()         const { return PointerMemSize; }
153   /// Target pointer size, in bits
154   unsigned char getPointerSizeInBits()   const { return 8*PointerMemSize; }
155
156   /// getTypeSize - Return the number of bytes necessary to hold the specified
157   /// type.
158   uint64_t getTypeSize(const Type *Ty) const;
159
160   /// getTypeSizeInBits - Return the number of bits necessary to hold the
161   /// specified type.
162   uint64_t getTypeSizeInBits(const Type* Ty) const;
163
164   /// getABITypeAlignment - Return the minimum ABI-required alignment for the
165   /// specified type.
166   unsigned char getABITypeAlignment(const Type *Ty) const;
167
168   /// getPrefTypeAlignment - Return the preferred stack/global alignment for
169   /// the specified type.
170   unsigned char getPrefTypeAlignment(const Type *Ty) const;
171
172   /// getPreferredTypeAlignmentShift - Return the preferred alignment for the
173   /// specified type, returned as log2 of the value (a shift amount).
174   ///
175   unsigned char getPreferredTypeAlignmentShift(const Type *Ty) const;
176
177   /// getIntPtrType - Return an unsigned integer type that is the same size or
178   /// greater to the host pointer size.
179   ///
180   const Type *getIntPtrType() const;
181
182   /// getIndexedOffset - return the offset from the beginning of the type for the
183   /// specified indices.  This is used to implement getelementptr.
184   ///
185   uint64_t getIndexedOffset(const Type *Ty,
186                             Value* const* Indices, unsigned NumIndices) const;
187   
188   /// getStructLayout - Return a StructLayout object, indicating the alignment
189   /// of the struct, its size, and the offsets of its fields.  Note that this
190   /// information is lazily cached.
191   const StructLayout *getStructLayout(const StructType *Ty) const;
192   
193   /// InvalidateStructLayoutInfo - TargetData speculatively caches StructLayout
194   /// objects.  If a TargetData object is alive when types are being refined and
195   /// removed, this method must be called whenever a StructType is removed to
196   /// avoid a dangling pointer in this cache.
197   void InvalidateStructLayoutInfo(const StructType *Ty) const;
198
199   /// getPreferredAlignmentLog - Return the preferred alignment of the
200   /// specified global, returned in log form.  This includes an explicitly
201   /// requested alignment (if the global has one).
202   unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const;
203 };
204
205 /// StructLayout - used to lazily calculate structure layout information for a
206 /// target machine, based on the TargetData structure.
207 ///
208 class StructLayout {
209   uint64_t StructSize;
210   unsigned StructAlignment;
211   unsigned NumElements;
212   uint64_t MemberOffsets[1];  // variable sized array!
213 public:
214
215   uint64_t getSizeInBytes() const {
216     return StructSize;
217   }
218   
219   unsigned getAlignment() const {
220     return StructAlignment;
221   }
222     
223   /// getElementContainingOffset - Given a valid offset into the structure,
224   /// return the structure index that contains it.
225   ///
226   unsigned getElementContainingOffset(uint64_t Offset) const;
227
228   uint64_t getElementOffset(unsigned Idx) const {
229     assert(Idx < NumElements && "Invalid element idx!");
230     return MemberOffsets[Idx];
231   }
232   
233 private:
234   friend class TargetData;   // Only TargetData can create this class
235   StructLayout(const StructType *ST, const TargetData &TD);
236 };
237
238 } // End llvm namespace
239
240 #endif