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