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