9a723084a6c5b633b16921adb7a6c8d5941c8c49
[oota-llvm.git] / include / llvm / DerivedTypes.h
1 //===-- llvm/DerivedTypes.h - Classes for handling data types ---*- 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 contains the declarations of classes that represent "derived
11 // types".  These are things like "arrays of x" or "structure of x, y, z" or
12 // "function returning x taking (y,z) as parameters", etc...
13 //
14 // The implementations of these classes live in the Type.cpp file.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #ifndef LLVM_DERIVED_TYPES_H
19 #define LLVM_DERIVED_TYPES_H
20
21 #include "llvm/Type.h"
22 #include "llvm/Support/Compiler.h"
23 #include "llvm/Support/DataTypes.h"
24
25 namespace llvm {
26
27 class Value;
28 class APInt;
29 class LLVMContext;
30 template<typename T> class ArrayRef;
31 class StringRef;
32
33 /// Class to represent integer types. Note that this class is also used to
34 /// represent the built-in integer types: Int1Ty, Int8Ty, Int16Ty, Int32Ty and
35 /// Int64Ty.
36 /// @brief Integer representation type
37 class IntegerType : public Type {
38   friend class LLVMContextImpl;
39   
40 protected:
41   explicit IntegerType(LLVMContext &C, unsigned NumBits) : Type(C, IntegerTyID){
42     setSubclassData(NumBits);
43   }
44 public:
45   /// This enum is just used to hold constants we need for IntegerType.
46   enum {
47     MIN_INT_BITS = 1,        ///< Minimum number of bits that can be specified
48     MAX_INT_BITS = (1<<23)-1 ///< Maximum number of bits that can be specified
49       ///< Note that bit width is stored in the Type classes SubclassData field
50       ///< which has 23 bits. This yields a maximum bit width of 8,388,607 bits.
51   };
52
53   /// This static method is the primary way of constructing an IntegerType.
54   /// If an IntegerType with the same NumBits value was previously instantiated,
55   /// that instance will be returned. Otherwise a new one will be created. Only
56   /// one instance with a given NumBits value is ever created.
57   /// @brief Get or create an IntegerType instance.
58   static IntegerType *get(LLVMContext &C, unsigned NumBits);
59
60   /// @brief Get the number of bits in this IntegerType
61   unsigned getBitWidth() const { return getSubclassData(); }
62
63   /// getBitMask - Return a bitmask with ones set for all of the bits
64   /// that can be set by an unsigned version of this type.  This is 0xFF for
65   /// i8, 0xFFFF for i16, etc.
66   uint64_t getBitMask() const {
67     return ~uint64_t(0UL) >> (64-getBitWidth());
68   }
69
70   /// getSignBit - Return a uint64_t with just the most significant bit set (the
71   /// sign bit, if the value is treated as a signed number).
72   uint64_t getSignBit() const {
73     return 1ULL << (getBitWidth()-1);
74   }
75
76   /// For example, this is 0xFF for an 8 bit integer, 0xFFFF for i16, etc.
77   /// @returns a bit mask with ones set for all the bits of this type.
78   /// @brief Get a bit mask for this type.
79   APInt getMask() const;
80
81   /// This method determines if the width of this IntegerType is a power-of-2
82   /// in terms of 8 bit bytes.
83   /// @returns true if this is a power-of-2 byte width.
84   /// @brief Is this a power-of-2 byte-width IntegerType ?
85   bool isPowerOf2ByteWidth() const;
86
87   // Methods for support type inquiry through isa, cast, and dyn_cast.
88   static inline bool classof(const IntegerType *) { return true; }
89   static inline bool classof(const Type *T) {
90     return T->getTypeID() == IntegerTyID;
91   }
92 };
93
94
95 /// FunctionType - Class to represent function types
96 ///
97 class FunctionType : public Type {
98   FunctionType(const FunctionType &) LLVM_DELETED_FUNCTION;
99   const FunctionType &operator=(const FunctionType &) LLVM_DELETED_FUNCTION;
100   FunctionType(Type *Result, ArrayRef<Type*> Params, bool IsVarArgs);
101
102 public:
103   /// FunctionType::get - This static method is the primary way of constructing
104   /// a FunctionType.
105   ///
106   static FunctionType *get(Type *Result,
107                            ArrayRef<Type*> Params, bool isVarArg);
108
109   /// FunctionType::get - Create a FunctionType taking no parameters.
110   ///
111   static FunctionType *get(Type *Result, bool isVarArg);
112   
113   /// isValidReturnType - Return true if the specified type is valid as a return
114   /// type.
115   static bool isValidReturnType(Type *RetTy);
116
117   /// isValidArgumentType - Return true if the specified type is valid as an
118   /// argument type.
119   static bool isValidArgumentType(Type *ArgTy);
120
121   bool isVarArg() const { return getSubclassData(); }
122   Type *getReturnType() const { return ContainedTys[0]; }
123
124   typedef Type::subtype_iterator param_iterator;
125   param_iterator param_begin() const { return ContainedTys + 1; }
126   param_iterator param_end() const { return &ContainedTys[NumContainedTys]; }
127
128   // Parameter type accessors.
129   Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
130
131   /// getNumParams - Return the number of fixed parameters this function type
132   /// requires.  This does not consider varargs.
133   ///
134   unsigned getNumParams() const { return NumContainedTys - 1; }
135
136   // Methods for support type inquiry through isa, cast, and dyn_cast.
137   static inline bool classof(const FunctionType *) { return true; }
138   static inline bool classof(const Type *T) {
139     return T->getTypeID() == FunctionTyID;
140   }
141 };
142
143
144 /// CompositeType - Common super class of ArrayType, StructType, PointerType
145 /// and VectorType.
146 class CompositeType : public Type {
147 protected:
148   explicit CompositeType(LLVMContext &C, TypeID tid) : Type(C, tid) { }
149 public:
150
151   /// getTypeAtIndex - Given an index value into the type, return the type of
152   /// the element.
153   ///
154   Type *getTypeAtIndex(const Value *V);
155   Type *getTypeAtIndex(unsigned Idx);
156   bool indexValid(const Value *V) const;
157   bool indexValid(unsigned Idx) const;
158
159   // Methods for support type inquiry through isa, cast, and dyn_cast.
160   static inline bool classof(const CompositeType *) { return true; }
161   static inline bool classof(const Type *T) {
162     return T->getTypeID() == ArrayTyID ||
163            T->getTypeID() == StructTyID ||
164            T->getTypeID() == PointerTyID ||
165            T->getTypeID() == VectorTyID;
166   }
167 };
168
169
170 /// StructType - Class to represent struct types.  There are two different kinds
171 /// of struct types: Literal structs and Identified structs.
172 ///
173 /// Literal struct types (e.g. { i32, i32 }) are uniqued structurally, and must
174 /// always have a body when created.  You can get one of these by using one of
175 /// the StructType::get() forms.
176 ///  
177 /// Identified structs (e.g. %foo or %42) may optionally have a name and are not
178 /// uniqued.  The names for identified structs are managed at the LLVMContext
179 /// level, so there can only be a single identified struct with a given name in
180 /// a particular LLVMContext.  Identified structs may also optionally be opaque
181 /// (have no body specified).  You get one of these by using one of the
182 /// StructType::create() forms.
183 ///
184 /// Independent of what kind of struct you have, the body of a struct type are
185 /// laid out in memory consequtively with the elements directly one after the
186 /// other (if the struct is packed) or (if not packed) with padding between the
187 /// elements as defined by TargetData (which is required to match what the code
188 /// generator for a target expects).
189 ///
190 class StructType : public CompositeType {
191   StructType(const StructType &) LLVM_DELETED_FUNCTION;
192   const StructType &operator=(const StructType &) LLVM_DELETED_FUNCTION;
193   StructType(LLVMContext &C)
194     : CompositeType(C, StructTyID), SymbolTableEntry(0) {}
195   enum {
196     // This is the contents of the SubClassData field.
197     SCDB_HasBody = 1,
198     SCDB_Packed = 2,
199     SCDB_IsLiteral = 4,
200     SCDB_IsSized = 8
201   };
202
203   /// SymbolTableEntry - For a named struct that actually has a name, this is a
204   /// pointer to the symbol table entry (maintained by LLVMContext) for the
205   /// struct.  This is null if the type is an literal struct or if it is
206   /// a identified type that has an empty name.
207   /// 
208   void *SymbolTableEntry;
209 public:
210   ~StructType() {
211     delete [] ContainedTys; // Delete the body.
212   }
213
214   /// StructType::create - This creates an identified struct.
215   static StructType *create(LLVMContext &Context, StringRef Name);
216   static StructType *create(LLVMContext &Context);
217   
218   static StructType *create(ArrayRef<Type*> Elements,
219                             StringRef Name,
220                             bool isPacked = false);
221   static StructType *create(ArrayRef<Type*> Elements);
222   static StructType *create(LLVMContext &Context,
223                             ArrayRef<Type*> Elements,
224                             StringRef Name,
225                             bool isPacked = false);
226   static StructType *create(LLVMContext &Context, ArrayRef<Type*> Elements);
227   static StructType *create(StringRef Name, Type *elt1, ...) END_WITH_NULL;
228
229   /// StructType::get - This static method is the primary way to create a
230   /// literal StructType.
231   static StructType *get(LLVMContext &Context, ArrayRef<Type*> Elements,
232                          bool isPacked = false);
233
234   /// StructType::get - Create an empty structure type.
235   ///
236   static StructType *get(LLVMContext &Context, bool isPacked = false);
237   
238   /// StructType::get - This static method is a convenience method for creating
239   /// structure types by specifying the elements as arguments.  Note that this
240   /// method always returns a non-packed struct, and requires at least one
241   /// element type.
242   static StructType *get(Type *elt1, ...) END_WITH_NULL;
243
244   bool isPacked() const { return (getSubclassData() & SCDB_Packed) != 0; }
245   
246   /// isLiteral - Return true if this type is uniqued by structural
247   /// equivalence, false if it is a struct definition.
248   bool isLiteral() const { return (getSubclassData() & SCDB_IsLiteral) != 0; }
249   
250   /// isOpaque - Return true if this is a type with an identity that has no body
251   /// specified yet.  These prints as 'opaque' in .ll files.
252   bool isOpaque() const { return (getSubclassData() & SCDB_HasBody) == 0; }
253
254   /// isSized - Return true if this is a sized type.
255   bool isSized() const;
256   
257   /// hasName - Return true if this is a named struct that has a non-empty name.
258   bool hasName() const { return SymbolTableEntry != 0; }
259   
260   /// getName - Return the name for this struct type if it has an identity.
261   /// This may return an empty string for an unnamed struct type.  Do not call
262   /// this on an literal type.
263   StringRef getName() const;
264   
265   /// setName - Change the name of this type to the specified name, or to a name
266   /// with a suffix if there is a collision.  Do not call this on an literal
267   /// type.
268   void setName(StringRef Name);
269
270   /// setBody - Specify a body for an opaque identified type.
271   void setBody(ArrayRef<Type*> Elements, bool isPacked = false);
272   void setBody(Type *elt1, ...) END_WITH_NULL;
273   
274   /// isValidElementType - Return true if the specified type is valid as a
275   /// element type.
276   static bool isValidElementType(Type *ElemTy);
277   
278
279   // Iterator access to the elements.
280   typedef Type::subtype_iterator element_iterator;
281   element_iterator element_begin() const { return ContainedTys; }
282   element_iterator element_end() const { return &ContainedTys[NumContainedTys];}
283
284   /// isLayoutIdentical - Return true if this is layout identical to the
285   /// specified struct.
286   bool isLayoutIdentical(StructType *Other) const;  
287   
288   // Random access to the elements
289   unsigned getNumElements() const { return NumContainedTys; }
290   Type *getElementType(unsigned N) const {
291     assert(N < NumContainedTys && "Element number out of range!");
292     return ContainedTys[N];
293   }
294
295   // Methods for support type inquiry through isa, cast, and dyn_cast.
296   static inline bool classof(const StructType *) { return true; }
297   static inline bool classof(const Type *T) {
298     return T->getTypeID() == StructTyID;
299   }
300 };
301
302 /// SequentialType - This is the superclass of the array, pointer and vector
303 /// type classes.  All of these represent "arrays" in memory.  The array type
304 /// represents a specifically sized array, pointer types are unsized/unknown
305 /// size arrays, vector types represent specifically sized arrays that
306 /// allow for use of SIMD instructions.  SequentialType holds the common
307 /// features of all, which stem from the fact that all three lay their
308 /// components out in memory identically.
309 ///
310 class SequentialType : public CompositeType {
311   Type *ContainedType;               ///< Storage for the single contained type.
312   SequentialType(const SequentialType &) LLVM_DELETED_FUNCTION;
313   const SequentialType &operator=(const SequentialType &) LLVM_DELETED_FUNCTION;
314
315 protected:
316   SequentialType(TypeID TID, Type *ElType)
317     : CompositeType(ElType->getContext(), TID), ContainedType(ElType) {
318     ContainedTys = &ContainedType;
319     NumContainedTys = 1;
320   }
321
322 public:
323   Type *getElementType() const { return ContainedTys[0]; }
324
325   // Methods for support type inquiry through isa, cast, and dyn_cast.
326   static inline bool classof(const SequentialType *) { return true; }
327   static inline bool classof(const Type *T) {
328     return T->getTypeID() == ArrayTyID ||
329            T->getTypeID() == PointerTyID ||
330            T->getTypeID() == VectorTyID;
331   }
332 };
333
334
335 /// ArrayType - Class to represent array types.
336 ///
337 class ArrayType : public SequentialType {
338   uint64_t NumElements;
339
340   ArrayType(const ArrayType &) LLVM_DELETED_FUNCTION;
341   const ArrayType &operator=(const ArrayType &) LLVM_DELETED_FUNCTION;
342   ArrayType(Type *ElType, uint64_t NumEl);
343 public:
344   /// ArrayType::get - This static method is the primary way to construct an
345   /// ArrayType
346   ///
347   static ArrayType *get(Type *ElementType, uint64_t NumElements);
348
349   /// isValidElementType - Return true if the specified type is valid as a
350   /// element type.
351   static bool isValidElementType(Type *ElemTy);
352
353   uint64_t getNumElements() const { return NumElements; }
354
355   // Methods for support type inquiry through isa, cast, and dyn_cast.
356   static inline bool classof(const ArrayType *) { return true; }
357   static inline bool classof(const Type *T) {
358     return T->getTypeID() == ArrayTyID;
359   }
360 };
361
362 /// VectorType - Class to represent vector types.
363 ///
364 class VectorType : public SequentialType {
365   unsigned NumElements;
366
367   VectorType(const VectorType &) LLVM_DELETED_FUNCTION;
368   const VectorType &operator=(const VectorType &) LLVM_DELETED_FUNCTION;
369   VectorType(Type *ElType, unsigned NumEl);
370 public:
371   /// VectorType::get - This static method is the primary way to construct an
372   /// VectorType.
373   ///
374   static VectorType *get(Type *ElementType, unsigned NumElements);
375
376   /// VectorType::getInteger - This static method gets a VectorType with the
377   /// same number of elements as the input type, and the element type is an
378   /// integer type of the same width as the input element type.
379   ///
380   static VectorType *getInteger(VectorType *VTy) {
381     unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
382     assert(EltBits && "Element size must be of a non-zero size");
383     Type *EltTy = IntegerType::get(VTy->getContext(), EltBits);
384     return VectorType::get(EltTy, VTy->getNumElements());
385   }
386
387   /// VectorType::getExtendedElementVectorType - This static method is like
388   /// getInteger except that the element types are twice as wide as the
389   /// elements in the input type.
390   ///
391   static VectorType *getExtendedElementVectorType(VectorType *VTy) {
392     unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
393     Type *EltTy = IntegerType::get(VTy->getContext(), EltBits * 2);
394     return VectorType::get(EltTy, VTy->getNumElements());
395   }
396
397   /// VectorType::getTruncatedElementVectorType - This static method is like
398   /// getInteger except that the element types are half as wide as the
399   /// elements in the input type.
400   ///
401   static VectorType *getTruncatedElementVectorType(VectorType *VTy) {
402     unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
403     assert((EltBits & 1) == 0 &&
404            "Cannot truncate vector element with odd bit-width");
405     Type *EltTy = IntegerType::get(VTy->getContext(), EltBits / 2);
406     return VectorType::get(EltTy, VTy->getNumElements());
407   }
408
409   /// isValidElementType - Return true if the specified type is valid as a
410   /// element type.
411   static bool isValidElementType(Type *ElemTy);
412
413   /// @brief Return the number of elements in the Vector type.
414   unsigned getNumElements() const { return NumElements; }
415
416   /// @brief Return the number of bits in the Vector type.
417   /// Returns zero when the vector is a vector of pointers.
418   unsigned getBitWidth() const {
419     return NumElements * getElementType()->getPrimitiveSizeInBits();
420   }
421
422   // Methods for support type inquiry through isa, cast, and dyn_cast.
423   static inline bool classof(const VectorType *) { return true; }
424   static inline bool classof(const Type *T) {
425     return T->getTypeID() == VectorTyID;
426   }
427 };
428
429
430 /// PointerType - Class to represent pointers.
431 ///
432 class PointerType : public SequentialType {
433   PointerType(const PointerType &) LLVM_DELETED_FUNCTION;
434   const PointerType &operator=(const PointerType &) LLVM_DELETED_FUNCTION;
435   explicit PointerType(Type *ElType, unsigned AddrSpace);
436 public:
437   /// PointerType::get - This constructs a pointer to an object of the specified
438   /// type in a numbered address space.
439   static PointerType *get(Type *ElementType, unsigned AddressSpace);
440
441   /// PointerType::getUnqual - This constructs a pointer to an object of the
442   /// specified type in the generic address space (address space zero).
443   static PointerType *getUnqual(Type *ElementType) {
444     return PointerType::get(ElementType, 0);
445   }
446
447   /// isValidElementType - Return true if the specified type is valid as a
448   /// element type.
449   static bool isValidElementType(Type *ElemTy);
450
451   /// @brief Return the address space of the Pointer type.
452   inline unsigned getAddressSpace() const { return getSubclassData(); }
453
454   // Implement support type inquiry through isa, cast, and dyn_cast.
455   static inline bool classof(const PointerType *) { return true; }
456   static inline bool classof(const Type *T) {
457     return T->getTypeID() == PointerTyID;
458   }
459 };
460
461 } // End llvm namespace
462
463 #endif