move the address space into the subclass data field, saving a word on PointerType.
[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/ADT/ArrayRef.h"
23 #include "llvm/Support/DataTypes.h"
24
25 namespace llvm {
26
27 class Value;
28 template<class ValType, class TypeClass> class TypeMap;
29 class FunctionValType;
30 class ArrayValType;
31 class StructValType;
32 class PointerValType;
33 class VectorValType;
34 class IntegerValType;
35 class APInt;
36 class LLVMContext;
37
38 class DerivedType : public Type {
39   friend class Type;
40
41 protected:
42   explicit DerivedType(LLVMContext &C, TypeID id) : Type(C, id) {}
43
44   /// notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type
45   /// that the current type has transitioned from being abstract to being
46   /// concrete.
47   ///
48   void notifyUsesThatTypeBecameConcrete();
49
50   /// dropAllTypeUses - When this (abstract) type is resolved to be equal to
51   /// another (more concrete) type, we must eliminate all references to other
52   /// types, to avoid some circular reference problems.
53   ///
54   void dropAllTypeUses();
55
56 public:
57
58   //===--------------------------------------------------------------------===//
59   // Abstract Type handling methods - These types have special lifetimes, which
60   // are managed by (add|remove)AbstractTypeUser. See comments in
61   // AbstractTypeUser.h for more information.
62
63   /// refineAbstractTypeTo - This function is used to when it is discovered that
64   /// the 'this' abstract type is actually equivalent to the NewType specified.
65   /// This causes all users of 'this' to switch to reference the more concrete
66   /// type NewType and for 'this' to be deleted.
67   ///
68   void refineAbstractTypeTo(const Type *NewType);
69
70   void dump() const { Type::dump(); }
71
72   // Methods for support type inquiry through isa, cast, and dyn_cast.
73   static inline bool classof(const DerivedType *) { return true; }
74   static inline bool classof(const Type *T) {
75     return T->isDerivedType();
76   }
77 };
78
79 /// Class to represent integer types. Note that this class is also used to
80 /// represent the built-in integer types: Int1Ty, Int8Ty, Int16Ty, Int32Ty and
81 /// Int64Ty.
82 /// @brief Integer representation type
83 class IntegerType : public DerivedType {
84   friend class LLVMContextImpl;
85   
86 protected:
87   explicit IntegerType(LLVMContext &C, unsigned NumBits) : 
88       DerivedType(C, IntegerTyID) {
89     setSubclassData(NumBits);
90   }
91   friend class TypeMap<IntegerValType, IntegerType>;
92 public:
93   /// This enum is just used to hold constants we need for IntegerType.
94   enum {
95     MIN_INT_BITS = 1,        ///< Minimum number of bits that can be specified
96     MAX_INT_BITS = (1<<23)-1 ///< Maximum number of bits that can be specified
97       ///< Note that bit width is stored in the Type classes SubclassData field
98       ///< which has 23 bits. This yields a maximum bit width of 8,388,607 bits.
99   };
100
101   /// This static method is the primary way of constructing an IntegerType.
102   /// If an IntegerType with the same NumBits value was previously instantiated,
103   /// that instance will be returned. Otherwise a new one will be created. Only
104   /// one instance with a given NumBits value is ever created.
105   /// @brief Get or create an IntegerType instance.
106   static const IntegerType *get(LLVMContext &C, unsigned NumBits);
107
108   /// @brief Get the number of bits in this IntegerType
109   unsigned getBitWidth() const { return getSubclassData(); }
110
111   /// getBitMask - Return a bitmask with ones set for all of the bits
112   /// that can be set by an unsigned version of this type.  This is 0xFF for
113   /// i8, 0xFFFF for i16, etc.
114   uint64_t getBitMask() const {
115     return ~uint64_t(0UL) >> (64-getBitWidth());
116   }
117
118   /// getSignBit - Return a uint64_t with just the most significant bit set (the
119   /// sign bit, if the value is treated as a signed number).
120   uint64_t getSignBit() const {
121     return 1ULL << (getBitWidth()-1);
122   }
123
124   /// For example, this is 0xFF for an 8 bit integer, 0xFFFF for i16, etc.
125   /// @returns a bit mask with ones set for all the bits of this type.
126   /// @brief Get a bit mask for this type.
127   APInt getMask() const;
128
129   /// This method determines if the width of this IntegerType is a power-of-2
130   /// in terms of 8 bit bytes.
131   /// @returns true if this is a power-of-2 byte width.
132   /// @brief Is this a power-of-2 byte-width IntegerType ?
133   bool isPowerOf2ByteWidth() const;
134
135   // Methods for support type inquiry through isa, cast, and dyn_cast.
136   static inline bool classof(const IntegerType *) { return true; }
137   static inline bool classof(const Type *T) {
138     return T->getTypeID() == IntegerTyID;
139   }
140 };
141
142
143 /// FunctionType - Class to represent function types
144 ///
145 class FunctionType : public DerivedType {
146   friend class TypeMap<FunctionValType, FunctionType>;
147   FunctionType(const FunctionType &);                   // Do not implement
148   const FunctionType &operator=(const FunctionType &);  // Do not implement
149   FunctionType(const Type *Result, ArrayRef<const Type*> Params,
150                bool IsVarArgs);
151
152 public:
153   /// FunctionType::get - This static method is the primary way of constructing
154   /// a FunctionType.
155   ///
156   static FunctionType *get(const Type *Result,
157                            ArrayRef<const Type*> Params, bool isVarArg);
158
159   /// FunctionType::get - Create a FunctionType taking no parameters.
160   ///
161   static FunctionType *get(const Type *Result, bool isVarArg) {
162     return get(Result, ArrayRef<const Type *>(), isVarArg);
163   }
164
165   /// isValidReturnType - Return true if the specified type is valid as a return
166   /// type.
167   static bool isValidReturnType(const Type *RetTy);
168
169   /// isValidArgumentType - Return true if the specified type is valid as an
170   /// argument type.
171   static bool isValidArgumentType(const Type *ArgTy);
172
173   bool isVarArg() const { return getSubclassData(); }
174   const Type *getReturnType() const { return ContainedTys[0]; }
175
176   typedef Type::subtype_iterator param_iterator;
177   param_iterator param_begin() const { return ContainedTys + 1; }
178   param_iterator param_end() const { return &ContainedTys[NumContainedTys]; }
179
180   // Parameter type accessors.
181   const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
182
183   /// getNumParams - Return the number of fixed parameters this function type
184   /// requires.  This does not consider varargs.
185   ///
186   unsigned getNumParams() const { return NumContainedTys - 1; }
187
188   // Implement the AbstractTypeUser interface.
189   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
190   virtual void typeBecameConcrete(const DerivedType *AbsTy);
191
192   // Methods for support type inquiry through isa, cast, and dyn_cast.
193   static inline bool classof(const FunctionType *) { return true; }
194   static inline bool classof(const Type *T) {
195     return T->getTypeID() == FunctionTyID;
196   }
197 };
198
199
200 /// CompositeType - Common super class of ArrayType, StructType, PointerType
201 /// and VectorType.
202 class CompositeType : public DerivedType {
203 protected:
204   explicit CompositeType(LLVMContext &C, TypeID tid) : DerivedType(C, tid) { }
205 public:
206
207   /// getTypeAtIndex - Given an index value into the type, return the type of
208   /// the element.
209   ///
210   virtual const Type *getTypeAtIndex(const Value *V) const = 0;
211   virtual const Type *getTypeAtIndex(unsigned Idx) const = 0;
212   virtual bool indexValid(const Value *V) const = 0;
213   virtual bool indexValid(unsigned Idx) const = 0;
214
215   // Methods for support type inquiry through isa, cast, and dyn_cast.
216   static inline bool classof(const CompositeType *) { return true; }
217   static inline bool classof(const Type *T) {
218     return T->getTypeID() == ArrayTyID ||
219            T->getTypeID() == StructTyID ||
220            T->getTypeID() == PointerTyID ||
221            T->getTypeID() == VectorTyID;
222   }
223 };
224
225
226 /// StructType - Class to represent struct types, both normal and packed.
227 ///
228 class StructType : public CompositeType {
229   friend class TypeMap<StructValType, StructType>;
230   StructType(const StructType &);                   // Do not implement
231   const StructType &operator=(const StructType &);  // Do not implement
232   StructType(LLVMContext &C, ArrayRef<const Type*> Types, bool isPacked);
233 public:
234   /// StructType::get - This static method is the primary way to create a
235   /// StructType.
236   ///
237   static StructType *get(LLVMContext &Context, ArrayRef<const Type*> Params,
238                          bool isPacked = false);
239
240   /// StructType::get - Create an empty structure type.
241   ///
242   static StructType *get(LLVMContext &Context, bool isPacked=false) {
243     return get(Context, llvm::ArrayRef<const Type*>(), isPacked);
244   }
245
246   /// StructType::get - This static method is a convenience method for
247   /// creating structure types by specifying the elements as arguments.
248   /// Note that this method always returns a non-packed struct.  To get
249   /// an empty struct, pass NULL, NULL.
250   static StructType *get(LLVMContext &Context, 
251                          const Type *type, ...) END_WITH_NULL;
252
253   /// isValidElementType - Return true if the specified type is valid as a
254   /// element type.
255   static bool isValidElementType(const Type *ElemTy);
256
257   bool isPacked() const { return getSubclassData() != 0 ? true : false; }
258
259   // Iterator access to the elements.
260   typedef Type::subtype_iterator element_iterator;
261   element_iterator element_begin() const { return ContainedTys; }
262   element_iterator element_end() const { return &ContainedTys[NumContainedTys];}
263
264   // Random access to the elements
265   unsigned getNumElements() const { return NumContainedTys; }
266   const Type *getElementType(unsigned N) const {
267     assert(N < NumContainedTys && "Element number out of range!");
268     return ContainedTys[N];
269   }
270
271   /// getTypeAtIndex - Given an index value into the type, return the type of
272   /// the element.  For a structure type, this must be a constant value...
273   ///
274   virtual const Type *getTypeAtIndex(const Value *V) const;
275   virtual const Type *getTypeAtIndex(unsigned Idx) const;
276   virtual bool indexValid(const Value *V) const;
277   virtual bool indexValid(unsigned Idx) const;
278
279   // Implement the AbstractTypeUser interface.
280   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
281   virtual void typeBecameConcrete(const DerivedType *AbsTy);
282
283   // Methods for support type inquiry through isa, cast, and dyn_cast.
284   static inline bool classof(const StructType *) { return true; }
285   static inline bool classof(const Type *T) {
286     return T->getTypeID() == StructTyID;
287   }
288 };
289
290 /// SequentialType - This is the superclass of the array, pointer and vector
291 /// type classes.  All of these represent "arrays" in memory.  The array type
292 /// represents a specifically sized array, pointer types are unsized/unknown
293 /// size arrays, vector types represent specifically sized arrays that
294 /// allow for use of SIMD instructions.  SequentialType holds the common
295 /// features of all, which stem from the fact that all three lay their
296 /// components out in memory identically.
297 ///
298 class SequentialType : public CompositeType {
299   PATypeHandle ContainedType;       ///< Storage for the single contained type.
300   SequentialType(const SequentialType &);                  // Do not implement!
301   const SequentialType &operator=(const SequentialType &); // Do not implement!
302
303   // avoiding warning: 'this' : used in base member initializer list
304   SequentialType *this_() { return this; }
305 protected:
306   SequentialType(TypeID TID, const Type *ElType)
307     : CompositeType(ElType->getContext(), TID), ContainedType(ElType, this_()) {
308     ContainedTys = &ContainedType;
309     NumContainedTys = 1;
310   }
311
312 public:
313   inline const Type *getElementType() const { return ContainedTys[0]; }
314
315   virtual bool indexValid(const Value *V) const;
316   virtual bool indexValid(unsigned) const {
317     return true;
318   }
319
320   /// getTypeAtIndex - Given an index value into the type, return the type of
321   /// the element.  For sequential types, there is only one subtype...
322   ///
323   virtual const Type *getTypeAtIndex(const Value *) const {
324     return ContainedTys[0];
325   }
326   virtual const Type *getTypeAtIndex(unsigned) const {
327     return ContainedTys[0];
328   }
329
330   // Methods for support type inquiry through isa, cast, and dyn_cast.
331   static inline bool classof(const SequentialType *) { return true; }
332   static inline bool classof(const Type *T) {
333     return T->getTypeID() == ArrayTyID ||
334            T->getTypeID() == PointerTyID ||
335            T->getTypeID() == VectorTyID;
336   }
337 };
338
339
340 /// ArrayType - Class to represent array types.
341 ///
342 class ArrayType : public SequentialType {
343   friend class TypeMap<ArrayValType, ArrayType>;
344   uint64_t NumElements;
345
346   ArrayType(const ArrayType &);                   // Do not implement
347   const ArrayType &operator=(const ArrayType &);  // Do not implement
348   ArrayType(const Type *ElType, uint64_t NumEl);
349 public:
350   /// ArrayType::get - This static method is the primary way to construct an
351   /// ArrayType
352   ///
353   static ArrayType *get(const Type *ElementType, uint64_t NumElements);
354
355   /// isValidElementType - Return true if the specified type is valid as a
356   /// element type.
357   static bool isValidElementType(const Type *ElemTy);
358
359   uint64_t getNumElements() const { return NumElements; }
360
361   // Implement the AbstractTypeUser interface.
362   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
363   virtual void typeBecameConcrete(const DerivedType *AbsTy);
364
365   // Methods for support type inquiry through isa, cast, and dyn_cast.
366   static inline bool classof(const ArrayType *) { return true; }
367   static inline bool classof(const Type *T) {
368     return T->getTypeID() == ArrayTyID;
369   }
370 };
371
372 /// VectorType - Class to represent vector types.
373 ///
374 class VectorType : public SequentialType {
375   friend class TypeMap<VectorValType, VectorType>;
376   unsigned NumElements;
377
378   VectorType(const VectorType &);                   // Do not implement
379   const VectorType &operator=(const VectorType &);  // Do not implement
380   VectorType(const Type *ElType, unsigned NumEl);
381 public:
382   /// VectorType::get - This static method is the primary way to construct an
383   /// VectorType.
384   ///
385   static VectorType *get(const Type *ElementType, unsigned NumElements);
386
387   /// VectorType::getInteger - This static method gets a VectorType with the
388   /// same number of elements as the input type, and the element type is an
389   /// integer type of the same width as the input element type.
390   ///
391   static VectorType *getInteger(const VectorType *VTy) {
392     unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
393     const Type *EltTy = IntegerType::get(VTy->getContext(), EltBits);
394     return VectorType::get(EltTy, VTy->getNumElements());
395   }
396
397   /// VectorType::getExtendedElementVectorType - This static method is like
398   /// getInteger except that the element types are twice as wide as the
399   /// elements in the input type.
400   ///
401   static VectorType *getExtendedElementVectorType(const VectorType *VTy) {
402     unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
403     const Type *EltTy = IntegerType::get(VTy->getContext(), EltBits * 2);
404     return VectorType::get(EltTy, VTy->getNumElements());
405   }
406
407   /// VectorType::getTruncatedElementVectorType - This static method is like
408   /// getInteger except that the element types are half as wide as the
409   /// elements in the input type.
410   ///
411   static VectorType *getTruncatedElementVectorType(const VectorType *VTy) {
412     unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
413     assert((EltBits & 1) == 0 &&
414            "Cannot truncate vector element with odd bit-width");
415     const Type *EltTy = IntegerType::get(VTy->getContext(), EltBits / 2);
416     return VectorType::get(EltTy, VTy->getNumElements());
417   }
418
419   /// isValidElementType - Return true if the specified type is valid as a
420   /// element type.
421   static bool isValidElementType(const Type *ElemTy);
422
423   /// @brief Return the number of elements in the Vector type.
424   unsigned getNumElements() const { return NumElements; }
425
426   /// @brief Return the number of bits in the Vector type.
427   unsigned getBitWidth() const {
428     return NumElements * getElementType()->getPrimitiveSizeInBits();
429   }
430
431   // Implement the AbstractTypeUser interface.
432   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
433   virtual void typeBecameConcrete(const DerivedType *AbsTy);
434
435   // Methods for support type inquiry through isa, cast, and dyn_cast.
436   static inline bool classof(const VectorType *) { return true; }
437   static inline bool classof(const Type *T) {
438     return T->getTypeID() == VectorTyID;
439   }
440 };
441
442
443 /// PointerType - Class to represent pointers.
444 ///
445 class PointerType : public SequentialType {
446   friend class TypeMap<PointerValType, PointerType>;
447
448   PointerType(const PointerType &);                   // Do not implement
449   const PointerType &operator=(const PointerType &);  // Do not implement
450   explicit PointerType(const Type *ElType, unsigned AddrSpace);
451 public:
452   /// PointerType::get - This constructs a pointer to an object of the specified
453   /// type in a numbered address space.
454   static PointerType *get(const Type *ElementType, unsigned AddressSpace);
455
456   /// PointerType::getUnqual - This constructs a pointer to an object of the
457   /// specified type in the generic address space (address space zero).
458   static PointerType *getUnqual(const Type *ElementType) {
459     return PointerType::get(ElementType, 0);
460   }
461
462   /// isValidElementType - Return true if the specified type is valid as a
463   /// element type.
464   static bool isValidElementType(const Type *ElemTy);
465
466   /// @brief Return the address space of the Pointer type.
467   inline unsigned getAddressSpace() const { return getSubclassData(); }
468
469   // Implement the AbstractTypeUser interface.
470   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
471   virtual void typeBecameConcrete(const DerivedType *AbsTy);
472
473   // Implement support type inquiry through isa, cast, and dyn_cast.
474   static inline bool classof(const PointerType *) { return true; }
475   static inline bool classof(const Type *T) {
476     return T->getTypeID() == PointerTyID;
477   }
478 };
479
480
481 /// OpaqueType - Class to represent opaque types.
482 ///
483 class OpaqueType : public DerivedType {
484   friend class LLVMContextImpl;
485   OpaqueType(const OpaqueType &);                   // DO NOT IMPLEMENT
486   const OpaqueType &operator=(const OpaqueType &);  // DO NOT IMPLEMENT
487   OpaqueType(LLVMContext &C);
488 public:
489   /// OpaqueType::get - Static factory method for the OpaqueType class.
490   ///
491   static OpaqueType *get(LLVMContext &C);
492
493   // Implement support for type inquiry through isa, cast, and dyn_cast.
494   static inline bool classof(const OpaqueType *) { return true; }
495   static inline bool classof(const Type *T) {
496     return T->getTypeID() == OpaqueTyID;
497   }
498 };
499
500 } // End llvm namespace
501
502 #endif