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